Skip to content

Commit

Permalink
Clear SQLAlchemy tables after each test
Browse files Browse the repository at this point in the history
Not as efficient as wrapping the test in a transaction, but allows to
reset sequences and remove all table data, so that each test starts from
a clean slate.
  • Loading branch information
francoisfreitag committed Jun 1, 2022
1 parent 0999e78 commit 299634a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 0 additions & 3 deletions tests/alchemyapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,3 @@ class SpecialFieldModel(Base):

id = Column(Integer(), primary_key=True)
session = Column(Unicode(20))


Base.metadata.create_all(engine)
4 changes: 4 additions & 0 deletions tests/test_alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ class Meta:


class TransactionTestCase(unittest.TestCase):
def setUp(self):
models.Base.metadata.create_all(models.engine)

def tearDown(self):
models.session.rollback()
models.Base.metadata.drop_all(models.engine)


class SQLAlchemyPkSequenceTestCase(TransactionTestCase):
Expand Down

0 comments on commit 299634a

Please sign in to comment.