Coverage for databases/sync_tests/test_find_unique.py: 100%

88 statements  

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

1import pytest 

2 

3from prisma import Prisma, errors 

4 

5 

6def test_find_unique_id_field(client: Prisma) -> None: 

7 """Finding a record by an ID field""" 

8 post = client.post.create( 

9 { 

10 'title': 'My post title!', 

11 'published': False, 

12 } 

13 ) 

14 assert isinstance(post.id, str) 

15 

16 found = client.post.find_unique(where={'id': post.id}) 

17 assert found is not None 

18 assert found == post 

19 

20 

21def test_find_unique_missing_required_args(client: Prisma) -> None: 

22 """Missing field raises an error""" 

23 with pytest.raises(TypeError): 

24 client.post.find_unique() # type: ignore[call-arg] 

25 

26 # TODO: more constrained error type 

27 with pytest.raises(errors.DataError): 

28 client.post.find_unique( 

29 { 

30 'title': 'Hi from Prisma!', # pyright: ignore 

31 } 

32 ) 

33 

34 

35def test_find_unique_no_match(client: Prisma) -> None: 

36 """Looking for non-existent record does not error""" 

37 found = client.post.find_unique(where={'id': 'sjbsjahs'}) 

38 assert found is None 

39 

40 

41def test_multiple_fields_are_not_allowed(client: Prisma) -> None: 

42 """Multiple fields cannot be passed at once""" 

43 with pytest.raises(errors.DataError): 

44 client.user.find_unique( 

45 where={ 

46 'id': 'foo', 

47 'email': 'foo', # type: ignore 

48 }, 

49 ) 

50 

51 

52def test_unique1(client: Prisma) -> None: 

53 """Standard combined unique constraint""" 

54 model = client.unique1.create( 

55 data={ 

56 'name': 'Robert', 

57 'surname': 'Craigie', 

58 }, 

59 ) 

60 found = client.unique1.find_unique( 

61 where={ 

62 'name_surname': { 

63 'name': 'Robert', 

64 'surname': 'Craigie', 

65 }, 

66 }, 

67 ) 

68 assert found is not None 

69 assert found.name == model.name 

70 assert found.surname == model.surname 

71 

72 

73def test_unique2(client: Prisma) -> None: 

74 """Combined unique constraint with an aditional unique field""" 

75 model = client.unique2.create( 

76 data={ 

77 'name': 'Robert', 

78 'surname': 'Craigie', 

79 }, 

80 ) 

81 

82 found = client.unique2.find_unique( 

83 where={ 

84 'name_surname': { 

85 'name': 'Robert', 

86 'surname': 'Craigie', 

87 }, 

88 }, 

89 ) 

90 assert found is not None 

91 assert found.name == model.name 

92 assert found.surname == model.surname 

93 

94 found = client.unique2.find_unique( 

95 where={ 

96 'surname': 'Craigie', 

97 }, 

98 ) 

99 assert found is not None 

100 assert found.name == model.name 

101 assert found.surname == model.surname 

102 

103 

104def test_unique3(client: Prisma) -> None: 

105 """Combined unique constraint with an ID field and a unique field""" 

106 model = client.unique3.create( 

107 data={ 

108 'name': 'Robert', 

109 'surname': 'Craigie', 

110 }, 

111 ) 

112 

113 found = client.unique3.find_unique( 

114 where={ 

115 'name_surname': { 

116 'name': 'Robert', 

117 'surname': 'Craigie', 

118 }, 

119 }, 

120 ) 

121 assert found is not None 

122 assert found.id == model.id 

123 

124 found = client.unique3.find_unique( 

125 where={ 

126 'surname': 'Craigie', 

127 }, 

128 ) 

129 assert found is not None 

130 assert found.id == model.id 

131 

132 found = client.unique3.find_unique( 

133 where={ 

134 'id': model.id, 

135 }, 

136 ) 

137 assert found is not None 

