Replies: 2 comments 3 replies
-
Update: |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi @cmflynn @fixture(autouse=True, scope="session")
async def create_test_database():
"""SetUp & TearDown Fixture to Create & Delete Database by Metadata"""
async with engine.begin() as conn:
await conn.run_sync(metadata.create_all)
yield
await conn.run_sync(metadata.drop_all) Then you will empty the tables with below fixture: @fixture(autouse=True, scope="function")
async def clean_tables_database():
"""Clearing Database Tables to Delete All Rows for Each Test Case"""
try:
yield
finally:
async with engine.begin() as conn:
for table in reversed(metadata.sorted_tables):
await conn.execute(table.delete()) Or you write this at the beginning of each test: @pytest.mark.asyncio
async def test_something():
async with database:
async with database.transaction(force_rollback=True):
# write your tests |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey im not sure if this truly a bug or not, so im opening the discussion here. perhaps related to: encode/databases#456
so Im trying to configure pytest fixtures to run each test in a transaction that will rollback after the test finishes. problem is the rollback is not actually preventing the data from being comitted in the first test, thus throwing primary key errors in the second test.
Ive verified the code in the testing docs here: https://collerek.github.io/ormar/transactions/ works in isolation, but cant seem to get it working in pytest fixtures. ive used a very similar pattern in sqlalchemy and it works fine so not sure what im missing here.
Beta Was this translation helpful? Give feedback.
All reactions