|
| 1 | +from django.urls import reverse |
| 2 | + |
| 3 | +from rest_framework.settings import api_settings |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from example.tests.utils import dump_json, redump_json |
| 8 | + |
| 9 | +pytestmark = pytest.mark.django_db |
| 10 | + |
| 11 | + |
| 12 | +class TestJsonApiFilter(object): |
| 13 | + |
| 14 | + def test_request_without_filter(self, client, comment_factory): |
| 15 | + comment = comment_factory() |
| 16 | + comment2 = comment_factory() |
| 17 | + |
| 18 | + expected = { |
| 19 | + "links": { |
| 20 | + "first": "http://testserver/comments?page=1", |
| 21 | + "last": "http://testserver/comments?page=2", |
| 22 | + "next": "http://testserver/comments?page=2", |
| 23 | + "prev": None |
| 24 | + }, |
| 25 | + "data": [ |
| 26 | + { |
| 27 | + "type": "comments", |
| 28 | + "id": str(comment.pk), |
| 29 | + "attributes": { |
| 30 | + "body": comment.body |
| 31 | + }, |
| 32 | + "relationships": { |
| 33 | + "entry": { |
| 34 | + "data": { |
| 35 | + "type": "entries", |
| 36 | + "id": str(comment.entry.pk) |
| 37 | + } |
| 38 | + }, |
| 39 | + "author": { |
| 40 | + "data": { |
| 41 | + "type": "authors", |
| 42 | + "id": str(comment.author.pk) |
| 43 | + } |
| 44 | + }, |
| 45 | + } |
| 46 | + } |
| 47 | + ], |
| 48 | + "meta": { |
| 49 | + "pagination": { |
| 50 | + "page": 1, |
| 51 | + "pages": 2, |
| 52 | + "count": 2 |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + response = client.get('/comments') |
| 58 | + # assert 0 |
| 59 | + |
| 60 | + assert response.status_code == 200 |
| 61 | + actual = redump_json(response.content) |
| 62 | + expected_json = dump_json(expected) |
| 63 | + assert actual == expected_json |
| 64 | + |
| 65 | + def test_request_with_filter(self, client, comment_factory): |
| 66 | + comment = comment_factory(body='Body for comment 1') |
| 67 | + comment2 = comment_factory() |
| 68 | + |
| 69 | + expected = { |
| 70 | + "links": { |
| 71 | + "first": "http://testserver/comments?filter%5Bbody%5D=Body+for+comment+1&page=1", |
| 72 | + "last": "http://testserver/comments?filter%5Bbody%5D=Body+for+comment+1&page=1", |
| 73 | + "next": None, |
| 74 | + "prev": None |
| 75 | + }, |
| 76 | + "data": [ |
| 77 | + { |
| 78 | + "type": "comments", |
| 79 | + "id": str(comment.pk), |
| 80 | + "attributes": { |
| 81 | + "body": comment.body |
| 82 | + }, |
| 83 | + "relationships": { |
| 84 | + "entry": { |
| 85 | + "data": { |
| 86 | + "type": "entries", |
| 87 | + "id": str(comment.entry.pk) |
| 88 | + } |
| 89 | + }, |
| 90 | + "author": { |
| 91 | + "data": { |
| 92 | + "type": "authors", |
| 93 | + "id": str(comment.author.pk) |
| 94 | + } |
| 95 | + }, |
| 96 | + } |
| 97 | + } |
| 98 | + ], |
| 99 | + "meta": { |
| 100 | + "pagination": { |
| 101 | + "page": 1, |
| 102 | + "pages": 1, |
| 103 | + "count": 1 |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + response = client.get('/comments?filter[body]=Body for comment 1') |
| 109 | + |
| 110 | + assert response.status_code == 200 |
| 111 | + actual = redump_json(response.content) |
| 112 | + expected_json = dump_json(expected) |
| 113 | + assert actual == expected_json |
| 114 | + |
| 115 | + def test_failed_request_with_filter(self, client, comment_factory): |
| 116 | + comment = comment_factory(body='Body for comment 1') |
| 117 | + comment2 = comment_factory() |
| 118 | + |
| 119 | + expected = { |
| 120 | + "links": { |
| 121 | + "first": "http://testserver/comments?filter%5Bbody%5D=random+comment&page=1", |
| 122 | + "last": "http://testserver/comments?filter%5Bbody%5D=random+comment&page=1", |
| 123 | + "next": None, |
| 124 | + "prev": None |
| 125 | + }, |
| 126 | + "data": [], |
| 127 | + "meta": { |
| 128 | + "pagination": { |
| 129 | + "page": 1, |
| 130 | + "pages": 1, |
| 131 | + "count": 0 |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + response = client.get('/comments?filter[body]=random comment') |
| 137 | + assert response.status_code == 200 |
| 138 | + actual = redump_json(response.content) |
| 139 | + expected_json = dump_json(expected) |
| 140 | + assert actual == expected_json |
0 commit comments