Exemple #1
0
    def test_include_data_load_null(self, post_with_null_author):
        serialized = unpack(PostSchema(
            include_data=('author', 'post_comments')
        ).dump(post_with_null_author))

        with pytest.raises(ValidationError) as excinfo:
            PostSchema().load(serialized)
        err = excinfo.value
        assert 'author' in err.field_names
Exemple #2
0
    def test_include_data_load_null(self, post_with_null_author):
        serialized = PostSchema(
            include_data=("author",
                          "post_comments")).dump(post_with_null_author)

        with pytest.raises(ValidationError) as excinfo:
            PostSchema().load(serialized)
        err = excinfo.value
        assert "author" in err.args[0]
Exemple #3
0
    def test_include_data_load(self, post):
        serialized = unpack(
            PostSchema(include_data=("author", "post_comments",
                                     "post_comments.author")).dump(post))
        loaded = unpack(PostSchema().load(serialized))

        assert "author" in loaded
        assert loaded["author"]["id"] == str(post.author.id)
        assert loaded["author"]["first_name"] == post.author.first_name

        assert "comments" in loaded
        assert len(loaded["comments"]) == len(post.comments)
        for comment in loaded["comments"]:
            assert "body" in comment
            assert comment["id"] in [str(c.id) for c in post.comments]
Exemple #4
0
    def test_include_data_load(self, post):
        serialized = unpack(PostSchema(
            include_data=('author', 'post_comments', 'post_comments.author')
        ).dump(post))
        loaded = unpack(PostSchema().load(serialized))

        assert 'author' in loaded
        assert loaded['author']['id'] == str(post.author.id)
        assert loaded['author']['first_name'] == post.author.first_name

        assert 'comments' in loaded
        assert len(loaded['comments']) == len(post.comments)
        for comment in loaded['comments']:
            assert 'body' in comment
            assert comment['id'] in [str(c.id) for c in post.comments]
Exemple #5
0
 def test_include_data_with_single(self, post):
     data = unpack(PostSchema(include_data=('author',)).dump(post))
     assert 'included' in data
     assert len(data['included']) == 1
     author = data['included'][0]
     assert 'attributes' in author
     assert 'first_name' in author['attributes']
Exemple #6
0
 def test_include_data_with_many(self, post):
     data = unpack(PostSchema(include_data=('post_comments', 'post_comments.author')).dump(post))
     assert 'included' in data
     assert len(data['included']) == 4
     first_comment = [i for i in data['included'] if i['type'] == 'comments'][0]
     assert 'attributes' in first_comment
     assert 'body' in first_comment['attributes']
    def test_include_data_load_without_schema_loads_only_ids(
            self, post):
        class PostInnerSchemalessSchema(Schema):
            id = fields.Str()
            comments = fields.Relationship(
                'http://test.test/posts/{id}/comments/',
                related_url_kwargs={'id': '<id>'},
                data_key='post-comments',
                load_from='post-comments',
                many=True, type_='comments'
            )

            class Meta:
                type_ = 'posts'
                strict = True

        serialized = unpack(PostSchema(
            include_data=('author', 'post_comments')
        ).dump(post))

        if _MARSHMALLOW_VERSION_INFO[0] >= 3:
            from marshmallow import INCLUDE
            load_kwargs = {'unknown': INCLUDE}
        else:
            load_kwargs = {}

        loaded = unpack(PostInnerSchemalessSchema(**load_kwargs).load(serialized))

        assert 'comments' in loaded
        assert len(loaded['comments']) == len(post.comments)
        for comment_id in loaded['comments']:
            assert int(comment_id) in [c.id for c in post.comments]
Exemple #8
0
 def test_dump_to(self, post):
     data = unpack(PostSchema().dump(post))
     assert "data" in data
     assert "attributes" in data["data"]
     assert "title" in data["data"]["attributes"]
     assert "relationships" in data["data"]
     assert "post-comments" in data["data"]["relationships"]
Exemple #9
0
    def test_include_data_load_without_schema_loads_only_ids(self, post):
        class PostInnerSchemalessSchema(Schema):
            id = fields.Str()
            comments = fields.Relationship(
                "http://test.test/posts/{id}/comments/",
                related_url_kwargs={"id": "<id>"},
                data_key="post-comments",
                load_from="post-comments",
                many=True,
                type_="comments",
            )

            class Meta:
                type_ = "posts"
                strict = True

        serialized = unpack(
            PostSchema(include_data=("author", "post_comments")).dump(post))

        if _MARSHMALLOW_VERSION_INFO[0] >= 3:
            from marshmallow import INCLUDE

            load_kwargs = {"unknown": INCLUDE}
        else:
            load_kwargs = {}

        loaded = unpack(
            PostInnerSchemalessSchema(**load_kwargs).load(serialized))

        assert "comments" in loaded
        assert len(loaded["comments"]) == len(post.comments)
        for comment_id in loaded["comments"]:
            assert int(comment_id) in [c.id for c in post.comments]
