Coverage for databases/sync_tests/types/test_decimal.py: 98%

79 statements  

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

1from decimal import Decimal, getcontext 

2 

3import pytest 

4from dirty_equals import IsPartialDict 

5 

6from prisma import Prisma 

7from prisma.models import Types 

8from prisma._compat import PYDANTIC_V2, model_json_schema 

9 

10DEFAULT_PRECISION = getcontext().prec 

11 

12 

13@pytest.fixture(autouse=True) 

14def setup_decimal_module() -> None: 

15 # ensure that any modifications to the decimal precision 

16 # are not leaked between tests 

17 getcontext().prec = DEFAULT_PRECISION 

18 

19 

20def test_serialising(client: Prisma) -> None: 

21 """Decimal values of any precision are correctly serialised / deserialised""" 

22 model = client.types.create( 

23 data={ 

24 'decimal_': Decimal(1), 

25 }, 

26 ) 

27 assert model.decimal_ == Decimal(1) 

28 

29 getcontext().prec = 16 

30 

31 value = Decimal(1) / Decimal(7) 

32 model = client.types.create( 

33 data={ 

34 'decimal_': value, 

35 }, 

36 ) 

37 assert value == model.decimal_ 

38 assert str(model.decimal_) == '0.1428571428571429' 

39 

40 

41# TODO: split up this test into multiple tests 

42 

43 

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

45 """Finding records by a Decimal value""" 

46 with client.batch_() as batcher: 

47 batcher.types.create({'decimal_': Decimal(1)}) 

48 batcher.types.create({'decimal_': Decimal('2.1234')}) 

49 batcher.types.create({'decimal_': Decimal(3)}) 

50 

51 total = client.types.count( 

52 where={ 

53 'decimal_': Decimal(1), 

54 }, 

55 ) 

56 assert total == 1 

57 

58 found = client.types.find_first( 

59 where={ 

60 'decimal_': { 

61 'equals': Decimal('2.1234'), 

62 }, 

63 }, 

64 ) 

65 assert found is not None 

66 assert str(found.decimal_) == '2.1234' 

67 

68 results = client.types.find_many( 

69 where={ 

70 'decimal_': { 

71 'not_in': [Decimal(1), Decimal(3)], 

72 }, 

73 }, 

74 ) 

75 assert len(results) == 1 

76 assert results[0].decimal_ == Decimal('2.1234') 

77 

78 results = client.types.find_many( 

79 where={ 

80 'decimal_': { 

81 'lt': Decimal(2), 

82 }, 

83 }, 

84 ) 

85 assert len(results) == 1 

86 assert results[0].decimal_ == Decimal(1) 

87 

88 found = client.types.find_first( 

89 where={ 

90 'decimal_': { 

91 'lt': Decimal(1), 

92 }, 

93 }, 

94 ) 

95 assert found is None 

96 

97 results = client.types.find_many( 

98 where={ 

99 'decimal_': { 

100 'lte': Decimal(3), 

101 }, 

102 }, 

103 ) 

104 assert len(results) == 3 

105 

106 found = client.types.find_first( 

107 where={ 

108 'decimal_': { 

109 'lte': Decimal(1), 

110 }, 

111 }, 

112 ) 

113 assert found is not None 

114 assert found.decimal_ == Decimal(1) 

115 

116 found = client.types.find_first( 

117 where={ 

118 'decimal_': { 

119 'lte': Decimal('0.99999'), 

120 }, 

121 }, 

122 ) 

123 assert found is None 

124 

125 found = client.types.find_first( 

126 where={ 

127 'decimal_': { 

128 'gt': Decimal('0.99999'), 

129 }, 

130 }, 

131 ) 

132 assert found is not None 

133 assert found.decimal_ == Decimal(1) 

134 

135 found = client.types.find_first( 

136 where={ 

137 'decimal_': { 

138 'gt': Decimal('4'), 

139 }, 

140 }, 

141 ) 

