def setUp(self):
     self.encoder = DjangoFormToJSONSchema()
     self.schema = self.encoder.convert_form(SurveyForm)
     
     class TargetForm(forms.Form):
         ticket = forms.CharField()
         
         #presumable a very configurable survey can be plugged in here
         survey_entry = JSONSchemaField(schema=self.schema)
     
     self.targetform = TargetForm
class JSONSchemaFieldTestCase(unittest.TestCase):
    def setUp(self):
        self.encoder = DjangoFormToJSONSchema()
        self.schema = self.encoder.convert_form(SurveyForm)

        class TargetForm(forms.Form):
            ticket = forms.CharField()

            # presumable a very configurable survey can be plugged in here
            survey_entry = JSONSchemaField(schema=self.schema)

        self.targetform = TargetForm

    def test_field_presentation(self):
        field = self.targetform()["survey_entry"]
        html = field.as_widget()
        self.assertTrue("data-schemajson" in html)

    def test_field_validation(self):
        survey_response = {"name": "John Smith", "ranking": "1"}
        post = {"survey_entry": json.dumps(survey_response), "ticket": "text"}
        form = self.targetform(data=post)
        self.assertTrue(form.is_valid(), str(form.errors))

        survey_response = {"name": "John Smith", "ranking": "5"}
        post = {"survey_entry": json.dumps(survey_response), "ticket": "text"}
        form = self.targetform(data=post)
        self.assertFalse(form.is_valid())
        print form.errors
class JSONSchemaFieldTestCase(unittest.TestCase):
    def setUp(self):
        self.encoder = DjangoFormToJSONSchema()
        self.schema = self.encoder.convert_form(SurveyForm)

        class TargetForm(forms.Form):
            ticket = forms.CharField()

            #presumable a very configurable survey can be plugged in here
            survey_entry = JSONSchemaField(schema=self.schema)

        self.targetform = TargetForm

    def test_field_presentation(self):
        field = self.targetform()['survey_entry']
        html = field.as_widget()
        self.assertTrue('data-schemajson' in html)

    def test_field_validation(self):
        survey_response = {
            'name': 'John Smith',
            'ranking': '1',
        }
        post = {'survey_entry': json.dumps(survey_response), 'ticket': 'text'}
        form = self.targetform(data=post)
        self.assertTrue(form.is_valid(), str(form.errors))

        survey_response = {
            'name': 'John Smith',
            'ranking': '5',
        }
        post = {'survey_entry': json.dumps(survey_response), 'ticket': 'text'}
        form = self.targetform(data=post)
        self.assertFalse(form.is_valid())
        print(form.errors)
    def setUp(self):
        self.encoder = DjangoFormToJSONSchema()
        self.schema = self.encoder.convert_form(SurveyForm)

        class TargetForm(forms.Form):
            ticket = forms.CharField()

            # presumable a very configurable survey can be plugged in here
            survey_entry = JSONSchemaField(schema=self.schema)

        self.targetform = TargetForm
Esempio n. 5
0
def get_schema(language_name):
    if language_name not in LANGUAGE_SCHEMAS:
        form_translator = DjangoFormToJSONSchema()
        language = LANGUAGES[language_name]
        schema = {
            "$schema": "http://json-schema.org/draft-04/schema#",
            "description": "Language schema for %s" % language_name,
            "definitions": {
                "basecondition": {
                    "type": "object",
                },
                "conditionArray": {
                    "type": "array",
                    "minItems": 1,
                    #CONSIDER: perhaps we want to enum the available implementing conditions
                    "items": {"$ref": "#/definitions/basecondition"},
                },
            },
            "properties": {
                "conditions": {
                    {"$ref": "#/definitions/conditionArray"}
                },
            },
        }
        
        for name, condition_cls in language.iteritems():
            definition = form_translator.convert_form(condition_cls)
            definition['extends'] = '#/definitions/basecondition'
            definition['condition_type'] = {"enum": [name]}
            schema['definitions'][name] = definition
        ifcondition = get_ifcondition_for_language(language, schema)
        definition = form_translator.convert_form(ifcondition)
        definition['extends'] = '#/definitions/basecondition'
        definition['condition_type'] = {"enum": ['ifcondition']}
        schema['definitions']['ifcondition'] = definition
        language['ifcondition'] = ifcondition
        LANGUAGE_SCHEMAS[language_name] = schema
    return LANGUAGE_SCHEMAS[language_name]
Esempio n. 6
0
 def setUp(self):
     self.encoder = DjangoFormToJSONSchema()
