Exemple #1
0
 def test_deserialize_non_many_list_relationship(self):
     field = Relationship(many=False,
                          include_resource_linkage=True,
                          type_='comments')
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({'data': ['1']})
     assert excinfo.value.args[0] == 'Relationship is not list-like'
Exemple #2
0
 def test_include_resource_linkage_single_foreign_key(self, post):
     field = Relationship(related_url='/posts/{post_id}/author/',
                          related_url_kwargs={'post_id': '<id>'},
                          include_resource_linkage=True,
                          type_='people')
     result = field.serialize('author_id', post)
     assert result['data']['id'] == str(post.author_id)
 def test_deserialize_many_non_list_relationship(self):
     field = Relationship(many=True,
                          include_resource_linkage=True,
                          type_="comments")
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({"data": "1"})
     assert excinfo.value.args[0] == "Relationship is list-like"
 def test_serialize_relationship_link(self, post):
     field = Relationship("http://example.com/posts/{id}/comments",
                          related_url_kwargs={"id": "<id>"})
     result = field.serialize("comments", post)
     assert field.serialize("comments", post)
     related = result["links"]["related"]
     assert related == f"http://example.com/posts/{post.id}/comments"
 def test_include_resource_linkage_single_foreign_key(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/author/',
         related_url_kwargs={'post_id': '<id>'},
         include_resource_linkage=True, type_='people'
     )
     result = field.serialize('author_id', post)
     assert result['data']['id'] == str(post.author_id)
 def test_deserialize_empty_data_list(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=True, include_resource_linkage=False, type_='comments'
     )
     result = field.deserialize({'data': []})
     assert result == []
 def test_deserialize_null_data_value(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'}, allow_none=True,
         many=False, include_resource_linkage=False, type_='comments'
     )
     result = field.deserialize({'data': None})
     assert result is None
 def test_deserialize_null_data_value(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'}, allow_none=True,
         many=False, include_data=False, type_='comments'
     )
     result = field.deserialize({'data': None})
     assert result is None
Exemple #9
0
 def test_deserialize_empty_data_list(self):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=True,
                          include_resource_linkage=False,
                          type_='comments')
     result = field.deserialize({'data': []})
     assert result == []
Exemple #10
0
 def test_include_null_data_single(self, post_with_null_author):
     field = Relationship(related_url='posts/{post_id}/author',
                          related_url_kwargs={'post_id': '<id>'},
                          include_data=True,
                          type_='people')
     result = field.serialize('author', post_with_null_author)
     assert result['author'] and result['author']['links']['related']
     assert result['author']['data'] == None
Exemple #11
0
 def test_serialize_relationship_link(self, post):
     field = Relationship('http://example.com/posts/{id}/comments',
                          related_url_kwargs={'id': '<id>'})
     result = field.serialize('comments', post)
     assert field.serialize('comments', post)
     related = result['comments']['links']['related']
     assert related == 'http://example.com/posts/{id}/comments'.format(
         id=post.id)
 def test_include_resource_linkage_id_field_from_string(self):
     field = Relationship(
         include_resource_linkage=True, type_="authors", id_field="name"
     )
     result = field.serialize("author", {"author": {"name": "Ray Bradbury"}})
     assert "data" in result
     assert result["data"]
     assert result["data"]["id"] == "Ray Bradbury"
 def test_deserialize_empty_relationship_node(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=False, include_resource_linkage=False, type_='comments'
     )
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({})
     assert excinfo.value.args[0] == 'Must include a `data` key'
 def test_deserialize_null_value_disallow_none(self):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'}, allow_none=False,
         many=False, include_resource_linkage=False, type_='comments'
     )
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({'data': None})
     assert excinfo.value.args[0] == 'Field may not be null.'
 def test_exclude_data(self, post_with_null_comment):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=True, include_resource_linkage=False, type_='comments'
     )
     result = field.serialize('comments', post_with_null_comment)
     assert result and result['links']['related']
     assert 'data' not in result
 def test_include_null_data_single(self, post_with_null_author):
     field = Relationship(
         related_url='posts/{post_id}/author',
         related_url_kwargs={'post_id': '<id>'},
         include_resource_linkage=True, type_='people'
     )
     result = field.serialize('author', post_with_null_author)
     assert result and result['links']['related']
     assert result['data'] is None
 def test_serialize_self_link(self, post):
     field = Relationship(
         self_url='http://example.com/posts/{id}/relationships/comments',
         self_url_kwargs={'id': '<id>'}
     )
     result = field.serialize('comments', post)
     related = result['links']['self']
     assert 'related' not in result['links']
     assert related == 'http://example.com/posts/{id}/relationships/comments'.format(id=post.id)
 def test_include_resource_linkage_single_foreign_key(self, post):
     field = Relationship(
         related_url="/posts/{post_id}/author/",
         related_url_kwargs={"post_id": "<id>"},
         include_resource_linkage=True,
         type_="people",
     )
     result = field.serialize("author_id", post)
     assert result["data"]["id"] == str(post.author_id)
