def test_virtual_date_drf_support(self):
        class FakeModel(object):
            date = '2014-08-08'

        virtual_date = SchemaDataBag()._hstore_virtual_fields['date']

        self.assertEqual(virtual_date.value_to_string(FakeModel()), '2014-08-08')
 def test_schemadatabag_assignment(self):
     d = SchemaDataBag()
     d.number = 4
     self.assertEqual(d.data['number'], 4)
     d.float = 2.5
     self.assertEqual(d.data['float'], 2.5)
     d.data['number'] = 5
     self.assertEqual(d.number, 5)
 def test_to_python_conversion(self):
     d = SchemaDataBag().data
     d['number'] = 2
     self.assertEqual(d['number'], 2)
     d['boolean'] = True
     self.assertTrue(d['boolean'])
     d['boolean'] = False
     self.assertFalse(d['boolean'])
     d['float'] = 2.5
     self.assertEqual(d['float'], 2.5)
     self.assertEqual(d.get('float'), 2.5)
        def test_reload_schema(self):
            # cache some stuff
            f = SchemaDataBag._meta.get_field('data')
            original_schema = list(f.schema)
            concrete_fields_length = len(SchemaDataBag._meta.concrete_fields)
            hstore_virtual_fields_keys = list(
                SchemaDataBag._hstore_virtual_fields.keys())

            # original state
            d = SchemaDataBag()
            self.assertTrue(d.data.schema_mode)
            self.assertEqual(len(SchemaDataBag._meta.virtual_fields),
                             len(original_schema))
            self.assertEqual(len(SchemaDataBag._meta.fields),
                             len(original_schema) + concrete_fields_length)
            self.assertEqual(len(SchemaDataBag._meta.local_fields),
                             len(original_schema) + concrete_fields_length)
            self.assertTrue(hasattr(SchemaDataBag, '_hstore_virtual_fields'))
            for key in hstore_virtual_fields_keys:
                self.assertTrue(hasattr(d, key))

            # schema erased
            f.reload_schema(None)
            self.assertIsNone(f.schema)
            self.assertFalse(f.schema_mode)
            self.assertTrue(f.editable)
            self.assertEqual(len(SchemaDataBag._meta.virtual_fields), 0)
            self.assertEqual(len(SchemaDataBag._meta.fields),
                             concrete_fields_length)
            self.assertEqual(len(SchemaDataBag._meta.local_fields),
                             concrete_fields_length)
            self.assertFalse(hasattr(SchemaDataBag, '_hstore_virtual_fields'))
            d = SchemaDataBag()
            self.assertFalse(d.data.schema_mode)
            for key in hstore_virtual_fields_keys:
                self.assertFalse(hasattr(d, key))

            # reload original schema
            f.reload_schema(original_schema)
            d = SchemaDataBag()
            self.assertTrue(d.data.schema_mode)
            self.assertEqual(len(SchemaDataBag._meta.virtual_fields),
                             len(original_schema))
            self.assertEqual(len(SchemaDataBag._meta.fields),
                             len(original_schema) + concrete_fields_length)
            self.assertEqual(len(SchemaDataBag._meta.local_fields),
                             len(original_schema) + concrete_fields_length)
            self.assertTrue(hasattr(SchemaDataBag, '_hstore_virtual_fields'))
            for key in hstore_virtual_fields_keys:
                self.assertTrue(hasattr(d, key))
 def test_virtual_field_default_value(self):
     d = SchemaDataBag()
     self.assertEqual(d.number, 0)
     self.assertEqual(d.float, 1.0)
     # accessing the HStoreDict key raises KeyError if not assigned previously
     with self.assertRaises(KeyError):
         d.data['number']
 def test_schemadatabag_save(self):
     d = SchemaDataBag()
     d.name = 'test'
     d.number = 4
     d.float = 2.0
     d.save()
     d = SchemaDataBag.objects.get(pk=d.id)
     self.assertEqual(d.number, 4)
     self.assertEqual(d.data['number'], 4)
 def test_datetime_is_none(self):
     """ issue #82 https://github.com/djangonauts/django-hstore/issues/82 """
     d = SchemaDataBag()
     d.name = 'datetime'
     self.assertIsNone(d.datetime, None)
     d.full_clean()
     d.save()
     d = SchemaDataBag.objects.get(name='datetime')
     self.assertIsNone(d.datetime, None)
    def test_admin_change(self):
        self._login_as_admin()
        d = SchemaDataBag()
        d.name = 'test1'
        d.number = 1
        d.float = 2.5
        d.save()
        url = reverse('admin:django_hstore_tests_schemadatabag_change', args=[d.id])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        response = self.client.post(url, {'name': 'test_change', 'number': 6, 'float': 2.6})
        d = SchemaDataBag.objects.get(pk=d.id)
        self.assertEqual(d.name, 'test_change')
        self.assertEqual(d.number, 6)
        self.assertEqual(d.data['number'], 6)
        self.assertEqual(d.float, 2.6)
        self.assertEqual(d.data['float'], 2.6)
    def test_schemadatabag_validation_error(self):
        d = SchemaDataBag()
        d.name = 'test'
        d.number = 'WRONG'
        d.float = 2.0

        with self.assertRaises(ValidationError):
            d.full_clean()

        d.number = 9
        d.float = 'WRONG'
        with self.assertRaises(ValidationError):
            d.full_clean()

        d.float = 2.0
        d.char = 'test'
        d.choice = 'choice1'
        d.full_clean()
        d.save()
 def test_dict_get(self):
     d = SchemaDataBag().data
     d['number'] = 2
     self.assertEqual(d.get('number'), 2)
     self.assertEqual(d.get('default_test', 'default'), 'default')
     self.assertIsNone(d.get('default_test'))
 def test_str(self):
     d = SchemaDataBag()
     self.assertEqual(str(d.data), '{}')
 def test_basefield_attribute(self):
     virtual_field = SchemaDataBag()._hstore_virtual_fields['char']
     self.assertEqual(virtual_field.__basefield__.__name__, 'CharField')
 def test_extra_key_regression(self):
     s = SchemaDataBag()
     s.data['extrakey'] = 2
     self.assertEqual(s.data['extrakey'], '2')
    def test_utf8(self):
        d = SchemaDataBag()
        d.name = 'test'
        d.number = 4
        d.float = 2.0
        d.char = 'è'
        d.full_clean()
        d.save()

        d = SchemaDataBag.objects.get(pk=d.id)
        self.assertEqual(d.char, u'è')

        d.char = u'è'
        d.full_clean()
        d.save()

        d = SchemaDataBag.objects.get(pk=d.id)
        self.assertEqual(d.char, u'è')