Python django.contrib.postgres.fields.HStoreField() Examples

The following are 20 code examples of django.contrib.postgres.fields.HStoreField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.postgres.fields , or try the search function .
Example #1
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_invalid_default(self):
        class MyModel(PostgreSQLModel):
            field = HStoreField(default={})

        model = MyModel()
        self.assertEqual(model.check(), [
            checks.Warning(
                msg=(
                    "HStoreField default should be a callable instead of an "
                    "instance so that it's not shared between all field "
                    "instances."
                ),
                hint='Use a callable instead, e.g., use `dict` instead of `{}`.',
                obj=MyModel._meta.get_field('field'),
                id='postgres.E003',
            )
        ]) 
Example #2
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_invalid_default(self):
        class MyModel(PostgreSQLModel):
            field = HStoreField(default={})

        model = MyModel()
        self.assertEqual(model.check(), [
            checks.Warning(
                msg=(
                    "HStoreField default should be a callable instead of an "
                    "instance so that it's not shared between all field "
                    "instances."
                ),
                hint='Use a callable instead, e.g., use `dict` instead of `{}`.',
                obj=MyModel._meta.get_field('field'),
                id='postgres.E003',
            )
        ]) 
Example #3
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_model_field_formfield(self):
        model_field = HStoreField()
        form_field = model_field.formfield()
        self.assertIsInstance(form_field, forms.HStoreField) 
Example #4
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_empty(self):
        field = forms.HStoreField(required=False)
        value = field.clean('')
        self.assertEqual(value, {}) 
Example #5
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_none_value(self):
        field = forms.HStoreField()
        value = field.clean('{"a": null}')
        self.assertEqual(value, {'a': None}) 
Example #6
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_string_values(self):
        field = forms.HStoreField()
        value = field.clean('{"a": 1}')
        self.assertEqual(value, {'a': '1'}) 
Example #7
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_non_dict_json(self):
        field = forms.HStoreField()
        msg = 'Input must be a JSON dictionary.'
        with self.assertRaisesMessage(exceptions.ValidationError, msg) as cm:
            field.clean('["a", "b", 1]')
        self.assertEqual(cm.exception.code, 'invalid_format') 
Example #8
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid(self):
        field = forms.HStoreField()
        value = field.clean('{"a": "b"}')
        self.assertEqual(value, {'a': 'b'}) 
Example #9
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_none_allowed_as_value(self):
        field = HStoreField()
        self.assertEqual(field.clean({'a': None}, None), {'a': None}) 
Example #10
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_a_string(self):
        field = HStoreField()
        with self.assertRaises(exceptions.ValidationError) as cm:
            field.clean({'a': 1}, None)
        self.assertEqual(cm.exception.code, 'not_a_string')
        self.assertEqual(cm.exception.message % cm.exception.params, 'The value of "a" is not a string or null.') 
Example #11
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_default(self):
        class MyModel(PostgreSQLModel):
            field = HStoreField(default=dict)

        self.assertEqual(MyModel().check(), []) 
Example #12
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_model_field_formfield(self):
        model_field = HStoreField()
        form_field = model_field.formfield()
        self.assertIsInstance(form_field, forms.HStoreField) 
Example #13
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_empty(self):
        field = forms.HStoreField(required=False)
        value = field.clean('')
        self.assertEqual(value, {}) 
Example #14
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_none_value(self):
        field = forms.HStoreField()
        value = field.clean('{"a": null}')
        self.assertEqual(value, {'a': None}) 
Example #15
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_string_values(self):
        field = forms.HStoreField()
        value = field.clean('{"a": 1}')
        self.assertEqual(value, {'a': '1'}) 
Example #16
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_non_dict_json(self):
        field = forms.HStoreField()
        msg = 'Input must be a JSON dictionary.'
        with self.assertRaisesMessage(exceptions.ValidationError, msg) as cm:
            field.clean('["a", "b", 1]')
        self.assertEqual(cm.exception.code, 'invalid_format') 
Example #17
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid(self):
        field = forms.HStoreField()
        value = field.clean('{"a": "b"}')
        self.assertEqual(value, {'a': 'b'}) 
Example #18
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_none_allowed_as_value(self):
        field = HStoreField()
        self.assertEqual(field.clean({'a': None}, None), {'a': None}) 
Example #19
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_a_string(self):
        field = HStoreField()
        with self.assertRaises(exceptions.ValidationError) as cm:
            field.clean({'a': 1}, None)
        self.assertEqual(cm.exception.code, 'not_a_string')
        self.assertEqual(cm.exception.message % cm.exception.params, 'The value of "a" is not a string or null.') 
Example #20
Source File: test_hstore.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_default(self):
        class MyModel(PostgreSQLModel):
            field = HStoreField(default=dict)

        self.assertEqual(MyModel().check(), [])