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

1import pytest 

2 

3from prisma import Prisma 

4 

5# TODO: more tests 

6 

7 

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 

13 

14 

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 

20 

21 

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}) 

29 

30 total = await client.post.count(take=1) 

31 assert total == 1 

32 

33 

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}) 

41 

42 total = await client.post.count(skip=1) 

43 assert total == 2 

44 

45 

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'}) 

52 

53 count = await client.post.count( 

54 select={}, 

55 ) 

56 assert count == {'_all': 2} 

57 

58 count = await client.post.count( 

59 select={ 

60 'description': True, 

61 }, 

62 ) 

63 assert count == {'description': 1} 

64 

65 count = await client.post.count( 

66 select={ 

67 '_all': True, 

68 'description': True, 

69 }, 

70 ) 

71 assert count == {'_all': 2, 'description': 1}