File: test_transaction_as_fixture.py

package info (click to toggle)
ormar 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,952 kB
  • sloc: python: 24,085; makefile: 34; sh: 14
file content (55 lines) | stat: -rw-r--r-- 1,205 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import ormar
import pytest

from tests.lifespan import init_tests
from tests.settings import create_config

base_ormar_config = create_config()


class Jimmy(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="jimmies")
    name = ormar.String(primary_key=True, max_length=42)


create_test_database = init_tests(base_ormar_config)


@pytest.fixture(scope="session")
def anyio_backend():
    return "asyncio"


@pytest.mark.anyio
@pytest.fixture(scope="function", autouse=True)
async def db_session():
    async with base_ormar_config.database:
        async with base_ormar_config.database.transaction(force_rollback=True):
            yield


@pytest.mark.anyio
async def test_jimmy_can():
    await Jimmy.objects.create(
        name="jimmy",
    )
    await Jimmy.objects.create(
        name="jimmyRus",
    )
    jimmies = await Jimmy.objects.all()
    assert len(jimmies) == 2


@pytest.mark.anyio
async def test_jimmy_cant():
    jimmies = await Jimmy.objects.all()
    assert not jimmies

    await Jimmy.objects.create(
        name="jimmy",
    )
    await Jimmy.objects.create(
        name="jimmyRus",
    )
    jimmies = await Jimmy.objects.all()
    assert len(jimmies) == 2