Skip to content

Commit d03ce1b

Browse files
committed
Fixing tests
1 parent f55319d commit d03ce1b

4 files changed

Lines changed: 51 additions & 61 deletions

File tree

services_backend/routes/button.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def create_button(button_inp: ButtonCreate, category_id: int):
1414
if not category:
1515
raise HTTPException(status_code=404, detail="Category does not exist")
1616
last_button = db.session.query(Button).order_by(Button.order.desc()).first()
17-
button = Button(**button_inp.dict())
17+
button = Button(**button_inp.dict(exclude_none=True))
1818
button.category_id = category_id
1919
if last_button:
2020
button.order = last_button.order + 1
@@ -48,7 +48,7 @@ def get_button(button_id: int, category_id: int):
4848
"name": button.name,
4949
"icon": button.icon,
5050
"link": button.link,
51-
"type": button.type,}
51+
"type": button.type}
5252

5353

5454
@button.delete("/{button_id}", response_model=None)
@@ -97,7 +97,7 @@ def update_button(button_inp: ButtonUpdate, button_id: int, category_id: int):
9797
.filter(Button.order > button.one().order) \
9898
.update({"order": Button.order - 1})
9999
button.update(
100-
button_inp.dict(exclude_unset=True)
100+
button_inp.dict(exclude_unset=True, exclude_none=True)
101101
)
102102
ret = button.one()
103103
db.session.commit()

services_backend/routes/category.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@
1010
@category.post("/", response_model=CategoryGet)
1111
def create_category(category_inp: CategoryCreate):
1212
last_category = db.session.query(Category).order_by(Category.order.desc()).first()
13-
category = Category(**category_inp.dict())
13+
category = Category(**category_inp.dict(exclude_none=True))
1414
if last_category:
1515
category.order = last_category.order + 1
1616
db.session.add(category)
1717
db.session.commit()
1818
return category
1919

2020

21-
@category.get("/", response_model=list[CategoryGet], response_model_exclude_unset=True)
21+
@category.get("/", response_model=list[CategoryGet])
2222
def get_categories(offset: int = 0, limit: int = 100):
23-
return db.session.query(Category).order_by(Category.order).offset(offset).limit(limit).all()
23+
if (offset < 0) or (limit < 0):
24+
raise HTTPException(400, detail="Offset or limit cant be negative")
25+
return [{"id": category.id, "order": category.order, "name": category.name, "type": category.type} for category in db.session.query(Category).order_by(Category.order).offset(offset).limit(limit).all()]
2426

2527

26-
@category.get("/{category_id}", response_model=CategoryGet, response_model_exclude_unset=True)
28+
@category.get("/{category_id}", response_model=CategoryGet)
2729
def get_category(category_id: int):
2830
category = db.session.query(Category).filter(Category.id == category_id).one_or_none()
2931
if not category:
@@ -76,7 +78,7 @@ def update_category(category_inp: CategoryUpdate, category_id: int):
7678
.update({"order": Category.order - 1})
7779

7880
category.update(
79-
category_inp.dict(exclude_unset=True)
81+
category_inp.dict(exclude_unset=True, exclude_none=True)
8082
)
8183
ret = category.one()
8284
db.session.commit()

tests/api/button.py

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,167 +5,155 @@
55

66

77
class TestButton:
8-
_url = '/button/'
98
settings = get_settings()
109

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}")
1312
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
1614

1715
def test_post_success(self, client, db_category, dbsession):
1816
body = {
19-
"category_id": db_category.id,
2017
"icon": "https://lh3.googleusercontent.com/yURn6ISxDySTdXZAW2PUcADMnU3y9YX0M1RyXOH8a3sa1Tr0pHhPLGw5BKuiLiXa3Eh0fyHm7Dfsd9FodK3fxJge6g=w640-h400-e365-rj-sc0x00ffffff",
2118
"name": "string",
2219
"link": "google.com",
2320
"type": "test"
2421
}
25-
res = client.post(self._url, data=json.dumps(body))
22+
res = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body))
2623
assert res.status_code == status.HTTP_200_OK
2724
res_body = res.json()
28-
assert res_body["category_id"] == body["category_id"]
2925
assert res_body["icon"] == body["icon"]
3026
assert res_body["order"] == 1
3127
assert res_body["name"] == body["name"]
3228
assert res_body["link"] == body["link"]
3329
assert res_body["type"] == body["type"]
3430
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()
3632
)
3733
assert db_button_created
3834
assert db_button_created.icon == body["icon"]
39-
assert db_button_created.category_id == body["category_id"]
4035
assert db_button_created.name == body["name"]
4136
assert db_button_created.category == db_category
4237
assert db_button_created.link == body["link"]
4338
assert db_button_created.type == body["type"]
4439
assert db_button_created.order == 1
4540

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}")
4843
assert res.status_code == status.HTTP_200_OK
4944
res_body = res.json()
50-
assert res_body['category_id'] == db_button.category_id
5145
assert res_body['icon'] == db_button.icon
5246
assert res_body['name'] == db_button.name
5347
assert res_body['order'] == db_button.order
5448
assert res_body['link'] == db_button.link
5549
assert res_body['type'] == db_button.type
5650

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}")
5953
assert res.status_code == status.HTTP_200_OK
6054
q = dbsession.query(Button).filter(Button.id == db_button.id)
6155
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}")
6357
assert get_res.status_code == status.HTTP_404_NOT_FOUND
6458

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,
6761
"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))
6963
assert res.status_code == status.HTTP_200_OK
7064
res_body = res.json()
71-
assert res_body["category_id"] == body["category_id"]
7265
assert res_body["icon"] == body["icon"]
7366
assert res_body["order"] == body["order"]
7467
assert res_body["name"] == body["name"]
7568
assert res_body["link"] == body["link"]
7669
assert res_body["type"] == body["type"]
7770

