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

76 statements  

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

1import pytest 

2 

3from prisma import Prisma 

4from prisma.errors import DataError 

5 

6 

7@pytest.mark.asyncio 

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

9 """Finding records by a a float value""" 

10 async with client.batch_() as batcher: 

11 for i in range(10): 

12 batcher.types.create({'float_': i + 1}) 

13 

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

15 assert total == 6 

16 

17 found = await client.types.find_first( 

18 where={ 

19 'float_': { 

20 'equals': 2, 

21 }, 

22 }, 

23 ) 

24 assert found is not None 

25 assert found.float_ == 2 

26 

27 results = await client.types.find_many( 

28 where={ 

29 'float_': { 

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

31 }, 

32 }, 

33 order={ 

34 'float_': 'asc', 

35 }, 

36 ) 

37 assert len(results) == 3 

38 assert results[0].float_ == 1 

39 assert results[1].float_ == 5 

40 assert results[2].float_ == 7 

41 

42 results = await client.types.find_many( 

43 where={ 

44 'float_': { 

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

46 }, 

47 }, 

48 order={ 

49 'float_': 'asc', 

50 }, 

51 ) 

52 assert len(results) == 2 

53 assert results[0].float_ == 5 

54 assert results[1].float_ == 10 

55 

56 found = await client.types.find_first( 

57 where={ 

58 'float_': { 

59 'lt': 5, 

60 }, 

61 }, 

62 order={ 

63 'float_': 'desc', 

64 }, 

65 ) 

66 assert found is not None 

67 assert found.float_ == 4 

68 

69 found = await client.types.find_first( 

70 where={ 

71 'float_': { 

72 'lte': 5, 

73 }, 

74 }, 

75 order={ 

76 'float_': 'desc', 

77 }, 

78 ) 

79 assert found is not None 

80 assert found.float_ == 5 

81 

82 found = await client.types.find_first( 

83 where={ 

84 'float_': { 

85 'gt': 5, 

86 }, 

87 }, 

88 order={ 

89 'float_': 'asc', 

90 }, 

91 ) 

92 assert found is not None 

93 assert found.float_ == 6 

94 

95 found = await client.types.find_first( 

96 where={ 

97 'float_': { 

98 'gte': 6, 

99 }, 

100 }, 

101 order={ 

102 'float_': 'asc', 

103 }, 

104 ) 

105 assert found is not None 

106 assert found.float_ == 6 

107 

108 found = await client.types.find_first( 

109 where={ 

110 'float_': { 

111 'not': 1, 

112 }, 

113 }, 

114 order={'float_': 'asc'}, 

115 ) 

116 assert found is not None 

117 assert found.float_ == 2 

118 

119 

120@pytest.mark.asyncio 

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

122 """Atomically updating a float value""" 

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

124 assert model.float_ == 1 

125 

126 updated = await client.types.update( 

127 where={ 

128 'id': 1, 

129 }, 

130 data={ 

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

132 }, 

133 ) 

134 assert updated is not None 

135 assert updated.float_ == 6 

136 

137 updated = await client.types.update( 

138 where={ 

139 'id': 1, 

140 }, 

141 data={ 

142 'float_': { 

143 'set': 20, 

144 }, 

145 }, 

146 ) 

147 assert updated is not None 

148 assert updated.float_ == 20 

149 

150 updated = await client.types.update( 

151 where={ 

152 'id': 1, 

153 }, 

154 data={ 

155 'float_': { 

156 'decrement': 5, 

157 }, 

158 }, 

159 ) 

160 assert updated is not None 

161 assert updated.float_ == 15 

162 

163 updated = await client.types.update( 

164 where={ 

165 'id': 1, 

166 }, 

167 data={ 

168 'float_': { 

169 'multiply': 2, 

170 }, 

171 }, 

172 ) 

173 assert updated is not None 

174 assert updated.float_ == 30 

175 

176 updated = await client.types.update( 

177 where={ 

178 'id': 1, 

179 }, 

180 data={ 

181 'float_': { 

182 'divide': 3, 

183 }, 

184 }, 

185 ) 

186 assert updated is not None 

187 assert updated.float_ == 10 

188 

189 

190@pytest.mark.asyncio 

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

192 """Float atomic update only allows one field to be passed""" 

193 with pytest.raises(DataError) as exc: 

194 await client.types.update( 

195 where={ 

196 'id': 1, 

197 }, 

198 data={ 

199 'float_': { # type: ignore 

200 'divide': 1, 

201 'multiply': 2, 

202 }, 

203 }, 

204 ) 

205 

206 message = exc.value.args[0] 

207 assert isinstance(message, str) 

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

209 

210 

211@pytest.mark.asyncio 

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

213 """None is a valid filter for nullable Float fields""" 

214 await client.types.create( 

215 { 

216 'string': 'a', 

217 'optional_float': None, 

218 }, 

219 ) 

220 await client.types.create( 

221 { 

222 'string': 'b', 

223 'optional_float': 1.2, 

224 }, 

225 ) 

226 await client.types.create( 

227 { 

228 'string': 'c', 

229 'optional_float': 5, 

230 }, 

231 ) 

232 

233 found = await client.types.find_first( 

234 where={ 

235 'NOT': [ 

236 { 

237 'optional_float': None, 

238 }, 

239 ], 

240 }, 

241 order={ 

242 'string': 'asc', 

243 }, 

244 ) 

245 assert found is not None 

246 assert found.string == 'b' 

247 assert found.optional_float == 1.2 

248 

249 count = await client.types.count( 

250 where={ 

251 'optional_float': None, 

252 }, 

253 ) 

254 assert count == 1 

255 

256 count = await client.types.count( 

257 where={ 

258 'NOT': [ 

259 { 

260 'optional_float': None, 

261 }, 

262 ], 

263 }, 

264 ) 

265 assert count == 2