| 1 |
import pickle |
| 2 |
from functools import wraps |
| 3 |
|
| 4 |
from django.db import IntegrityError, connections, transaction |
| 5 |
from django.test import TestCase, skipUnlessDBFeature |
| 6 |
from django.test.testcases import ( |
| 7 |
DatabaseOperationForbidden, |
| 8 |
SimpleTestCase, |
| 9 |
TestData, |
| 10 |
is_pickable, |
| 11 |
) |
| 12 |
|
| 13 |
from .models import Car, Person, PossessedCar |
| 14 |
|
| 15 |
|
| 16 |
class UnpicklableObject: |
| 17 |
def __getstate__(self): |
| 18 |
raise pickle.PickleError("cannot be pickled for testing reasons") |
| 19 |
|
| 20 |
|
| 21 |
class TestSimpleTestCase(SimpleTestCase): |
| 22 |
def test_is_picklable_with_non_picklable_properties(self): |
| 23 |
"""ParallelTestSuite requires that all TestCases are picklable.""" |
| 24 |
self.non_picklable = lambda: 0 |
| 25 |
self.assertEqual(self, pickle.loads(pickle.dumps(self))) |
| 26 |
|
| 27 |
def test_is_picklable_with_non_picklable_object(self): |
| 28 |
unpicklable_obj = UnpicklableObject() |
| 29 |
self.assertEqual(is_pickable(unpicklable_obj), False) |
| 30 |
|
| 31 |
|
| 32 |
class TestTestCase(TestCase): |
| 33 |
@skipUnlessDBFeature("can_defer_constraint_checks") |
| 34 |
@skipUnlessDBFeature("supports_foreign_keys") |
| 35 |
def test_fixture_teardown_checks_constraints(self): |
| 36 |
rollback_atomics = self._rollback_atomics |
| 37 |
self._rollback_atomics = lambda connection: None # noop |
| 38 |
try: |
| 39 |
car = PossessedCar.objects.create(car_id=1, belongs_to_id=1) |
| 40 |
with self.assertRaises(IntegrityError), transaction.atomic(): |
| 41 |
self._fixture_teardown() |
| 42 |
car.delete() |
| 43 |
finally: |
| 44 |
self._rollback_atomics = rollback_atomics |
| 45 |
|
| 46 |
def test_disallowed_database_connection(self): |
| 47 |
message = ( |
| 48 |
"Database connections to 'other' are not allowed in this test. " |
| 49 |
"Add 'other' to test_utils.test_testcase.TestTestCase.databases to " |
| 50 |
"ensure proper test isolation and silence this failure." |
| 51 |
) |
| 52 |
with self.assertRaisesMessage(DatabaseOperationForbidden, message): |
| 53 |
connections["other"].connect() |
| 54 |
with self.assertRaisesMessage(DatabaseOperationForbidden, message): |
| 55 |
connections["other"].temporary_connection() |
| 56 |
|
| 57 |
def test_disallowed_database_queries(self): |
| 58 |
message = ( |
| 59 |
"Database queries to 'other' are not allowed in this test. " |
| 60 |
"Add 'other' to test_utils.test_testcase.TestTestCase.databases to " |
| 61 |
"ensure proper test isolation and silence this failure." |
| 62 |
) |
| 63 |
with self.assertRaisesMessage(DatabaseOperationForbidden, message): |
| 64 |
Car.objects.using("other").get() |
| 65 |
|
| 66 |
@skipUnlessDBFeature("supports_transactions") |
| 67 |
def test_reset_sequences(self): |
| 68 |
old_reset_sequences = self.__class__.reset_sequences |
| 69 |
self.__class__.reset_sequences = True |
| 70 |
self.addCleanup(setattr, self.__class__, "reset_sequences", old_reset_sequences) |
| 71 |
msg = "reset_sequences cannot be used on TestCase instances" |
| 72 |
with self.assertRaisesMessage(TypeError, msg): |
| 73 |
self._fixture_setup() |
| 74 |
|
| 75 |
|
| 76 |
def assert_no_queries(test): |
| 77 |
@wraps(test) |
| 78 |
def inner(self): |
| 79 |
with self.assertNumQueries(0): |
| 80 |
test(self) |
| 81 |
|
| 82 |
return inner |
| 83 |
|
| 84 |
|
| 85 |
# On databases with no transaction support (for instance, MySQL with the MyISAM |
| 86 |
# engine), setUpTestData() is called before each test, so there is no need to |
| 87 |
# clone class level test data. |
| 88 |
@skipUnlessDBFeature("supports_transactions") |
| 89 |
class TestDataTests(TestCase): |
| 90 |
# setUpTestData re-assignment are also wrapped in TestData. |
| 91 |
jim_douglas = None |
| 92 |
|
| 93 |
@classmethod |
| 94 |
def setUpTestData(cls): |
| 95 |
cls.jim_douglas = Person.objects.create(name="Jim Douglas") |
| 96 |
cls.car = Car.objects.create(name="1963 Volkswagen Beetle") |
| 97 |
cls.herbie = cls.jim_douglas.possessed_cars.create( |
| 98 |
car=cls.car, |
| 99 |
belongs_to=cls.jim_douglas, |
| 100 |
) |
| 101 |
|
| 102 |
cls.person_binary = Person.objects.create(name="Person", data=b"binary data") |
| 103 |
cls.person_binary_get = Person.objects.get(pk=cls.person_binary.pk) |
| 104 |
|
| 105 |
@assert_no_queries |
| 106 |
def test_class_attribute_equality(self): |
| 107 |
"""Class level test data is equal to instance level test data.""" |
| 108 |
self.assertEqual(self.jim_douglas, self.__class__.jim_douglas) |
| 109 |
self.assertEqual(self.person_binary, self.__class__.person_binary) |
| 110 |
self.assertEqual(self.person_binary_get, self.__class__.person_binary_get) |
| 111 |
|
| 112 |
@assert_no_queries |
| 113 |
def test_class_attribute_identity(self): |
| 114 |
""" |
| 115 |
Class level test data is not identical to instance level test data. |
| 116 |
""" |
| 117 |
self.assertIsNot(self.jim_douglas, self.__class__.jim_douglas) |
| 118 |
self.assertIsNot(self.person_binary, self.__class__.person_binary) |
| 119 |
self.assertIsNot(self.person_binary_get, self.__class__.person_binary_get) |
| 120 |
|
| 121 |
@assert_no_queries |
| 122 |
def test_binaryfield_data_type(self): |
| 123 |
self.assertEqual(bytes(self.person_binary.data), b"binary data") |
| 124 |
self.assertEqual(bytes(self.person_binary_get.data), b"binary data") |
| 125 |
self.assertEqual( |
| 126 |
type(self.person_binary_get.data), |
| 127 |
type(self.__class__.person_binary_get.data), |
| 128 |
) |
| 129 |
self.assertEqual( |
| 130 |
type(self.person_binary.data), |
| 131 |
type(self.__class__.person_binary.data), |
| 132 |
) |
| 133 |
|
| 134 |
@assert_no_queries |
| 135 |
def test_identity_preservation(self): |
| 136 |
"""Identity of test data is preserved between accesses.""" |
| 137 |
self.assertIs(self.jim_douglas, self.jim_douglas) |
| 138 |
|
| 139 |
@assert_no_queries |
| 140 |
def test_known_related_objects_identity_preservation(self): |
| 141 |
"""Known related objects identity is preserved.""" |
| 142 |
self.assertIs(self.herbie.car, self.car) |
| 143 |
self.assertIs(self.herbie.belongs_to, self.jim_douglas) |
| 144 |
|
| 145 |
def test_repr(self): |
| 146 |
self.assertEqual( |
| 147 |
repr(TestData("attr", "value")), |
| 148 |
"<TestData: name='attr', data='value'>", |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
class SetupTestDataIsolationTests(TestCase): |
| 153 |
""" |
| 154 |
In-memory data isolation is respected for model instances assigned to class |
| 155 |
attributes during setUpTestData. |
| 156 |
""" |
| 157 |
|
| 158 |
@classmethod |
| 159 |
def setUpTestData(cls): |
| 160 |
cls.car = Car.objects.create(name="Volkswagen Beetle") |
| 161 |
|
| 162 |
def test_book_name_deutsh(self): |
| 163 |
self.assertEqual(self.car.name, "Volkswagen Beetle") |
| 164 |
self.car.name = "VW sKäfer" |
| 165 |
self.car.save() |
| 166 |
|
| 167 |
def test_book_name_french(self): |
| 168 |
self.assertEqual(self.car.name, "Volkswagen Beetle") |
| 169 |
self.car.name = "Volkswagen Coccinelle" |
| 170 |
self.car.save() |