Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
- A virtual environment, namely [venv](https://docs.python.org/3/library/venv.html).

### Setup

- Create and activate the environment,

In Windows:
Expand Down Expand Up @@ -111,8 +110,9 @@ python3 -m pytest -v
```

The database tests will be done in the table `tests` of the database specified by the variable `DATABASE_URI`. If this
variable is not specified it will automatically connect to `sqlite:///tests.db`. This will do, because we are using SQL
Alchemy to be able connect to any SQL database maintaining the same codebase.
variable is not specified it will automatically connect to SQLite database in-memory. This will do, because we are using
[SQL Alchemy](https://www.sqlalchemy.org/features.html) to be able connect to any SQL database maintaining the same
codebase.


The option `-v` shows which tests failed or succeeded. Have into account that you can also debug each test
Expand All @@ -132,10 +132,12 @@ To get a report table
```

To get a full report in html

```bash
coverage html
```
Then check in the [htmlcov/index.html](./htmlcov/index.html) to see it

Then check in the [htmlcov/index.html](./htmlcov/index.html) to see it.

If you want that previously collected coverage data is erased, you can execute:

Expand All @@ -144,7 +146,6 @@ coverage erase
```

### CLI

There are available commands, aware of the API, that can be very helpful to you. You
can check them out by running

Expand All @@ -160,7 +161,6 @@ python cli.py gen_swagger_json -f ~/Downloads/swagger.json
```

## Run as docker container

1. Build image
```bash
docker build -t time_tracker_api:local .
Expand All @@ -178,8 +178,8 @@ docker run -p 5000:5000 time_tracker_api:local
the win.
- [Flask](http://flask.pocoo.org/) as the micro framework of choice.
- [Flask RestPlus](https://flask-restplus.readthedocs.io/en/stable/) for building Restful APIs with Swagger.
- [Pytest](https://docs.pytest.org/en/latest/index.html) for tests
- [Coverage](https://coverage.readthedocs.io/en/coverage-4.5.4/) for coverage
- [Pytest](https://docs.pytest.org/en/latest/index.html) for tests.
- [Coverage](https://coverage.readthedocs.io/en/coverage-4.5.4/) for coverage.
- [Swagger](https://swagger.io/) for documentation and standardization, taking into account the
[API import restrictions and known issues](https://docs.microsoft.com/en-us/azure/api-management/api-management-api-import-restrictions)
in Azure.
Expand Down
2 changes: 2 additions & 0 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Flask-Script==2.0.6

# SQL database (MS SQL)
pyodbc==4.0.30
SQLAlchemy==1.3.15
SQLAlchemy-Utils==0.36.3
flask_sqlalchemy==2.4.1

# Handling requests
Expand Down
36 changes: 18 additions & 18 deletions tests/projects/projects_namespace_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from faker import Faker
from flask import json
from flask.testing import FlaskClient
from flask_restplus._http import HTTPStatus
from pytest_mock import MockFixture

from time_tracker_api.projects.projects_model import PROJECT_TYPE
Expand All @@ -25,7 +26,7 @@ def test_create_project_should_succeed_with_valid_request(client: FlaskClient, m

response = client.post("/projects", json=valid_project_data, follow_redirects=True)

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


Expand All @@ -40,7 +41,7 @@ def test_create_project_should_reject_bad_request(client: FlaskClient, mocker: M

response = client.post("/projects", json=invalid_project_data, follow_redirects=True)

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


Expand All @@ -52,24 +53,21 @@ def test_list_all_projects(client: FlaskClient, mocker: MockFixture):

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

assert 200 == response.status_code
json_data = json.loads(response.data)
assert [] == json_data
assert HTTPStatus.OK == response.status_code
assert [] == json.loads(response.data)
repository_find_all_mock.assert_called_once()


def test_get_project_should_succeed_with_valid_id(client: FlaskClient, mocker: MockFixture):
from time_tracker_api.projects.projects_namespace import project_dao

valid_id = fake.random_int(1, 9999)

repository_find_mock = mocker.patch.object(project_dao.repository,
'find',
return_value=fake_project)

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

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

Expand All @@ -86,11 +84,12 @@ def test_get_project_should_return_not_found_with_invalid_id(client: FlaskClient

response = client.get("/projects/%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))


def test_get_project_should_return_422_for_invalid_id_format(client: FlaskClient, mocker: MockFixture):
def test_get_project_should_response_with_unprocessable_entity_for_invalid_id_format(client: FlaskClient,
mocker: MockFixture):
from time_tracker_api.projects.projects_namespace import project_dao
from werkzeug.exceptions import UnprocessableEntity

Expand All @@ -102,7 +101,7 @@ def test_get_project_should_return_422_for_invalid_id_format(client: FlaskClient

response = client.get("/projects/%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 @@ -116,7 +115,7 @@ def test_update_project_should_succeed_with_valid_data(client: FlaskClient, mock
valid_id = fake.random_int(1, 9999)
response = client.put("/projects/%s" % valid_id, json=valid_project_data, follow_redirects=True)

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

Expand All @@ -133,7 +132,7 @@ def test_update_project_should_reject_bad_request(client: FlaskClient, mocker: M
valid_id = fake.random_int(1, 9999)
response = client.put("/projects/%s" % valid_id, json=invalid_project_data, follow_redirects=True)

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


Expand All @@ -151,7 +150,7 @@ def test_update_project_should_return_not_found_with_invalid_id(client: FlaskCli
json=valid_project_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_project_data)


Expand All @@ -166,7 +165,7 @@ def test_delete_project_should_succeed_with_valid_id(client: FlaskClient, mocker

response = client.delete("/projects/%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 @@ -183,11 +182,12 @@ def test_delete_project_should_return_not_found_with_invalid_id(client: FlaskCli

response = client.delete("/projects/%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))


def test_delete_project_should_return_422_for_invalid_id_format(client: FlaskClient, mocker: MockFixture):
def test_delete_project_should_return_unprocessable_entity_for_invalid_id_format(client: FlaskClient,
mocker: MockFixture):
from time_tracker_api.projects.projects_namespace import project_dao
from werkzeug.exceptions import UnprocessableEntity

Expand All @@ -199,5 +199,5 @@ def test_delete_project_should_return_422_for_invalid_id_format(client: FlaskCli

response = client.delete("/projects/%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))
2 changes: 1 addition & 1 deletion tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class PersonSQLModel(db.Model, AuditedSQLModel):
__tablename__ = 'tests'
__tablename__ = 'test'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=False, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
Expand Down
23 changes: 23 additions & 0 deletions tests/smoke_test.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
import pyodbc

import pytest
from flask.testing import FlaskClient
from flask_restplus._http import HTTPStatus
from pytest_mock import MockFixture


def test_app_exists(app):
assert app is not None


unexpected_errors_to_be_handled = [pyodbc.OperationalError]


@pytest.mark.parametrize("error_type", unexpected_errors_to_be_handled)
def test_exceptions_are_handled(error_type, client: FlaskClient, mocker: MockFixture):
from time_tracker_api.time_entries.time_entries_namespace import time_entries_dao
mocker.patch.object(time_entries_dao,
"get_all",
side_effect=error_type)

response = client.get('/time-entries', follow_redirects=True)

assert HTTPStatus.INTERNAL_SERVER_ERROR != response.status_code
Empty file added tests/time_entries/__init__.py
Empty file.
Loading