Ejemplo n.º 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'
Ejemplo n.º 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"
Ejemplo n.º 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)
Ejemplo n.º 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 == []
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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 == []
Ejemplo n.º 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
Ejemplo n.º 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)
Ejemplo n.º 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"
Ejemplo n.º 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'
Ejemplo n.º 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.'
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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)
Ejemplo n.º 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']
Ejemplo n.º 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']
Ejemplo n.º 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'
Ejemplo n.º 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)
Ejemplo n.º 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'
Ejemplo n.º 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.'
Ejemplo n.º 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']
Ejemplo n.º 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'] == []
Ejemplo n.º 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)
Ejemplo n.º 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
Ejemplo n.º 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']
Ejemplo n.º 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]
Ejemplo n.º 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']
Ejemplo n.º 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]
Ejemplo n.º 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']
Ejemplo n.º 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']
Ejemplo n.º 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]
Ejemplo n.º 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']
Ejemplo n.º 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)
Ejemplo n.º 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 == []
Ejemplo n.º 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)
Ejemplo n.º 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
Ejemplo n.º 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'