Exemple #10
0
 def test_include_data_with_single(self, post):
     data = unpack(PostSchema(include_data=("author", )).dump(post))
     assert "included" in data
     assert len(data["included"]) == 1
     author = data["included"][0]
     assert "attributes" in author
     assert "first_name" in author["attributes"]
Exemple #11
0
 def test_dump_to(self, post):
     data = unpack(PostSchema().dump(post))
     assert 'data' in data
     assert 'attributes' in data['data']
     assert 'title' in data['data']['attributes']
     assert 'relationships' in data['data']
     assert 'post-comments' in data['data']['relationships']
Exemple #12
0
    def test_include_data_load_without_schema_loads_only_ids(
            self, post):
        class PostInnerSchemalessSchema(Schema):
            id = fields.Str()
            comments = fields.Relationship(
                'http://test.test/posts/{id}/comments/',
                related_url_kwargs={'id': '<id>'},
                data_key='post-comments',
                load_from='post-comments',
                many=True, type_='comments'
            )

            class Meta:
                type_ = 'posts'
                strict = True

        serialized = unpack(PostSchema(
            include_data=('author', 'post_comments')
        ).dump(post))

        loaded = unpack(PostInnerSchemalessSchema().load(serialized))

        assert 'comments' in loaded
        assert len(loaded['comments']) == len(post.comments)
        for comment_id in loaded['comments']:
            assert int(comment_id) in [c.id for c in post.comments]
Exemple #13
0
 def test_include_data_with_many(self, post):
     data = unpack(
         PostSchema(include_data=("post_comments",
                                  "post_comments.author")).dump(post))
     assert "included" in data
     assert len(data["included"]) == 4
     first_comment = [
         i for i in data["included"] if i["type"] == "comments"
     ][0]
     assert "attributes" in first_comment
     assert "body" in first_comment["attributes"]
Exemple #14
0
    def test_include_data_with_nested_exclude_arg(self, post):
        data = unpack(PostSchema(
            exclude=('post_comments.author.twitter',),
            include_data=('post_comments', 'post_comments.author')
        ).dump(post))

        assert 'included' in data
        assert len(data['included']) == 4

        first_author = [i for i in data['included'] if i['type'] == 'people'][0]
        assert 'twitter' not in first_author['attributes']
        for attribute in ('first_name', 'last_name'):
            assert attribute in first_author['attributes']
Exemple #15
0
    def test_include_data_with_nested_exclude_arg(self, post):
        data = PostSchema(
            exclude=("post_comments.author.twitter", ),
            include_data=("post_comments", "post_comments.author"),
        ).dump(post)

        assert "included" in data
        assert len(data["included"]) == 4

        first_author = [i for i in data["included"]
                        if i["type"] == "people"][0]
        assert "twitter" not in first_author["attributes"]
        for attribute in ("first_name", "last_name"):
            assert attribute in first_author["attributes"]
Exemple #16
0
 def test_include_data_with_all_relations(self, post):
     data = unpack(PostSchema(include_data=('author',
                                            'post_comments',
                                            'post_comments.author')).dump(post))
     assert 'included' in data
     assert len(data['included']) == 5
     for included in data['included']:
         assert included['id']
         assert included['type'] in ('people', 'comments')
     expected_comments_author_ids = set([str(comment.author.id) for comment in post.comments])
     included_comments_author_ids = set([i['id'] for i in data['included']
                                         if i['type'] == 'people' and
                                         i['id'] != str(post.author.id)])
     assert included_comments_author_ids == expected_comments_author_ids
Exemple #17
0
 def test_include_data_with_all_relations(self, post):
     data = unpack(
         PostSchema(include_data=("author", "post_comments",
                                  "post_comments.author")).dump(post))
     assert "included" in data
     assert len(data["included"]) == 5
     for included in data["included"]:
         assert included["id"]
         assert included["type"] in ("people", "comments")
     expected_comments_author_ids = {
         str(comment.author.id)
         for comment in post.comments
     }
     included_comments_author_ids = {
         i["id"]
         for i in data["included"]
         if i["type"] == "people" and i["id"] != str(post.author.id)
     }
     assert included_comments_author_ids == expected_comments_author_ids
Exemple #18
0
    def test_include_data_load_without_schema_loads_only_ids(self, post):
        class PostInnerSchemalessSchema(Schema):
            id = fields.Str()
            comments = fields.Relationship(
                "http://test.test/posts/{id}/comments/",
                related_url_kwargs={"id": "<id>"},
                data_key="post-comments",
                load_from="post-comments",
                many=True,
                type_="comments",
            )

            class Meta:
                type_ = "posts"
                strict = True

        serialized = PostSchema(include_data=("author",
                                              "post_comments")).dump(post)
        loaded = PostInnerSchemalessSchema(unknown=INCLUDE).load(serialized)

        assert "comments" in loaded
        assert len(loaded["comments"]) == len(post.comments)
        for comment_id in loaded["comments"]:
            assert int(comment_id) in [c.id for c in post.comments]
Exemple #19
0
 def test_include_no_data(self, post):
     data = unpack(PostSchema(include_data=()).dump(post))
     assert "included" not in data