Пример #1
0
    def test_no_type_raises_error(self):
        author = {'data': {'attributes': {'first_name': 'Dan', 'password': '******'}}}
        with pytest.raises(ValidationError) as excinfo:
            AuthorSchema().load(author)

        expected = {
            'errors': [
                {
                    'detail': '`data` object must include `type` key.',
                    'source': {
                        'pointer': '/data'
                    }
                }
            ]
        }
        assert excinfo.value.messages == expected

        # This assertion is only valid on newer versions of marshmallow, which
        # have this bugfix: https://github.com/marshmallow-code/marshmallow/pull/530
        if _MARSHMALLOW_VERSION_INFO >= (2, 10, 1):
            try:
                errors = AuthorSchema().validate(author)
            except ValidationError as err:  # marshmallow 2
                errors = err.messages
            assert errors == expected
Пример #2
0
    def test_no_type_raises_error(self):
        author = {
            "data": {
                "attributes": {
                    "first_name": "Dan",
                    "password": "******"
                }
            }
        }
        with pytest.raises(ValidationError) as excinfo:
            AuthorSchema().load(author)

        expected = {
            "errors": [{
                "detail": "`data` object must include `type` key.",
                "source": {
                    "pointer": "/data"
                },
            }]
        }
        assert excinfo.value.messages == expected

        # This assertion is only valid on newer versions of marshmallow, which
        # have this bugfix: https://github.com/marshmallow-code/marshmallow/pull/530
        if _MARSHMALLOW_VERSION_INFO >= (2, 10, 1):
            try:
                errors = AuthorSchema().validate(author)
            except ValidationError as err:  # marshmallow 2
                errors = err.messages
            assert errors == expected
Пример #3
0
    def test_validate_id(self):
        """ the pointer for id should be at the data object, not attributes """
        author = {
            'data': {
                'type': 'people',
                'id': 123,
                'attributes': {
                    'first_name': 'Rob',
                    'password': '******'
                }
            }
        }
        try:
            errors = AuthorSchema().validate(author)
        except ValidationError as err:
            errors = err.messages
        assert 'errors' in errors
        assert len(errors['errors']) == 2

        lname_err = get_error_by_field(errors, 'last_name')
        assert lname_err
        assert lname_err['source']['pointer'] == '/data/attributes/last_name'
        assert lname_err['detail'] == 'Missing data for required field.'

        id_err = get_error_by_field(errors, 'id')
        assert id_err
        assert id_err['source']['pointer'] == '/data/id'
        assert id_err['detail'] == 'Not a valid string.'
Пример #4
0
    def test_validate_id(self):
        """ the pointer for id should be at the data object, not attributes """
        author = {
            "data": {
                "type": "people",
                "id": 123,
                "attributes": {
                    "first_name": "Rob",
                    "password": "******"
                },
            }
        }
        try:
            errors = AuthorSchema().validate(author)
        except ValidationError as err:
            errors = err.messages
        assert "errors" in errors
        assert len(errors["errors"]) == 2

        lname_err = get_error_by_field(errors, "last_name")
        assert lname_err
        assert lname_err["source"]["pointer"] == "/data/attributes/last_name"
        assert lname_err["detail"] == "Missing data for required field."

        id_err = get_error_by_field(errors, "id")
        assert id_err
        assert id_err["source"]["pointer"] == "/data/id"
        assert id_err["detail"] == "Not a valid string."
Пример #5
0
 def test_errors_is_empty_if_valid(self):
     errors = AuthorSchema().validate(
         make_serialized_author({
             'first_name': 'Dan',
             'last_name': 'Gebhardt',
             'password': '******'
         }))
     assert errors == {}
Пример #6
0
    def test_dump_empty_list(self):
        data = unpack(AuthorSchema(many=True).dump([]))

        assert 'data' in data
        assert type(data['data']) is list
        assert len(data['data']) == 0
        assert 'links' in data
        assert data['links']['self'] == '/authors/'
