def test_formfield(self): field = JSONField("test") field.set_attributes_from_name("json") formfield = field.formfield() self.assertEqual(type(formfield), forms.CharField) self.assertEqual(type(formfield.widget), forms.Textarea)
def test_formfield_clean_none(self): field = JSONField("test") formfield = field.formfield() self.assertRaisesMessage(forms.ValidationError, force_text( formfield.error_messages['required']), formfield.clean, value=None)
def test_formfield_clean_blank(self): field = JSONField("test") formfield = field.formfield() self.assertRaisesMessage( forms.ValidationError, force_text(formfield.error_messages["required"]), formfield.clean, value="", )
def test_db_prep_save(self): field = JSONField("test") field.set_attributes_from_name("json") self.assertEqual(None, field.get_db_prep_save(None, connection=None)) self.assertEqual( '{"spam": "eggs"}', field.get_db_prep_save({"spam": "eggs"}, connection=None))
class DocIntegration(Model): """ Document based integrations can be found in Sentry, but are installed via code change. """ __include_in_export__ = False name = models.CharField(max_length=64) slug = models.CharField(max_length=64, unique=True) author = models.CharField(max_length=255) description = models.TextField() url = models.URLField() popularity = models.PositiveSmallIntegerField(null=True, default=1) # Quickly hide/show integration, used for logo uploads. is_draft = models.BooleanField(default=True) # Allow future extensions by adding more metadata. # To start, only { resources: [{title, link}] }. metadata = JSONField(null=True) class Meta: app_label = "sentry" db_table = "sentry_docintegration"
def test_formfield_null_and_blank_clean_blank(self): field = JSONField("test", null=True, blank=True) formfield = field.formfield() self.assertEqual(formfield.clean(value=''), '')
class CallableDefaultModel(models.Model): json = JSONField(default=lambda: {'x': 2}) class Meta: app_label = 'sentry'
class BlankJSONFieldTestModel(models.Model): null_json = JSONField(null=True) blank_json = JSONField(blank=True) class Meta: app_label = 'sentry'
def test_invalid_json_default(self): with self.assertRaises(ValueError): JSONField('test', default='{"foo"}')
class JSONFieldWithDefaultTestModel(models.Model): json = JSONField(default={"sukasuka": "YAAAAAZ"}) class Meta: app_label = 'sentry'
class JSONFieldTestModel(models.Model): json = JSONField("test", null=True, blank=True) class Meta: app_label = 'sentry'
class CallableDefaultModel(models.Model): json = JSONField(default=default) class Meta: app_label = "sentry"
def test_formfield_blank_clean_blank(self): field = JSONField("test", null=False, blank=True) formfield = field.formfield() self.assertEqual(formfield.clean(value=""), "")