142 assert found is None 

143 

144 found = client.types.find_first( 

145 where={ 

146 'decimal_': { 

147 'gte': Decimal('1'), 

148 }, 

149 }, 

150 ) 

151 assert found is not None 

152 assert found.decimal_ == Decimal('1') 

153 

154 found = client.types.find_first( 

155 where={ 

156 'decimal_': { 

157 'gte': Decimal('4'), 

158 }, 

159 }, 

160 ) 

161 assert found is None 

162 

163 results = client.types.find_many( 

164 where={ 

165 'decimal_': { 

166 'in': [Decimal(3), Decimal(1), Decimal(2)], 

167 }, 

168 }, 

169 order={ 

170 'decimal_': 'asc', 

171 }, 

172 ) 

173 assert len(results) == 2 

174 assert results[0].decimal_ == Decimal(1) 

175 assert results[1].decimal_ == Decimal(3) 

176 

177 found = client.types.find_first( 

178 where={ 

179 'decimal_': { 

180 'not': Decimal('1'), 

181 }, 

182 }, 

183 order={ 

184 'decimal_': 'asc', 

185 }, 

186 ) 

187 assert found is not None 

188 assert found.decimal_ == Decimal('2.1234') 

189 

190 found = client.types.find_first( 

191 where={ 

192 'decimal_': { 

193 'not': {'equals': Decimal('1')}, 

194 }, 

195 }, 

196 order={ 

197 'decimal_': 'asc', 

198 }, 

199 ) 

200 assert found is not None 

201 assert found.decimal_ == Decimal('2.1234') 

202 

203 

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

205 """None is a valid filter for nullable Decimal fields""" 

206 client.types.create( 

207 { 

208 'string': 'a', 

209 'optional_decimal': None, 

210 }, 

211 ) 

212 client.types.create( 

213 { 

214 'string': 'b', 

215 'optional_decimal': Decimal('3'), 

216 }, 

217 ) 

218 client.types.create( 

219 { 

220 'string': 'c', 

221 'optional_decimal': Decimal('4'), 

222 }, 

223 ) 

224 

225 found = client.types.find_first( 

226 where={ 

227 'NOT': [ 

228 { 

229 'optional_decimal': None, 

230 }, 

231 ], 

232 }, 

233 order={ 

234 'string': 'asc', 

235 }, 

236 ) 

237 assert found is not None 

238 assert found.string == 'b' 

239 assert found.optional_decimal == Decimal('3') 

240 

241 count = client.types.count( 

242 where={ 

243 'optional_decimal': None, 

244 }, 

245 ) 

246 assert count == 1 

247 

248 count = client.types.count( 

249 where={ 

250 'NOT': [ 

251 { 

252 'optional_decimal': None, 

253 }, 

254 ], 

255 }, 

256 ) 

257 assert count == 2 

258 

259 

260def test_json_schema() -> None: 

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

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

263 assert model_json_schema(Types) == IsPartialDict( 

264 properties=IsPartialDict( 

265 { 

266 'decimal_': { 

267 'title': 'Decimal ', 

268 'anyOf': [{'type': 'number'}, {'type': 'string'}], 

269 }, 

270 'optional_decimal': { 

271 'title': 'Optional Decimal', 

272 'anyOf': [ 

273 {'type': 'number'}, 

274 {'type': 'string'}, 

275 {'type': 'null'}, 

276 ], 

277 'default': None, 

278 }, 

279 } 

280 ) 

281 ) 

282 else: 

283 assert model_json_schema(Types) == IsPartialDict( 

284 properties=IsPartialDict( 

285 { 

286 'decimal_': { 

287 'title': 'Decimal ', 

288 'type': 'number', 

289 }, 

290 'optional_decimal': { 

291 'title': 'Optional Decimal', 

292 'type': 'number', 

293 }, 

294 } 

295 ) 

296 )