78-
def test_patch_unset_params(self, client, db_button):
71+
def test_patch_unset_params(self, client, db_button, db_category):
7972
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))
8174
assert res.status_code == status.HTTP_400_BAD_REQUEST
82-
body["category_id"] = db_button.category_id
8375
body["icon"] = "string"
8476
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))
8678
assert res.status_code == status.HTTP_200_OK
8779
assert res.json()["icon"] == body["icon"]
8880
body_name = {
8981
"name": "string",
9082
"order": 1
9183
}
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))
9385
assert res.status_code == status.HTTP_200_OK
9486
assert res.json()["name"] == body_name["name"]
9587

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}")
9890
assert res.status_code == status.HTTP_404_NOT_FOUND
9991

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}")
10294
assert res.status_code == status.HTTP_404_NOT_FOUND
10395

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))
10799
assert res.status_code == status.HTTP_404_NOT_FOUND
108100

109-
def test_create_first(self, client, db_button):
101+
def test_create_first(self, client, db_button, db_category):
110102
body = {
111-
"category_id": db_button.category_id,
112103
"icon": "test",
113104
"name": "test",
114105
"link": "test",
115106
"type": "test",
116107
}
117108

118-
res = client.post(self._url, data=json.dumps(body))
109+
res = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body))
119110
assert res.status_code == status.HTTP_200_OK
120111

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}))
122113
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}")
124115
assert res_old.json()["order"] == 2
125116

126-
def test_patch_order_fail(self, client, db_button):
117+
def test_patch_order_fail(self, client, db_button, db_category):
127118
body = {
128-
"category_id": db_button.category_id,
129119
"icon": "test",
130120
"name": "new",
131121
"link": "test",
132122
"type": "test",
133123
}
134-
res1 = client.post(self._url, data=json.dumps(body))
124+
res1 = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body))
135125
assert res1.status_code == status.HTTP_200_OK
136126

137127
body_patch = {
138128
"name": db_button.name,
139129
"order": 44,
140130
}
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))
142132
assert res.status_code == status.HTTP_400_BAD_REQUEST
143133

144-
def test_patch_negative_order_fail(self, db_button, client):
134+
def test_patch_negative_order_fail(self, db_button, client, db_category):
145135
body = {
146-
"category_id": db_button.category_id,
147136
"icon": "test",
148137
"name": "new",
149138
"link": "test",
150139
"type": "test",
151140
}
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}))
154143
assert res1.status_code == status.HTTP_400_BAD_REQUEST
155144

156-
def test_delete_order(self, db_button, client):
145+
def test_delete_order(self, db_button, client, db_category):
157146
body = {
158-
"category_id": db_button.category_id,
159147
"icon": "test",
160148
"name": "new",
161149
"link": "test",
162150
"type": "test",
163151
}
164-
res1 = client.post(self._url, data=json.dumps(body))
152+
res1 = client.post(f"/category/{db_category.id}/button/", data=json.dumps(body))
165153
assert res1.status_code == status.HTTP_200_OK
166154

167-
res = client.delete(f"{self._url}{res1.json()['id']}")
155+
res = client.delete(f"/category/{db_category.id}/button/{res1.json()['id']}")
168156
assert res.status_code == status.HTTP_200_OK
169157

170-
res = client.get(f"{self._url}{db_button.id}")
158+
res = client.get(f"/category/{db_category.id}/button/{db_button.id}")
171159
assert res.json()['order'] == 1

tests/api/category.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_get_success(self, client, db_category):
1717
assert res_body[0]['type'] == db_category.type
1818
assert res_body[0]['name'] == db_category.name
1919
assert res_body[0]['order'] == db_category.order
20-
assert res_body[0]['buttons'] == []
20+
assert not res_body[0]['buttons']
2121

2222
def test_post_success(self, client, dbsession):
2323
body = {"type": "string", "name": "string"}
@@ -32,7 +32,7 @@ def test_post_success(self, client, dbsession):
3232
assert db_category_created.name == body["name"]
3333
assert db_category_created.type == body["type"]
3434
assert db_category_created.order == 1
35-
assert db_category_created.buttons == []
35+
assert not db_category_created.buttons
3636

3737
def test_get_by_id_success(self, client, db_category):
3838
res = client.get(f'{self._url}{db_category.id}')
@@ -42,7 +42,7 @@ def test_get_by_id_success(self, client, db_category):
4242
assert res_body['type'] == db_category.type
4343
assert res_body['name'] == db_category.name
4444
assert res_body['order'] == db_category.order
45-
assert res_body['buttons'] == []
45+
assert not res_body['buttons']
4646

4747
def test_delete_by_id_success(self, client, dbsession, db_category):
4848
res = client.delete(f'{self._url}{db_category.id}')

0 commit comments

Comments
 (0)