Coverage for databases/sync_tests/types/test_string.py: 97%

54 statements  

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

1from dirty_equals import IsPartialDict 

2 

3from prisma import Prisma 

4from prisma.models import Types 

5from prisma._compat import PYDANTIC_V2, model_json_schema 

6 

7 

8def test_filtering(client: Prisma) -> None: 

9 """Finding records by a String value""" 

10 start = ord('a') 

11 with client.batch_() as batcher: 

12 for i in range(10): 

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

14 

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

16 assert total == 6 

17 

18 found = client.types.find_first( 

19 where={ 

20 'string': { 

21 'equals': 'a', 

22 }, 

23 }, 

24 ) 

25 assert found is not None 

26 assert found.string == 'a' 

27 

28 results = client.types.find_many( 

29 where={ 

30 'string': { 

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

32 }, 

33 }, 

34 order={ 

35 'string': 'asc', 

36 }, 

37 ) 

38 assert len(results) == 3 

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

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

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

42 

43 results = client.types.find_many( 

44 where={ 

45 'string': { 

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

47 }, 

48 }, 

49 order={ 

50 'string': 'asc', 

51 }, 

52 ) 

53 assert len(results) == 2 

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

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

56 

57 found = client.types.find_first( 

58 where={ 

59 'string': { 

60 'lt': 'g', 

61 }, 

62 }, 

63 order={ 

64 'string': 'desc', 

65 }, 

66 ) 

67 assert found is not None 

68 assert found.string == 'f' 

69 

70 found = client.types.find_first( 

71 where={ 

72 'string': { 

73 'lte': 'f', 

74 }, 

75 }, 

76 order={ 

77 'string': 'desc', 

78 }, 

79 ) 

80 assert found is not None 

81 assert found.string == 'f' 

82 

83 found = client.types.find_first( 

84 where={ 

85 'string': { 

86 'gt': 'f', 

87 }, 

88 }, 

89 order={ 

90 'string': 'asc', 

91 }, 

92 ) 

93 assert found is not None 

94 assert found.string == 'g' 

95 

96 found = client.types.find_first( 

97 where={ 

98 'string': { 

99 'gte': 'g', 

100 }, 

101 }, 

102 order={ 

103 'string': 'asc', 

104 }, 

105 ) 

106 assert found is not None 

107 assert found.string == 'g' 

108 

109 found = client.types.find_first( 

110 where={ 

111 'string': { 

112 'not': 'a', 

113 }, 

114 }, 

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

116 ) 

117 assert found is not None 

118 assert found.string == 'b' 

119 

120 

121def test_filtering_nulls(client: Prisma) -> None: 

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

123 client.types.create( 

124 { 

125 'string': 'a', 

126 'optional_string': None, 

127 }, 

128 ) 

129 client.types.create( 

130 { 

131 'string': 'b', 

132 'optional_string': 'null', 

133 }, 

134 ) 

135 client.types.create( 

136 { 

137 'string': 'c', 

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

139 }, 

140 ) 

141 

142 found = client.types.find_first( 

143 where={ 

144 'NOT': [ 

145 { 

146 'optional_string': None, 

147 }, 

148 ], 

149 }, 

150 order={ 

151 'string': 'asc', 

152 }, 

153 ) 

154 assert found is not None 

155 assert found.string == 'b' 

156 assert found.optional_string == 'null' 

157 

158 count = client.types.count( 

159 where={ 

160 'optional_string': None, 

161 }, 

162 ) 

163 assert count == 1 

164 

165 count = client.types.count( 

166 where={ 

167 'NOT': [ 

168 { 

169 'optional_string': None, 

170 }, 

171 ], 

172 }, 

173 ) 

174 assert count == 2 

175 

176 # TODO: test passing 'null' 

177 

178 

179def test_json_schema() -> None: 

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

181 if PYDANTIC_V2: 181 ↛ 198line 181 didn't jump to line 198, because the condition on line 181 was never false

182 assert model_json_schema(Types) == IsPartialDict( 

183 properties=IsPartialDict( 

184 { 

185 'string': { 

186 'title': 'String', 

187 'type': 'string', 

188 }, 

189 'optional_string': { 

190 'title': 'Optional String', 

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

192 'default': None, 

193 }, 

194 } 

195 ) 

196 ) 

197 else: 

198 assert model_json_schema(Types) == IsPartialDict( 

199 properties=IsPartialDict( 

200 { 

201 'string': { 

202 'title': 'String', 

203 'type': 'string', 

204 }, 

205 'optional_string': { 

206 'title': 'Optional String', 

207 'type': 'string', 

208 }, 

209 } 

210 ) 

211 )