Ejemplo n.º 1
0
def test_nested_required_field():

    output_schema = {
        HTTPStatus.OK: {
            "basic_string":
            prop.String(),
            "basic_boolean":
            prop.Boolean(),
            "object":
            prop.Object(
                structure={
                    "sub_string": prop.String("the string", required=False),
                    "required_string": prop.String(required=True)
                })
        }
    }

    result = HTTPStatus.OK, {
        "basic_string": "test string",
        "basic_boolean": True,
        "object": {
            "sub_string": "hello",
        },
    }

    with pytest.raises(prop.ValidationError) as e:
        create_output(result, output_schema)

    assert e.value.message == "The field 'required_string' is required but not found in the body!"
Ejemplo n.º 2
0
def test_create_output_identical_dict():

    output_schema = {
        HTTPStatus.OK: {
            "basic_string": prop.String(),
            "basic_boolean": prop.Boolean(),
            "sub_dict": prop.Object(structure={"sub_string": prop.String()})
        }
    }

    result = HTTPStatus.OK, {
        "basic_string": "test string",
        "basic_boolean": True,
        "sub_dict": {
            "sub_string": "hello",
        }
    }

    http_status_code, returned_dict = create_output(result, output_schema)
    assert http_status_code == 200

    assert returned_dict == {
        "basic_string": "test string",
        "basic_boolean": True,
        "sub_dict": {
            "sub_string": "hello"
        }
    }
Ejemplo n.º 3
0
def test_triple_nested_dicts():

    output_schema = {
        HTTPStatus.OK: {
            "basic_string":
            prop.String(),
            "basic_boolean":
            prop.Boolean(),
            "sub_dict":
            prop.Object(structure={
                "sub_string":
                prop.String("the string"),
                "sub_sub_dict":
                prop.Object(structure={"boo": prop.Boolean()})
            },
                        description="A list of plans.")
        }
    }

    result = HTTPStatus.OK, {
        "basic_string": "test string",
        "basic_boolean": True,
        "sub_dict": {
            "sub_string": "hello",
            "not_this": "Not this",
            "sub_sub_dict": {
                "boo": False,
                "why_is_this_here": "ignore"
            }
        }
    }

    http_status_code, returned_dict = create_output(result, output_schema)
    assert http_status_code == 200

    assert returned_dict == {
        "basic_string": "test string",
        "basic_boolean": True,
        "sub_dict": {
            "sub_string": "hello",
            "sub_sub_dict": {
                "boo": False
            }
        }
    }
Ejemplo n.º 4
0
Archivo: schema.py Proyecto: pr/dos
    def __init__(self, validated_fields=None):
        if validated_fields is not None:
            fields = {}

            for field in validated_fields:
                fields[field] = prop.String(
                    f"A error message specific to the {field} field.")

            self.base_schema["field_error_messages"] = prop.Object(
                structure=fields, required=False, nullable=True)

        super().__init__(self.base_schema)
Ejemplo n.º 5
0
class NestedFields(Fields):
    base_schema = {
        "top_level_id":
        prop.Integer(description="The top level ID.", ),
        "top_level_array":
        prop.Array(repeated_structure=prop.Object(
            structure={
                "nested_id":
                prop.Integer("The nested ID."),
                "nested_array":
                prop.Array(repeated_structure=prop.Object(
                    structure={
                        "super_nested_id":
                        prop.Integer(description="The super nested ID."),
                        "another_number":
                        prop.Number(description="The super nested number.", ),
                    }), ),
            })),
    }

    def __init__(self):
        super().__init__(self.base_schema)
Ejemplo n.º 6
0
def test_nested_specializer_object_with_results_structure_and_specialize():

    output_schema = {
        HTTPStatus.OK: {
            "results":
            prop.Array(repeated_structure=prop.Object(
                structure=NestedFields().specialize(only=["top_level_id"]),
                description="A basic field object"))
        }
    }

    result = HTTPStatus.OK, {
        "results": [
            {
                "top_level_id": 31,
                "don_t_return_me": "super secret data"
            },
            {
                "top_level_id": 32
            },
            {
                "top_level_id": 33
            },
            {
                "top_level_id": 34,
                "don_t_return_me": "even more secret data"
            },
        ]
    }

    http_status_code, returned_dict = create_output(result, output_schema)
    assert http_status_code == 200
    assert returned_dict == {
        "results": [
            {
                "top_level_id": 31
            },
            {
                "top_level_id": 32
            },
            {
                "top_level_id": 33
            },
            {
                "top_level_id": 34
            },
        ]
    }
