def test_schema_generation_for_text_field(self): class TestForm(forms.ModelForm): class Meta: model = TestModel fields = ('text_field', ) schema = Schema().render(TestForm(data={})) expected = { 'model': { 'text_field': None, }, 'schema': { 'fields': [ { 'default': None, 'hint': '', 'label': 'Text field', 'model': 'text_field', 'required': True, 'rows': 10, 'type': 'textArea' }, ], }, } self.assertEqual(schema, expected)
def test_schema_generation_without_data(self): class TestForm(forms.ModelForm): class Meta: model = TestModel fields = ('char_field', ) schema = Schema().render(TestForm()) expected = { 'model': { 'char_field': None, }, 'schema': { 'fields': [ { 'default': None, 'hint': '', 'label': 'Char field', 'model': 'char_field', 'required': True, 'type': 'text' }, ], }, } self.assertEqual(schema, expected)
def test_schema_generation_with_dotted_name(self): class TestForm(forms.ModelForm): class Meta: model = TestModel fields = ('field__with__dot', ) schema = Schema().render(TestForm()) expected = { 'model': { 'field': { 'with': { 'dot': None, }, }, }, 'schema': { 'fields': [ { 'default': None, 'hint': '', 'label': 'Field with dot', 'model': 'field.with.dot', 'required': True, 'type': 'text' }, ], }, } self.assertEqual(schema, expected)
def test_schema_generation_with_form_cls(self): class TestForm(forms.ModelForm): class Meta: model = TestModel fields = ('char_field', ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') schema = Schema().render(TestForm) expected = { 'model': { 'char_field': None, }, 'schema': { 'fields': [ { 'default': None, 'hint': '', 'label': 'Char field', 'model': 'char_field', 'required': True, 'type': 'text' }, ], }, } self.assertEqual(schema, expected) self.assertEqual(len(w), 1) self.assertIs(w[0].category, DeprecationWarning) self.assertIn("Deprecated", str(w[0].message))
def test_schema_generation_for_related_field(self): class TestForm(forms.ModelForm): class Meta: model = OtherModel fields = ('other_field', ) self.maxDiff = None schema = Schema().render(TestForm(data={})) expected = { 'model': { 'other_field': None, }, 'schema': { 'fields': [ { 'default': None, 'hint': '', 'label': 'Other field', 'model': 'other_field', 'required': True, 'type': 'select', 'values': [ { 'id': '', 'name': '---------' }, ], }, ], }, } self.assertEqual(schema, expected)
def get_schema(form): schema = Schema().render(form) for field in schema['schema']['fields']: if field['type'] in SCHEMA_DICT.keys(): field['inputType'] = EXTRA_FIELDS_DICT.get(field['model'], field['type']) field['type'] = SCHEMA_DICT[field['type']] return schema
def test_schema_generation_for_choice(self): class TestForm(forms.ModelForm): class Meta: model = TestModel fields = ('choice_field', ) schema = Schema().render(TestForm(data={})) expected = { 'model': { 'choice_field': None, }, 'schema': { 'fields': [{ 'default': None, 'hint': '', 'label': 'Choice field', 'model': 'choice_field', 'required': True, 'type': 'select', 'values': [ { 'id': '', 'name': '---------' }, { 'id': 'FR', 'name': 'Freshman' }, { 'id': 'SO', 'name': 'Sophomore' }, { 'id': 'JR', 'name': 'Junior' }, { 'id': 'SR', 'name': 'Senior' }, ], }], }, } self.assertEqual(schema, expected)
def test_correct_error_thrown(self): from django.forms.widgets import Widget class NotARealWidget(Widget): def __repr__(self): return "NotARealWidget" class TestForm(forms.Form): not_a_real = forms.CharField(widget=NotARealWidget) expected = 'Could not find component "NotARealWidget"' with self.assertRaises(KeyError, msg=expected) as context: schema = Schema().render(TestForm(data={}))
def test_schema_generation_for_boolean(self): class TestForm(forms.ModelForm): class Meta: model = TestModel fields = ('boolean_field', ) schema = Schema().render(TestForm()) expected = { 'model': { 'boolean_field': False, }, 'schema': { 'fields': [{ 'default': None, 'hint': '', 'label': 'Boolean field', 'model': 'boolean_field', 'required': False, 'type': 'checkbox' }], }, } self.assertEqual(schema, expected)
def test_schema_generation_for_integer(self): class TestForm(forms.ModelForm): class Meta: model = TestModel fields = ('integer_field', ) schema = Schema().render(TestForm(data={})) expected = { 'model': { 'integer_field': None, }, 'schema': { 'fields': [{ 'default': None, 'hint': '', 'label': 'Integer field', 'model': 'integer_field', 'required': True, 'type': 'number', }], }, } self.assertEqual(schema, expected)