diff --git a/tests/test_hashes/test_many_to_many.py b/tests/test_hashes/test_many_to_many.py index 86d78d4b9..20417010f 100644 --- a/tests/test_hashes/test_many_to_many.py +++ b/tests/test_hashes/test_many_to_many.py @@ -5,6 +5,7 @@ import ormar import pymysql import pytest +import pytest_asyncio from tests.lifespan import init_tests from tests.settings import create_config @@ -52,10 +53,25 @@ class AuthorXPosts(ormar.Model): create_test_database = init_tests(base_ormar_config) +@pytest_asyncio.fixture(scope="function", autouse=True) +async def cleanup(): + yield + async with base_ormar_config.database: + await Post.ormar_config.model_fields["categories"].through.objects.delete( + each=True + ) + await Post.ormar_config.model_fields["authors"].through.objects.delete( + each=True + ) + await Post.objects.delete(each=True) + await Category.objects.delete(each=True) + await Author.objects.delete(each=True) + + @pytest.mark.asyncio async def test_adding_same_m2m_model_twice(): async with base_ormar_config.database: - async with base_ormar_config.database.transaction(rollback=True): + async with base_ormar_config.database: post = await Post.objects.create(title="Hello, M2M") news = await Category(name="News").save() @@ -69,7 +85,7 @@ async def test_adding_same_m2m_model_twice(): @pytest.mark.asyncio async def test_adding_same_m2m_model_twice_with_unique(): async with base_ormar_config.database: - async with base_ormar_config.database.transaction(rollback=True): + async with base_ormar_config.database: post = await Post.objects.create(title="Hello, M2M") redactor = await Author(name="News").save()