def test_get_children(): animal = Category(name='animal') cat = Category(name='cat') dog = Category(name='dog') animal._children = [cat, dog] assert animal.children == [cat, dog]
def test_path_property(): big_cat = Category(name='big cat') assert big_cat.path == 'big cat' cat = Category(name='cat', children=[big_cat]) assert big_cat.path == 'cat/big cat' assert cat.path == 'cat' animal = Category(name='animal', children=[cat]) assert big_cat.path == 'animal/cat/big cat' assert cat.path == 'animal/cat' assert animal.path == 'animal'
def test_remove_child(): animal = Category(name='animal') cat = Category(name='cat') cat._parent = animal dog = Category(name='dog') dog._parent = animal animal._children = [cat, dog] animal._remove_child(cat) assert animal._children == [dog] assert cat.parent is None
def test_add(animal): bird = Category(name='bird') animal.add(bird) assert bird in animal._children wild_dog = Category(name='wild_dog') animal.add(wild_dog, 'dog') assert wild_dog in animal.get('dog')._children tiger = Category(name='tiger') animal.add(tiger, 'cat') assert tiger in animal.get('cat')._children white_lion = Category(name='white_lion') animal.add(white_lion, 'cat/lion') assert white_lion in animal.get('cat/lion')._children
def test_add_child(): animal = Category(name='animal') cat = Category(name='cat') animal._add_child(cat) assert cat in animal.children assert cat.parent is animal # sibling with same name with pytest.raises(DuplicateNameError): another_cat = Category(name='cat') animal._add_child(another_cat) # ancestor with same name with pytest.raises(DuplicateNameError): another_cat = Category(name='cat') cat._add_child(another_cat) with pytest.raises(NotACategoryError): animal._add_child('cat') with pytest.raises(NotACategoryError): animal._add_child(id(cat)) with pytest.raises(NotACategoryError): animal._add_child([cat])
def test_is_root(): big_cat = Category('big cat') cat = Category('cat', children=[big_cat]) animal = Category('animal', children=[cat]) assert animal.is_root() is True assert cat.is_root() is False assert big_cat.is_root() is False
def test_set_children(): animal = Category(name='animal') cat = Category(name='cat') dog = Category(name='dog') animal.children = [] assert animal._children == [] animal.children = [cat] assert animal._children == [cat] animal.children = [cat, dog] assert animal._children == [cat, dog] with pytest.raises(ChildrenNotIterableError): animal.children = cat with pytest.raises(NotACategoryError): animal.children = [cat, 'dog'] with pytest.raises(NotACategoryError): animal.children = [cat, 7] with pytest.raises(NotACategoryError): animal.children = [7]
def test_set_parent(): animal = Category(name='animal') animal.parent = None assert animal._parent == None creature = Category(name='creature') animal.parent = creature assert animal._parent == creature with pytest.raises(NotACategoryError): animal.parent = '' with pytest.raises(NotACategoryError): animal.parent = 5 with pytest.raises(NotACategoryError): animal.parent = [] with pytest.raises(ParentLoopError): animal.parent = animal with pytest.raises(SameNameParentError): animal.parent = Category(name='animal')
def test_add_parent(): # add on top of root animal = Category(name='animal') cat = Category(name='cat') cat._add_parent(animal) assert cat.parent is animal assert animal.children == [cat] # add instead of previous parent animal = Category(name='animal') lion = Category(name='lion') animal._children = [lion] lion._parent = animal cat = Category(name='cat') lion._add_parent(cat) assert lion.parent is cat assert cat.children == [lion] assert animal.children == [cat]
def test_get_parent(): animal = Category(name='animal') cat = Category(name='cat') cat._parent = animal assert cat.parent == animal