Ejemplo n.º 1
0
 def testTriangle(self):
     """Test Triangle"""
     tri = Triangle(shape_type="Triangle",
                    triangle_type="EquilateralTriangle")
     assert isinstance(tri, equilateral_triangle.EquilateralTriangle)
     tri = Triangle(shape_type="Triangle",
                    triangle_type="IsoscelesTriangle")
     assert isinstance(tri, isosceles_triangle.IsoscelesTriangle)
     tri = Triangle(shape_type="Triangle", triangle_type="ScaleneTriangle")
     assert isinstance(tri, scalene_triangle.ScaleneTriangle)
Ejemplo n.º 2
0
    def testDeepCopyAllOf(self):
        """test deepcopy"""
        obj = Triangle(shape_type="Triangle",
                       triangle_type="EquilateralTriangle",
                       foo="blah")
        assert id(deepcopy(obj)) != id(obj)
        assert deepcopy(obj) == obj

        obj = Triangle._new_from_openapi_data(
            shape_type="Triangle",
            triangle_type="EquilateralTriangle",
            foo="blah")
        assert id(deepcopy(obj)) != id(obj)
        assert deepcopy(obj) == obj
Ejemplo n.º 3
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,
            )
Ejemplo n.º 4
0
 def testTriangle(self):
     """Test Triangle"""
     tri_classes = [EquilateralTriangle, IsoscelesTriangle, ScaleneTriangle]
     for tri_class in tri_classes:
         tri = Triangle(shapeType="Triangle",
                        triangleType=tri_class.__name__)
         assert isinstance(tri, tri_class)
         assert isinstance(tri, Triangle)
         assert isinstance(tri, TriangleInterface)
         assert isinstance(tri, frozendict)
Ejemplo n.º 5
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")
                           ])