138 assert found.id == model.id 

139 

140 

141def test_unique4(client: Prisma) -> None: 

142 """Explicitly named unique constraint""" 

143 model = client.unique4.create( 

144 data={ 

145 'name': 'Robert', 

146 'surname': 'Craigie', 

147 }, 

148 ) 

149 

150 found = client.unique4.find_unique( 

151 where={ 

152 'my_unique': { 

153 'name': 'Robert', 

154 'surname': 'Craigie', 

155 }, 

156 }, 

157 ) 

158 assert found is not None 

159 assert found.name == model.name 

160 assert found.surname == model.surname 

161 

162 

163def test_unique5(client: Prisma) -> None: 

164 """Combined unique constraint with 3 fields""" 

165 model = client.unique5.create( 

166 data={ 

167 'name': 'Robert', 

168 'middlename': 'Cosmo', 

169 'surname': 'Craigie', 

170 }, 

171 ) 

172 

173 found = client.unique5.find_unique( 

174 where={ 

175 'name_middlename_surname': { 

176 'name': 'Robert', 

177 'middlename': 'Cosmo', 

178 'surname': 'Craigie', 

179 }, 

180 }, 

181 ) 

182 assert found is not None 

183 assert found.name == model.name 

184 assert found.middlename == model.middlename 

185 assert found.surname == model.surname 

186 

187 

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

189 """Standard combined ID constraint""" 

190 model = client.id1.create( 

191 data={ 

192 'name': 'Robert', 

193 'surname': 'Craigie', 

194 }, 

195 ) 

196 found = client.id1.find_unique( 

197 where={ 

198 'name_surname': { 

199 'name': 'Robert', 

200 'surname': 'Craigie', 

201 }, 

202 }, 

203 ) 

204 assert found is not None 

205 assert found.name == model.name 

206 assert found.surname == model.surname 

207 

208 

209def test_id2(client: Prisma) -> None: 

210 """Combined ID constraint with a unique field""" 

211 model = client.id2.create( 

212 data={ 

213 'name': 'Robert', 

214 'surname': 'Craigie', 

215 }, 

216 ) 

217 

218 found = client.id2.find_unique( 

219 where={ 

220 'name_surname': { 

221 'name': 'Robert', 

222 'surname': 'Craigie', 

223 }, 

224 }, 

225 ) 

226 assert found is not None 

227 assert found.name == model.name 

228 assert found.surname == model.surname 

229 

230 found = client.id2.find_unique( 

231 where={ 

232 'surname': 'Craigie', 

233 }, 

234 ) 

235 assert found is not None 

236 assert found.name == model.name 

237 assert found.surname == model.surname 

238 

239 

240def test_id3(client: Prisma) -> None: 

241 """Explicitly named combined ID constraint""" 

242 model = client.id3.create( 

243 data={ 

244 'name': 'Robert', 

245 'surname': 'Craigie', 

246 }, 

247 ) 

248 

249 found = client.id3.find_unique( 

250 where={ 

251 'my_id': { 

252 'name': 'Robert', 

253 'surname': 'Craigie', 

254 }, 

255 }, 

256 ) 

257 assert found is not None 

258 assert found.name == model.name 

259 assert found.surname == model.surname 

260 

261 

262def test_id4(client: Prisma) -> None: 

263 """Combined ID constraint with 3 fields""" 

264 model = client.id4.create( 

265 data={ 

266 'name': 'Robert', 

267 'middlename': 'Cosmo', 

268 'surname': 'Craigie', 

269 }, 

270 ) 

271 

272 found = client.id4.find_unique( 

273 where={ 

274 'name_middlename_surname': { 

275 'name': 'Robert', 

276 'middlename': 'Cosmo', 

277 'surname': 'Craigie', 

278 }, 

279 }, 

280 ) 

281 assert found is not None 

282 assert found.name == model.name 

283 assert found.middlename == model.middlename 

284 assert found.surname == model.surname