diff --git a/tests/postgres_tests/migrations/0002_create_test_models.py b/tests/postgres_tests/migrations/0002_create_test_models.py index 4cd37e5e43..8cbd95c3bb 100644 --- a/tests/postgres_tests/migrations/0002_create_test_models.py +++ b/tests/postgres_tests/migrations/0002_create_test_models.py @@ -45,6 +45,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('field', HStoreField(blank=True, null=True)), + ('array_field', ArrayField(HStoreField(), null=True)), ], options={ 'required_db_vendor': 'postgresql', diff --git a/tests/postgres_tests/models.py b/tests/postgres_tests/models.py index 001ed00d0c..e1c276890c 100644 --- a/tests/postgres_tests/models.py +++ b/tests/postgres_tests/models.py @@ -71,6 +71,7 @@ class OtherTypesArrayModel(PostgreSQLModel): class HStoreModel(PostgreSQLModel): field = HStoreField(blank=True, null=True) + array_field = ArrayField(HStoreField(), null=True) class CharFieldModel(models.Model): diff --git a/tests/postgres_tests/test_hstore.py b/tests/postgres_tests/test_hstore.py index b7656eee9e..069e570f51 100644 --- a/tests/postgres_tests/test_hstore.py +++ b/tests/postgres_tests/test_hstore.py @@ -55,6 +55,19 @@ class SimpleTests(HStoreTestCase): instance = HStoreModel.objects.get(field__has_keys=[2, 'a', 'ï']) self.assertEqual(instance.field, expected_value) + def test_array_field(self): + value = [ + {'a': 1, 'b': 'B', 2: 'c', 'ï': 'ê'}, + {'a': 1, 'b': 'B', 2: 'c', 'ï': 'ê'}, + ] + expected_value = [ + {'a': '1', 'b': 'B', '2': 'c', 'ï': 'ê'}, + {'a': '1', 'b': 'B', '2': 'c', 'ï': 'ê'}, + ] + instance = HStoreModel.objects.create(array_field=value) + instance.refresh_from_db() + self.assertEqual(instance.array_field, expected_value) + class TestQuerying(HStoreTestCase): @@ -166,17 +179,27 @@ class TestQuerying(HStoreTestCase): class TestSerialization(HStoreTestCase): - test_data = ('[{"fields": {"field": "{\\"a\\": \\"b\\"}"}, ' - '"model": "postgres_tests.hstoremodel", "pk": null}]') + test_data = json.dumps([{ + 'model': 'postgres_tests.hstoremodel', + 'pk': None, + 'fields': { + 'field': json.dumps({'a': 'b'}), + 'array_field': json.dumps([ + json.dumps({'a': 'b'}), + json.dumps({'b': 'a'}), + ]), + }, + }]) def test_dumping(self): - instance = HStoreModel(field={'a': 'b'}) + instance = HStoreModel(field={'a': 'b'}, array_field=[{'a': 'b'}, {'b': 'a'}]) data = serializers.serialize('json', [instance]) self.assertEqual(json.loads(data), json.loads(self.test_data)) def test_loading(self): instance = list(serializers.deserialize('json', self.test_data))[0].object self.assertEqual(instance.field, {'a': 'b'}) + self.assertEqual(instance.array_field, [{'a': 'b'}, {'b': 'a'}]) def test_roundtrip_with_null(self): instance = HStoreModel(field={'a': 'b', 'c': None})