Exemple #19
0
 def test_exclude_data(self, post_with_null_comment):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=True,
                          include_data=False,
                          type_='comments')
     result = field.serialize('comments', post_with_null_comment)
     assert result['comments'] and result['comments']['links']['related']
     assert 'data' not in result['comments']
 def test_deserialize_data_many(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=True, include_resource_linkage=True, type_='comments'
     )
     value = {'data': [{'type': 'comments', 'id': '1'}]}
     result = field.deserialize(value)
     assert result == ['1']
 def test_deserialize_data_single(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=False, include_data=True, type_='comments'
     )
     value = {'data': {'type': 'comments', 'id': '1'}}
     result = field.deserialize(value)
     assert result == '1'
Exemple #22
0
 def test_serialize_self_link(self, post):
     field = Relationship(
         self_url='http://example.com/posts/{id}/relationships/comments',
         self_url_kwargs={'id': '<id>'})
     result = field.serialize('comments', post)
     related = result['links']['self']
     assert 'related' not in result['links']
     assert related == 'http://example.com/posts/{id}/relationships/comments'.format(
         id=post.id)
Exemple #23
0
 def test_deserialize_empty_relationship_node(self):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=False,
                          include_resource_linkage=False,
                          type_='comments')
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({})
     assert excinfo.value.args[0] == 'Must include a `data` key'
 def test_deserialize_null_value_disallow_none(self):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'}, allow_none=False,
         many=False, include_data=False, type_='comments'
     )
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({'data': None})
     assert excinfo.value.args[0] == 'Field may not be null.'
Exemple #25
0
 def test_deserialize_data_many(self):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=True,
                          include_resource_linkage=True,
                          type_='comments')
     value = {'data': [{'type': 'comments', 'id': '1'}]}
     result = field.deserialize(value)
     assert result == ['1']
Exemple #26
0
 def test_include_null_data_many(self, post_with_null_comment):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=True,
                          include_resource_linkage=True,
                          type_='comments')
     result = field.serialize('comments', post_with_null_comment)
     assert result and result['links']['related']
     assert result['data'] == []
 def test_serialize_relationship_link(self, post):
     field = Relationship(
         'http://example.com/posts/{id}/comments',
         related_url_kwargs={'id': '<id>'}
     )
     result = field.serialize('comments', post)
     assert field.serialize('comments', post)
     related = result['comments']['links']['related']
     assert related == 'http://example.com/posts/{id}/comments'.format(id=post.id)
Exemple #28
0
    def test_empty_relationship_with_alternative_identifier_field(
            self, post_with_null_author):
        field = Relationship(
            related_url='/authors/{author_id}',
            related_url_kwargs={'author_id': '<author.last_name>'},
            default=None)
        result = field.serialize('author', post_with_null_author)

        assert not result
 def test_include_null_data_single(self, post_with_null_author):
     field = Relationship(
         related_url="posts/{post_id}/author",
         related_url_kwargs={"post_id": "<id>"},
         include_resource_linkage=True,
         type_="people",
     )
     result = field.serialize("author", post_with_null_author)
     assert result and result["links"]["related"]
     assert result["data"] is None
    def test_empty_relationship_with_alternative_identifier_field(
            self, post_with_null_author):
        field = Relationship(
            related_url="/authors/{author_id}",
            related_url_kwargs={"author_id": "<author.last_name>"},
            default=None,
        )
        result = field.serialize("author", post_with_null_author)

        assert not result
Exemple #31
0
 def test_deserialize_data_incorrect_type(self):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=False,
                          include_resource_linkage=True,
                          type_='comments')
     with pytest.raises(ValidationError) as excinfo:
         value = {'data': {'type': 'posts', 'id': '1'}}
         field.deserialize(value)
     assert excinfo.value.args[0] == ['Invalid `type` specified']
 def test_include_resource_linkage_many(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=True, include_resource_linkage=True, type_='comments'
     )
     result = field.serialize('comments', post)
     assert 'data' in result
     ids = [each['id'] for each in result['data']]
     assert ids == [str(each.id) for each in post.comments]
