|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import django |
3 | 3 | from django.core.exceptions import FieldError |
4 | | -from django.db import connection, models, IntegrityError |
| 4 | +from django.db import connection, models, IntegrityError, transaction |
5 | 5 | from django.db.models.query_utils import Q |
| 6 | +from django.test.testcases import TransactionTestCase |
6 | 7 | from django.utils import translation |
7 | 8 | from hvad.exceptions import WrongManager |
8 | 9 | from hvad.models import (TranslatedFields, TranslatableModel) |
@@ -32,22 +33,33 @@ def test_relation(self): |
32 | 33 | self.assertEqual(related.normal.translated_field, normal.translated_field) |
33 | 34 | self.assertTrue(related in normal.rel1.all()) |
34 | 35 |
|
35 | | - def test_failed_relation(self): |
36 | | - related = Related.objects.create() |
37 | | - related.normal_id = 999 |
38 | | - if connection.features.supports_forward_references: |
39 | | - related.save() |
40 | | - self.assertRaises(Normal.DoesNotExist, getattr, related, 'normal') |
41 | | - else: |
42 | | - self.assertRaises(IntegrityError, related.save) |
43 | | - |
44 | 36 | def test_reverse_relation(self): |
45 | 37 | normal = Normal.objects.language('en').get(pk=self.normal_id[1]) |
46 | 38 | related = Related.objects.language('en').create(normal=normal) |
47 | 39 |
|
48 | 40 | self.assertEqual(normal.rel1.language('en').get().pk, related.pk) |
49 | 41 |
|
50 | 42 |
|
| 43 | +class NormalToNormalFKTest2(TransactionTestCase, NormalFixture): |
| 44 | + normal_count = 1 |
| 45 | + |
| 46 | + def test_failed_relation(self): |
| 47 | + related = Related.objects.create() |
| 48 | + related.normal_id = 999 |
| 49 | + if connection.features.supports_foreign_keys: |
| 50 | + if connection.features.supports_forward_references: |
| 51 | + try: |
| 52 | + transaction.set_autocommit(False) |
| 53 | + related.save() |
| 54 | + self.assertRaises(Normal.DoesNotExist, getattr, related, 'normal') |
| 55 | + self.assertRaises(IntegrityError, transaction.commit) |
| 56 | + finally: |
| 57 | + transaction.rollback() |
| 58 | + transaction.set_autocommit(True) |
| 59 | + else: |
| 60 | + self.assertRaises(IntegrityError, related.save) |
| 61 | + |
| 62 | + |
51 | 63 | class StandardToTransFKTest(HvadTestCase, StandardFixture, NormalFixture): |
52 | 64 | normal_count = 2 |
53 | 65 | standard_count = 2 |
|
0 commit comments