예제 #1
0
    def test_nested_schemas(self):
        # Arrange
        schema = {
            "name": "TestSchema",
            "description": "Unit testing schema",
            "properties": {
                "prop1": {"type": "string", "description": "prop1 description"},
                "prop2": {"type": [{"$ref": "schema2"}], "description": "prop2 description"},
            },
        }
        schema2 = {
            "name": "TestSchema2",
            "description": "Unit testing schema2",
            "properties": {"p1": {"type": "string", "description": "prop1 description"}},
        }
        data = {"prop1": "value1", "prop2": {"p1": "value2"}}
        Schema = JSONSchemaModel.json_model_factory("Schema", schema)
        Schema2 = JSONSchemaModel.json_model_factory("Schema2", schema2, extends=Schema)
        loader_mock = SchemasLoader()
        loader_mock.get_class = MagicMock()
        loader_mock.get_class.return_value = Schema2

        # Act
        schemaModel = Schema(data, schemas_loader=loader_mock)
        # Assert
        self.assertTrue(hasattr(schemaModel, "prop1"))
        self.assertTrue(hasattr(schemaModel, "prop2"))
        self.assertEqual(schemaModel.prop1, data["prop1"])
        self.assertEqual(schemaModel.prop2, data["prop2"])
        self.assertEqual(schemaModel["prop1"], data["prop1"])
        self.assertEqual(schemaModel["prop2"], data["prop2"])
        self.assertEqual(type(schemaModel.prop2), Schema2)
예제 #2
0
    def test_json_model_factory(self):
        # Arrange
        schema = {
            "name": "TestSchema",
            "description": "Unit testing schema",
            "properties": {
                "prop1": {
                    "type": "string",
                    "description": "prop1 description",
                },
                "prop2": {
                    "type": "string",
                    "description": "prop2 description",
                },
            },
            "patternProperties": {
                "prop?": {
                    "type": "string",
                    "description": "pattern prop",
                },
            },
        }
        schema2 = {
            "name": "TestSchema2",
            "description": "Unit testing schema2",
            "properties": {
                "p1": {
                    "type": "string",
                    "description": "prop1 description",
                }
            }
        }

        # Act
        Schema = JSONSchemaModel.json_model_factory("Schema", schema)
        Schema2 = JSONSchemaModel.json_model_factory("Schema2",
                                                     schema2,
                                                     extends=Schema)
        schemaModel = Schema()
        schemaModel.prop1 = "value1"
        schemaModel["prop2"] = "value2"
        schemaModel.prop3 = "value3"
        schemaModel["prop4"] = "value4"
        schemaModel2 = Schema2()
        # Assert
        self.assertTrue(hasattr(schemaModel, "prop1"))
        self.assertTrue(hasattr(schemaModel, "prop2"))
        self.assertTrue(hasattr(schemaModel, "prop3"))
        self.assertTrue(hasattr(schemaModel, "prop4"))
        self.assertEqual(schemaModel.prop1, "value1")
        self.assertEqual(schemaModel.prop2, "value2")
        self.assertEqual(schemaModel.prop3, "value3")
        self.assertEqual(schemaModel.prop4, "value4")
        self.assertEqual(schemaModel["prop1"], "value1")
        self.assertEqual(schemaModel["prop2"], "value2")
        self.assertEqual(schemaModel["prop3"], "value3")
        self.assertEqual(schemaModel["prop4"], "value4")
예제 #3
0
 def test_json_model_factory(self):
     # Arrange
     schema = {"name": "TestSchema",
         "description": "Unit testing schema",
         "properties": {
             "prop1": {
                 "type": "string",
                 "description": "prop1 description",
             },
             "prop2": {
                 "type": "string",
                 "description": "prop2 description",
             },
         },
         "patternProperties": {
             "prop?": {
                 "type": "string",
                 "description": "pattern prop",
             },
         },
     }
     schema2 = {"name": "TestSchema2",
         "description": "Unit testing schema2",
         "properties": {
             "p1": {
                 "type": "string",
                 "description": "prop1 description",
             }
         }
     }
     
     # Act
     Schema = JSONSchemaModel.json_model_factory("Schema", schema)
     Schema2 = JSONSchemaModel.json_model_factory("Schema2", schema2,
         extends=Schema)
     schemaModel = Schema()
     schemaModel.prop1 = "value1"
     schemaModel["prop2"] = "value2"
     schemaModel.prop3 = "value3"
     schemaModel["prop4"] = "value4"
     schemaModel2 = Schema2()
     # Assert
     self.assertTrue(hasattr(schemaModel, "prop1"))
     self.assertTrue(hasattr(schemaModel, "prop2"))
     self.assertTrue(hasattr(schemaModel, "prop3"))
     self.assertTrue(hasattr(schemaModel, "prop4"))
     self.assertEqual(schemaModel.prop1, "value1")
     self.assertEqual(schemaModel.prop2, "value2")
     self.assertEqual(schemaModel.prop3, "value3")
     self.assertEqual(schemaModel.prop4, "value4")
     self.assertEqual(schemaModel["prop1"], "value1")
     self.assertEqual(schemaModel["prop2"], "value2")
     self.assertEqual(schemaModel["prop3"], "value3")
     self.assertEqual(schemaModel["prop4"], "value4")
