Coverage for databases/tests/test_find_unique_or_raise.py: 100%

87 statements  

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

1import pytest 

2 

3from prisma import Prisma, errors 

4 

5 

6@pytest.mark.asyncio 

7async def test_id_field(client: Prisma) -> None: 

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

9 post = await client.post.create( 

10 { 

11 'title': 'My post title!', 

12 'published': False, 

13 } 

14 ) 

15 assert isinstance(post.id, str) 

16 

17 found = await client.post.find_unique_or_raise(where={'id': post.id}) 

18 assert found == post 

19 

20 

21@pytest.mark.asyncio 

22async def test_missing_required_args(client: Prisma) -> None: 

23 """Missing field raises an error""" 

24 with pytest.raises(TypeError): 

25 await client.post.find_unique_or_raise() # type: ignore[call-arg] 

26 

27 # TODO: more constrained error type 

28 with pytest.raises(errors.DataError): 

29 await client.post.find_unique_or_raise( 

30 { 

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

32 } 

33 ) 

34 

35 

36@pytest.mark.asyncio 

37async def test_no_match(client: Prisma) -> None: 

38 """Looking for non-existent record raises an error""" 

39 with pytest.raises( 

40 errors.RecordNotFoundError, 

41 match=r'depends on one or more records that were required but not found', 

42 ): 

43 await client.post.find_unique_or_raise(where={'id': 'sjbsjahs'}) 

44 

45 

46@pytest.mark.asyncio 

47async def test_multiple_fields_are_not_allowed(client: Prisma) -> None: 

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

49 with pytest.raises(errors.DataError): 

50 await client.user.find_unique_or_raise( 

51 where={ 

52 'id': 'foo', 

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

54 }, 

55 ) 

56 

57 

58@pytest.mark.asyncio 

59async def test_unique1(client: Prisma) -> None: 

60 """Standard combined unique constraint""" 

61 model = await client.unique1.create( 

62 data={ 

63 'name': 'Robert', 

64 'surname': 'Craigie', 

65 }, 

66 ) 

67 found = await client.unique1.find_unique_or_raise( 

68 where={ 

69 'name_surname': { 

70 'name': 'Robert', 

71 'surname': 'Craigie', 

72 }, 

73 }, 

74 ) 

75 assert found.name == model.name 

76 assert found.surname == model.surname 

77 

78 

79@pytest.mark.asyncio 

80async def test_unique2(client: Prisma) -> None: 

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

82 model = await client.unique2.create( 

83 data={ 

84 'name': 'Robert', 

85 'surname': 'Craigie', 

86 }, 

87 ) 

88 

89 found = await client.unique2.find_unique_or_raise( 

90 where={ 

91 'name_surname': { 

92 'name': 'Robert', 

93 'surname': 'Craigie', 

94 }, 

95 }, 

96 ) 

97 assert found.name == model.name 

98 assert found.surname == model.surname 

99 

100 found = await client.unique2.find_unique_or_raise( 

101 where={ 

102 'surname': 'Craigie', 

103 }, 

104 ) 

105 assert found.name == model.name 

106 assert found.surname == model.surname 

107 

108 

109@pytest.mark.asyncio 

110async def test_unique3(client: Prisma) -> None: 

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

112 model = await client.unique3.create( 

113 data={ 

114 'name': 'Robert', 

115 'surname': 'Craigie', 

116 }, 

117 ) 

118 

119 found = await client.unique3.find_unique_or_raise( 

120 where={ 

121 'name_surname': { 

122 'name': 'Robert', 

123 'surname': 'Craigie', 

124 }, 

125 }, 

126 ) 

127 assert found.id == model.id 

128 

129 found = await client.unique3.find_unique_or_raise( 

130 where={ 

131 'surname': 'Craigie', 

132 }, 

133 ) 

134 assert found.id == model.id 

135 

136 found = await client.unique3.find_unique_or_raise( 

137 where={ 

138 'id': model.id, 

139 }, 

140 ) 

141 assert found.id == model.id 

142 

143 

144@pytest.mark.asyncio 

