Skip to content

Commit b683c62

Browse files
committed
Fixes #14 - apply PR observations
1 parent 63bcdd4 commit b683c62

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

tests/activities/activities_namespace_test.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from flask import json
33
from flask.testing import FlaskClient
44
from pytest_mock import MockFixture
5-
5+
from flask_restplus._http import HTTPStatus
66

77
fake = Faker()
88

@@ -24,7 +24,7 @@ def test_create_activity_should_succeed_with_valid_request(client: FlaskClient,
2424

2525
response = client.post("/activities", json=valid_activity_data, follow_redirects=True)
2626

27-
assert 201 == response.status_code
27+
assert HTTPStatus.CREATED == response.status_code
2828
repository_create_mock.assert_called_once_with(valid_activity_data)
2929

3030

@@ -39,7 +39,7 @@ def test_create_activity_should_reject_bad_request(client: FlaskClient, mocker:
3939

4040
response = client.post("/activities", json=invalid_activity_data, follow_redirects=True)
4141

42-
assert 400 == response.status_code
42+
assert HTTPStatus.BAD_REQUEST == response.status_code
4343
repository_create_mock.assert_not_called()
4444

4545

@@ -51,7 +51,7 @@ def test_list_all_activities(client: FlaskClient, mocker: MockFixture):
5151

5252
response = client.get("/activities", follow_redirects=True)
5353

54-
assert 200 == response.status_code
54+
assert HTTPStatus.OK == response.status_code
5555
json_data = json.loads(response.data)
5656
assert [] == json_data
5757
repository_find_all_mock.assert_called_once()
@@ -68,7 +68,7 @@ def test_get_activity_should_succeed_with_valid_id(client: FlaskClient, mocker:
6868

6969
response = client.get("/activities/%s" % valid_id, follow_redirects=True)
7070

71-
assert 200 == response.status_code
71+
assert HTTPStatus.OK == response.status_code
7272
fake_activity == json.loads(response.data)
7373
repository_find_mock.assert_called_once_with(str(valid_id))
7474

@@ -85,7 +85,7 @@ def test_get_activity_should_return_not_found_with_invalid_id(client: FlaskClien
8585

8686
response = client.get("/activities/%s" % invalid_id, follow_redirects=True)
8787

88-
assert 404 == response.status_code
88+
assert HTTPStatus.NOT_FOUND == response.status_code
8989
repository_find_mock.assert_called_once_with(str(invalid_id))
9090

9191

@@ -101,7 +101,7 @@ def test_get_activity_should_return_422_for_invalid_id_format(client: FlaskClien
101101

102102
response = client.get("/activities/%s" % invalid_id, follow_redirects=True)
103103

104-
assert 422 == response.status_code
104+
assert HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code
105105
repository_find_mock.assert_called_once_with(str(invalid_id))
106106

107107

@@ -115,7 +115,7 @@ def test_update_activity_should_succeed_with_valid_data(client: FlaskClient, moc
115115
valid_id = fake.random_int(1, 9999)
116116
response = client.put("/activities/%s" % valid_id, json=valid_activity_data, follow_redirects=True)
117117

118-
assert 200 == response.status_code
118+
assert HTTPStatus.OK == response.status_code
119119
fake_activity == json.loads(response.data)
120120
repository_update_mock.assert_called_once_with(str(valid_id), valid_activity_data)
121121

@@ -132,7 +132,7 @@ def test_update_activity_should_reject_bad_request(client: FlaskClient, mocker:
132132
valid_id = fake.random_int(1, 9999)
133133
response = client.put("/activities/%s" % valid_id, json=invalid_activity_data, follow_redirects=True)
134134

135-
assert 400 == response.status_code
135+
assert HTTPStatus.BAD_REQUEST == response.status_code
136136
repository_update_mock.assert_not_called()
137137

138138

@@ -150,7 +150,7 @@ def test_update_activity_should_return_not_found_with_invalid_id(client: FlaskCl
150150
json=valid_activity_data,
151151
follow_redirects=True)
152152

153-
assert 404 == response.status_code
153+
assert HTTPStatus.NOT_FOUND == response.status_code
154154
repository_update_mock.assert_called_once_with(str(invalid_id), valid_activity_data)
155155

156156

@@ -165,7 +165,7 @@ def test_delete_activity_should_succeed_with_valid_id(client: FlaskClient, mocke
165165

166166
response = client.delete("/activities/%s" % valid_id, follow_redirects=True)
167167

168-
assert 204 == response.status_code
168+
assert HTTPStatus.NO_CONTENT == response.status_code
169169
assert b'' == response.data
170170
repository_remove_mock.assert_called_once_with(str(valid_id))
171171

@@ -182,7 +182,7 @@ def test_delete_activity_should_return_not_found_with_invalid_id(client: FlaskCl
182182

183183
response = client.delete("/activities/%s" % invalid_id, follow_redirects=True)
184184

185-
assert 404 == response.status_code
185+
assert HTTPStatus.NOT_FOUND == response.status_code
186186
repository_remove_mock.assert_called_once_with(str(invalid_id))
187187

188188

@@ -198,5 +198,5 @@ def test_delete_activity_should_return_422_for_invalid_id_format(client: FlaskCl
198198

199199
response = client.delete("/activities/%s" % invalid_id, follow_redirects=True)
200200

201-
assert 422 == response.status_code
201+
assert HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code
202202
repository_remove_mock.assert_called_once_with(str(invalid_id))

time_tracker_api/activities/activities_namespace.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@
4949
@ns.route('')
5050
class Activities(Resource):
5151
@ns.doc('list_activities')
52-
@ns.marshal_list_with(activity, code=HTTPStatus.OK)
52+
@ns.marshal_list_with(activity)
5353
def get(self):
5454
"""List all activities"""
55-
return activity_dao.get_all(), HTTPStatus.OK
55+
return activity_dao.get_all()
5656

5757
@ns.doc('create_activity')
58-
@ns.response(HTTPStatus.BAD_REQUEST, 'Bad request')
58+
@ns.response(HTTPStatus.CONFLICT, 'This activity already exists')
59+
@ns.response(HTTPStatus.BAD_REQUEST, 'Invalid format or structure of the attributes of the activity')
5960
@ns.expect(activity_input)
6061
@ns.marshal_with(activity, code=HTTPStatus.CREATED)
6162
def post(self):
@@ -65,25 +66,25 @@ def post(self):
6566

6667
@ns.route('/<string:id>')
6768
@ns.response(HTTPStatus.NOT_FOUND, 'Activity not found')
69+
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
6870
@ns.param('id', 'The activity identifier')
6971
class Activity(Resource):
7072
@ns.doc('get_activity')
71-
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
72-
@ns.marshal_with(activity, code=HTTPStatus.OK)
73+
@ns.marshal_with(activity)
7374
def get(self, id):
7475
"""Get an activity"""
75-
return activity_dao.get(id), HTTPStatus.OK
76+
return activity_dao.get(id)
7677

7778
@ns.doc('update_activity')
78-
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The data has an invalid format.')
7979
@ns.expect(activity_input)
80+
@ns.response(HTTPStatus.BAD_REQUEST, 'Invalid format or structure of the activity')
81+
@ns.response(HTTPStatus.CONFLICT, 'An activity already exists with this new data')
8082
@ns.marshal_with(activity)
8183
def put(self, id):
8284
"""Update an activity"""
8385
return activity_dao.update(id, ns.payload)
8486

8587
@ns.doc('delete_activity')
86-
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
8788
@ns.response(HTTPStatus.NO_CONTENT, 'Activity deleted successfully')
8889
def delete(self, id):
8990
"""Delete an activity"""

0 commit comments

Comments
 (0)