Exemple #33
0
 def test_deserialize_data_missing_id(self):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=False,
                          include_resource_linkage=True,
                          type_='comments')
     with pytest.raises(ValidationError) as excinfo:
         value = {'data': {'type': 'comments'}}
         field.deserialize(value)
     assert excinfo.value.args[0] == ['Must have an `id` field']
Exemple #34
0
 def test_include_resource_linkage_many(self, post):
     field = Relationship(related_url='/posts/{post_id}/comments',
                          related_url_kwargs={'post_id': '<id>'},
                          many=True,
                          include_resource_linkage=True,
                          type_='comments')
     result = field.serialize('comments', post)
     assert 'data' in result
     ids = [each['id'] for each in result['data']]
     assert ids == [str(each.id) for each in post.comments]
 def test_deserialize_empty_data_node(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=False, include_data=False, type_='comments'
     )
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({'data': {}})
     assert excinfo.value.args[0] == [
         'Must have an `id` field', 'Must have a `type` field']
 def test_deserialize_data_incorrect_type(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=False, include_resource_linkage=True, type_='comments'
     )
     with pytest.raises(ValidationError) as excinfo:
         value = {'data': {'type': 'posts', 'id': '1'}}
         field.deserialize(value)
     assert excinfo.value.args[0] == ['Invalid `type` specified']
Exemple #37
0
 def test_include_resource_linkage_many_with_schema_overriding_get_attribute(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/keywords',
         related_url_kwargs={'post_id': '<id>'},
         many=True, include_resource_linkage=True, type_='keywords', schema='KeywordSchema'
     )
     result = field.serialize('keywords', post)
     assert 'data' in result
     ids = [each['id'] for each in result['data']]
     assert ids == [md5(each.keyword.encode('utf-8')).hexdigest() for each in post.keywords]
 def test_deserialize_data_missing_id(self, post):
     field = Relationship(
         related_url='/posts/{post_id}/comments',
         related_url_kwargs={'post_id': '<id>'},
         many=False, include_resource_linkage=True, type_='comments'
     )
     with pytest.raises(ValidationError) as excinfo:
         value = {'data': {'type': 'comments'}}
         field.deserialize(value)
     assert excinfo.value.args[0] == ['Must have an `id` field']
Exemple #39
0
 def test_deserialize_missing(self):
     field = Relationship(
         related_url="/posts/{post_id}/comments",
         related_url_kwargs={"post_id": "<id>"},
         many=False,
         include_resource_linkage=True,
         type_="comments",
     )
     result = field.deserialize(missing_)
     assert result is missing_
 def test_serialize_self_link(self, post):
     field = Relationship(
         self_url="http://example.com/posts/{id}/relationships/comments",
         self_url_kwargs={"id": "<id>"},
     )
     result = field.serialize("comments", post)
     related = result["links"]["self"]
     assert "related" not in result["links"]
     assert related == "http://example.com/posts/{id}/relationships/comments".format(
         id=post.id)
Exemple #41
0
    def test_include_data_single(self, post):
        field = Relationship(related_url='/posts/{post_id}/author/',
                             related_url_kwargs={'post_id': '<id>'},
                             include_data=True,
                             type_='people')
        result = field.serialize('author', post)
        assert 'data' in result['author']
        assert result['author']['data']

        assert result['author']['data']['id'] == post.author.id
 def test_deserialize_empty_data_list(self):
     field = Relationship(
         related_url="/posts/{post_id}/comments",
         related_url_kwargs={"post_id": "<id>"},
         many=True,
         include_resource_linkage=False,
         type_="comments",
     )
     result = field.deserialize({"data": []})
     assert result == []
Exemple #43
0
 def test_include_resource_linkage_single_with_schema(self, post):
     field = Relationship(related_url='/posts/{post_id}/author/',
                          related_url_kwargs={'post_id': '<id>'},
                          include_resource_linkage=True,
                          type_='people',
                          schema='PostSchema')
     result = field.serialize('author', post)
     assert 'data' in result
     assert result['data']
     assert result['data']['id'] == str(post.author.id)
    def test_include_data_single(self, post):
        field = Relationship(
            related_url='/authors/{author_id}',
            related_url_kwargs={'author_id': '<author.id>'},
            include_data=True, type_='people'
        )
        result = field.serialize('author', post)
        assert 'data' in result['author']
        assert result['author']['data']

        assert result['author']['data']['id'] == post.author.id
 def test_deserialize_non_many_list_relationship(self):
     field = Relationship(many=False, include_resource_linkage=True, type_='comments')
     with pytest.raises(ValidationError) as excinfo:
         field.deserialize({'data': ['1']})
     assert excinfo.value.args[0] == 'Relationship is not list-like'