Skip to content

Commit

Permalink
Test for #123
Browse files Browse the repository at this point in the history
  • Loading branch information
ktosiek committed Jun 30, 2015
1 parent 3b235cb commit 3216fdc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
46 changes: 45 additions & 1 deletion tests/django_hstore_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ 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

@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=[
Expand Down Expand Up @@ -200,6 +236,14 @@ class SchemaDataBag(HStoreModel):
'blank': True
}
},
{
'name': 'custom',
'class': CustomField,
'kwargs': {
'blank': True,
'null': True
}
},
])

class NullSchemaDataBag(HStoreModel):
Expand All @@ -223,7 +267,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:
Expand Down
12 changes: 11 additions & 1 deletion tests/django_hstore_tests/tests/test_schema_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -65,6 +66,15 @@ def test_dict_get(self):
self.assertEqual(d.get('default_test', 'default'), 'default')
self.assertIsNone(d.get('default_test'))

def test_save_custom_type(self):
d = SchemaDataBag()
v = CustomType('some value')
d.custom = v
d.save()
d = SchemaDataBag.objects.get(pk=d.id)
self.assertEqual(dict.get(d.data, 'custom'), '[some value]')

This comment has been minimized.

Copy link
@ktosiek

ktosiek Jun 30, 2015

Author

I don't know if this line should stay, but the error it generates shows my current problem rather nicely - v is serialized as "<django_hstore_tests.models.CustomType object at 0x...>" instead of "[some value]".

This comment has been minimized.

Copy link
@nemesifier

nemesifier Jun 30, 2015

Member

does self.assertEqual(d.custom, v) yeld a different result?

This comment has been minimized.

Copy link
@ktosiek

ktosiek via email Jun 30, 2015

Author

This comment has been minimized.

Copy link
@ktosiek

ktosiek via email Jun 30, 2015

Author

This comment has been minimized.

Copy link
@nemesifier

nemesifier Jun 30, 2015

Member

no hurry, take your time to add the __eq__ method.
It would also be better if the second assertion was done in a separate function, so that we have 2 failing tests, as those are two different cases

self.assertEqual(d.custom, v)

def test_virtual_field_default_value(self):
d = SchemaDataBag()
self.assertEqual(d.number, 0)
Expand Down

0 comments on commit 3216fdc

Please sign in to comment.