|
1 | 1 | from tests import testmodels |
2 | 2 | from tortoise.contrib import test |
3 | | -from tortoise.exceptions import IntegrityError, NoValuesFetched, OperationalError |
| 3 | +from tortoise.exceptions import ( |
| 4 | + IntegrityError, |
| 5 | + NoValuesFetched, |
| 6 | + OperationalError, |
| 7 | + ValidationError, |
| 8 | +) |
4 | 9 | from tortoise.queryset import QuerySet |
5 | 10 |
|
6 | 11 |
|
7 | 12 | class TestForeignKeyField(test.TestCase): |
| 13 | + def assertRaisesWrongTypeException(self, relation_name: str): |
| 14 | + return self.assertRaisesRegex( |
| 15 | + ValidationError, f"Invalid type for relationship field '{relation_name}'" |
| 16 | + ) |
| 17 | + |
8 | 18 | async def test_empty(self): |
9 | 19 | with self.assertRaises(IntegrityError): |
10 | 20 | await testmodels.MinRelation.create() |
@@ -151,6 +161,11 @@ async def test_minimal__instantiated_create(self): |
151 | 161 | tour = await testmodels.Tournament.create(name="Team1") |
152 | 162 | await testmodels.MinRelation.create(tournament=tour) |
153 | 163 |
|
| 164 | + async def test_minimal__instantiated_create_wrong_type(self): |
| 165 | + author = await testmodels.Author.create(name="Author1") |
| 166 | + with self.assertRaisesWrongTypeException("tournament"): |
| 167 | + await testmodels.MinRelation.create(tournament=author) |
| 168 | + |
154 | 169 | async def test_minimal__instantiated_iterate(self): |
155 | 170 | tour = await testmodels.Tournament.create(name="Team1") |
156 | 171 | async for _ in tour.minrelations: |
@@ -229,3 +244,57 @@ async def test_event__offset(self): |
229 | 244 | event2 = await testmodels.Event.create(name="Event2", tournament=tour) |
230 | 245 | event3 = await testmodels.Event.create(name="Event3", tournament=tour) |
231 | 246 | self.assertEqual(await tour.events.offset(1).order_by("name"), [event2, event3]) |
| 247 | + |
| 248 | + async def test_fk_correct_type_assignment(self): |
| 249 | + tour1 = await testmodels.Tournament.create(name="Team1") |
| 250 | + tour2 = await testmodels.Tournament.create(name="Team2") |
| 251 | + event = await testmodels.Event(name="Event1", tournament=tour1) |
| 252 | + |
| 253 | + event.tournament = tour2 |
| 254 | + await event.save() |
| 255 | + self.assertEqual(event.tournament_id, tour2.id) |
| 256 | + |
| 257 | + async def test_fk_wrong_type_assignment(self): |
| 258 | + tour = await testmodels.Tournament.create(name="Team1") |
| 259 | + author = await testmodels.Author.create(name="Author") |
| 260 | + rel = await testmodels.MinRelation.create(tournament=tour) |
| 261 | + |
| 262 | + with self.assertRaisesWrongTypeException("tournament"): |
| 263 | + rel.tournament = author |
| 264 | + |
| 265 | + async def test_fk_none_assignment(self): |
| 266 | + manager = await testmodels.Employee.create(name="Manager") |
| 267 | + employee = await testmodels.Employee.create(name="Employee", manager=manager) |
| 268 | + |
| 269 | + employee.manager = None |
| 270 | + await employee.save() |
| 271 | + self.assertIsNone(employee.manager) |
| 272 | + |
| 273 | + async def test_fk_update_wrong_type(self): |
| 274 | + tour = await testmodels.Tournament.create(name="Team1") |
| 275 | + rel = await testmodels.MinRelation.create(tournament=tour) |
| 276 | + author = await testmodels.Author.create(name="Author1") |
| 277 | + |
| 278 | + with self.assertRaisesWrongTypeException("tournament"): |
| 279 | + await testmodels.MinRelation.filter(id=rel.id).update(tournament=author) |
| 280 | + |
| 281 | + async def test_fk_bulk_create_wrong_type(self): |
| 282 | + author = await testmodels.Author.create(name="Author") |
| 283 | + with self.assertRaisesWrongTypeException("tournament"): |
| 284 | + await testmodels.MinRelation.bulk_create( |
| 285 | + [testmodels.MinRelation(tournament=author) for _ in range(10)] |
| 286 | + ) |
| 287 | + |
| 288 | + async def test_fk_bulk_update_wrong_type(self): |
| 289 | + tour = await testmodels.Tournament.create(name="Team1") |
| 290 | + await testmodels.MinRelation.bulk_create( |
| 291 | + [testmodels.MinRelation(tournament=tour) for _ in range(1, 10)] |
| 292 | + ) |
| 293 | + author = await testmodels.Author.create(name="Author") |
| 294 | + |
| 295 | + with self.assertRaisesWrongTypeException("tournament"): |
| 296 | + relations = await testmodels.MinRelation.all() |
| 297 | + await testmodels.MinRelation.bulk_update( |
| 298 | + [testmodels.MinRelation(id=rel.id, tournament=author) for rel in relations], |
| 299 | + fields=["tournament"], |
| 300 | + ) |
0 commit comments