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_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_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_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_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
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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_include_resource_linkage_id_field_from_schema(self): class AuthorSchema(Schema): id = Str(attribute="name") class Meta: type_ = "authors" strict = True field = Relationship( include_resource_linkage=True, type_="authors", schema=AuthorSchema ) result = field.serialize("author", {"author": {"name": "Ray Bradbury"}}) assert "data" in result assert result["data"] assert result["data"]["id"] == "Ray Bradbury"
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_include_resource_linkage_id_field_from_schema(self): class AuthorSchema(Schema): id = Str(attribute='name') class Meta: type_ = 'authors' strict = True field = Relationship( include_resource_linkage=True, type_='authors', schema=AuthorSchema, ) result = field.serialize('author', {'author': { 'name': 'Ray Bradbury' }}) assert 'data' in result assert result['data'] assert result['data']['id'] == 'Ray Bradbury'