Пример #1
0
def test_meta():
    response = PostResponder.build(
        {'id': 1, 'title': 'Yeah'},
        meta={'key': 'value'},
    )

    assert response['meta']['key'] == 'value'
Пример #2
0
    def test_one_to_one_with_missing_value(self):
        author = {'id': 1}
        p1 = {'id': 1, 'title': 'My title', 'author': author}
        p2 = {'id': 2, 'title': 'My other title', 'author': None}

        response = PostResponder.build([p1, p2], links=['author'])

        assert response == {
            'jsonapi': JSONAPI_VERSION_DICT,
            'data': [{
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                },
            }, {
                'id': 2,
                'title': 'My other title',
                'links': {},
            }],
            'links': {
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                }
            }
        }
Пример #3
0
    def test_one_to_one_with_missing_value(self):
        author = {'id': 1}
        p1 = {'id': 1, 'title': 'My title', 'author': author}
        p2 = {'id': 2, 'title': 'My other title', 'author': None}

        response = PostResponder.build([p1, p2], links=['author'])

        assert response == {
            'posts': [{
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                },
            }, {
                'id': 2,
                'title': 'My other title',
                'links': {},
            }],
            'links': {
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                }
            }
        }
Пример #4
0
    def test_one_to_many(self):
        comments = [
            {'id': 1, 'content': 'My comment'},
            {'id': 2, 'content': 'Another comment'},
        ]
        post = {'id': 1, 'title': 'My title', 'comments': comments}

        response = PostResponder.build(post, links=['comments'])

        assert response == {
            'jsonapi': JSONAPI_VERSION_DICT,
            'data': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'comments': [1, 2],
                }
            },
            'links': {
                'posts.comments': {
                    'href': 'http://example.com/comments/{posts.comments}',
                    'type': 'comments',
                }
            }
        }
Пример #5
0
    def test_one_to_one_with_collect(self):
        author = {'id': 1}
        post = {'id': 1, 'title': 'My title', 'author': author}

        response = PostResponder.build(post, collect=True)

        assert response == {
            'jsonapi': JSONAPI_VERSION_DICT,
            'data': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                }
            },
            'included': {
                'people': [author],
            },
            'links': {
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                },
            }
        }
Пример #6
0
def test_one_to_many():
    comments = [
        {'id': 1, 'content': 'My comment'},
        {'id': 2, 'content': 'Another comment'},
    ]
    post = {'id': 1, 'title': 'My title', 'comments': comments}

    data = PostResponder.build(post, links=['comments'])

    assert data == {
        'posts': [
            {
                'id': 1,
                'title': 'My title',
                'links': {
                    'comments': [1, 2],
                }
            },
        ],
        'links': {
            'posts.comments': {
                'href': 'http://example.com/comments/{posts.comments}',
                'type': 'comments',
            }
        }
    }
Пример #7
0
    def test_multiple(self):
        response = PostResponder.build([
            {
                'id': 1,
                'title': 'A title'
            },
            {
                'id': 2,
                'title': 'Another title'
            },
        ])

        assert response == {
            'data': [{
                'attributes': {
                    'title': 'A title'
                },
                'id': 1,
                'type': 'posts'
            }, {
                'attributes': {
                    'title': 'Another title'
                },
                'id': 2,
                'type': 'posts'
            }],
            'jsonapi':
            JSONAPI_VERSION_DICT
        }
Пример #8
0
    def test_single(self):
        response = PostResponder.respond(
            {'id': 1, 'title': 'A title'}
        )

        assert json.loads(response) == {
            'posts': {'id': 1, 'title': 'A title'}
        }
Пример #9
0
def test_dumps():
    data = PostResponder.dumps([
        {'id': 1, 'title': 'A title'}
    ])

    assert json.loads(data) == {
        'posts': [
            {'id': 1, 'title': 'A title'}
        ]
    }
Пример #10
0
def test_multiple():
    data = PostResponder.build([
        {'id': 1, 'title': 'A title'},
        {'id': 2, 'title': 'Another title'},
    ])

    assert data == {
        'posts': [
            {'id': 1, 'title': 'A title'},
            {'id': 2, 'title': 'Another title'},
        ]
    }
Пример #11
0
    def test_multiple(self):
        response = PostResponder.build([
            {'id': 1, 'title': 'A title'},
            {'id': 2, 'title': 'Another title'},
        ])

        assert response == {
            'posts': [
                {'id': 1, 'title': 'A title'},
                {'id': 2, 'title': 'Another title'},
            ]
        }
Пример #12
0
    def test_single(self):
        response = PostResponder.respond({'id': 1, 'title': 'A title'})

        assert json.loads(response) == {
            'data': {
                'attributes': {
                    'title': 'A title'
                },
                'id': 1,
                'type': 'posts'
            },
            'jsonapi': JSONAPI_VERSION_DICT
        }
Пример #13
0
    def test_single(self):
        response = PostResponder.build({'id': 1, 'title': 'My title'})

        assert response == {
            'data': {
                'attributes': {
                    'title': 'My title'
                },
                'id': 1,
                'type': 'posts'
            },
            'jsonapi': JSONAPI_VERSION_DICT
        }
