Coverage for databases/tests/test_count.py: 100%
37 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import pytest
3from prisma import Prisma
5# TODO: more tests
8@pytest.mark.asyncio
9async def test_count(client: Prisma) -> None:
10 """Basic usage with a result"""
11 await client.post.create({'title': 'post 1', 'published': False})
12 assert await client.post.count() == 1
15@pytest.mark.asyncio
16async def test_count_no_results(client: Prisma) -> None:
17 """No results returns 0"""
18 total = await client.post.count(where={'title': 'kdbsajdh'})
19 assert total == 0
22@pytest.mark.asyncio
23async def test_take(client: Prisma) -> None:
24 """Take argument limits the maximum value"""
25 async with client.batch_() as batcher:
26 batcher.post.create({'title': 'Foo 1', 'published': False})
27 batcher.post.create({'title': 'Foo 2', 'published': False})
28 batcher.post.create({'title': 'Foo 3', 'published': False})
30 total = await client.post.count(take=1)
31 assert total == 1
34@pytest.mark.asyncio
35async def test_skip(client: Prisma) -> None:
36 """Skip argument ignores the first N records"""
37 async with client.batch_() as batcher:
38 batcher.post.create({'title': 'Foo 1', 'published': False})
39 batcher.post.create({'title': 'Foo 2', 'published': False})
40 batcher.post.create({'title': 'Foo 3', 'published': False})
42 total = await client.post.count(skip=1)
43 assert total == 2
46@pytest.mark.asyncio
47async def test_select(client: Prisma) -> None:
48 """Selecting a field counts non-null values"""
49 async with client.batch_() as batcher:
50 batcher.post.create({'title': 'Foo', 'published': False})
51 batcher.post.create({'title': 'Foo 2', 'published': False, 'description': 'A'})
53 count = await client.post.count(
54 select={},
55 )
56 assert count == {'_all': 2}
58 count = await client.post.count(
59 select={
60 'description': True,
61 },
62 )
63 assert count == {'description': 1}
65 count = await client.post.count(
66 select={
67 '_all': True,
68 'description': True,
69 },
70 )
71 assert count == {'_all': 2, 'description': 1}