|
5 | 5 |
|
6 | 6 |
|
7 | 7 | class TestButton: |
8 | | - _url = '/button/' |
9 | 8 | settings = get_settings() |
10 | 9 |
|
11 | | - def test_get_success(self, client, db_button): |
12 | | - res = client.get(self._url) |
| 10 | + def test_get_success(self, client, db_button, db_category): |
| 11 | + res = client.get(f"/category/{db_category.id}/button/{db_button.id}") |
13 | 12 | assert res.status_code == status.HTTP_200_OK |
14 | | - assert len(res.json()) == 1 |
15 | | - assert res.json()[0]['id'] == db_button.id |
| 13 | + assert res.json()['id'] == db_button.id |
16 | 14 |
|
17 | 15 | def test_post_success(self, client, db_category, dbsession): |
18 | 16 | body = { |
19 | | - "category_id": db_category.id, |
20 | 17 | "icon": "https://lh3.googleusercontent.com/yURn6ISxDySTdXZAW2PUcADMnU3y9YX0M1RyXOH8a3sa1Tr0pHhPLGw5BKuiLiXa3Eh0fyHm7Dfsd9FodK3fxJge6g=w640-h400-e365-rj-sc0x00ffffff", |
21 | 18 | "name": "string", |
22 | 19 | "link": "google.com", |
23 | 20 | "type": "test" |
24 | 21 | } |
25 | | - res = client.post(self._url, data=json.dumps(body)) |
| 22 | + res = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body)) |
26 | 23 | assert res.status_code == status.HTTP_200_OK |
27 | 24 | res_body = res.json() |
28 | | - assert res_body["category_id"] == body["category_id"] |
29 | 25 | assert res_body["icon"] == body["icon"] |
30 | 26 | assert res_body["order"] == 1 |
31 | 27 | assert res_body["name"] == body["name"] |
32 | 28 | assert res_body["link"] == body["link"] |
33 | 29 | assert res_body["type"] == body["type"] |
34 | 30 | db_button_created: Button = ( |
35 | | - dbsession.query(Button).filter(Button.category_id == body["category_id"]).one_or_none() |
| 31 | + dbsession.query(Button).filter(Button.id == res_body["id"]).one_or_none() |
36 | 32 | ) |
37 | 33 | assert db_button_created |
38 | 34 | assert db_button_created.icon == body["icon"] |
39 | | - assert db_button_created.category_id == body["category_id"] |
40 | 35 | assert db_button_created.name == body["name"] |
41 | 36 | assert db_button_created.category == db_category |
42 | 37 | assert db_button_created.link == body["link"] |
43 | 38 | assert db_button_created.type == body["type"] |
44 | 39 | assert db_button_created.order == 1 |
45 | 40 |
|
46 | | - def test_get_by_id_success(self, client, db_button): |
47 | | - res = client.get(f'{self._url}{db_button.id}') |
| 41 | + def test_get_by_id_success(self, client, db_button, db_category): |
| 42 | + res = client.get(f"/category/{db_category.id}/button/{db_button.id}") |
48 | 43 | assert res.status_code == status.HTTP_200_OK |
49 | 44 | res_body = res.json() |
50 | | - assert res_body['category_id'] == db_button.category_id |
51 | 45 | assert res_body['icon'] == db_button.icon |
52 | 46 | assert res_body['name'] == db_button.name |
53 | 47 | assert res_body['order'] == db_button.order |
54 | 48 | assert res_body['link'] == db_button.link |
55 | 49 | assert res_body['type'] == db_button.type |
56 | 50 |
|
57 | | - def test_delete_by_id_success(self, client, dbsession, db_button): |
58 | | - res = client.delete(f"{self._url}{db_button.id}") |
| 51 | + def test_delete_by_id_success(self, client, dbsession, db_button, db_category): |
| 52 | + res = client.delete(f"/category/{db_category.id}/button/{db_button.id}") |
59 | 53 | assert res.status_code == status.HTTP_200_OK |
60 | 54 | q = dbsession.query(Button).filter(Button.id == db_button.id) |
61 | 55 | assert not q.one_or_none() |
62 | | - get_res = client.get(f"{self._url}{db_button.id}") |
| 56 | + get_res = client.get(f"/category/{db_category.id}/button/{db_button.id}") |
63 | 57 | assert get_res.status_code == status.HTTP_404_NOT_FOUND |
64 | 58 |
|
65 | | - def test_patch_by_id_success(self, db_button, client): |
66 | | - body = {"category_id": db_button.category_id, "icon": "cool icon", "name": "nice name", "order": 2, |
| 59 | + def test_patch_by_id_success(self, db_button, client, db_category): |
| 60 | + body = {"icon": "cool icon", "name": "nice name", "order": 2, |
67 | 61 | "link": "ya.ru", "type": "nice type"} |
68 | | - res = client.patch(f"{self._url}{db_button.id}", data=json.dumps(body)) |
| 62 | + res = client.patch(f"/category/{db_category.id}/button/{db_button.id}", data=json.dumps(body)) |
69 | 63 | assert res.status_code == status.HTTP_200_OK |
70 | 64 | res_body = res.json() |
71 | | - assert res_body["category_id"] == body["category_id"] |
72 | 65 | assert res_body["icon"] == body["icon"] |
73 | 66 | assert res_body["order"] == body["order"] |
74 | 67 | assert res_body["name"] == body["name"] |
75 | 68 | assert res_body["link"] == body["link"] |
76 | 69 | assert res_body["type"] == body["type"] |
77 | 70 |
|
78 | | - def test_patch_unset_params(self, client, db_button): |
| 71 | + def test_patch_unset_params(self, client, db_button, db_category): |
79 | 72 | body = {} |
80 | | - res = client.patch(f"{self._url}{db_button.id}", data=json.dumps(body)) |
| 73 | + res = client.patch(f"/category/{db_category.id}/button/{db_button.id}", data=json.dumps(body)) |
81 | 74 | assert res.status_code == status.HTTP_400_BAD_REQUEST |
82 | | - body["category_id"] = db_button.category_id |
83 | 75 | body["icon"] = "string" |
84 | 76 | body["order"] = 1 |
85 | | - res = client.patch(f"{self._url}{db_button.id}", data=json.dumps(body)) |
| 77 | + res = client.patch(f"/category/{db_category.id}/button/{db_button.id}", data=json.dumps(body)) |
86 | 78 | assert res.status_code == status.HTTP_200_OK |
87 | 79 | assert res.json()["icon"] == body["icon"] |
88 | 80 | body_name = { |
89 | 81 | "name": "string", |
90 | 82 | "order": 1 |
91 | 83 | } |
92 | | - res = client.patch(f"{self._url}{db_button.id}", data=json.dumps(body_name)) |
| 84 | + res = client.patch(f"/category/{db_category.id}/button/{db_button.id}", data=json.dumps(body_name)) |
93 | 85 | assert res.status_code == status.HTTP_200_OK |
94 | 86 | assert res.json()["name"] == body_name["name"] |
95 | 87 |
|
96 | | - def test_get_by_id_not_found(self, client, db_button): |
97 | | - res = client.get(f'{self._url}{db_button.id + 1}') |
| 88 | + def test_get_by_id_not_found(self, client, db_button, db_category): |
| 89 | + res = client.get(f"/category/{db_category.id}/button/{db_button.id + 1}") |
98 | 90 | assert res.status_code == status.HTTP_404_NOT_FOUND |
99 | 91 |
|
100 | | - def test_delete_by_id_not_found(self, client, db_button): |
101 | | - res = client.delete(f"{self._url}{db_button.id + 1}") |
| 92 | + def test_delete_by_id_not_found(self, client, db_button, db_category): |
| 93 | + res = client.delete(f"/category/{db_category.id}/button/{db_button.id + 1}") |
102 | 94 | assert res.status_code == status.HTTP_404_NOT_FOUND |
103 | 95 |
|
104 | | - def test_patch_by_id_not_found(self, client, db_button): |
105 | | - body = {"category_id": db_button.category_id, "icon": "cool icon", "name": "nice name"} |
106 | | - res = client.patch(f"{self._url}{db_button.id + 1}", data=json.dumps(body)) |
| 96 | + def test_patch_by_id_not_found(self, client, db_button, db_category): |
| 97 | + body = {"icon": "cool icon", "name": "nice name"} |
| 98 | + res = client.patch(f"/category/{db_category.id}/button/{db_button.id + 1}", data=json.dumps(body)) |
107 | 99 | assert res.status_code == status.HTTP_404_NOT_FOUND |
108 | 100 |
|
109 | | - def test_create_first(self, client, db_button): |
| 101 | + def test_create_first(self, client, db_button, db_category): |
110 | 102 | body = { |
111 | | - "category_id": db_button.category_id, |
112 | 103 | "icon": "test", |
113 | 104 | "name": "test", |
114 | 105 | "link": "test", |
115 | 106 | "type": "test", |
116 | 107 | } |
117 | 108 |
|
118 | | - res = client.post(self._url, data=json.dumps(body)) |
| 109 | + res = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body)) |
119 | 110 | assert res.status_code == status.HTTP_200_OK |
120 | 111 |
|
121 | | - res = client.patch(f'{self._url}{res.json()["id"]}', data=json.dumps({"order": 1})) |
| 112 | + res = client.patch(f"/category/{db_category.id}/button/{res.json()['id']}", data=json.dumps({"order": 1})) |
122 | 113 | assert res.json()["order"] == 1 |
123 | | - res_old = client.get(f"{self._url}{db_button.id}") |
| 114 | + res_old = client.get(f"/category/{db_category.id}/button/{db_button.id}") |
124 | 115 | assert res_old.json()["order"] == 2 |
125 | 116 |
|
126 | | - def test_patch_order_fail(self, client, db_button): |
| 117 | + def test_patch_order_fail(self, client, db_button, db_category): |
127 | 118 | body = { |
128 | | - "category_id": db_button.category_id, |
129 | 119 | "icon": "test", |
130 | 120 | "name": "new", |
131 | 121 | "link": "test", |
132 | 122 | "type": "test", |
133 | 123 | } |
134 | | - res1 = client.post(self._url, data=json.dumps(body)) |
| 124 | + res1 = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body)) |
135 | 125 | assert res1.status_code == status.HTTP_200_OK |
136 | 126 |
|
137 | 127 | body_patch = { |
138 | 128 | "name": db_button.name, |
139 | 129 | "order": 44, |
140 | 130 | } |
141 | | - res = client.patch(f"{self._url}{res1.json()['id']}", data=json.dumps(body_patch)) |
| 131 | + res = client.patch(f"/category/{db_category.id}/button/{res1.json()['id']}", data=json.dumps(body_patch)) |
142 | 132 | assert res.status_code == status.HTTP_400_BAD_REQUEST |
143 | 133 |
|
144 | | - def test_patch_negative_order_fail(self, db_button, client): |
| 134 | + def test_patch_negative_order_fail(self, db_button, client, db_category): |
145 | 135 | body = { |
146 | | - "category_id": db_button.category_id, |
147 | 136 | "icon": "test", |
148 | 137 | "name": "new", |
149 | 138 | "link": "test", |
150 | 139 | "type": "test", |
151 | 140 | } |
152 | | - res = client.post(self._url, data=json.dumps(body)) |
153 | | - res1 = client.patch(f"{self._url}{res.json()['id']}", data=json.dumps({"order": -10})) |
| 141 | + res = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body)) |
| 142 | + res1 = client.patch(f"/category/{db_category.id}/button/{res.json()['id']}", data=json.dumps({"order": -10})) |
154 | 143 | assert res1.status_code == status.HTTP_400_BAD_REQUEST |
155 | 144 |
|
156 | | - def test_delete_order(self, db_button, client): |
| 145 | + def test_delete_order(self, db_button, client, db_category): |
157 | 146 | body = { |
158 | | - "category_id": db_button.category_id, |
159 | 147 | "icon": "test", |
160 | 148 | "name": "new", |
161 | 149 | "link": "test", |
162 | 150 | "type": "test", |
163 | 151 | } |
164 | | - res1 = client.post(self._url, data=json.dumps(body)) |
| 152 | + res1 = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body)) |
165 | 153 | assert res1.status_code == status.HTTP_200_OK |
166 | 154 |
|
167 | | - res = client.delete(f"{self._url}{res1.json()['id']}") |
| 155 | + res = client.delete(f"/category/{db_category.id}/button/{res1.json()['id']}") |
168 | 156 | assert res.status_code == status.HTTP_200_OK |
169 | 157 |
|
170 | | - res = client.get(f"{self._url}{db_button.id}") |
| 158 | + res = client.get(f"/category/{db_category.id}/button/{db_button.id}") |
171 | 159 | assert res.json()['order'] == 1 |
0 commit comments