-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
804120c
Fixes #14 - Add Activity model
Angeluz-07 d5c11c5
Fixes #14 - Add tests for activity
Angeluz-07 587855b
Fixes #14 - replace http codes by constants
Angeluz-07 63bcdd4
Fixes #14 - merge with time-entry model
Angeluz-07 b683c62
Fixes #14 - apply PR observations
Angeluz-07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| from flask_restplus._http import HTTPStatus | ||
|
|
||
| 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 HTTPStatus.CREATED == 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 HTTPStatus.BAD_REQUEST == 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 HTTPStatus.OK == 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 HTTPStatus.OK == 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 HTTPStatus.NOT_FOUND == 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 HTTPStatus.UNPROCESSABLE_ENTITY == 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 HTTPStatus.OK == 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 HTTPStatus.BAD_REQUEST == 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 HTTPStatus.NOT_FOUND == 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 HTTPStatus.NO_CONTENT == 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 HTTPStatus.NOT_FOUND == 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 HTTPStatus.UNPROCESSABLE_ENTITY == response.status_code | ||
| repository_remove_mock.assert_called_once_with(str(invalid_id)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.