-
Notifications
You must be signed in to change notification settings - Fork 0
Implement activities model #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
804120c
d5c11c5
587855b
63bcdd4
b683c62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| from faker import Faker | ||
| from flask import json | ||
| from flask.testing import FlaskClient | ||
| from pytest_mock import MockFixture | ||
|
|
||
|
|
||
| fake = Faker() | ||
|
|
||
| valid_activity_data = { | ||
| "name": fake.company(), | ||
| "description": fake.paragraph() | ||
| } | ||
|
|
||
| fake_activity = ({ | ||
| "id": fake.random_int(1, 9999) | ||
| }).update(valid_activity_data) | ||
|
|
||
|
|
||
| def test_create_activity_should_succeed_with_valid_request(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| repository_create_mock = mocker.patch.object(activity_dao.repository, | ||
| 'create', | ||
| return_value=fake_activity) | ||
|
|
||
| response = client.post("/activities", json=valid_activity_data, follow_redirects=True) | ||
|
|
||
| assert 201 == response.status_code | ||
| repository_create_mock.assert_called_once_with(valid_activity_data) | ||
|
|
||
|
|
||
| def test_create_activity_should_reject_bad_request(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| invalid_activity_data = valid_activity_data.copy().update({ | ||
| "invalid_field": 123, | ||
| }) | ||
| repository_create_mock = mocker.patch.object(activity_dao.repository, | ||
| 'create', | ||
| return_value=fake_activity) | ||
|
|
||
| response = client.post("/activities", json=invalid_activity_data, follow_redirects=True) | ||
|
|
||
| assert 400 == response.status_code | ||
| repository_create_mock.assert_not_called() | ||
|
|
||
|
|
||
| def test_list_all_activities(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| repository_find_all_mock = mocker.patch.object(activity_dao.repository, | ||
| 'find_all', | ||
| return_value=[]) | ||
|
|
||
| response = client.get("/activities", follow_redirects=True) | ||
|
|
||
| assert 200 == response.status_code | ||
| json_data = json.loads(response.data) | ||
| assert [] == json_data | ||
| repository_find_all_mock.assert_called_once() | ||
|
|
||
| #HEY | ||
| def test_get_activity_should_succeed_with_valid_id(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
|
|
||
| valid_id = fake.random_int(1, 9999) | ||
|
|
||
| repository_find_mock = mocker.patch.object(activity_dao.repository, | ||
| 'find', | ||
| return_value=fake_activity) | ||
|
|
||
| response = client.get("/activities/%s" % valid_id, follow_redirects=True) | ||
|
|
||
| assert 200 == response.status_code | ||
| fake_activity == json.loads(response.data) | ||
| repository_find_mock.assert_called_once_with(str(valid_id)) | ||
|
|
||
|
|
||
| def test_get_activity_should_return_not_found_with_invalid_id(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| from werkzeug.exceptions import NotFound | ||
|
|
||
| invalid_id = fake.random_int(1, 9999) | ||
|
|
||
| repository_find_mock = mocker.patch.object(activity_dao.repository, | ||
| 'find', | ||
| side_effect=NotFound) | ||
|
|
||
| response = client.get("/activities/%s" % invalid_id, follow_redirects=True) | ||
|
|
||
| assert 404 == response.status_code | ||
| repository_find_mock.assert_called_once_with(str(invalid_id)) | ||
|
|
||
|
|
||
| def test_get_activity_should_return_422_for_invalid_id_format(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| from werkzeug.exceptions import UnprocessableEntity | ||
|
|
||
| invalid_id = fake.company() | ||
|
|
||
| repository_find_mock = mocker.patch.object(activity_dao.repository, | ||
| 'find', | ||
| side_effect=UnprocessableEntity) | ||
|
|
||
| response = client.get("/activities/%s" % invalid_id, follow_redirects=True) | ||
|
|
||
| assert 422 == response.status_code | ||
| repository_find_mock.assert_called_once_with(str(invalid_id)) | ||
|
|
||
|
|
||
| def test_update_activity_should_succeed_with_valid_data(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
|
|
||
| repository_update_mock = mocker.patch.object(activity_dao.repository, | ||
| 'update', | ||
| return_value=fake_activity) | ||
|
|
||
| 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 | ||
| fake_activity == json.loads(response.data) | ||
| repository_update_mock.assert_called_once_with(str(valid_id), valid_activity_data) | ||
|
|
||
|
|
||
| def test_update_activity_should_reject_bad_request(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| invalid_activity_data = valid_activity_data.copy().update({ | ||
| "invalid_field": 123, | ||
| }) | ||
| repository_update_mock = mocker.patch.object(activity_dao.repository, | ||
| 'update', | ||
| return_value=fake_activity) | ||
|
|
||
| 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 | ||
| repository_update_mock.assert_not_called() | ||
|
|
||
|
|
||
| def test_update_activity_should_return_not_found_with_invalid_id(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| from werkzeug.exceptions import NotFound | ||
|
|
||
| invalid_id = fake.random_int(1, 9999) | ||
|
|
||
| repository_update_mock = mocker.patch.object(activity_dao.repository, | ||
| 'update', | ||
| side_effect=NotFound) | ||
|
|
||
| response = client.put("/activities/%s" % invalid_id, | ||
| json=valid_activity_data, | ||
| follow_redirects=True) | ||
|
|
||
| assert 404 == response.status_code | ||
| repository_update_mock.assert_called_once_with(str(invalid_id), valid_activity_data) | ||
|
|
||
|
|
||
| def test_delete_activity_should_succeed_with_valid_id(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
|
|
||
| valid_id = fake.random_int(1, 9999) | ||
|
|
||
| repository_remove_mock = mocker.patch.object(activity_dao.repository, | ||
| 'remove', | ||
| return_value=None) | ||
|
|
||
| response = client.delete("/activities/%s" % valid_id, follow_redirects=True) | ||
|
|
||
| assert 204 == response.status_code | ||
| assert b'' == response.data | ||
| repository_remove_mock.assert_called_once_with(str(valid_id)) | ||
|
|
||
|
|
||
| def test_delete_activity_should_return_not_found_with_invalid_id(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| from werkzeug.exceptions import NotFound | ||
|
|
||
| invalid_id = fake.random_int(1, 9999) | ||
|
|
||
| repository_remove_mock = mocker.patch.object(activity_dao.repository, | ||
| 'remove', | ||
| side_effect=NotFound) | ||
|
|
||
| response = client.delete("/activities/%s" % invalid_id, follow_redirects=True) | ||
|
|
||
| assert 404 == response.status_code | ||
| repository_remove_mock.assert_called_once_with(str(invalid_id)) | ||
|
|
||
|
|
||
| def test_delete_activity_should_return_422_for_invalid_id_format(client: FlaskClient, mocker: MockFixture): | ||
| from time_tracker_api.activities.activities_namespace import activity_dao | ||
| from werkzeug.exceptions import UnprocessableEntity | ||
|
|
||
| invalid_id = fake.company() | ||
|
|
||
| repository_remove_mock = mocker.patch.object(activity_dao.repository, | ||
| 'remove', | ||
| side_effect=UnprocessableEntity) | ||
|
|
||
| response = client.delete("/activities/%s" % invalid_id, follow_redirects=True) | ||
|
|
||
| assert 422 == response.status_code | ||
| repository_remove_mock.assert_called_once_with(str(invalid_id)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
| from time_tracker_api.database import CRUDDao | ||
|
|
||
|
|
||
| class ActivitiesDao(CRUDDao): | ||
| class ActivityDao(CRUDDao): | ||
| pass | ||
|
|
||
|
|
||
| def create_dao() -> ActivitiesDao: | ||
| def create_dao() -> ActivityDao: | ||
| from time_tracker_api.sql_repository import db | ||
| from time_tracker_api.sql_repository import SQLCRUDDao, AuditedSQLModel | ||
|
|
||
| class ActivitySQLModel(db.Model, AuditedSQLModel): | ||
| __tablename__ = 'activity' | ||
| id = db.Column(db.Integer, primary_key=True) | ||
| name = db.Column(db.String(50), unique=True, nullable=False) | ||
| description = db.Column(db.String(250), unique=False, nullable=False) | ||
|
|
||
| class ActivitiesSQLDao(SQLCRUDDao): | ||
| def __repr__(self): | ||
| return '<Activity %r>' % self.name | ||
|
|
||
| def __str___(self): | ||
| return "the activity \"%s\"" % self.name | ||
|
|
||
| class ActivitySQLDao(SQLCRUDDao): | ||
| def __init__(self): | ||
| SQLCRUDDao.__init__(self, ActivitySQLModel) | ||
|
|
||
| return ActivitiesSQLDao() | ||
| return ActivitySQLDao() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| from faker import Faker | ||
| from flask_restplus import fields, Resource, Namespace | ||
| from flask_restplus._http import HTTPStatus | ||
|
|
||
| from time_tracker_api.activities.activities_model import create_dao | ||
| from time_tracker_api.api import audit_fields | ||
| from time_tracker_api.activities.activities_model import create_dao | ||
|
|
||
| faker = Faker() | ||
|
|
||
|
|
@@ -41,41 +43,49 @@ | |
| activity_response_fields | ||
| ) | ||
|
|
||
| activities_dao = create_dao() | ||
| activity_dao = create_dao() | ||
|
|
||
|
|
||
| @ns.route('') | ||
| class Activities(Resource): | ||
| @ns.doc('list_activities') | ||
| @ns.marshal_list_with(activity, code=200) | ||
| @ns.marshal_list_with(activity, code=HTTPStatus.OK) | ||
|
||
| def get(self): | ||
| return [] | ||
| """List all activities""" | ||
| return activity_dao.get_all(), HTTPStatus.OK | ||
|
|
||
| @ns.doc('create_activity') | ||
| @ns.response(HTTPStatus.BAD_REQUEST, 'Bad request') | ||
|
||
| @ns.expect(activity_input) | ||
| @ns.marshal_with(activity, code=201) | ||
| @ns.response(400, 'Invalid format of the attributes of the activity.') | ||
| @ns.marshal_with(activity, code=HTTPStatus.CREATED) | ||
| def post(self): | ||
| return ns.payload, 201 | ||
| """Create an activity""" | ||
| return activity_dao.create(ns.payload), HTTPStatus.CREATED | ||
|
|
||
|
|
||
| @ns.route('/<string:id>') | ||
| @ns.response(404, 'Activity not found') | ||
| @ns.param('id', 'The unique identifier of the activity') | ||
| @ns.response(HTTPStatus.NOT_FOUND, 'Activity not found') | ||
| @ns.param('id', 'The activity identifier') | ||
| class Activity(Resource): | ||
EliuX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @ns.doc('get_activity') | ||
| @ns.marshal_with(activity) | ||
| @ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format') | ||
|
||
| @ns.marshal_with(activity, code=HTTPStatus.OK) | ||
| def get(self, id): | ||
| return {} | ||
|
|
||
| @ns.doc('delete_activity') | ||
| @ns.response(204, 'The activity was deleted successfully (No content is returned)') | ||
| def delete(self, id): | ||
| return None, 204 | ||
| """Get an activity""" | ||
| return activity_dao.get(id), HTTPStatus.OK | ||
|
||
|
|
||
| @ns.doc('put_activity') | ||
| @ns.response(400, 'Invalid format of the attributes of the activity.') | ||
| @ns.doc('update_activity') | ||
| @ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The data has an invalid format.') | ||
|
||
| @ns.expect(activity_input) | ||
| @ns.marshal_with(activity) | ||
| def put(self, id): | ||
| return ns.payload | ||
| """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""" | ||
| activity_dao.delete(id) | ||
| return None, HTTPStatus.NO_CONTENT | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Status codes use constants if you don't mind :D