Пример #7
0
    def test_dump_empty_list(self):
        data = unpack(AuthorSchema(many=True).dump([]))

        assert "data" in data
        assert type(data["data"]) is list
        assert len(data["data"]) == 0
        assert "links" in data
        assert data["links"]["self"] == "/authors/"
Пример #8
0
    def test_load_bulk_id_fields(self):
        request = {"data": [{"id": "1", "type": "people"}]}

        result = AuthorSchema(only=("id",), many=True).load(request)
        assert type(result) is list

        response = result[0]
        assert response["id"] == request["data"][0]["id"]
Пример #9
0
    def test_load_bulk_id_fields(self):
        request = {'data': [{'id': '1', 'type': 'people'}]}

        result = unpack(AuthorSchema(only=('id', ), many=True).load(request))
        assert type(result) is list

        response = result[0]
        assert response['id'] == request['data'][0]['id']
Пример #10
0
 def test_errors_is_empty_if_valid(self):
     errors = AuthorSchema().validate(
         make_serialized_author({
             "first_name": "Dan",
             "last_name": "Gebhardt",
             "password": "******",
         }))
     assert errors == {}
Пример #11
0
    def test_dump_single(self, author):
        data = unpack(AuthorSchema().dump(author))

        assert 'data' in data
        assert type(data['data']) is dict

        assert data['data']['id'] == str(author.id)
        assert data['data']['type'] == 'people'
        attribs = data['data']['attributes']

        assert attribs['first_name'] == author.first_name
        assert attribs['last_name'] == author.last_name
Пример #12
0
    def test_dump_single(self, author):
        data = unpack(AuthorSchema().dump(author))

        assert "data" in data
        assert type(data["data"]) is dict

        assert data["data"]["id"] == str(author.id)
        assert data["data"]["type"] == "people"
        attribs = data["data"]["attributes"]

        assert attribs["first_name"] == author.first_name
        assert attribs["last_name"] == author.last_name
Пример #13
0
    def test_dump_many(self, authors):
        data = unpack(AuthorSchema(many=True).dump(authors))
        assert "data" in data
        assert type(data["data"]) is list

        first = data["data"][0]
        assert first["id"] == str(authors[0].id)
        assert first["type"] == "people"

        attribs = first["attributes"]

        assert attribs["first_name"] == authors[0].first_name
        assert attribs["last_name"] == authors[0].last_name
Пример #14
0
    def test_dump_many(self, authors):
        data = unpack(AuthorSchema(many=True).dump(authors))
        assert 'data' in data
        assert type(data['data']) is list

        first = data['data'][0]
        assert first['id'] == str(authors[0].id)
        assert first['type'] == 'people'

        attribs = first['attributes']

        assert attribs['first_name'] == authors[0].first_name
        assert attribs['last_name'] == authors[0].last_name
Пример #15
0
    def test_load(self):
        with pytest.raises(ValidationError) as excinfo:
            AuthorSchema().load(make_serialized_author({'first_name': 'Dan', 'password': '******'}))
        errors = excinfo.value.messages
        assert 'errors' in errors
        assert len(errors['errors']) == 2

        password_err = get_error_by_field(errors, 'password')
        assert password_err
        assert password_err['detail'] == 'Shorter than minimum length 6.'

        lname_err = get_error_by_field(errors, 'last_name')
        assert lname_err
        assert lname_err['detail'] == 'Missing data for required field.'
Пример #16
0
    def test_errors_many(self):
        authors = make_serialized_authors([
            {'first_name': 'Dan', 'last_name': 'Gebhardt', 'password': '******'},
            {'first_name': 'Dan', 'last_name': 'Gebhardt', 'password': '******'},
        ])
        try:
            errors = AuthorSchema(many=True).validate(authors)['errors']
        except ValidationError as err:
            errors = err.messages['errors']

        assert len(errors) == 1

        err = errors[0]
        assert 'source' in err
        assert err['source']['pointer'] == '/data/0/attributes/password'
