From cb724ef6c0fd71cd5bdbdc27c02f4da0f7e2ad08 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Fri, 3 Dec 2021 11:56:22 +0100 Subject: [3.2.x] Fixed #33333 -- Fixed setUpTestData() crash with models.BinaryField on PostgreSQL. This makes models.BinaryField pickleable on PostgreSQL. Regression in 3cf80d3fcf7446afdde16a2be515c423f720e54d. Thanks Adam Zimmerman for the report. Backport of 2c7846d992ca512d36a73f518205015c88ed088c from main. --- tests/queryset_pickle/models.py | 1 + tests/queryset_pickle/tests.py | 4 ++++ tests/test_utils/models.py | 1 + tests/test_utils/test_testcase.py | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+) (limited to 'tests') diff --git a/tests/queryset_pickle/models.py b/tests/queryset_pickle/models.py index 689a11f65e..9a033ffe95 100644 --- a/tests/queryset_pickle/models.py +++ b/tests/queryset_pickle/models.py @@ -46,6 +46,7 @@ class Happening(models.Model): number1 = models.IntegerField(blank=True, default=standalone_number) number2 = models.IntegerField(blank=True, default=Numbers.get_static_number) event = models.OneToOneField(Event, models.CASCADE, null=True) + data = models.BinaryField(null=True) class Container: diff --git a/tests/queryset_pickle/tests.py b/tests/queryset_pickle/tests.py index d0ae963cd9..da5e2a73a4 100644 --- a/tests/queryset_pickle/tests.py +++ b/tests/queryset_pickle/tests.py @@ -16,6 +16,10 @@ class PickleabilityTestCase(TestCase): def assert_pickles(self, qs): self.assertEqual(list(pickle.loads(pickle.dumps(qs))), list(qs)) + def test_binaryfield(self): + Happening.objects.create(data=b'binary data') + self.assert_pickles(Happening.objects.all()) + def test_related_field(self): g = Group.objects.create(name="Ponies Who Own Maybachs") self.assert_pickles(Event.objects.filter(group=g.id)) diff --git a/tests/test_utils/models.py b/tests/test_utils/models.py index b0497f12b9..57518aef90 100644 --- a/tests/test_utils/models.py +++ b/tests/test_utils/models.py @@ -8,6 +8,7 @@ class Car(models.Model): class Person(models.Model): name = models.CharField(max_length=100) cars = models.ManyToManyField(Car, through='PossessedCar') + data = models.BinaryField(null=True) class PossessedCar(models.Model): diff --git a/tests/test_utils/test_testcase.py b/tests/test_utils/test_testcase.py index 5ec71f84d0..e833a29727 100644 --- a/tests/test_utils/test_testcase.py +++ b/tests/test_utils/test_testcase.py @@ -71,10 +71,15 @@ class TestDataTests(TestCase): ) cls.non_deepcopy_able = NonDeepCopyAble() + cls.person_binary = Person.objects.create(name='Person', data=b'binary data') + cls.person_binary_get = Person.objects.get(pk=cls.person_binary.pk) + @assert_no_queries def test_class_attribute_equality(self): """Class level test data is equal to instance level test data.""" self.assertEqual(self.jim_douglas, self.__class__.jim_douglas) + self.assertEqual(self.person_binary, self.__class__.person_binary) + self.assertEqual(self.person_binary_get, self.__class__.person_binary_get) @assert_no_queries def test_class_attribute_identity(self): @@ -82,6 +87,21 @@ class TestDataTests(TestCase): Class level test data is not identical to instance level test data. """ self.assertIsNot(self.jim_douglas, self.__class__.jim_douglas) + self.assertIsNot(self.person_binary, self.__class__.person_binary) + self.assertIsNot(self.person_binary_get, self.__class__.person_binary_get) + + @assert_no_queries + def test_binaryfield_data_type(self): + self.assertEqual(bytes(self.person_binary.data), b'binary data') + self.assertEqual(bytes(self.person_binary_get.data), b'binary data') + self.assertEqual( + type(self.person_binary_get.data), + type(self.__class__.person_binary_get.data), + ) + self.assertEqual( + type(self.person_binary.data), + type(self.__class__.person_binary.data), + ) @assert_no_queries def test_identity_preservation(self): -- cgit v1.3