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

57 statements  

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

1import pytest 

2from dirty_equals import IsPartialDict 

3 

4from prisma import Prisma 

5from prisma.models import Types 

6from prisma._compat import PYDANTIC_V2, model_json_schema 

7 

8 

9@pytest.mark.asyncio 

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

11 """Finding records by a String value""" 

12 start = ord('a') 

13 async with client.batch_() as batcher: 

14 for i in range(10): 

15 batcher.types.create({'string': chr(start + i)}) 

16 

17 total = await client.types.count(where={'string': {'gte': 'e'}}) 

18 assert total == 6 

19 

20 found = await client.types.find_first( 

21 where={ 

22 'string': { 

23 'equals': 'a', 

24 }, 

25 }, 

26 ) 

27 assert found is not None 

28 assert found.string == 'a' 

29 

30 results = await client.types.find_many( 

31 where={ 

32 'string': { 

33 'in': ['a', 'd', 'f', 'z'], 

34 }, 

35 }, 

36 order={ 

37 'string': 'asc', 

38 }, 

39 ) 

40 assert len(results) == 3 

41 assert results[0].string == 'a' 

42 assert results[1].string == 'd' 

43 assert results[2].string == 'f' 

44 

45 results = await client.types.find_many( 

46 where={ 

47 'string': { 

48 'not_in': ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'i'], 

49 }, 

50 }, 

51 order={ 

52 'string': 'asc', 

53 }, 

54 ) 

55 assert len(results) == 2 

56 assert results[0].string == 'e' 

57 assert results[1].string == 'j' 

58 

59 found = await client.types.find_first( 

60 where={ 

61 'string': { 

62 'lt': 'g', 

63 }, 

64 }, 

65 order={ 

66 'string': 'desc', 

67 }, 

68 ) 

69 assert found is not None 

70 assert found.string == 'f' 

71 

72 found = await client.types.find_first( 

73 where={ 

74 'string': { 

75 'lte': 'f', 

76 }, 

77 }, 

78 order={ 

79 'string': 'desc', 

80 }, 

81 ) 

82 assert found is not None 

83 assert found.string == 'f' 

84 

85 found = await client.types.find_first( 

86 where={ 

87 'string': { 

88 'gt': 'f', 

89 }, 

90 }, 

91 order={ 

92 'string': 'asc', 

93 }, 

94 ) 

95 assert found is not None 

96 assert found.string == 'g' 

97 

98 found = await client.types.find_first( 

99 where={ 

100 'string': { 

101 'gte': 'g', 

102 }, 

103 }, 

104 order={ 

105 'string': 'asc', 

106 }, 

107 ) 

108 assert found is not None 

109 assert found.string == 'g' 

110 

111 found = await client.types.find_first( 

112 where={ 

113 'string': { 

114 'not': 'a', 

115 }, 

116 }, 

117 order={'string': 'asc'}, 

118 ) 

119 assert found is not None 

120 assert found.string == 'b' 

121 

122 

123@pytest.mark.asyncio 

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

125 """None is a valid filter for nullable String fields""" 

126 await client.types.create( 

127 { 

128 'string': 'a', 

129 'optional_string': None, 

130 }, 

131 ) 

132 await client.types.create( 

133 { 

134 'string': 'b', 

135 'optional_string': 'null', 

136 }, 

137 ) 

138 await client.types.create( 

139 { 

140 'string': 'c', 

141 'optional_string': 'robert@craigie.dev', 

142 }, 

143 ) 

144 

145 found = await client.types.find_first( 

146 where={ 

147 'NOT': [ 

148 { 

149 'optional_string': None, 

150 }, 

151 ], 

152 }, 

153 order={ 

154 'string': 'asc', 

155 }, 

156 ) 

157 assert found is not None 

158 assert found.string == 'b' 

159 assert found.optional_string == 'null' 

160 

161 count = await client.types.count( 

162 where={ 

163 'optional_string': None, 

164 }, 

165 ) 

166 assert count == 1 

167 

168 count = await client.types.count( 

169 where={ 

170 'NOT': [ 

171 { 

172 'optional_string': None, 

173 }, 

174 ], 

175 }, 

176 ) 

177 assert count == 2 

178 

179 # TODO: test passing 'null' 

180 

181 

182def test_json_schema() -> None: 

183 """Ensure a JSON Schema definition can be created""" 

184 if PYDANTIC_V2: 

185 assert model_json_schema(Types) == IsPartialDict( 

186 properties=IsPartialDict( 

187 { 

188 'string': { 

189 'title': 'String', 

190 'type': 'string', 

191 }, 

192 'optional_string': { 

193 'title': 'Optional String', 

194 'anyOf': [{'type': 'string'}, {'type': 'null'}], 

195 'default': None, 

196 }, 

197 } 

198 ) 

199 ) 

200 else: 

201 assert model_json_schema(Types) == IsPartialDict( 

202 properties=IsPartialDict( 

203 { 

204 'string': { 

205 'title': 'String', 

206 'type': 'string', 

207 }, 

208 'optional_string': { 

209 'title': 'Optional String', 

210 'type': 'string', 

211 }, 

212 } 

213 ) 

214 )