Ejemplo n.º 1
0
 def test_delete_cascade(self, class_entity_repo, monkeypatch):
     new_parent1 = MyEntityParent()
     new_ent1 = MyEntity()
     new_ent1.parent = new_parent1
     new_child1 = MyEntityChild()
     new_child1.parent = new_ent1
     child_rel_agg = self._make_rel_agg(class_entity_repo, new_ent1)
     child_rel_agg.add(new_child1)
     new_parent1.id = 1
     new_ent1.id = 1
     new_child1.id = 1
     agg = class_entity_repo.get_aggregate(IMyEntity)
     child_agg = class_entity_repo.get_aggregate(IMyEntityChild)
     assert len(list(child_agg.iterator())) == 1
     assert len(list(agg.iterator())) == 1
     assert new_ent1.children == [new_child1]
     assert new_child1.parent == new_ent1
     csc = DEFAULT_CASCADE | RELATION_OPERATIONS.REMOVE
     children_attr = get_domain_class_attribute(MyEntity, 'children')
     parent_attr = get_domain_class_attribute(MyEntityChild, 'parent')
     monkeypatch.setattr(children_attr, 'cascade', csc)
     monkeypatch.setattr(parent_attr, 'cascade', csc)
     child_rel_agg.remove(new_child1)
     assert new_ent1.children == []
     assert new_child1.parent is None
     assert len(list(child_agg.iterator())) == 0
     if self.__class__.__name__.startswith('TestMemory'):
         # FIXME: Transparent modification of RDB mapper cascades
         #        does not work yet.
         assert len(list(agg.iterator())) == 0
     assert len(list(child_rel_agg.iterator())) == 0
Ejemplo n.º 2
0
 def test_update_member_with_link(self):
     my_entity = create_entity()
     new_parent = MyEntityParent()
     new_parent.text = self.UPDATED_TEXT
     new_parent.id = 2
     my_entity.parent = new_parent
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     attribute_options = {
         ('parent', ): {
             WRITE_AS_LINK_OPTION: True
         },
         ('nested_parent', ): {
             IGNORE_OPTION: True
         }
     }
     mp_cloned = mp.clone(attribute_options=attribute_options)
     data_el = mp_cloned.map_to_data_element(member)
     # The linked-to parent needs to be in the root collection.
     my_entity.parent = None
     del member
     del my_entity
     parent_coll = get_root_collection(IMyEntityParent)
     parent_coll.create_member(new_parent)
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Ejemplo n.º 3
0
 def test_update_member_with_link(self):
     my_entity = create_entity()
     new_parent = MyEntityParent()
     new_parent.text = self.UPDATED_TEXT
     new_parent.id = 2
     my_entity.parent = new_parent
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     attribute_options = {('parent',):{WRITE_AS_LINK_OPTION:True},
                        ('nested_parent',):{IGNORE_OPTION:True}}
     mp_cloned = mp.clone(attribute_options=attribute_options)
     data_el = mp_cloned.map_to_data_element(member)
     # The linked-to parent needs to be in the root collection.
     my_entity.parent = None
     del member
     del my_entity
     parent_coll = get_root_collection(IMyEntityParent)
     parent_coll.create_member(new_parent)
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Ejemplo n.º 4
0
 def _make_child(self, child_agg, child_id=0):
     new_parent = MyEntityParent()
     new_ent = MyEntity()
     new_ent.parent = new_parent
     new_child = MyEntityChild()
     new_ent.children.append(new_child)
     if new_child.parent is None:
         new_child.parent = new_ent
     child_agg.add(new_child)
     new_parent.id = child_id
     new_ent.id = child_id
     new_child.id = child_id
     return new_child
