From edfdf060db2d423fadb2e0b07c6fd451f153157a Mon Sep 17 00:00:00 2001 From: Tomasz Kontusz Date: Tue, 30 Jun 2015 09:20:28 +0200 Subject: [PATCH] Tests for #123 --- tests/django_hstore_tests/models.py | 50 ++++++++++++++++++- .../tests/test_schema_mode.py | 14 +++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/tests/django_hstore_tests/models.py b/tests/django_hstore_tests/models.py index dbca53f..295bff1 100644 --- a/tests/django_hstore_tests/models.py +++ b/tests/django_hstore_tests/models.py @@ -92,6 +92,46 @@ class Meta: unique_together = ('name', 'data') if get_version()[0:3] >= '1.6': + class CustomType(object): + """Custom type, for use as an example for (de)serialization.""" + def __init__(self, value): + self.value = value + + def __eq__(self, other): + return isinstance(other, CustomType) and \ + self.value == other.value + + @classmethod + def deserialize(cls, raw): + if raw: + assert raw.startswith('[') + assert raw.endswith(']') + return cls(raw[1:-1]) + + def serialize(self): + return '[' + self.value + ']' + + + class CustomField(models.Field): + def db_type(self, connection): + return 'text' + + def from_db_value(self, value, expression, connection, context): + """DB value -> Python type""" + return CustomType.deserialize(value) + + def get_prep_value(self, value): + """Python type -> DB value""" + return value.serialize() + + def to_python(self, value): + """text or Python type -> Python type""" + if isinstance(value, CustomType): + return value + + return CustomType.deserialize(value) + + class SchemaDataBag(HStoreModel): name = models.CharField(max_length=32) data = hstore.DictionaryField(schema=[ @@ -200,6 +240,14 @@ class SchemaDataBag(HStoreModel): 'blank': True } }, + { + 'name': 'custom', + 'class': CustomField, + 'kwargs': { + 'blank': True, + 'null': True + } + }, ]) class NullSchemaDataBag(HStoreModel): @@ -223,7 +271,7 @@ class NullSchemaDataBag(HStoreModel): __all__.append('SchemaDataBag') __all__.append('NullSchemaDataBag') - + # if geodjango is in use define Location model, which contains GIS data if GEODJANGO_INSTALLED: diff --git a/tests/django_hstore_tests/tests/test_schema_mode.py b/tests/django_hstore_tests/tests/test_schema_mode.py index 4d2d383..90b4835 100644 --- a/tests/django_hstore_tests/tests/test_schema_mode.py +++ b/tests/django_hstore_tests/tests/test_schema_mode.py @@ -19,7 +19,8 @@ from django_hstore import hstore from django_hstore.virtual import create_hstore_virtual_field -from django_hstore_tests.models import SchemaDataBag, NullSchemaDataBag +from django_hstore_tests.models import (CustomType, SchemaDataBag, + NullSchemaDataBag) MIGRATION_PATH = '{0}/../{1}'.format(os.path.dirname(__file__), 'migrations') @@ -65,6 +66,17 @@ def test_dict_get(self): self.assertEqual(d.get('default_test', 'default'), 'default') self.assertIsNone(d.get('default_test')) + def test_save_and_reload_custom_type(self): + d = SchemaDataBag() + v = CustomType('some value') + d.custom = v + self.assertEqual(d.custom, v) + + def test_serializing_custom_type(self): + d = SchemaDataBag() + d.custom = CustomType('some value') + self.assertEqual(dict.get(d.data, 'custom'), '[some value]') + def test_virtual_field_default_value(self): d = SchemaDataBag() self.assertEqual(d.number, 0)