Ejemplo n.º 7
0
def test_create_output_array_of_objects():

    output_schema = {
        HTTPStatus.OK: {
            "basic_string":
            prop.String(),
            "basic_boolean":
            prop.Boolean(),
            "array_of_objects":
            prop.Array(repeated_structure=prop.Object(
                structure={"sub_string": prop.String("the string")}),
                       description="A list of plans.")
        }
    }

    result = HTTPStatus.OK, {
        "basic_string":
        "test string",
        "basic_boolean":
        True,
        "array_of_objects": [{
            "sub_string": "hello",
            "not_this": "not this"
        }, {
            "sub_string": "hello",
            "not_this": "still not this",
            "another_one": "???"
        }]
    }

    http_status_code, returned_dict = create_output(result, output_schema)
    assert http_status_code == 200

    assert returned_dict == {
        "basic_string": "test string",
        "basic_boolean": True,
        "array_of_objects": [{
            "sub_string": "hello",
        }, {
            "sub_string": "hello",
        }]
    }
Ejemplo n.º 8
0
def test_very_nested_nullable_field():

    output_schema = {
        HTTPStatus.OK: {
            "basic_string":
            prop.String(),
            "basic_boolean":
            prop.Boolean(),
            "array_of_objects":
            prop.Array(repeated_structure=prop.Object(
                structure={
                    "sub_string":
                    prop.String("the string", required=True, nullable=False),
                    "required_one":
                    prop.String(required=True, nullable=False)
                }),
                       description="A list of plans.")
        }
    }

    result = HTTPStatus.OK, {
        "basic_string":
        "test string",
        "basic_boolean":
        True,
        "array_of_objects": [{
            "sub_string": None,
            "required_one": "here"
        }, {
            "sub_string": "hello",
            "required_one": "here_too"
        }]
    }

    with pytest.raises(prop.ValidationError) as e:
        create_output(result, output_schema)

    assert e.value.message == "Non nullable field 'sub_string' is null!"
Ejemplo n.º 9
0
def test_validate_input_exact_length_object():

    input_schema = {
        "basic_object":
        prop.Object(structure={"basic_string": prop.String()},
                    validators=[validators.ExactLength(8)]),
    }

    given_request = {
        "basic_object": {
            "basic_string": "this is irrelevant",
        }
    }

    http_status, reject_dict = validate_input(given_request, input_schema)
    assert reject_dict == {
        'message': 'A field has an error.',
        'field_error_messages': {
            'basic_object': 'ExactLength is not supported for class Object!!'
        }
    }

    assert http_status == HTTPStatus.BAD_REQUEST
Ejemplo n.º 10
0
def test_nested_specializer_object_with_results_structure():

    output_schema = {
        HTTPStatus.OK: {
            "results":
            prop.Array(repeated_structure=prop.Object(
                structure=SimpleFields().all(),
                description="A basic field object"))
        }
    }

    result = HTTPStatus.OK, {
        "results": [{
            "message": "hello"
        }, {
            "message": "hello"
        }, {
            "message": "hello"
        }, {
            "message": "hello"
        }]
    }

    http_status_code, returned_dict = create_output(result, output_schema)
    assert http_status_code == 200
    assert returned_dict == {
        "results": [{
            "message": "hello"
        }, {
            "message": "hello"
        }, {
            "message": "hello"
        }, {
            "message": "hello"
        }]
    }
Ejemplo n.º 11
0
def test_create_output_different_dict():

    output_schema = {
        HTTPStatus.OK: {
            "basic_string":
            prop.String(),
            "basic_boolean":
            prop.Boolean(),
            "sub_dict":
            prop.Object(
                structure={"sub_string": prop.String()},
                description="This is a fake object nested in a dictionary",
                required=True,
                nullable=False)
        }
    }

    result = HTTPStatus.OK, {
        "basic_string": "test string",
        "basic_boolean": True,
        "sub_dict": {
            "sub_string": "hello",
            "not_this": "Not this"
        }
    }

    http_status_code, returned_dict = create_output(result, output_schema)
    assert http_status_code == 200

    assert returned_dict == {
        "basic_string": "test string",
        "basic_boolean": True,
        "sub_dict": {
            "sub_string": "hello"
        }
    }