예제 #1
0
 def test_name_uniqueness_accept(self, app):
     with app.app_context():
         Node.create('Unique', Node.get_root_node())
         try:
             Node.create('Unique', Node.get_root_node())
         except ValueError:
             pytest.fail()
예제 #2
0
 def test_keep_name_unique(self, app, example_hierarchy):
     with app.app_context():
         node3 = Node.get_by_id(3)
         node7 = Node.get_by_id(7)
         Node.create(name='I am unique within my tree', parent_node=node3)
         Node.create(name='I am unique within my tree', parent_node=node7)
         with pytest.raises(ValueError):
             node3.move(node7)
예제 #3
0
    def test_create_node(self, app):
        expected_root_node = Node(id=1, name='root', parent_id=None, lft=0, rgt=3)
        expected_new_node = Node(id=2, name='new node', parent_id=1, lft=1, rgt=2, tree_id=2)
        with app.app_context():
            new_node = Node.create(expected_new_node.name, Node.get_by_id(expected_new_node.parent_id))
            assert new_node == Node.get_by_id(expected_new_node.id)
            root_node = Node.get_by_id(expected_root_node.id)

        assert root_node == expected_root_node and new_node == expected_new_node
예제 #4
0
 def test_update_name(self, client, app):
     with app.app_context():
         node = Node.create(name='old name',
                            parent_node=Node.get_root_node())
     updated_item = client.post(f'/item/{node.id}',
                                json={
                                    'name': 'new name'
                                }).get_json()
     assert updated_item == client.get(f'/item/{node.id}').get_json()
     assert updated_item == {
         'id': node.id,
         'name': 'new name',
         'parent_id': None
     }
예제 #5
0
def item():
    data = request.get_json()
    if 'name' not in data:
        return api_message(400, '"name" required')

    if 'parent_id' not in data:
        return api_message(400, '"parent_id" required')

    parent_id = data['parent_id']
    if parent_id is None:
        parent_id = Node.get_root_id()

    parent_node = Node.get_by_id(parent_id)
    if parent_node is None:
        return api_message(400, 'Invalid "parent_id"')

    try:
        node = Node.create(name=data['name'], parent_node=parent_node)
    except ValueError as e:
        return api_message(400, str(e))
    
    return '', 201, {'Location': url_for('.item_by_id', item_id=node.id)}
예제 #6
0
 def test_name_uniqueness_fail(self, app):
     with app.app_context():
         node = Node.create('Unique', Node.get_root_node())
         with pytest.raises(ValueError):
                 Node.create('Unique', node)