def test_to_python_pickle_str(self): # Github issue #2085 # Not all pickles can be converted to Py3 str at all so use an actual # production value field = DictAsJsonField() orig_value = {'refresh_interval': 600000} value = "(dp0\nS'refresh_interval'\np1\nI600000\ns." result = field.to_python(value) assert result == orig_value
def test_to_python_json(self): field = DictAsJsonField() value = u'{"a": "b"}' result = field.to_python(value) self.assertEqual(result, {"a": "b"}) value = u'[1, 2, 3]' result = field.to_python(value) self.assertEqual(result, [1, 2, 3]) value = b'[1, 2, 3]' result = field.to_python(value) self.assertEqual(result, [1, 2, 3])
def test_to_python_json(self): field = DictAsJsonField() value = u'{"a": "b"}' result = field.to_python(value) assert result == {"a": "b"} value = u'[1, 2, 3]' result = field.to_python(value) assert result == [1, 2, 3] value = b'[1, 2, 3]' result = field.to_python(value) assert result == [1, 2, 3]
class AccountNavlet(models.Model): """Store information about a users navlets""" navlet = VarcharField() order = models.IntegerField(default=0, db_column='displayorder') account = models.ForeignKey( Account, on_delete=models.CASCADE, db_column='account' ) preferences = DictAsJsonField(null=True) column = models.IntegerField(db_column='col') dashboard = models.ForeignKey( AccountDashboard, on_delete=models.CASCADE, related_name='widgets' ) def __str__(self): return "%s - %s" % (self.navlet, self.account) def to_json_dict(self): return { 'navlet': self.navlet, 'preferences': self.preferences, 'column': self.column, 'order': self.order, } class Meta(object): db_table = 'account_navlet' ordering = ['order']
def test_to_python_dict(self): field = DictAsJsonField() value = {'a': 'b'} result = field.to_python(value) self.assertEqual(result, value)
def test_get_prep_value_filled(self): field = DictAsJsonField() result = field.get_prep_value({'a': 'b'}) self.assertEqual(result, u'{"a": "b"}')
def test_get_prep_value_empty(self): field = DictAsJsonField() result = field.get_prep_value(None) self.assertEqual(result, None)
def test_to_python_pickle(self): field = DictAsJsonField() orig_value = 2 value = pickle.dumps(orig_value) result = field.to_python(value) self.assertEqual(result, orig_value)
def test_to_python_dict(self): field = DictAsJsonField() value = {'a': 'b'} result = field.to_python(value) assert result == value
def test_get_prep_value_filled(self): field = DictAsJsonField() result = field.get_prep_value({'a': 'b'}) assert result == u'{"a": "b"}'
def test_get_prep_value_empty(self): field = DictAsJsonField() result = field.get_prep_value(None) assert result is None
def test_to_python_pickle(self): field = DictAsJsonField() orig_value = 2 value = pickle.dumps(orig_value, protocol=1) result = field.to_python(value) assert result == orig_value