Exemple #1
0
    def test_resolve_schema_dict_auto_reference(self, schema):
        def resolver(schema):
            return schema.__name__

        spec = APISpec(
            title="Test auto-reference",
            version="0.1",
            openapi_version="2.0",
            plugins=(SerpycoPlugin(
                schema_name_resolver=schema_name_resolver), ),
        )
        assert {} == get_definitions(spec)

        spec.components.schema("analysis", schema=schema)
        spec.path(
            "/test",
            operations={
                "get": {
                    "responses": {
                        "200": {
                            "schema": {
                                "$ref": "#/definitions/analysis"
                            }
                        }
                    }
                }
            },
        )
        definitions = get_definitions(spec)
        assert 3 == len(definitions)

        assert "analysis" in definitions
        assert "SampleSchema" in definitions
        assert "RunSchema_exclude_sample" in definitions
Exemple #2
0
    def test_can_use_custom_field_decorator(self, spec_fixture):
        @dataclass
        class CustomPetASchema(PetSchema):
            email: str = serpyco.string_field(
                format_=serpyco.StringFormat.EMAIL,
                pattern="^[A-Z]",
                min_length=3,
                max_length=24,
            )

        @dataclass
        class CustomPetBSchema(PetSchema):
            age: int = serpyco.number_field(minimum=1, maximum=120)

        @dataclass
        class WithStringField(object):
            """String field test class"""

            foo: str = serpyco.string_field(
                format_=serpyco.StringFormat.EMAIL,
                pattern="^[A-Z]",
                min_length=3,
                max_length=24,
            )

        serializer = serpyco.Serializer(WithStringField)
        serializer.json_schema()

        spec_fixture.spec.components.schema("Pet", schema=PetSchema)
        spec_fixture.spec.components.schema("CustomPetA",
                                            schema=CustomPetASchema)
        spec_fixture.spec.components.schema("CustomPetB",
                                            schema=CustomPetBSchema)

        props_0 = get_definitions(spec_fixture.spec)["Pet"]["properties"]
        props_a = get_definitions(
            spec_fixture.spec)["CustomPetA"]["properties"]
        props_b = get_definitions(
            spec_fixture.spec)["CustomPetB"]["properties"]

        assert props_0["name"]["type"] == "string"
        assert "format" not in props_0["name"]

        assert props_a["email"]["type"] == "string"
        assert json.dumps(props_a["email"]["format"]) == '"email"'
        assert props_a["email"]["pattern"] == "^[A-Z]"
        assert props_a["email"]["maxLength"] == 24
        assert props_a["email"]["minLength"] == 3

        assert props_b["age"]["type"] == "integer"
        assert props_b["age"]["minimum"] == 1
        assert props_b["age"]["maximum"] == 120
Exemple #3
0
    def test_can_use_schema_as_definition(self, spec, schema):
        spec.components.schema("Pet", schema=schema)
        definitions = get_definitions(spec)
        props = definitions["Pet"]["properties"]

        assert props["id"]["type"] == "integer"
        assert props["name"]["type"] == "string"
Exemple #4
0
 def test_circular_referencing_schemas(self, spec):
     spec.components.schema("Analysis", schema=AnalysisSchema)
     spec.components.schema("Sample", schema=SampleSchema)
     spec.components.schema("Run", schema=RunSchema)
     definitions = get_definitions(spec)
     ref = definitions["Analysis"]["properties"]["sample"]["$ref"]
     assert ref == ref_path(spec) + "tests.test_ext_serpyco.SampleSchema"
Exemple #5
0
 def test_schema_helper_without_schema(self, spec):
     spec.components.schema("Pet",
                            {"properties": {
                                "key": {
                                    "type": "integer"
                                }
                            }})
     definitions = get_definitions(spec)
     assert definitions["Pet"]["properties"] == {"key": {"type": "integer"}}
Exemple #6
0
 def test_self_referencing_field_many(self, spec):
     spec.components.schema("SelfReference", schema=SelfReferencingSchema)
     definitions = get_definitions(spec)
     result = definitions["SelfReference"]["properties"]["many"]
     assert result == {
         "type": "array",
         "items": {
             "$ref": ref_path(spec) + "SelfReference"
         },
     }
Exemple #7
0
    def test_schema_with_optional_string(self, spec):
        @dataclasses.dataclass
        class MySchema:
            id: int
            name: typing.Optional[str]

        spec.components.schema("MySchema", schema=MySchema)
        definitions = get_definitions(spec)
        props = definitions["MySchema"]["properties"]

        assert "required" in definitions["MySchema"]
        assert ['id'] == definitions["MySchema"]["required"]
        assert {'id': {'type': 'integer'}, 'name': {'type': 'string'}} == props
Exemple #8
0
    def test_schema_with_optional_string(self, spec):
        @dataclasses.dataclass
        class MySchema:
            id: int
            name: typing.Optional[str] = None

        spec.components.schema("MySchema", schema=MySchema)
        definitions = get_definitions(spec)
        props = definitions["MySchema"]["properties"]

        assert "required" in definitions["MySchema"]
        assert ["id"] == definitions["MySchema"]["required"]
        assert {"id": {"type": "integer"}, "name": {"type": "string"}} == props
Exemple #9
0
    def test_schema_with_optional_string_in_related_schema(self, spec):
        @dataclasses.dataclass
        class MyChildSchema:
            id: int
            name: typing.Optional[str]

        @dataclasses.dataclass
        class MyParentSchema:
            id: int
            child: MyChildSchema

        spec.components.schema("MyParentSchema", schema=MyParentSchema)
        definitions = get_definitions(spec)
        props = definitions['tests.test_ext_serpyco.MyChildSchema'][
            "properties"]
        definition = definitions['tests.test_ext_serpyco.MyChildSchema']
        assert "required" in definition
        assert ['id'] == definition["required"]
        assert {'id': {'type': 'integer'}, 'name': {'type': 'string'}} == props
Exemple #10
0
    def test_schema_with_optional_string_in_related_schema(self, spec):
        @dataclasses.dataclass
        class MyChildSchema:
            id: int
            name: typing.Optional[str] = None

        @dataclasses.dataclass
        class MyParentSchema:
            id: int
            child: MyChildSchema

        spec.components.schema("MyParentSchema", schema=MyParentSchema)
        definitions = get_definitions(spec)
        props = definitions["tests.test_ext_serpyco.MyChildSchema"][
            "properties"]
        definition = definitions["tests.test_ext_serpyco.MyChildSchema"]
        assert "required" in definition
        assert ["id"] == definition["required"]
        assert {"id": {"type": "integer"}, "name": {"type": "string"}} == props
Exemple #11
0
    def test_unit__reference_with_exclude__ok__nominal_case(self, spec):
        spec.components.schema("assembly", schema=Assembly)
        definitions = get_definitions(spec)

        pass
Exemple #12
0
 def test_schema_with_default_values(self, spec):
     spec.components.schema("DefaultValuesSchema",
                            schema=DefaultValuesSchema)
     definitions = get_definitions(spec)
     props = definitions["DefaultValuesSchema"]["properties"]
     assert props["number_auto_default"]["default"] == 12
Exemple #13
0
 def test_self_referencing_field_single(self, spec):
     spec.components.schema("SelfReference", schema=SelfReferencingSchema)
     definitions = get_definitions(spec)
     ref = definitions["SelfReference"]["properties"]["single"]["$ref"]
     assert ref == ref_path(spec) + "SelfReference"