Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fixes #14 - apply PR observations
  • Loading branch information
Angeluz-07 committed Mar 26, 2020
commit b683c62fe3564993c60971df8d2fea1a93443b51
26 changes: 13 additions & 13 deletions tests/activities/activities_namespace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import json
from flask.testing import FlaskClient
from pytest_mock import MockFixture

from flask_restplus._http import HTTPStatus

fake = Faker()

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

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

assert 201 == response.status_code
assert HTTPStatus.CREATED == response.status_code
repository_create_mock.assert_called_once_with(valid_activity_data)


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

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

assert 400 == response.status_code
assert HTTPStatus.BAD_REQUEST == response.status_code
repository_create_mock.assert_not_called()


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

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

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

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

assert 200 == response.status_code
assert HTTPStatus.OK == response.status_code
fake_activity == json.loads(response.data)
repository_find_mock.assert_called_once_with(str(valid_id))

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

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

assert 404 == response.status_code
assert HTTPStatus.NOT_FOUND == response.status_code
repository_find_mock.assert_called_once_with(str(invalid_id))


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

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

assert 422 == response.status_code
assert HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code
repository_find_mock.assert_called_once_with(str(invalid_id))


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

assert 200 == response.status_code
assert HTTPStatus.OK == response.status_code
fake_activity == json.loads(response.data)
repository_update_mock.assert_called_once_with(str(valid_id), valid_activity_data)

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

assert 400 == response.status_code
assert HTTPStatus.BAD_REQUEST == response.status_code
repository_update_mock.assert_not_called()


Expand All @@ -150,7 +150,7 @@ def test_update_activity_should_return_not_found_with_invalid_id(client: FlaskCl
json=valid_activity_data,
follow_redirects=True)

assert 404 == response.status_code
assert HTTPStatus.NOT_FOUND == response.status_code
repository_update_mock.assert_called_once_with(str(invalid_id), valid_activity_data)


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

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

assert 204 == response.status_code
assert HTTPStatus.NO_CONTENT == response.status_code
assert b'' == response.data
repository_remove_mock.assert_called_once_with(str(valid_id))

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

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

assert 404 == response.status_code
assert HTTPStatus.NOT_FOUND == response.status_code
repository_remove_mock.assert_called_once_with(str(invalid_id))


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

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

assert 422 == response.status_code
assert HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code
repository_remove_mock.assert_called_once_with(str(invalid_id))
17 changes: 9 additions & 8 deletions time_tracker_api/activities/activities_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@
@ns.route('')
class Activities(Resource):
@ns.doc('list_activities')
@ns.marshal_list_with(activity, code=HTTPStatus.OK)
@ns.marshal_list_with(activity)
def get(self):
"""List all activities"""
return activity_dao.get_all(), HTTPStatus.OK
return activity_dao.get_all()

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

@ns.route('/<string:id>')
@ns.response(HTTPStatus.NOT_FOUND, 'Activity not found')
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
@ns.param('id', 'The activity identifier')
class Activity(Resource):
@ns.doc('get_activity')
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
@ns.marshal_with(activity, code=HTTPStatus.OK)
@ns.marshal_with(activity)
def get(self, id):
"""Get an activity"""
return activity_dao.get(id), HTTPStatus.OK
return activity_dao.get(id)

@ns.doc('update_activity')
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The data has an invalid format.')
@ns.expect(activity_input)
@ns.response(HTTPStatus.BAD_REQUEST, 'Invalid format or structure of the activity')
@ns.response(HTTPStatus.CONFLICT, 'An activity already exists with this new data')
@ns.marshal_with(activity)
def put(self, id):
"""Update an activity"""
return activity_dao.update(id, ns.payload)

@ns.doc('delete_activity')
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
@ns.response(HTTPStatus.NO_CONTENT, 'Activity deleted successfully')
def delete(self, id):
"""Delete an activity"""
Expand Down