示例#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
文件: test_links.py 项目: allanon/hyp
    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
文件: test_links.py 项目: newsomc/hyp
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'}]}