예제 #4
0
    def test_nested_schemas(self):
        # Arrange
        schema = {
            "name": "TestSchema",
            "description": "Unit testing schema",
            "properties": {
                "prop1": {
                    "type": "string",
                    "description": "prop1 description",
                },
                "prop2": {
                    "type": [{
                        "$ref": "schema2"
                    }],
                    "description": "prop2 description",
                },
            }
        }
        schema2 = {
            "name": "TestSchema2",
            "description": "Unit testing schema2",
            "properties": {
                "p1": {
                    "type": "string",
                    "description": "prop1 description",
                }
            }
        }
        data = {
            "prop1": "value1",
            "prop2": {
                "p1": "value2",
            }
        }
        Schema = JSONSchemaModel.json_model_factory("Schema", schema)
        Schema2 = JSONSchemaModel.json_model_factory("Schema2",
                                                     schema2,
                                                     extends=Schema)
        loader_mock = SchemasLoader()
        loader_mock.get_class = MagicMock()
        loader_mock.get_class.return_value = Schema2

        # Act
        schemaModel = Schema(data, schemas_loader=loader_mock)
        # Assert
        self.assertTrue(hasattr(schemaModel, "prop1"))
        self.assertTrue(hasattr(schemaModel, "prop2"))
        self.assertEqual(schemaModel.prop1, data["prop1"])
        self.assertEqual(schemaModel.prop2, data["prop2"])
        self.assertEqual(schemaModel["prop1"], data["prop1"])
        self.assertEqual(schemaModel["prop2"], data["prop2"])
        self.assertEqual(type(schemaModel.prop2), Schema2)
예제 #5
0
 def test_init(self):
     # Arrange
     schema = {
         "name": "TestSchema",
         "description": "Unit testing schema",
         "additionalProperties": False,
         "properties": {
             "prop1": {
                 "type": "string",
                 "description": "prop1 description",
             },
             "prop2": {
                 "type": "integer",
                 "description": "prop2 description",
             },
         },
         "patternPropeties": {
             "prop?": {
                 "type": "string",
                 "description": "pattern prop",
             },
         },
     }
     # Act
     schemaModel = JSONSchemaModel.json_model_factory("schemaModel", schema)
     # Assert
     self.assertTrue(issubclass(schemaModel, JSONSchemaModel))
     self.assertEqual(schemaModel.__doc__, schema["description"])
     self.assertTrue(hasattr(schemaModel, "prop1"))
     self.assertTrue(hasattr(schemaModel, "prop2"))
예제 #6
0
 def test_init(self):
     # Arrange
     schema = {"name": "TestSchema",
         "description": "Unit testing schema",
         "additionalProperties": False,
         "properties": {
             "prop1": {
                 "type": "string",
                 "description": "prop1 description",
             },
             "prop2": {
                 "type": "integer",
                 "description": "prop2 description",
             },
         },
         "patternPropeties": {
             "prop?": {
                 "type": "string",
                 "description": "pattern prop",
             },
         },
     }
     # Act
     schemaModel = JSONSchemaModel.json_model_factory("schemaModel", schema)
     # Assert
     self.assertTrue(issubclass(schemaModel, JSONSchemaModel))
     self.assertEqual(schemaModel.__doc__, schema["description"])
     self.assertTrue(hasattr(schemaModel, "prop1"))
     self.assertTrue(hasattr(schemaModel, "prop2"))
예제 #7
0
 def test_set_value(self):
     # Arrange
     schema = {
         "name": "TestSchema",
         "description": "Unit testing schema",
         "properties": {
             "prop1": {"type": "string", "description": "prop1 description"},
             "prop2": {"type": "string", "description": "prop2 description"},
         },
         "patternProperties": {"prop?": {"type": "string", "description": "pattern prop"}},
     }
     SchemaModel = JSONSchemaModel.json_model_factory("schemaModel", schema)
     schemaModel = SchemaModel()
     # Act
     schemaModel.prop1 = "value1"
     schemaModel["prop2"] = "value2"
     schemaModel.prop3 = "value3"
     schemaModel["prop4"] = "value4"
     # Assert
     self.assertTrue(hasattr(schemaModel, "prop1"))
     self.assertTrue(hasattr(schemaModel, "prop2"))
     self.assertTrue(hasattr(schemaModel, "prop3"))
     self.assertTrue(hasattr(schemaModel, "prop4"))
     self.assertEqual(schemaModel.prop1, "value1")
     self.assertEqual(schemaModel.prop2, "value2")
     self.assertEqual(schemaModel.prop3, "value3")
     self.assertEqual(schemaModel.prop4, "value4")
     self.assertEqual(schemaModel["prop1"], "value1")
     self.assertEqual(schemaModel["prop2"], "value2")
     self.assertEqual(schemaModel["prop3"], "value3")
     self.assertEqual(schemaModel["prop4"], "value4")