Added a test for ArrayField(HStoreField()).
This commit is contained in:
parent
67702f3505
commit
f37467ec7a
|
@ -45,6 +45,7 @@ class Migration(migrations.Migration):
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||||
('field', HStoreField(blank=True, null=True)),
|
('field', HStoreField(blank=True, null=True)),
|
||||||
|
('array_field', ArrayField(HStoreField(), null=True)),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'required_db_vendor': 'postgresql',
|
'required_db_vendor': 'postgresql',
|
||||||
|
|
|
@ -71,6 +71,7 @@ class OtherTypesArrayModel(PostgreSQLModel):
|
||||||
|
|
||||||
class HStoreModel(PostgreSQLModel):
|
class HStoreModel(PostgreSQLModel):
|
||||||
field = HStoreField(blank=True, null=True)
|
field = HStoreField(blank=True, null=True)
|
||||||
|
array_field = ArrayField(HStoreField(), null=True)
|
||||||
|
|
||||||
|
|
||||||
class CharFieldModel(models.Model):
|
class CharFieldModel(models.Model):
|
||||||
|
|
|
@ -55,6 +55,19 @@ class SimpleTests(HStoreTestCase):
|
||||||
instance = HStoreModel.objects.get(field__has_keys=[2, 'a', 'ï'])
|
instance = HStoreModel.objects.get(field__has_keys=[2, 'a', 'ï'])
|
||||||
self.assertEqual(instance.field, expected_value)
|
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):
|
class TestQuerying(HStoreTestCase):
|
||||||
|
|
||||||
|
@ -166,17 +179,27 @@ class TestQuerying(HStoreTestCase):
|
||||||
|
|
||||||
|
|
||||||
class TestSerialization(HStoreTestCase):
|
class TestSerialization(HStoreTestCase):
|
||||||
test_data = ('[{"fields": {"field": "{\\"a\\": \\"b\\"}"}, '
|
test_data = json.dumps([{
|
||||||
'"model": "postgres_tests.hstoremodel", "pk": null}]')
|
'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):
|
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])
|
data = serializers.serialize('json', [instance])
|
||||||
self.assertEqual(json.loads(data), json.loads(self.test_data))
|
self.assertEqual(json.loads(data), json.loads(self.test_data))
|
||||||
|
|
||||||
def test_loading(self):
|
def test_loading(self):
|
||||||
instance = list(serializers.deserialize('json', self.test_data))[0].object
|
instance = list(serializers.deserialize('json', self.test_data))[0].object
|
||||||
self.assertEqual(instance.field, {'a': 'b'})
|
self.assertEqual(instance.field, {'a': 'b'})
|
||||||
|
self.assertEqual(instance.array_field, [{'a': 'b'}, {'b': 'a'}])
|
||||||
|
|
||||||
def test_roundtrip_with_null(self):
|
def test_roundtrip_with_null(self):
|
||||||
instance = HStoreModel(field={'a': 'b', 'c': None})
|
instance = HStoreModel(field={'a': 'b', 'c': None})
|
||||||
|
|
Loading…
Reference in New Issue