def test_hstoredictionaryexception(self): # ok HStoreDict({}) # json object string allowed HStoreDict('{}') # None is ok, will be converted to empty dict HStoreDict(None) HStoreDict() # non-json string not allowed with self.assertRaises(HStoreDictException): HStoreDict('wrong') # list not allowed with self.assertRaises(HStoreDictException): HStoreDict(['wrong']) # json array string representation not allowed with self.assertRaises(HStoreDictException): HStoreDict('["wrong"]') # number not allowed with self.assertRaises(HStoreDictException): HStoreDict(3)
def to_native(self, value): if isinstance(value, dict) or value is None: return value value = HStoreDict(value) return value
def from_native(self, value): if value: try: return HStoreDict(value) except HStoreDictException as e: raise ValidationError( _('Invalid JSON: %s' % e.json_error_message)) else: return None
def test_hstoredictionary_unicoce_vs_str(self): d = HStoreDict({ 'test': 'test' }) self.assertEqual(d.__str__(), d.__unicode__())
def test_get_default(self): d = HStoreDict() self.assertIsNone(d.get('none_key', None)) self.assertIsNone(d.get('none_key'))
def test_hstoredictionary_unicode_vs_str(self): d = HStoreDict({'test': 'test'}) self.assertEqual(d.__str__(), d.__unicode__())
def test_get_default(self): d = HStoreDict() self.assertIsNone(d.get('none_key', None)) with self.assertRaises(KeyError): d.get('none_key')