Coverage for databases/sync_tests/types/test_bigint.py: 100%

69 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 

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

10 """Finding records by a BigInt value""" 

11 with client.batch_() as batcher: 

12 for i in range(10): 

13 batcher.types.create({'bigint': i + 1}) 

14 

15 total = client.types.count(where={'bigint': {'gte': 5}}) 

16 assert total == 6 

17 

18 found = client.types.find_first( 

19 where={ 

20 'bigint': { 

21 'equals': 2, 

22 }, 

23 }, 

24 ) 

25 assert found is not None 

26 assert found.bigint == 2 

27 

28 results = client.types.find_many( 

29 where={ 

30 'bigint': { 

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

32 }, 

33 }, 

34 order={ 

35 'bigint': 'asc', 

36 }, 

37 ) 

38 assert len(results) == 3 

39 assert results[0].bigint == 1 

40 assert results[1].bigint == 5 

41 assert results[2].bigint == 7 

42 

43 results = client.types.find_many( 

44 where={ 

45 'bigint': { 

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

47 }, 

48 }, 

49 order={ 

50 'bigint': 'asc', 

51 }, 

52 ) 

53 assert len(results) == 2 

54 assert results[0].bigint == 5 

55 assert results[1].bigint == 10 

56 

57 found = client.types.find_first( 

58 where={ 

59 'bigint': { 

60 'lt': 5, 

61 }, 

62 }, 

63 order={ 

64 'bigint': 'desc', 

65 }, 

66 ) 

67 assert found is not None 

68 assert found.bigint == 4 

69 

70 found = client.types.find_first( 

71 where={ 

72 'bigint': { 

73 'lte': 5, 

74 }, 

75 }, 

76 order={ 

77 'bigint': 'desc', 

78 }, 

79 ) 

80 assert found is not None 

81 assert found.bigint == 5 

82 

83 found = client.types.find_first( 

84 where={ 

85 'bigint': { 

86 'gt': 5, 

87 }, 

88 }, 

89 order={ 

90 'bigint': 'asc', 

91 }, 

92 ) 

93 assert found is not None 

94 assert found.bigint == 6 

95 

96 found = client.types.find_first( 

97 where={ 

98 'bigint': { 

99 'gte': 6, 

100 }, 

101 }, 

102 order={ 

103 'bigint': 'asc', 

104 }, 

105 ) 

106 assert found is not None 

107 assert found.bigint == 6 

108 

109 found = client.types.find_first( 

110 where={ 

111 'bigint': { 

112 'not': 1, 

113 }, 

114 }, 

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

116 ) 

117 assert found is not None 

118 assert found.bigint == 2 

119 

120 

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

122 """Atomically updating a BigInt value""" 

123 model = client.types.create({'id': 1, 'bigint': 1}) 

124 assert model.bigint == 1 

125 

126 updated = client.types.update( 

127 where={ 

128 'id': 1, 

129 }, 

130 data={ 

131 'bigint': {'increment': 5}, 

132 }, 

133 ) 

134 assert updated is not None 

135 assert updated.bigint == 6 

136 

137 updated = client.types.update( 

138 where={ 

139 'id': 1, 

140 }, 

141 data={ 

142 'bigint': { 

143 'set': 20, 

144 }, 

145 }, 

146 ) 

147 assert updated is not None 

148 assert updated.bigint == 20 

149 

150 updated = client.types.update( 

151 where={ 

152 'id': 1, 

153 }, 

154 data={ 

155 'bigint': { 

156 'decrement': 5, 

157 }, 

158 }, 

159 ) 

160 assert updated is not None 

161 assert updated.bigint == 15 

162 

163 updated = client.types.update( 

164 where={ 

165 'id': 1, 

166 }, 

167 data={ 

168 'bigint': { 

169 'multiply': 2, 

170 }, 

171 }, 

172 ) 

173 assert updated is not None 

174 assert updated.bigint == 30 

175 

176 

177@pytest.mark.skipif( 

178 CURRENT_DATABASE == 'cockroachdb', 

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

180) 

181def test_atomic_update_divide(client: Prisma) -> None: 

182 """Atomically dividing a `BigInt` value""" 

183 model = client.types.create({'id': 1, 'bigint': 30}) 

184 assert model.bigint == 30 

185 

186 updated = client.types.update( 

187 where={ 

188 'id': 1, 

189 }, 

190 data={ 

191 'bigint': { 

192 'divide': 3, 

193 }, 

194 }, 

195 ) 

196 assert updated is not None 

197 assert updated.bigint == 10 

198 

199 

200def test_atomic_update_invalid_input(client: Prisma) -> None: 

201 """BigInt atomic update only allows one field to be passed""" 

202 with pytest.raises(DataError) as exc: 

203 client.types.update( 

204 where={ 

205 'id': 1, 

206 }, 

207 data={ 

208 'bigint': { # type: ignore 

209 'divide': 1, 

210 'multiply': 2, 

211 }, 

212 }, 

213 ) 

214 

215 message = exc.value.args[0] 

216 assert isinstance(message, str) 

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

218 

219 

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

221 """None is a valid filter for nullable BigInt fields""" 

222 client.types.create( 

223 { 

224 'string': 'a', 

225 'optional_bigint': None, 

226 }, 

227 ) 

228 client.types.create( 

229 { 

230 'string': 'b', 

231 'optional_bigint': 12437823782382, 

232 }, 

233 ) 

234 client.types.create( 

235 { 

236 'string': 'c', 

237 'optional_bigint': 8239829842494, 

238 }, 

239 ) 

240 

241 found = client.types.find_first( 

242 where={ 

243 'NOT': [ 

244 { 

245 'optional_bigint': None, 

246 }, 

247 ], 

248 }, 

249 order={ 

250 'string': 'asc', 

251 }, 

252 ) 

253 assert found is not None 

254 assert found.string == 'b' 

255 assert found.optional_bigint == 12437823782382 

256 

257 count = client.types.count( 

258 where={ 

259 'optional_bigint': None, 

260 }, 

261 ) 

262 assert count == 1 

263 

264 count = client.types.count( 

265 where={ 

266 'NOT': [ 

267 { 

268 'optional_bigint': None, 

269 }, 

270 ], 

271 }, 

272 ) 

273 assert count == 2