Пример #14
0
    def test_nested_collection(self):
        author = {'id': 1}
        flamer = {'id': 2}
        comment = {'id': 1, 'content': '<redacted>', 'author': flamer}
        post = {
            'id': 1,
            'title': 'My title',
            'author': author,
            'comments': [comment],
        }

        response = PostResponder.build(post, collect=True)

        assert response == {
            'posts': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                    'comments': [1],
                }
            },
            'linked': {
                'people': [author, flamer],
                'comments': [
                    {
                        'id': 1,
                        'content': '<redacted>',
                        'links': {
                            'author': 2
                        }
                    }
                ],
            },
            'links': {
                'comments.author': {
                    'href': 'http://example.com/people/{comments.author}',
                    'type': 'people',
                },
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                },
                'posts.comments': {
                    'href': 'http://example.com/comments/{posts.comments}',
                    'type': 'comments',
                },
            }
        }
Пример #15
0
    def test_nested_collection(self):
        author = {'id': 1}
        flamer = {'id': 2}
        comment = {'id': 1, 'content': '<redacted>', 'author': flamer}
        post = {
            'id': 1,
            'title': 'My title',
            'author': author,
            'comments': [comment],
        }

        response = PostResponder.build(post, collect=True)

        assert response == {
            'jsonapi': JSONAPI_VERSION_DICT,
            'data': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                    'comments': [1],
                }
            },
            'included': {
                'people': [author, flamer],
                'comments': [{
                    'id': 1,
                    'content': '<redacted>',
                    'links': {
                        'author': 2
                    }
                }],
            },
            'links': {
                'comments.author': {
                    'href': 'http://example.com/people/{comments.author}',
                    'type': 'people',
                },
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                },
                'posts.comments': {
                    'href': 'http://example.com/comments/{posts.comments}',
                    'type': 'comments',
                },
            }
        }
Пример #16
0
    def test_one_to_many_with_missing_values(self):
        comments = [None, None]
        post = {'id': 1, 'title': 'My title', 'comments': comments}

        response = PostResponder.build(post, links=['comments'])

        assert response == {
            'posts': {
                'id': 1,
                'title': 'My title',
                'links': {},
            },
            'links': {
                'posts.comments': {
                    'href': 'http://example.com/comments/{posts.comments}',
                    'type': 'comments',
                }
            }
        }
Пример #17
0
    def test_single(self):
        author = {'id': 1, 'name': 'John'}
        comments = [
            {'id': 1, 'content': 'My comment'},
            {'id': 2, 'content': 'Another comment'},
        ]
        post = {'id': 1, 'title': 'My title', 'comments': comments, 'author': author}

        response = PostResponder.build(post, related={
            'comments': comments, 'author': [author]
        })

        assert response == {
            'jsonapi': JSONAPI_VERSION_DICT,
            'data': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                    'comments': [1, 2],
                }
            },
            'links': {
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                },
                'posts.comments': {
                    'href': 'http://example.com/comments/{posts.comments}',
                    'type': 'comments',
                }
            },
            'included': {
                'comments': [
                    {'id': 1, 'content': 'My comment'},
                    {'id': 2, 'content': 'Another comment'},
                ],
                'people': [
                    {'id': 1, 'name': 'John'},
                ]
            }
        }
Пример #18
0
    def test_single(self):
        author = {'id': 1, 'name': 'John'}
        comments = [
            {'id': 1, 'content': 'My comment'},
            {'id': 2, 'content': 'Another comment'},
        ]
        post = {'id': 1, 'title': 'My title', 'comments': comments, 'author': author}

        response = PostResponder.build(post, linked={
            'comments': comments, 'author': [author]
        })

        assert response == {
            'posts': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                    'comments': [1, 2],
                }
            },
            'links': {
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                },
                'posts.comments': {
                    'href': 'http://example.com/comments/{posts.comments}',
                    'type': 'comments',
                }
            },
            'linked': {
                'comments': [
                    {'id': 1, 'content': 'My comment'},
                    {'id': 2, 'content': 'Another comment'},
                ],
                'people': [
                    {'id': 1, 'name': 'John'},
                ]
            }
        }
Пример #19
0
    def test_one_to_one(self):
        author = {'id': 1}
        post = {'id': 1, 'title': 'My title', 'author': author}

        response = PostResponder.build(post, links=['author'])

        assert response == {
            'posts': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                }
            },
            'links': {
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                }
            }
        }
Пример #20
0
    def test_one_to_one_with_collect(self):
        author = {'id': 1}
        post = {'id': 1, 'title': 'My title', 'author': author}

        response = PostResponder.build(post, collect=True)

        assert response == {
            'posts': {
                'id': 1,
                'title': 'My title',
                'links': {
                    'author': 1,
                }
            },
            'linked': {
                'people': [author],
            },
            'links': {
                'posts.author': {
                    'href': 'http://example.com/people/{posts.author}',
                    'type': 'people',
                },
            }
        }
Пример #21
0
    def test_single(self):
        response = PostResponder.build({'id': 1, 'title': 'My title'})

        assert response == {'posts': {'id': 1, 'title': 'My title'}}
Пример #22
0
def test_single_object():
    data = PostResponder.build({'id': 1, 'title': 'My title'})

    assert data == {'posts': [{'id': 1, 'title': 'My title'}]}