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
Next Next commit
Closes #23 Add mocking support
  • Loading branch information
EliuX committed Mar 16, 2020
commit 04a16e76c5cff62051215bbcfcb6dca4d7bb6b2f
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ We are using Pytest](https://docs.pytest.org/en/latest/index.html) for tests. Th

To run the tests just execute:

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

Expand Down
5 changes: 4 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
# For development

# Tests
pytest==4.1.1
pytest==5.2.0

# Mocking
pytest-mock==2.0.0

# Coverage
coverage==4.5.1
11 changes: 7 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@

import pytest
from flask import Flask
from flask.testing import FlaskClient

from time_tracker_api import create_app


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


@pytest.fixture
def client(app):
def client(app: Flask) -> FlaskClient:
"""A test client for the app."""
with app.test_client() as c:
return c
return c
9 changes: 8 additions & 1 deletion tests/time_entries/time_enties_namespace_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from flask import json
from flask.testing import FlaskClient
from pytest_mock import MockFixture

def test_list_should_return_empty_array(client):

def test_list_should_return_empty_array(mocker: MockFixture, client: FlaskClient):
from time_tracker_api.time_entries.time_entries_namespace import model
"""Should return an empty array"""
model_mock = mocker.patch.object(model, 'find_all', return_value=[])

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

assert 200 == response.status_code

json_data = json.loads(response.data)
assert [] == json_data
model_mock.assert_called_once()
5 changes: 3 additions & 2 deletions time_tracker_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os

from flask import Flask


def create_app(config_path='time_tracker_api.config.WhateverDevelopConfig',
def create_app(config_path='time_tracker_api.config.DefaultConfig',
config_data=None):
flask_app = Flask(__name__)

Expand All @@ -12,7 +13,7 @@ def create_app(config_path='time_tracker_api.config.WhateverDevelopConfig',
return flask_app


def init_app_config(app, config_path, config_data=None):
def init_app_config(app: Flask, config_path: str, config_data: dict = None):
if config_path:
app.config.from_object(config_path)
else:
Expand Down
3 changes: 3 additions & 0 deletions time_tracker_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ class WhateverDevelopConfig(Config):
FLASK_DEBUG = True
FLASK_ENV = "develop"
DATABASE = "whatever"


DefaultConfig = WhateverDevelopConfig
2 changes: 1 addition & 1 deletion time_tracker_api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def create(model_name: str):

def use_whatever():
from time_tracker_api import whatever_repository
return whatever_repository
return whatever_repository