2

I am running through a little strange behaviour on my test cases with django-hstore.

I am working on a django rest framework project and models may look like below

models

from django_hstore.hstore import DictionaryField

class Config(models.Model):

    data = data = DictionaryField(db_index=True)

And i am trying to test a scenario based on Config model and does something like this

Tests

class ConfigTestCase(TestCase):

    def setUp(self):
        Config.objects.create(data={'pagination_number': '50'})

    def test_config_data(self):
        # Below code is getting failed
        Config.objects.first().data.get('pagination_number')

When i do Config.objects.first().data

i am getting '"pagination_number"=>"50"'

i was expecting {'pagination_number': 50}

This is happening only when i run tests

When i am performing create operation on my command shell manually everything works fine and code also executes fine

I am using django-hstore 1.2.1

PostgreSQL 9.4.4

I am unable to figure out the reason for this

omkarvijay
  • 101
  • 1
  • 6

1 Answers1

0

Trying to find the solution for your problem, I ended up finding this increasing discussion a while ago, in django-hstore group

Andrey Antukh reproduced the error.

================================================== ====================
FAIL: test_properties_hstore (tests.django_hstore_tests.tests.HstoreTest)
-------------------------------------------------- --------------------
Traceback (most recent call last):
  File "/home/niwi/devel/django-hstore/tests/django_hstore_tests/tests.py", line 471, in test_properties_hstore
    self.assertEqual (type (instance.data), HStoreDict) # TEST FAILS HERE
AssertionError: <class 'str'> = <class 'django_hstore.fields.HStoreDict'>

if you read the documentation of hstore:

1.4. Limitations PostgreSQL's implementation of hstore has no concept of type; it stores the mapping of string keys to string values. Values ​​are strings stored in the database Regarding of Their original type. This limitation can be Overcome by using either the schema mode since version 1.3.0 or by using the serialized dictionary field since version 1.3.6 of django_hstore.

Update the version of django-hstore. this maybe solve the problem.

Paulo Pessoa
  • 2,509
  • 19
  • 30