def test_map_key_types(self):
        class A(Unimodel):
            a = Field(Map(UTF8, Int))
            b = Field(Map(Binary, Int))
            c = Field(Map(Int8, Int))
            d = Field(Map(Int, Int))
            e = Field(Map(Enum({1: "one", 2: "two"}), Int))

        data = A(
            a={"a": 1},
            b={bin(173): 1},
            c={1: 1},
            d={2 ** 40: 1},
            e={2: 1})

        serializer = JSONSerializer()
        s = serializer.serialize(data)
        read_data = serializer.deserialize(A, s)
        self.assertEquals(data, read_data)

        class B(Unimodel):
            a = Field(Map(Double, Int))

        exc = None
        try:
            print serializer.serialize(B(a={2.333: 1}))
        except Exception as exc:
            pass
 def test_serialize_all_types_data(self):
     """ serialize a complex recursive datatype into JSON """
     serializer = JSONSerializer()
     for ix in xrange(0, len(all_types_data)):
         s = serializer.serialize(all_types_data[ix])
         d = serializer.deserialize(all_types_data[ix].__class__, s)
         self.assertEquals(d, all_types_data[ix])
 def test_enum_serialize(self):
     """ serialize a complex recursive datatype into JSON """
     class A(Unimodel):
         f = Field(Enum({1: "one", 2: "two"}))
     serializer = JSONSerializer()
     s = serializer.serialize(A(f=1))
     parsed_json = json.loads(s)
     self.assertEquals(parsed_json['f'], "one")
     d = serializer.deserialize(A, s)
     self.assertEquals(d.f, 1)
     exc = None
     try:
         serializer.serialize(A(f=33))
     except Exception as exc:
         pass
     self.assertTrue(isinstance(exc, ValueTypeException))
Exemple #4
0
 def test_validate_recursive_schema(self):
     # based on http://sacharya.com/validating-json-using-python-jsonschema/
     schema_writer = JSONSchemaWriter()
     schema = schema_writer.get_schema_ast(TreeNode)
     serializer = JSONSerializer()
     json_data = json.loads(serializer.serialize(tree_data))
     jsonschema.validate(json_data, schema)
 def test_serialize_tree_data(self):
     """ serialize a complex recursive datatype into JSON """
     pre_flattened = flatten(tree_data)
     serializer = JSONSerializer()
     s = serializer.serialize(tree_data)
     d = serializer.deserialize(TreeNode, s)
     self.assertEquals(d.__class__, TreeNode)
     post_flattened = flatten(d)
Exemple #6
0
 def test_validate_all_types(self):
     # based on http://sacharya.com/validating-json-using-python-jsonschema/
     schema_writer = JSONSchemaWriter()
     schema = schema_writer.get_schema_ast(AllTypes)
     serializer = JSONSerializer()
     for ix in xrange(0, len(all_types_data)):
         json_data = json.loads(serializer.serialize(all_types_data[ix]))
         jsonschema.validate(json_data, schema)
 def test_simple_struct(self):
     schema_writer = JSONSchemaWriter()
     schema = schema_writer.get_schema_ast(TreeNode)
     generator = JSONSchemaModelGenerator('untitled', schema)
     serializer = JSONSerializer()
     json_data = json.loads(
         serializer.serialize(
             generator.generate_model_schema()))
    def test_custom_field_names(self):
        NAME = "/-/"

        class A(Unimodel):
            a = Field(Map(UTF8, Int), metadata=Metadata(
                backend_data={'json': {MDK_FIELD_NAME: NAME}}))

        serializer = JSONSerializer()
        data = A(a={"a": 1})
        s = serializer.serialize(data)
        parsed_json = json.loads(s)
        self.assertTrue(NAME in parsed_json)
        self.assertEquals(data, serializer.deserialize(A, s))
    def test_unicode_and_binary(self):
        """ serialize unicode and binary data """
        class ExampleClass(Unimodel):
            u = Field(UTF8, required=True)
            s = Field(Binary)

        test_string1 = unichr(40960)
        test_string2 = b"alma"
        data = ExampleClass(u=test_string1, s=test_string2)
        serializer = JSONSerializer()
        s = serializer.serialize(data)
        json_data = json.loads(s)
        d = serializer.deserialize(ExampleClass, s)
        self.assertEquals(d.s, data.s)
        self.assertEquals(d.u, data.u)
        self.assertEquals(type(d.u), unicode)
        self.assertNotEquals(d.s, json_data['s'])
    def test_unboxed_struct(self):
        class Unboxed(Unimodel):
            a = Field(Int)
            b = Field(Int)

        class Parent(Unimodel):
            a = Field(
                Struct(Unboxed),
                metadata=Metadata(
                    backend_data={'json': {MDK_TYPE_STRUCT_UNBOXED: True}}))
            c = Field(Int)

        serializer = JSONSerializer()
        data = Parent(a=Unboxed(a=1, b=2), c=3)
        s = serializer.serialize(data)
        parsed_json = json.loads(s)
        self.assertEquals(sorted(parsed_json.keys()), ["a", "b", "c"])
        self.assertEquals(data, serializer.deserialize(Parent, s))
    def test_swagger_struct(self):
        with open("/Users/neumark/git/swagger-spec/schemas/v2.0/schema.json", "r") as f:
            schema = json.loads(f.read())
        schema_name = 'swagger'

        generator = JSONSchemaModelGenerator(schema_name, schema)
        serializer = JSONSerializer()
        model_schema = generator.generate_model_schema()

        #model_schema.validate()
        json_data = json.loads(serializer.serialize(model_schema))
        output_json = json.dumps(
            json_data,
            sort_keys=True,
            indent=4,
            separators=(',', ': '))
        #print output_json
        python_source = SchemaCompiler(model_schema).generate_model_classes()