Пример #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'
Пример #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"
Пример #5
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)
Пример #6
0
 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 == []
Пример #7
0
 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
Пример #8
0
 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
Пример #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 == []
Пример #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
Пример #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)
Пример #12
0
 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"
Пример #13
0
 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'
Пример #14
0
 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.'
Пример #15
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_resource_linkage=False, type_='comments'
     )
     result = field.serialize('comments', post_with_null_comment)
     assert result and result['links']['related']
     assert 'data' not in result
Пример #16
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_resource_linkage=True, type_='people'
     )
     result = field.serialize('author', post_with_null_author)
     assert result and result['links']['related']
     assert result['data'] is None
Пример #17
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)
 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)
Пример #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']
Пример #20
0
 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']
Пример #21
0
 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'
Пример #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)
Пример #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'
Пример #24
0
 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.'
Пример #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']
Пример #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'] == []
Пример #27
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)
Пример #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
Пример #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']
Пример #32
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]
Пример #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']
Пример #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]
Пример #35
0
 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']
Пример #36
0
 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']
Пример #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]
Пример #38
0
 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']
Пример #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)
Пример #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 == []
Пример #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)
Пример #44
0
    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
Пример #45
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'