def test_with_foreign_keys_parameter(self):
        user = self.User(first_name=u"John")
        objects = [
            self.Article(author=user),
            self.Article(),
            self.Article(owner=user),
            self.Article(author=user, owner=user),
            self.BlogPost(owner=user),
        ]
        self.session.add_all(objects)
        self.session.commit()

        deps = list(
            dependent_objects(
                user,
                (
                    fk
                    for fk in get_referencing_foreign_keys(self.User)
                    if fk.ondelete == "RESTRICT" or fk.ondelete is None
                ),
            ).limit(5)
        )
        assert len(deps) == 2
        assert objects[0] in deps
        assert objects[3] in deps
Example #2
0
    def test_with_foreign_keys_parameter(
        self,
        session,
        User,
        Article,
        BlogPost
    ):
        user = User(first_name=u'John')
        objects = [
            Article(author=user),
            Article(),
            Article(owner=user),
            Article(author=user, owner=user),
            BlogPost(owner=user)
        ]
        session.add_all(objects)
        session.commit()

        deps = list(
            dependent_objects(
                user,
                (
                    fk for fk in get_referencing_foreign_keys(User)
                    if fk.ondelete == 'RESTRICT' or fk.ondelete is None
                )
            ).limit(5)
        )
        assert len(deps) == 2
        assert objects[0] in deps
        assert objects[3] in deps
Example #3
0
 def from_tables(cls: Type["Relation"],
                 tables: List[Table]) -> List["Relation"]:
     """
     Constructor for a list of relations given a table
     Creates one relation for each foreign_key constraint associated with the table
     """
     return [
         cls.from_foreign_key(fk) for table in tables
         for fk in get_referencing_foreign_keys(table)
     ]
Example #4
0
    def can_be_deleted(self) -> bool:
        """
        Simple helper to check if the instance has entities
        that will prevent this from being deleted via a protected foreign key.
        """

        deps = list(
            dependent_objects(
                self,
                (
                    fk for fk in get_referencing_foreign_keys(self.__class__)
                    # On most databases RESTRICT is the default mode hence we
                    # check for None values also
                    if fk.ondelete == "RESTRICT" or fk.ondelete is None),
            ).limit(1))

        return not deps
 def test_with_declarative_class(self, Admin, TextItem):
     fks = get_referencing_foreign_keys(Admin)
     assert TextItem.__table__.foreign_keys == fks
 def test_with_table(self, User, Article):
     fks = get_referencing_foreign_keys(User.__table__)
     assert Article.__table__.foreign_keys == fks
 def test_with_declarative_class(self, User, Article):
     fks = get_referencing_foreign_keys(User)
     assert Article.__table__.foreign_keys == fks
 def test_with_table(self, Admin):
     fks = get_referencing_foreign_keys(Admin.__table__)
     assert fks == set([])
 def test_with_table(self):
     fks = get_referencing_foreign_keys(self.User.__table__)
     assert self.Article.__table__.foreign_keys == fks
Example #10
0
 def test_with_table(self, Admin):
     fks = get_referencing_foreign_keys(Admin.__table__)
     assert fks == set([])
Example #11
0
 def test_with_declarative_class(self, Admin, TextItem):
     fks = get_referencing_foreign_keys(Admin)
     assert TextItem.__table__.foreign_keys == fks
Example #12
0
 def test_with_table(self, User, Article):
     fks = get_referencing_foreign_keys(User.__table__)
     assert Article.__table__.foreign_keys == fks
Example #13
0
 def test_with_declarative_class(self, User, Article):
     fks = get_referencing_foreign_keys(User)
     assert Article.__table__.foreign_keys == fks
Example #14
0
 def test_with_self_reference(self, User, Admin, TextItem):
     fks = get_referencing_foreign_keys(User, self_reference=True)
     assert User.__table__.foreign_keys \
         | Admin.__table__.foreign_keys \
         | TextItem.__table__.foreign_keys == fks
Example #15
0
 def test_with_table(self):
     fks = get_referencing_foreign_keys(self.User.__table__)
     assert self.Article.__table__.foreign_keys == fks