-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5af3a07
commit 3ea345c
Showing
7 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
Integer, | ||
Sequence, | ||
String, | ||
Table, | ||
exc, | ||
func, | ||
select, | ||
|
@@ -128,6 +127,7 @@ def __repr__(self): | |
Base.metadata.drop_all(engine_testaccount) | ||
|
||
|
||
@pytest.mark.aws | ||
def test_orm_one_to_many_relationship_with_hybrid_table(engine_testaccount, snapshot): | ||
""" | ||
Tests One to Many relationship | ||
|
@@ -191,21 +191,79 @@ def __repr__(self): | |
Base.metadata.drop_all(engine_testaccount) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"table_class, prefix", [(Table, "tbl_"), (HybridTable, "hb_tbl_")] | ||
) | ||
def test_delete_cascade(engine_testaccount, table_class, prefix): | ||
def test_delete_cascade(engine_testaccount): | ||
""" | ||
Test delete cascade | ||
""" | ||
Base = declarative_base() | ||
prefix = "tbl_" | ||
|
||
class User(Base): | ||
__tablename__ = prefix + "user" | ||
|
||
id = Column(Integer, Sequence("user_id_seq"), primary_key=True) | ||
name = Column(String) | ||
fullname = Column(String) | ||
|
||
addresses = relationship( | ||
"Address", back_populates="user", cascade="all, delete, delete-orphan" | ||
) | ||
|
||
def __repr__(self): | ||
return f"<User({self.name!r}, {self.fullname!r})>" | ||
|
||
class Address(Base): | ||
__tablename__ = prefix + "address" | ||
|
||
id = Column(Integer, Sequence("address_id_seq"), primary_key=True) | ||
email_address = Column(String, nullable=False) | ||
user_id = Column(Integer, ForeignKey(f"{User.__tablename__}.id")) | ||
|
||
user = relationship(User, back_populates="addresses") | ||
|
||
def __repr__(self): | ||
return f"<Address({repr(self.email_address)})>" | ||
|
||
Base.metadata.create_all(engine_testaccount) | ||
|
||
try: | ||
jack = User(name="jack", fullname="Jack Bean") | ||
assert jack.addresses == [], "one to many record is empty list" | ||
|
||
jack.addresses = [ | ||
Address(email_address="[email protected]"), | ||
Address(email_address="[email protected]"), | ||
Address(email_address="[email protected]"), | ||
] | ||
|
||
session = Session(bind=engine_testaccount) | ||
session.add(jack) # cascade each Address into the Session as well | ||
session.commit() | ||
|
||
got_jack = session.query(User).first() | ||
assert got_jack == jack | ||
|
||
session.delete(jack) | ||
got_addresses = session.query(Address).all() | ||
assert len(got_addresses) == 0, "no address record" | ||
finally: | ||
Base.metadata.drop_all(engine_testaccount) | ||
|
||
|
||
@pytest.mark.aws | ||
def test_delete_cascade_hybrid_table(engine_testaccount): | ||
""" | ||
Test delete cascade | ||
""" | ||
Base = declarative_base() | ||
prefix = "hb_tbl_" | ||
|
||
class User(Base): | ||
__tablename__ = prefix + "user" | ||
|
||
@classmethod | ||
def __table_cls__(cls, name, metadata, *arg, **kw): | ||
return table_class(name, metadata, *arg, **kw) | ||
return HybridTable(name, metadata, *arg, **kw) | ||
|
||
id = Column(Integer, Sequence("user_id_seq"), primary_key=True) | ||
name = Column(String) | ||
|
@@ -223,7 +281,7 @@ class Address(Base): | |
|
||
@classmethod | ||
def __table_cls__(cls, name, metadata, *arg, **kw): | ||
return table_class(name, metadata, *arg, **kw) | ||
return HybridTable(name, metadata, *arg, **kw) | ||
|
||
id = Column(Integer, Sequence("address_id_seq"), primary_key=True) | ||
email_address = Column(String, nullable=False) | ||
|