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

72 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 

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

8 """Finding records by a a float value""" 

9 with client.batch_() as batcher: 

10 for i in range(10): 

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

12 

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

14 assert total == 6 

15 

16 found = client.types.find_first( 

17 where={ 

18 'float_': { 

19 'equals': 2, 

20 }, 

21 }, 

22 ) 

23 assert found is not None 

24 assert found.float_ == 2 

25 

26 results = client.types.find_many( 

27 where={ 

28 'float_': { 

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

30 }, 

31 }, 

32 order={ 

33 'float_': 'asc', 

34 }, 

35 ) 

36 assert len(results) == 3 

37 assert results[0].float_ == 1 

38 assert results[1].float_ == 5 

39 assert results[2].float_ == 7 

40 

41 results = client.types.find_many( 

42 where={ 

43 'float_': { 

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

45 }, 

46 }, 

47 order={ 

48 'float_': 'asc', 

49 }, 

50 ) 

51 assert len(results) == 2 

52 assert results[0].float_ == 5 

53 assert results[1].float_ == 10 

54 

55 found = client.types.find_first( 

56 where={ 

57 'float_': { 

58 'lt': 5, 

59 }, 

60 }, 

61 order={ 

62 'float_': 'desc', 

63 }, 

64 ) 

65 assert found is not None 

66 assert found.float_ == 4 

67 

68 found = client.types.find_first( 

69 where={ 

70 'float_': { 

71 'lte': 5, 

72 }, 

73 }, 

74 order={ 

75 'float_': 'desc', 

76 }, 

77 ) 

78 assert found is not None 

79 assert found.float_ == 5 

80 

81 found = client.types.find_first( 

82 where={ 

83 'float_': { 

84 'gt': 5, 

85 }, 

86 }, 

87 order={ 

88 'float_': 'asc', 

89 }, 

90 ) 

91 assert found is not None 

92 assert found.float_ == 6 

93 

94 found = client.types.find_first( 

95 where={ 

96 'float_': { 

97 'gte': 6, 

98 }, 

99 }, 

100 order={ 

101 'float_': 'asc', 

102 }, 

103 ) 

104 assert found is not None 

105 assert found.float_ == 6 

106 

107 found = client.types.find_first( 

108 where={ 

109 'float_': { 

110 'not': 1, 

111 }, 

112 }, 

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

114 ) 

115 assert found is not None 

116 assert found.float_ == 2 

117 

118 

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

120 """Atomically updating a float value""" 

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

122 assert model.float_ == 1 

123 

124 updated = client.types.update( 

125 where={ 

126 'id': 1, 

127 }, 

128 data={ 

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

130 }, 

131 ) 

132 assert updated is not None 

133 assert updated.float_ == 6 

134 

135 updated = client.types.update( 

136 where={ 

137 'id': 1, 

138 }, 

139 data={ 

140 'float_': { 

141 'set': 20, 

142 }, 

143 }, 

144 ) 

145 assert updated is not None 

146 assert updated.float_ == 20 

147 

148 updated = client.types.update( 

149 where={ 

150 'id': 1, 

151 }, 

152 data={ 

153 'float_': { 

154 'decrement': 5, 

155 }, 

156 }, 

157 ) 

158 assert updated is not None 

159 assert updated.float_ == 15 

160 

161 updated = client.types.update( 

162 where={ 

163 'id': 1, 

164 }, 

165 data={ 

166 'float_': { 

167 'multiply': 2, 

168 }, 

169 }, 

170 ) 

171 assert updated is not None 

172 assert updated.float_ == 30 

173 

174 updated = client.types.update( 

175 where={ 

176 'id': 1, 

177 }, 

178 data={ 

179 'float_': { 

180 'divide': 3, 

181 }, 

182 }, 

183 ) 

184 assert updated is not None 

185 assert updated.float_ == 10 

186 

187 

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

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

190 with pytest.raises(DataError) as exc: 

191 client.types.update( 

192 where={ 

193 'id': 1, 

194 }, 

195 data={ 

196 'float_': { # type: ignore 

197 'divide': 1, 

198 'multiply': 2, 

199 }, 

200 }, 

201 ) 

202 

203 message = exc.value.args[0] 

204 assert isinstance(message, str) 

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

206 

207 

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

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

210 client.types.create( 

211 { 

212 'string': 'a', 

213 'optional_float': None, 

214 }, 

215 ) 

216 client.types.create( 

217 { 

218 'string': 'b', 

219 'optional_float': 1.2, 

220 }, 

221 ) 

222 client.types.create( 

223 { 

224 'string': 'c', 

225 'optional_float': 5, 

226 }, 

227 ) 

228 

229 found = client.types.find_first( 

230 where={ 

231 'NOT': [ 

232 { 

233 'optional_float': None, 

234 }, 

235 ], 

236 }, 

237 order={ 

238 'string': 'asc', 

239 }, 

240 ) 

241 assert found is not None 

242 assert found.string == 'b' 

243 assert found.optional_float == 1.2 

244 

245 count = client.types.count( 

246 where={ 

247 'optional_float': None, 

248 }, 

249 ) 

250 assert count == 1 

251 

252 count = client.types.count( 

253 where={ 

254 'NOT': [ 

255 { 

256 'optional_float': None, 

257 }, 

258 ], 

259 }, 

260 ) 

261 assert count == 2