Ejemplo n.º 5
0
 def test_update_member(self):
     my_entity = create_entity()
     new_parent = MyEntityParent()
     new_parent.text = self.UPDATED_TEXT
     new_parent.id = 2
     my_entity.parent = new_parent
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     data_el = mp.map_to_data_element(member)
     del member
     del my_entity
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Ejemplo n.º 6
0
 def test_update_member(self):
     my_entity = create_entity()
     new_parent = MyEntityParent()
     new_parent.text = self.UPDATED_TEXT
     new_parent.id = 2
     my_entity.parent = new_parent
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     data_el = mp.map_to_data_element(member)
     del member
     del my_entity
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Ejemplo n.º 7
0
def create_entity(entity_id=0, entity_text=None):
    my_entity = MyEntity(text=entity_text)
    my_entity.id = entity_id
    my_entity_parent = MyEntityParent()
    my_entity_parent.id = entity_id
    my_entity.parent = my_entity_parent
    my_entity_child = MyEntityChild()
    my_entity_child.id = entity_id
    my_entity.children.append(my_entity_child)
    my_entity_grandchild = MyEntityGrandchild()
    my_entity_grandchild.id = entity_id
    my_entity_child.children.append(my_entity_grandchild)
    # If we run with the SQLAlchemy backend, the back references are populated
    # automatically.
    if my_entity_child.parent is None:
        my_entity_child.parent = my_entity
    if my_entity_grandchild.parent is None:
        my_entity_grandchild.parent = my_entity_child
    return my_entity
Ejemplo n.º 8
0
def _make_test_entity_member():
    parent = MyEntityParent()
    entity = MyEntity(parent=parent)
    if parent.child is None:
        parent.child = entity
    child = MyEntityChild()
    entity.children.append(child)
    if child.parent is None:
        child.parent = entity
    grandchild = MyEntityGrandchild()
    child.children.append(grandchild)
    if grandchild.parent is None:
        grandchild.parent = child
    coll = create_staging_collection(IMyEntity)
    mb = coll.create_member(entity)
    parent.id = 0
    entity.id = 0
    child.id = 0
    grandchild.id = 0
    return mb
Ejemplo n.º 9
0
def _make_test_entity_member():
    parent = MyEntityParent()
    entity = MyEntity(parent=parent)
    if parent.child is None:
        parent.child = entity
    child = MyEntityChild()
    entity.children.append(child)
    if child.parent is None:
        child.parent = entity
    grandchild = MyEntityGrandchild()
    child.children.append(grandchild)
    if grandchild.parent is None:
        grandchild.parent = child
    coll = create_staging_collection(IMyEntity)
    mb = coll.create_member(entity)
    parent.id = 0
    entity.id = 0
    child.id = 0
    grandchild.id = 0
    return mb
Ejemplo n.º 10
0
def create_entity(entity_id=0, entity_text=None):
    my_entity = MyEntity(text=entity_text)
    my_entity.id = entity_id
    my_entity_parent = MyEntityParent()
    my_entity_parent.id = entity_id
    my_entity.parent = my_entity_parent
    my_entity_child = MyEntityChild()
    my_entity_child.id = entity_id
    my_entity_child.parent = my_entity
    if len(my_entity.children) == 0:
        # Tests that use the ORM will not need to go here.
        my_entity.children.append(my_entity_child)
        assert len(my_entity.children) == 1
    my_entity_grandchild = MyEntityGrandchild()
    my_entity_grandchild.id = entity_id
    my_entity_grandchild.parent = my_entity_child
    # Tests that use the ORM will not need this.
    if len(my_entity.children) == 0:
        my_entity.children.append(my_entity_child)
    if len(my_entity_child.children) == 0:
        my_entity_child.children.append(my_entity_grandchild)
    return my_entity
Ejemplo n.º 11
0
def create_entity(entity_id=0, entity_text=None):
    my_entity = MyEntity(text=entity_text)
    my_entity.id = entity_id
    my_entity_parent = MyEntityParent()
    my_entity_parent.id = entity_id
    my_entity.parent = my_entity_parent
    my_entity_child = MyEntityChild()
    my_entity_child.id = entity_id
    my_entity_child.parent = my_entity
    if len(my_entity.children) == 0:
        # Tests that use the ORM will not need to go here.
        my_entity.children.append(my_entity_child)
        assert len(my_entity.children) == 1
    my_entity_grandchild = MyEntityGrandchild()
    my_entity_grandchild.id = entity_id
    my_entity_grandchild.parent = my_entity_child
    # Tests that use the ORM will not need this.
    if len(my_entity.children) == 0:
        my_entity.children.append(my_entity_child)
    if len(my_entity_child.children) == 0:
        my_entity_child.children.append(my_entity_grandchild)
    return my_entity