Coverage for databases/tests/types/test_bool.py: 100%

34 statements  

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

1import pytest 

2 

3from prisma import Prisma 

4 

5 

6@pytest.mark.asyncio 

7async def test_filtering(client: Prisma) -> None: 

8 """Finding records by a Boolean value""" 

9 async with client.batch_() as batcher: 

10 for i in range(10): 

11 batcher.types.create({'bool_': i % 2 == 0}) 

12 

13 total = await client.types.count(where={'bool_': {'equals': True}}) 

14 assert total == 5 

15 

16 found = await client.types.find_first( 

17 where={ 

18 'bool_': { 

19 'equals': False, 

20 }, 

21 }, 

22 ) 

23 assert found is not None 

24 assert found.bool_ is False 

25 

26 found = await client.types.find_first( 

27 where={ 

28 'bool_': { 

29 'not': True, 

30 }, 

31 }, 

32 ) 

33 assert found is not None 

34 assert found.bool_ is False 

35 

36 found = await client.types.find_first( 

37 where={ 

38 'bool_': { 

39 'not': False, 

40 }, 

41 }, 

42 ) 

43 assert found is not None 

44 assert found.bool_ is True 

45 

46 found = await client.types.find_first( 

47 where={ 

48 'bool_': False, 

49 }, 

50 ) 

51 assert found is not None 

52 assert found.bool_ is False 

53 

54 

55@pytest.mark.asyncio 

56async def test_filtering_nulls(client: Prisma) -> None: 

57 """None is a valid filter for nullable Boolean fields""" 

58 await client.types.create( 

59 { 

60 'string': 'a', 

61 'optional_bool': None, 

62 }, 

63 ) 

64 await client.types.create( 

65 { 

66 'string': 'b', 

67 'optional_bool': True, 

68 }, 

69 ) 

70 await client.types.create( 

71 { 

72 'string': 'c', 

73 'optional_bool': False, 

74 }, 

75 ) 

76 

77 found = await client.types.find_first( 

78 where={ 

79 'NOT': [ 

80 { 

81 'optional_bool': None, 

82 }, 

83 ], 

84 }, 

85 order={ 

86 'string': 'asc', 

87 }, 

88 ) 

89 assert found is not None 

90 assert found.string == 'b' 

91 assert found.optional_bool is True 

92 

93 count = await client.types.count( 

94 where={ 

95 'optional_bool': None, 

96 }, 

97 ) 

98 assert count == 1 

99 

100 count = await client.types.count( 

101 where={ 

102 'NOT': [ 

103 { 

104 'optional_bool': None, 

105 }, 

106 ], 

107 }, 

108 ) 

109 assert count == 2