Пример #17
0
    def test_no_type_raises_error(self):
        author = {
            "data": {
                "attributes": {
                    "first_name": "Dan",
                    "password": "******"
                }
            }
        }
        with pytest.raises(ValidationError) as excinfo:
            AuthorSchema().load(author)

        expected = {
            "errors": [{
                "detail": "`data` object must include `type` key.",
                "source": {
                    "pointer": "/data"
                },
            }]
        }
        assert excinfo.value.messages == expected

        errors = AuthorSchema().validate(author)
        assert errors == expected
Пример #18
0
    def test_validate(self):
        author = make_serialized_author({'first_name': 'Dan', 'password': '******'})
        try:
            errors = AuthorSchema().validate(author)
        except ValidationError as err:  # marshmallow 2
            errors = err.messages
        assert 'errors' in errors
        assert len(errors['errors']) == 2

        password_err = get_error_by_field(errors, 'password')
        assert password_err
        assert password_err['detail'] == 'Shorter than minimum length 6.'

        lname_err = get_error_by_field(errors, 'last_name')
        assert lname_err
        assert lname_err['detail'] == 'Missing data for required field.'
Пример #19
0
    def test_validate(self):
        author = make_serialized_author({
            "first_name": "Dan",
            "password": "******"
        })
        errors = AuthorSchema().validate(author)
        assert "errors" in errors
        assert len(errors["errors"]) == 2

        password_err = get_error_by_field(errors, "password")
        assert password_err
        assert password_err["detail"] == "Shorter than minimum length 6."

        lname_err = get_error_by_field(errors, "last_name")
        assert lname_err
        assert lname_err["detail"] == "Missing data for required field."
Пример #20
0
    def test_errors_many_not_list(self):
        authors = make_serialized_author({
            "first_name": "Dan",
            "last_name": "Gebhardt",
            "password": "******"
        })
        try:
            errors = AuthorSchema(many=True).validate(authors)["errors"]
        except ValidationError as err:
            errors = err.messages["errors"]

        assert len(errors) == 1

        err = errors[0]
        assert "source" in err
        assert err["source"]["pointer"] == "/data"
        assert err["detail"] == "`data` expected to be a collection."
Пример #21
0
    def test_errors_in_strict_mode(self):
        author = make_serialized_author({
            "first_name": "Dan",
            "password": "******"
        })
        with pytest.raises(ValidationError) as excinfo:
            AuthorSchema().load(author)
        errors = excinfo.value.messages
        assert "errors" in errors
        assert len(errors["errors"]) == 2
        password_err = get_error_by_field(errors, "password")
        assert password_err
        assert password_err["detail"] == "Shorter than minimum length 6."

        lname_err = get_error_by_field(errors, "last_name")
        assert lname_err
        assert lname_err["detail"] == "Missing data for required field."
Пример #22
0
    def test_errors_many_not_list(self):
        authors = make_serialized_author(
            {
                'first_name': 'Dan',
                'last_name': 'Gebhardt',
                'password': '******'
            }, )
        try:
            errors = AuthorSchema(many=True).validate(authors)['errors']
        except ValidationError as err:
            errors = err.messages['errors']

        assert len(errors) == 1

        err = errors[0]
        assert 'source' in err
        assert err['source']['pointer'] == '/data'
        assert err['detail'] == '`data` expected to be a collection.'
Пример #23
0
    def test_validate_no_data_raises_error(self):
        author = {'meta': {'this': 'that'}}

        with pytest.raises(ValidationError) as excinfo:
            AuthorSchema().load(author)

        errors = excinfo.value.messages

        expected = {
            'errors': [{
                'detail': 'Object must include `data` key.',
                'source': {
                    'pointer': '/'
                }
            }]
        }

        assert errors == expected
Пример #24
0
    def test_validate_no_data_raises_error(self):
        author = {"meta": {"this": "that"}}

        with pytest.raises(ValidationError) as excinfo:
            AuthorSchema().load(author)

        errors = excinfo.value.messages

        expected = {
            "errors": [{
                "detail": "Object must include `data` key.",
                "source": {
                    "pointer": "/"
                },
            }]
        }

        assert errors == expected
