コード例 #1
0
    def test_deserialize_oneof_reference(self):
        """
        Validate the scenario when the type of a OAS property is 'oneOf', and the 'oneOf'
        schema is specified as a reference ($ref), not an inline 'oneOf' schema.
        """
        isosceles_triangle = shape.Shape(
            shape_type="Triangle",
            triangle_type="IsoscelesTriangle"
        )
        from petstore_api.model.isosceles_triangle import IsoscelesTriangle
        from petstore_api.model.triangle import Triangle
        from petstore_api.model.equilateral_triangle import EquilateralTriangle

        assert isinstance(isosceles_triangle, IsoscelesTriangle)
        inst = Drawing(
            # 'main_shape' has type 'Shape', which is a oneOf [triangle, quadrilateral]
            # composed schema. So we should be able to assign a petstore_api.Triangle
            # to a 'main_shape'.
            main_shape=isosceles_triangle,
            shapes=[
                shape.Shape(
                    shape_type="Triangle",
                    triangle_type="EquilateralTriangle"
                ),
                Triangle(
                    shape_type="Triangle",
                    triangle_type="IsoscelesTriangle"
                ),
                EquilateralTriangle(
                    shape_type="Triangle",
                    triangle_type="EquilateralTriangle"
                ),
                shape.Shape(
                    shape_type="Quadrilateral",
                    quadrilateral_type="ComplexQuadrilateral"
                ),
            ],
        )
        from petstore_api.model.complex_quadrilateral import ComplexQuadrilateral
        assert isinstance(inst, Drawing)
        assert isinstance(inst.main_shape, IsoscelesTriangle)
        self.assertEqual(len(inst.shapes), 4)
        assert isinstance(inst.shapes[0], EquilateralTriangle)
        assert isinstance(inst.shapes[1], IsoscelesTriangle)
        assert isinstance(inst.shapes[2], EquilateralTriangle)
        assert isinstance(inst.shapes[3], ComplexQuadrilateral)

        # Validate we cannot assign the None value to main_shape because the 'null' type
        # is not one of the allowed types in the 'Shape' schema.
        err_msg = ("Invalid type for variable '{}'. "
                   "Required value type is {} and passed type was {} at {}")
        with self.assertRaisesRegexp(
                petstore_api.ApiTypeError,
                err_msg.format("main_shape", "Shape", "NoneType", "\['main_shape'\]")
        ):
            inst = Drawing(
                # 'main_shape' has type 'Shape', which is a oneOf [triangle, quadrilateral]
                # So the None value should not be allowed and an exception should be raised.
                main_shape=None,
            )
コード例 #2
0
    def test_create_instances(self):
        """
        Validate instance can be created using pythonic name or OAS names.
        """

        # Validate object can be created using pythonic names.
        inst = shape.Shape(shape_type="Triangle",
                           triangle_type="IsoscelesTriangle")
        from petstore_api.model.isosceles_triangle import IsoscelesTriangle
        assert isinstance(inst, IsoscelesTriangle)

        # Validate object can be created using OAS names.
        # For example, this can be used to construct objects on the client
        # when the input data is available as JSON documents.
        data = {'shapeType': "Triangle", 'triangleType': "IsoscelesTriangle"}
        inst = shape.Shape(_spec_property_naming=True, **data)
        assert isinstance(inst, IsoscelesTriangle)
コード例 #3
0
    def test_create_instances(self):
        """
        Validate instance can be created
        """

        inst = shape.Shape(shapeType="Triangle",
                           triangleType="IsoscelesTriangle")
        from petstore_api.model.isosceles_triangle import IsoscelesTriangle
        assert isinstance(inst, IsoscelesTriangle)
コード例 #4
0
    def test_deserialize_oneof_reference(self):
        """
        Validate the scenario when the type of a OAS property is 'oneOf', and the 'oneOf'
        schema is specified as a reference ($ref), not an inline 'oneOf' schema.
        """
        isosceles_triangle = shape.Shape(shapeType="Triangle",
                                         triangleType="IsoscelesTriangle")
        from petstore_api.model.isosceles_triangle import IsoscelesTriangle
        assert isinstance(isosceles_triangle, IsoscelesTriangle)
        from petstore_api.model.equilateral_triangle import EquilateralTriangle

        inst = Drawing(
            mainShape=isosceles_triangle,
            shapes=[
                shape.Shape(shapeType="Triangle",
                            triangleType="EquilateralTriangle"),
                shape.Shape(shapeType="Triangle",
                            triangleType="IsoscelesTriangle"),
                shape.Shape(shapeType="Triangle",
                            triangleType="EquilateralTriangle"),
                shape.Shape(shapeType="Quadrilateral",
                            quadrilateralType="ComplexQuadrilateral")
            ],
        )
        assert isinstance(inst, Drawing)
        assert isinstance(inst.mainShape, IsoscelesTriangle)
        self.assertEqual(len(inst.shapes), 4)
        from petstore_api.model.complex_quadrilateral import ComplexQuadrilateral
        assert isinstance(inst.shapes[0], EquilateralTriangle)
        assert isinstance(inst.shapes[1], IsoscelesTriangle)
        assert isinstance(inst.shapes[2], EquilateralTriangle)
        assert isinstance(inst.shapes[3], ComplexQuadrilateral)

        # Validate we cannot assign the None value to mainShape because the 'null' type
        # is not one of the allowed types in the 'Shape' schema.
        err_msg = (
            r"Invalid inputs given to generate an instance of .+?Shape.+? "
            r"None of the oneOf schemas matched the input data.")
        with self.assertRaisesRegex(petstore_api.ApiValueError, err_msg):
            inst = Drawing(
                # 'mainShape' has type 'Shape', which is a oneOf [triangle, quadrilateral]
                # So the None value should not be allowed and an exception should be raised.
                mainShape=None, )
        """
        we can't pass in an incorrect type for shapes
        'shapes' items has type 'Shape', which is a oneOf [Triangle, Quadrilateral]
        composed schema. We are not able to assign Triangle tor Quadrilateral
        to a shapes item because those instances do not include Shape validation
        Shape could require additional validations that Triangle + Quadrilateral do not include
        """
        from petstore_api.model.triangle import Triangle
        err_msg = (
            r"Incorrect type passed in, required type was <class 'petstore_api.model.shape.Shape'> "
            r"and passed type was <class 'petstore_api.schemas.DynamicSchema'> at "
            r"\('args\[0\]', 'shapes', 0\)")
        with self.assertRaisesRegex(petstore_api.ApiTypeError, err_msg):
            inst = Drawing(mainShape=isosceles_triangle,
                           shapes=[
                               Triangle(shapeType="Triangle",
                                        triangleType="EquilateralTriangle")
                           ])