Esempio n. 7
0
class FormToJsonSchemaTestCase(unittest.TestCase):
    def setUp(self):
        self.encoder = DjangoFormToJSONSchema()

    def test_convert_form(self):
        form_repr = self.encoder.convert_form(TestForm)
        print form_repr
        try:
            check_schema(form_repr)
        except Exception as error:
            print error.message
            print error.path
            print error.validator
            print error.cause
            raise
        instance_form_repr = self.encoder.convert_form(TestForm())
        check_schema(instance_form_repr)

    def test_convert_charfield(self):
        name = 'a_charfield'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'description': u'Any string',
            'title': 'A charfield',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_charfield(self):
        #CONSIDER: the json spec doesn't define a textarea, this is an option of alpacajs
        name = 'a_textarea'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'description': u'Any paragraph',
            'title': 'A textarea',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_urlfield(self):
        name = 'url'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'url',
            'description': u'',
            'title': 'Url',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_booleanfield(self):
        name = 'a_boolean'
        ideal_repr = {
            'required': [name],
            'type': 'boolean',
            'description': u'',
            'title': 'A boolean',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_select_option(self):
        name = 'select_option'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'description': u'',
            'title': 'Select option',
            'enum': ['first', 'second'],
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_date(self):
        name = 'a_date'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'date',
            'description': u'',
            'title': 'A date',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_datetime(self):
        name = 'a_datetime'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'datetime',
            'description': u'',
            'title': 'A datetime',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_decimal(self):
        name = 'a_decimal'
        ideal_repr = {
            'required': [name],
            'type': 'number',
            'description': u'',
            'title': 'A decimal',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_email(self):
        name = 'an_email'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'email',
            'description': u'',
            'title': 'An email',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_file(self):
        name = 'a_file'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'uri',
            'description': u'',
            'title': 'A file',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_float(self):
        name = 'a_float'
        ideal_repr = {
            'required': [name],
            'type': 'number',
            'description': u'',
            'title': 'A float',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)

    def test_convert_integer(self):
        name = 'an_integer'
        ideal_repr = {
            'required': [name],
            'type': 'integer',
            'description': u'',
            'title': 'An integer',
        }
        field = TestForm.base_fields[name]
        json_schema = {'properties': {}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
Esempio n. 8
0
 def to_representation(self, instance):
     form = PageForm(instance)
     return DjangoFormToJSONSchema().convert_form(form)
 def setUp(self):
     self.encoder = DjangoFormToJSONSchema()
class FormToJsonSchemaTestCase(unittest.TestCase):
    def setUp(self):
        self.encoder = DjangoFormToJSONSchema()
    
    def test_convert_form(self):
        form_repr = self.encoder.convert_form(TestForm)
        print form_repr
        try:
            check_schema(form_repr)
        except Exception as error:
            print error.message
            print error.path
            print error.validator
            print error.cause
            raise
        instance_form_repr = self.encoder.convert_form(TestForm())
        check_schema(instance_form_repr)
    
    def test_convert_charfield(self):
        name = 'a_charfield'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'description': u'Any string',
            'title': 'A charfield',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_charfield(self):
        #CONSIDER: the json spec doesn't define a textarea, this is an option of alpacajs
        name = 'a_textarea'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'description': u'Any paragraph',
            'title': 'A textarea',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_urlfield(self):
        name = 'url'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'url',
            'description': u'',
            'title': 'Url',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_booleanfield(self):
        name = 'a_boolean'
        ideal_repr = {
            'required': [name],
            'type': 'boolean',
            'description': u'',
            'title': 'A boolean',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_select_option(self):
        name = 'select_option'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'description': u'',
            'title': 'Select option',
            'enum': ['first', 'second'],
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_date(self):
        name = 'a_date'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'date',
            'description': u'',
            'title': 'A date',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_datetime(self):
        name = 'a_datetime'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'datetime',
            'description': u'',
            'title': 'A datetime',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_decimal(self):
        name = 'a_decimal'
        ideal_repr = {
            'required': [name],
            'type': 'number',
            'description': u'',
            'title': 'A decimal',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_email(self):
        name = 'an_email'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'email',
            'description': u'',
            'title': 'An email',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_file(self):
        name = 'a_file'
        ideal_repr = {
            'required': [name],
            'type': 'string',
            'format': 'uri',
            'description': u'',
            'title': 'A file',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_float(self):
        name = 'a_float'
        ideal_repr = {
            'required': [name],
            'type': 'number',
            'description': u'',
            'title': 'A float',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)
    
    def test_convert_integer(self):
        name = 'an_integer'
        ideal_repr = {
            'required': [name],
            'type': 'integer',
            'description': u'',
            'title': 'An integer',
        } 
        field = TestForm.base_fields[name]
        json_schema = {'properties':{}}
        schema_repr = self.encoder.convert_formfield(name, field, json_schema)
        self.assertEqual(schema_repr, ideal_repr)