Beispiel #1
0
 def test_definitions_with_refs(self):
     loader = jsonschema2popo.JsonSchema2Popo(use_types=False,
                                              constructor_type_check=False)
     loader.process(
         json.loads("""{
         "definitions": {
             "ABcd": {
                 "type": "object",
                 "properties": {
                     "Child1": {
                         "type": "integer"
                     },
                     "Child2": {
                         "type": "string"
                     }
                 }
             },
             "SubRef": {
                 "type": "object",
                 "properties": {
                     "ChildA": {
                         "$ref": "#/definitions/ABcd"
                     }
                 }
             },
             "DirectRef": {
                 "$ref": "#/definitions/ABcd"
             }
         }
     }"""))
     loader.write_file(self.test_file)
     format_file(self.test_file)
     self.assertFileEqual(self.test_file,
                          "valid/test_definitions_with_refs.py")
Beispiel #2
0
 def test_root_basic_generation(self):
     loader = jsonschema2popo.JsonSchema2Popo(use_types=False,
                                              constructor_type_check=False)
     loader.process(
         json.loads("""{
         "title": "ABcd",
         "type": "object",
         "properties": {
             "Int": {
                 "type": "integer"
             },
             "Float": {
                 "type": "number"
             },
             "ListInt": {
                 "type": "array",
                 "items": {
                     "type": "integer"
                 }
             },
             "String": {
                 "type": "string"
             },
             "Object": {
                 "type": "object"
             }
         }
     }"""))
     loader.write_file(self.test_file)
     format_file(self.test_file)
     self.assertFileEqual(self.test_file,
                          "valid/test_root_basic_generation.py")
Beispiel #3
0
 def test_root_string_enum(self):
     loader = jsonschema2popo.JsonSchema2Popo(use_types=False,
                                              constructor_type_check=False)
     loader.process(
         json.loads("""{
         "title": "ABcd",
         "type": "string",
         "enum": ["A", "B", "C"]
     }"""))
     loader.write_file(self.test_file)
     format_file(self.test_file)
     self.assertFileEqual(self.test_file, "valid/test_root_string_enum.py")
Beispiel #4
0
    def generate_files(self, schema, **kwargs):
        self.test_file = f"generated/{self.id()}.py"
        self.test_file_js = f"generated/{self.id()}.js"
        try:
            os.makedirs("generated", exist_ok=True)
        except:
            pass
        self.test_file_go = f"generated/{self.id()}.go"

        loader = jsonschema2popo.JsonSchema2Popo(
            language="python",
            **kwargs,
        )
        loader.update_args(
            argparse.Namespace(
                use_types=True,
                constructor_type_check=True,
                use_slots=True,
            ))
        loader.process(json.loads(schema))
        loader.write_file(self.test_file)
        Python.format_python_file(self.test_file)

        loader = jsonschema2popo.JsonSchema2Popo(
            language="js",
            **kwargs,
        )
        loader.update_args(argparse.Namespace(constructor_type_check=True))
        loader.process(json.loads(schema))
        loader.write_file(self.test_file_js)
        JS.format_js_file(self.test_file_js)

        loader = jsonschema2popo.JsonSchema2Popo(
            language="go",
            **kwargs,
        )
        loader.update_args(argparse.Namespace(package_name="generated", ))
        loader.process(json.loads(schema))
        loader.write_file(self.test_file_go)
        Go.format_go_file(self.test_file_go)
Beispiel #5
0
    def generate_files(self, schema, **kwargs):
        self.test_file = f"{self.id()}.py"
        self.test_file_js = f"{self.id()}.js"
        try:
            os.mkdir("generated")
        except:
            pass
        self.test_file_go = f"generated/{self.id()}.go"

        loader = jsonschema2popo.JsonSchema2Popo(
            use_types=True,
            constructor_type_check=True,
            use_slots=True,
            language="python",
            **kwargs,
        )
        loader.process(json.loads(schema))
        loader.write_file(self.test_file)
        format_python_file(self.test_file)

        loader = jsonschema2popo.JsonSchema2Popo(
            use_types=True,
            constructor_type_check=True,
            use_slots=True,
            language="js",
            **kwargs,
        )
        loader.process(json.loads(schema))
        loader.write_file(self.test_file_js)
        format_js_file(self.test_file_js)

        loader = jsonschema2popo.JsonSchema2Popo(
            language="go",
            package_name="generated",
            **kwargs,
        )
        loader.process(json.loads(schema))
        loader.write_file(self.test_file_go)
        format_go_file(self.test_file_go)
Beispiel #6
0
 def test_root_integer_enum(self):
     loader = jsonschema2popo.JsonSchema2Popo(use_types=False,
                                              constructor_type_check=False)
     loader.process(
         json.loads("""{
         "title": "ABcd",
         "type": "integer",
         "enum": [0, 1, 2, 3],
         "javaEnumNames": ["A", "B", "C", "D"]
     }"""))
     loader.write_file(self.test_file)
     format_file(self.test_file)
     self.assertFileEqual(self.test_file, "valid/test_root_integer_enum.py")
Beispiel #7
0
 def test_definitions_nested_objects(self):
     loader = jsonschema2popo.JsonSchema2Popo(use_types=False,
                                              constructor_type_check=False)
     loader.process(
         json.loads("""{
         "definitions": {
             "ABcd": {
                 "type": "object",
                 "properties": {
                     "Child1": {
                         "type": "object",
                         "properties": {
                             "IntVal": {
                                 "type": "integer"
                             },
                             "Child2": {
                                 "type": "object",
                                 "properties": {
                                     "IntVal": {
                                         "type": "integer"
                                     },
                                     "ListVal": {
                                         "type": "array",
                                         "items": {
                                             "type": "string"
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }"""))
     loader.write_file(self.test_file)
     format_file(self.test_file)
     self.assertFileEqual(self.test_file,
                          "valid/test_definitions_nested_objects.py")