Пример #25
0
 def test_validate_type(self):
     author = {
         "data": {
             "type": "invalid",
             "attributes": {
                 "first_name": "Dan",
                 "password": "******"
             },
         }
     }
     with pytest.raises(IncorrectTypeError) as excinfo:
         AuthorSchema().validate(author)
     assert excinfo.value.args[0] == 'Invalid type. Expected "people".'
     assert excinfo.value.messages == {
         "errors": [{
             "detail": 'Invalid type. Expected "people".',
             "source": {
                 "pointer": "/data/type"
             },
         }]
     }
Пример #26
0
 def test_validate_type(self):
     author = {
         'data': {
             'type': 'invalid',
             'attributes': {
                 'first_name': 'Dan',
                 'password': '******'
             }
         }
     }
     with pytest.raises(IncorrectTypeError) as excinfo:
         AuthorSchema().validate(author)
     assert excinfo.value.args[0] == 'Invalid type. Expected "people".'
     assert excinfo.value.messages == {
         'errors': [{
             'detail': 'Invalid type. Expected "people".',
             'source': {
                 'pointer': '/data/type'
             }
         }]
     }
Пример #27
0
    def test_many_id_errors(self):
        """ the pointer for id should be at the data object, not attributes """
        author = {
            "data": [
                {
                    "type": "people",
                    "id": "invalid",
                    "attributes": {
                        "first_name": "Rob",
                        "password": "******"
                    },
                },
                {
                    "type": "people",
                    "id": 37,
                    "attributes": {
                        "first_name": "Dan",
                        "last_name": "Gebhardt",
                        "password": "******",
                    },
                },
            ]
        }
        try:
            errors = AuthorSchema(many=True).validate(author)
        except ValidationError as err:  # marshmallow 2
            errors = err.messages
        assert "errors" in errors
        assert len(errors["errors"]) == 2

        lname_err = get_error_by_field(errors, "last_name")
        assert lname_err
        assert lname_err["source"]["pointer"] == "/data/0/attributes/last_name"
        assert lname_err["detail"] == "Missing data for required field."

        id_err = get_error_by_field(errors, "id")
        assert id_err
        assert id_err["source"]["pointer"] == "/data/1/id"
        assert id_err["detail"] == "Not a valid string."
Пример #28
0
    def test_many_id_errors(self):
        """ the pointer for id should be at the data object, not attributes """
        author = {
            'data': [
                {
                    'type': 'people',
                    'id': 'invalid',
                    'attributes': {
                        'first_name': 'Rob',
                        'password': '******'
                    },
                },
                {
                    'type': 'people',
                    'id': 37,
                    'attributes': {
                        'first_name': 'Dan',
                        'last_name': 'Gebhardt',
                        'password': '******',
                    },
                },
            ]
        }
        try:
            errors = AuthorSchema(many=True).validate(author)
        except ValidationError as err:  # marshmallow 2
            errors = err.messages
        assert 'errors' in errors
        assert len(errors['errors']) == 2

        lname_err = get_error_by_field(errors, 'last_name')
        assert lname_err
        assert lname_err['source']['pointer'] == '/data/0/attributes/last_name'
        assert lname_err['detail'] == 'Missing data for required field.'

        id_err = get_error_by_field(errors, 'id')
        assert id_err
        assert id_err['source']['pointer'] == '/data/1/id'
        assert id_err['detail'] == 'Not a valid string.'
Пример #29
0
    def test_errors_many(self):
        authors = make_serialized_authors([
            {
                "first_name": "Dan",
                "last_name": "Gebhardt",
                "password": "******"
            },
            {
                "first_name": "Dan",
                "last_name": "Gebhardt",
                "password": "******",
            },
        ])
        try:
            errors = AuthorSchema(many=True).validate(authors)["errors"]
        except ValidationError as err:
            errors = err.messages["errors"]

        assert len(errors) == 1

        err = errors[0]
        assert "source" in err
        assert err["source"]["pointer"] == "/data/0/attributes/password"
Пример #30
0
    def test_dump_none(self):
        data = unpack(AuthorSchema().dump(None))

        assert "data" in data
        assert data["data"] is None
        assert "links" not in data