From b3b47bf5156d400595363fa0029e51ce3f974ff0 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 1 Oct 2018 08:16:16 -0500 Subject: [PATCH] Added tests for using bytearray with BinaryField and corrected docs. --- docs/ref/models/fields.txt | 4 ++-- tests/model_fields/test_binaryfield.py | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 60d6fd5fc9..c7e7eb928c 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -416,8 +416,8 @@ guaranteed to fit numbers from ``-9223372036854775808`` to .. class:: BinaryField(max_length=None, **options) -A field to store raw binary data. It can be assigned :class:`bytes` or a -:class:`memoryview`. +A field to store raw binary data. It can be assigned :class:`bytes`, +:class:`bytearray`, or :class:`memoryview`. By default, ``BinaryField`` sets :attr:`~Field.editable` to ``False``, in which case it can't be included in a :class:`~django.forms.ModelForm`. diff --git a/tests/model_fields/test_binaryfield.py b/tests/model_fields/test_binaryfield.py index 9865f131d3..d9caf41112 100644 --- a/tests/model_fields/test_binaryfield.py +++ b/tests/model_fields/test_binaryfield.py @@ -9,7 +9,7 @@ class BinaryFieldTests(TestCase): binary_data = b'\x00\x46\xFE' def test_set_and_retrieve(self): - data_set = (self.binary_data, memoryview(self.binary_data)) + data_set = (self.binary_data, bytearray(self.binary_data), memoryview(self.binary_data)) for bdata in data_set: dm = DataModel(data=bdata) dm.save() @@ -40,6 +40,11 @@ class BinaryFieldTests(TestCase): DataModel.objects.create(data=b'\xef\xbb\xbf') self.assertSequenceEqual(DataModel.objects.filter(data=self.binary_data), [dm]) + def test_filter_bytearray(self): + dm = DataModel.objects.create(data=self.binary_data) + DataModel.objects.create(data=b'\xef\xbb\xbf') + self.assertSequenceEqual(DataModel.objects.filter(data=bytearray(self.binary_data)), [dm]) + def test_filter_memoryview(self): dm = DataModel.objects.create(data=self.binary_data) DataModel.objects.create(data=b'\xef\xbb\xbf')