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
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
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.'
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."
def test_errors_is_empty_if_valid(self): errors = AuthorSchema().validate( make_serialized_author({ 'first_name': 'Dan', 'last_name': 'Gebhardt', 'password': '******' })) assert errors == {}
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/'
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/"
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"]
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']
def test_errors_is_empty_if_valid(self): errors = AuthorSchema().validate( make_serialized_author({ "first_name": "Dan", "last_name": "Gebhardt", "password": "******", })) assert errors == {}
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
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
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
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
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.'
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'
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
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.'
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."
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."
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."
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.'
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
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
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" }, }] }
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' } }] }
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."
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.'
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"
def test_dump_none(self): data = unpack(AuthorSchema().dump(None)) assert "data" in data assert data["data"] is None assert "links" not in data