145async def test_unique4(client: Prisma) -> None: 

146 """Explicitly named unique constraint""" 

147 model = await client.unique4.create( 

148 data={ 

149 'name': 'Robert', 

150 'surname': 'Craigie', 

151 }, 

152 ) 

153 

154 found = await client.unique4.find_unique_or_raise( 

155 where={ 

156 'my_unique': { 

157 'name': 'Robert', 

158 'surname': 'Craigie', 

159 }, 

160 }, 

161 ) 

162 assert found.name == model.name 

163 assert found.surname == model.surname 

164 

165 

166@pytest.mark.asyncio 

167async def test_unique5(client: Prisma) -> None: 

168 """Combined unique constraint with 3 fields""" 

169 model = await client.unique5.create( 

170 data={ 

171 'name': 'Robert', 

172 'middlename': 'Cosmo', 

173 'surname': 'Craigie', 

174 }, 

175 ) 

176 

177 found = await client.unique5.find_unique_or_raise( 

178 where={ 

179 'name_middlename_surname': { 

180 'name': 'Robert', 

181 'middlename': 'Cosmo', 

182 'surname': 'Craigie', 

183 }, 

184 }, 

185 ) 

186 assert found.name == model.name 

187 assert found.middlename == model.middlename 

188 assert found.surname == model.surname 

189 

190 

191@pytest.mark.asyncio 

192async def test_id1(client: Prisma) -> None: 

193 """Standard combined ID constraint""" 

194 model = await client.id1.create( 

195 data={ 

196 'name': 'Robert', 

197 'surname': 'Craigie', 

198 }, 

199 ) 

200 found = await client.id1.find_unique_or_raise( 

201 where={ 

202 'name_surname': { 

203 'name': 'Robert', 

204 'surname': 'Craigie', 

205 }, 

206 }, 

207 ) 

208 assert found.name == model.name 

209 assert found.surname == model.surname 

210 

211 

212@pytest.mark.asyncio 

213async def test_id2(client: Prisma) -> None: 

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

215 model = await client.id2.create( 

216 data={ 

217 'name': 'Robert', 

218 'surname': 'Craigie', 

219 }, 

220 ) 

221 

222 found = await client.id2.find_unique_or_raise( 

223 where={ 

224 'name_surname': { 

225 'name': 'Robert', 

226 'surname': 'Craigie', 

227 }, 

228 }, 

229 ) 

230 assert found.name == model.name 

231 assert found.surname == model.surname 

232 

233 found = await client.id2.find_unique_or_raise( 

234 where={ 

235 'surname': 'Craigie', 

236 }, 

237 ) 

238 assert found.name == model.name 

239 assert found.surname == model.surname 

240 

241 

242@pytest.mark.asyncio 

243async def test_id3(client: Prisma) -> None: 

244 """Explicitly named combined ID constraint""" 

245 model = await client.id3.create( 

246 data={ 

247 'name': 'Robert', 

248 'surname': 'Craigie', 

249 }, 

250 ) 

251 

252 found = await client.id3.find_unique_or_raise( 

253 where={ 

254 'my_id': { 

255 'name': 'Robert', 

256 'surname': 'Craigie', 

257 }, 

258 }, 

259 ) 

260 assert found.name == model.name 

261 assert found.surname == model.surname 

262 

263 

264@pytest.mark.asyncio 

265async def test_id4(client: Prisma) -> None: 

266 """Combined ID constraint with 3 fields""" 

267 model = await client.id4.create( 

268 data={ 

269 'name': 'Robert', 

270 'middlename': 'Cosmo', 

271 'surname': 'Craigie', 

272 }, 

273 ) 

274 

275 found = await client.id4.find_unique_or_raise( 

276 where={ 

277 'name_middlename_surname': { 

278 'name': 'Robert', 

279 'middlename': 'Cosmo', 

280 'surname': 'Craigie', 

281 }, 

282 }, 

283 ) 

284 assert found.name == model.name 

285 assert found.middlename == model.middlename 

286 assert found.surname == model.surname