Coverage for databases/sync_tests/test_count.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-28 15:17 +0000

1from prisma import Prisma 

2 

3# TODO: more tests 

4 

5 

6def test_count(client: Prisma) -> None: 

7 """Basic usage with a result""" 

8 client.post.create({'title': 'post 1', 'published': False}) 

9 assert client.post.count() == 1 

10 

11 

12def test_count_no_results(client: Prisma) -> None: 

13 """No results returns 0""" 

14 total = client.post.count(where={'title': 'kdbsajdh'}) 

15 assert total == 0 

16 

17 

18def test_take(client: Prisma) -> None: 

19 """Take argument limits the maximum value""" 

20 with client.batch_() as batcher: 

21 batcher.post.create({'title': 'Foo 1', 'published': False}) 

22 batcher.post.create({'title': 'Foo 2', 'published': False}) 

23 batcher.post.create({'title': 'Foo 3', 'published': False}) 

24 

25 total = client.post.count(take=1) 

26 assert total == 1 

27 

28 

29def test_skip(client: Prisma) -> None: 

30 """Skip argument ignores the first N records""" 

31 with client.batch_() as batcher: 

32 batcher.post.create({'title': 'Foo 1', 'published': False}) 

33 batcher.post.create({'title': 'Foo 2', 'published': False}) 

34 batcher.post.create({'title': 'Foo 3', 'published': False}) 

35 

36 total = client.post.count(skip=1) 

37 assert total == 2 

38 

39 

40def test_select(client: Prisma) -> None: 

41 """Selecting a field counts non-null values""" 

42 with client.batch_() as batcher: 

43 batcher.post.create({'title': 'Foo', 'published': False}) 

44 batcher.post.create({'title': 'Foo 2', 'published': False, 'description': 'A'}) 

45 

46 count = client.post.count( 

47 select={}, 

48 ) 

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

50 

51 count = client.post.count( 

52 select={ 

53 'description': True, 

54 }, 

55 ) 

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

57 

58 count = client.post.count( 

59 select={ 

60 '_all': True, 

61 'description': True, 

62 }, 

63 ) 

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