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
Fixes #17 Add tests and coverage support
  • Loading branch information
EliuX committed Mar 13, 2020
commit 63181626e4f60f4fba3a65e67865809ce8dffacf
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
__pycache__/
*.py[cod]
*$py.class
*.egg-info/
htmlcov/
.tox/
.coverage
.coverage.*

# scrapy
.scrapy
Expand Down
73 changes: 67 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# time-tracker-api

The API of the TSheets killer app.

## Getting started
Follow the following instructions to get the project ready to use ASAP:
Follow the following instructions to get the project ready to use ASAP.

### Requirements
Be sure you have installed in your system
Expand All @@ -10,7 +12,7 @@ Be sure you have installed in your system
automatically [pip](https://pip.pypa.io/en/stable/) as well.
- A virtual environment, namely [venv](https://docs.python.org/3/library/venv.html).

## Setup
### Setup

- Create and activate the environment,

Expand All @@ -26,13 +28,16 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
virtualenv .venv
source .venv/bin/activate
```

- Install the requirements:
```
python3 -m pip install -r requirements/prod.txt
python3 -m pip install -r requirements/<stage>.txt
```

The `stage` can be `dev` or `prod`.
Remember to do it with Python 3.

## How to use it
### How to use it
- Set the env var `FLASK_APP` to `time_tracker_api` and start the app:

In Windows
Expand All @@ -50,7 +55,47 @@ automatically [pip](https://pip.pypa.io/en/stable/) as well.
a link to the swagger.json with the definition of the api.


## CLI
## Development

### Test
We are using Pytest](https://docs.pytest.org/en/latest/index.html) for tests. The tests are located in the package
`tests` and use the [conventions for python test discovery](https://docs.pytest.org/en/latest/goodpractices.html#test-discovery).

To run the tests just execute:

```
python3 -m pytest -v
```

The option `-v` shows which tests failed or succeeded. Have into account that you can also debug each test
(test_* files) with the help of an IDE like PyCharm.

#### Coverage
To check the coverage of the tests execute

```bash
coverage run -m pytest -v
```

To get a report table

```bash
coverage report
```

To get a full report in html
```bash
coverage html
```
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:

```
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 @@ -64,4 +109,20 @@ as well as its correspondent options.

```
python cli.py gen_swagger_json -f ~/Downloads/swagger.json
```
```

## Built with
- [Python version 3](https://www.python.org/download/releases/3.0/) as backend programming language. Strong typing for
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
- [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.


## License

Copyright 2020 ioet Inc. All Rights Reserved.
8 changes: 4 additions & 4 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/usr/bin/env python3

print("****************")
print("TimeTracker CLI")
print("****************")

import os

from flask import json
Expand Down Expand Up @@ -63,4 +59,8 @@ def save_data(data: str, filename: str) -> None:


if __name__ == "__main__":
print("****************")
print("TimeTracker CLI")
print("****************")

cli_manager.run()
11 changes: 11 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# requirements/dev.txt

-r prod.txt

# For development

# Tests
pytest==4.1.1

# Coverage
coverage==4.5.1
8 changes: 8 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tool:pytest]
testpaths = tests
addopts = -p no:warnings

[coverage:run]
branch = True
source =
time_tracker_api
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from setuptools import setup, find_packages

setup(
name="time-tracker-api",
packages=find_packages(),
include_package_data=True,
)
Empty file added tests/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import pytest

from time_tracker_api import create_app

@pytest.fixture(scope='session')
def app():
"""An instance of the app for tests"""
return create_app()

@pytest.fixture
def client(app):
"""A test client for the app."""
with app.test_client() as c:
return c
Empty file added tests/projects/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/projects/projects_namespace_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from flask import json


def test_list_should_return_nothing(client):
"""Should return an empty array"""
response = client.get("/projects", follow_redirects=True)

assert 200 == response.status_code

json_data = json.loads(response.data)
assert [] == json_data
4 changes: 4 additions & 0 deletions tests/smoke_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def test_app_exists(app):
"""Does app exists"""
assert app is not None
1 change: 0 additions & 1 deletion time_tracker_api/projects/projects_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def get(self, uid):
return project_dao.get(uid)

@ns.doc('update_project_status')
@ns.param('uid', 'The project identifier')
@ns.expect(project)
@ns.response(204, 'State of the project successfully updated')
def post(self, uid):
Expand Down