Commit 6af86c3d by dragonfi

Add StepMixin

parent 0c0ce8d3
class StepParentMixin(object):
"""
A parent containing the Step objects
The parent must have a get_children_objects() method.
"""
@property
def steps(self):
return [child for child in self.get_children_objects() if isinstance(child, StepMixin)]
class StepMixin(object):
@property
def step_number(self):
return self.parent.steps.index(self) + 1
@property
def lonely_step(self):
if self not in self.parent.steps:
raise ValueError("Step's parent should contain Step", self, self.parents.steps)
return len(self.parent.steps) == 1
import unittest
from lxml import etree
from mentoring import MentoringBlock
import mentoring
from mentoring.step import StepMixin, StepParentMixin
class Parent(StepParentMixin):
def get_children_objects(self):
return list(self._children)
def _set_children_for_test(self, *children):
self._children = children
for child in self._children:
try:
child.parent = self
except AttributeError:
pass
class Step(StepMixin):
def __init__(self): pass
class NotAStep(object): pass
class TestStepMixin(unittest.TestCase):
def test_single_step_is_returned_correctly(self):
block = Parent()
step = Step()
block._children = [step]
self.assertSequenceEqual(block.steps, [step])
def test_only_steps_are_returned(self):
block = Parent()
step1 = Step()
step2 = Step()
block._set_children_for_test(step1, 1, "2", "Step", NotAStep(), False, step2, NotAStep())
self.assertSequenceEqual(block.steps, [step1, step2])
def test_proper_number_is_returned_for_step(self):
block = Parent()
step1 = Step()
step2 = Step()
block._set_children_for_test(step1, 1, "2", "Step", NotAStep(), False, step2, NotAStep())
self.assertEquals(step1.step_number, 1)
self.assertEquals(step2.step_number, 2)
def test_the_number_does_not_represent_the_order_of_creation(self):
block = Parent()
step1 = Step()
step2 = Step()
block._set_children_for_test(step2, 1, "2", "Step", NotAStep(), False, step1, NotAStep())
self.assertEquals(step1.step_number, 2)
self.assertEquals(step2.step_number, 1)
def test_lonely_step_is_true_for_stand_alone_steps(self):
block = Parent()
step1 = Step()
block._set_children_for_test(1, "2", step1, "Step", NotAStep(), False)
self.assertTrue(step1.lonely_step)
def test_lonely_step_is_true_if_parent_have_more_steps(self):
block = Parent()
step1 = Step()
step2 = Step()
block._set_children_for_test(1, step2, "2", step1, "Step", NotAStep(), False)
self.assertFalse(step1.lonely_step)
self.assertFalse(step2.lonely_step)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment