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

74 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1import pytest 

2 

3from prisma import Prisma 

4from prisma.errors import DataError 

5 

6from ...utils import CURRENT_DATABASE 

7 

8 

9@pytest.mark.asyncio 

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

11 """Finding records by a an integer value""" 

12 async with client.batch_() as batcher: 

13 for i in range(10): 

14 batcher.types.create({'integer': i + 1}) 

15 

16 total = await client.types.count(where={'integer': {'gte': 5}}) 

17 assert total == 6 

18 

19 found = await client.types.find_first( 

20 where={ 

21 'integer': { 

22 'equals': 2, 

23 }, 

24 }, 

25 ) 

26 assert found is not None 

27 assert found.integer == 2 

28 

29 results = await client.types.find_many( 

30 where={ 

31 'integer': { 

32 'in': [1, 5, 7], 

33 }, 

34 }, 

35 order={ 

36 'integer': 'asc', 

37 }, 

38 ) 

39 assert len(results) == 3 

40 assert results[0].integer == 1 

41 assert results[1].integer == 5 

42 assert results[2].integer == 7 

43 

44 results = await client.types.find_many( 

45 where={ 

46 'integer': { 

47 'not_in': [1, 2, 3, 4, 6, 7, 8, 9], 

48 }, 

49 }, 

50 order={ 

51 'integer': 'asc', 

52 }, 

53 ) 

54 assert len(results) == 2 

55 assert results[0].integer == 5 

56 assert results[1].integer == 10 

57 

58 found = await client.types.find_first( 

59 where={ 

60 'integer': { 

61 'lt': 5, 

62 }, 

63 }, 

64 order={ 

65 'integer': 'desc', 

66 }, 

67 ) 

68 assert found is not None 

69 assert found.integer == 4 

70 

71 found = await client.types.find_first( 

72 where={ 

73 'integer': { 

74 'lte': 5, 

75 }, 

76 }, 

77 order={ 

78 'integer': 'desc', 

79 }, 

80 ) 

81 assert found is not None 

82 assert found.integer == 5 

83 

84 found = await client.types.find_first( 

85 where={ 

86 'integer': { 

87 'gt': 5, 

88 }, 

89 }, 

90 order={ 

91 'integer': 'asc', 

92 }, 

93 ) 

94 assert found is not None 

95 assert found.integer == 6 

96 

97 found = await client.types.find_first( 

98 where={ 

99 'integer': { 

100 'gte': 6, 

101 }, 

102 }, 

103 order={ 

104 'integer': 'asc', 

105 }, 

106 ) 

107 assert found is not None 

108 assert found.integer == 6 

109 

110 found = await client.types.find_first( 

111 where={ 

112 'integer': { 

113 'not': 1, 

114 }, 

115 }, 

116 order={'integer': 'asc'}, 

117 ) 

118 assert found is not None 

119 assert found.integer == 2 

120 

121 

122@pytest.mark.asyncio 

123async def test_atomic_update(client: Prisma) -> None: 

124 """Atomically updating an integer value""" 

125 model = await client.types.create({'id': 1, 'integer': 1}) 

126 assert model.integer == 1 

127 

128 updated = await client.types.update( 

129 where={ 

130 'id': 1, 

131 }, 

132 data={ 

133 'integer': {'increment': 5}, 

134 }, 

135 ) 

136 assert updated is not None 

137 assert updated.integer == 6 

138 

139 updated = await client.types.update( 

140 where={ 

141 'id': 1, 

142 }, 

143 data={ 

144 'integer': { 

145 'set': 20, 

146 }, 

147 }, 

148 ) 

149 assert updated is not None 

150 assert updated.integer == 20 

151 

152 updated = await client.types.update( 

153 where={ 

154 'id': 1, 

155 }, 

156 data={ 

157 'integer': { 

158 'decrement': 5, 

159 }, 

160 }, 

161 ) 

162 assert updated is not None 

163 assert updated.integer == 15 

164 

165 updated = await client.types.update( 

166 where={ 

167 'id': 1, 

168 }, 

169 data={ 

170 'integer': { 

171 'multiply': 2, 

172 }, 

173 }, 

174 ) 

175 assert updated is not None 

176 assert updated.integer == 30 

177 

178 

179@pytest.mark.asyncio 

180@pytest.mark.skipif( 

181 CURRENT_DATABASE == 'cockroachdb', 

182 reason='https://github.com/prisma/prisma/issues/16511', 

183) 

184async def test_atomic_update_divide(client: Prisma) -> None: 

185 """Atomically dividing an integer value""" 

186 model = await client.types.create({'id': 1, 'integer': 30}) 

187 assert model.integer == 30 

188 

189 updated = await client.types.update( 

190 where={ 

191 'id': 1, 

192 }, 

193 data={ 

194 'integer': { 

195 'divide': 3, 

196 }, 

197 }, 

198 ) 

199 assert updated is not None 

200 assert updated.integer == 10 

201 

202 

203@pytest.mark.asyncio 

204async def test_atomic_update_invalid_input(client: Prisma) -> None: 

205 """Integer atomic update only allows one field to be passed""" 

206 with pytest.raises(DataError) as exc: 

207 await client.types.update( 

208 where={ 

209 'id': 1, 

210 }, 

211 data={ 

212 'integer': { # type: ignore 

213 'divide': 1, 

214 'multiply': 2, 

215 }, 

216 }, 

217 ) 

218 

219 message = exc.value.args[0] 

220 assert isinstance(message, str) 

221 assert 'Expected exactly one field to be present, got 2' in message 

222 

223 

224@pytest.mark.asyncio 

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

226 """None is a valid filter for nullable Int fields""" 

227 await client.types.create( 

228 { 

229 'string': 'a', 

230 'optional_int': None, 

231 }, 

232 ) 

233 await client.types.create( 

234 { 

235 'string': 'b', 

236 'optional_int': 1, 

237 }, 

238 ) 

239 await client.types.create( 

240 { 

241 'string': 'c', 

242 'optional_int': 2, 

243 }, 

244 ) 

245 

246 found = await client.types.find_first( 

247 where={ 

248 'NOT': [ 

249 { 

250 'optional_int': None, 

251 }, 

252 ], 

253 }, 

254 order={ 

255 'string': 'asc', 

256 }, 

257 ) 

258 assert found is not None 

259 assert found.string == 'b' 

260 assert found.optional_int == 1 

261 

262 count = await client.types.count( 

263 where={ 

264 'optional_int': None, 

265 }, 

266 ) 

267 assert count == 1 

268 

269 count = await client.types.count( 

270 where={ 

271 'NOT': [ 

272 { 

273 'optional_int': None, 

274 }, 

275 ], 

276 }, 

277 ) 

278 assert count == 2