Skip to content

Commit 04a16e7

Browse files
author
EliuX
committed
Closes #23 Add mocking support
1 parent b94a95f commit 04a16e7

File tree

7 files changed

+27
-10
lines changed

7 files changed

+27
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ We are using Pytest](https://docs.pytest.org/en/latest/index.html) for tests. Th
6363
6464
To run the tests just execute:
6565
66-
```
66+
```bash
6767
python3 -m pytest -v
6868
```
6969

requirements/dev.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
# For development
66

77
# Tests
8-
pytest==4.1.1
8+
pytest==5.2.0
9+
10+
# Mocking
11+
pytest-mock==2.0.0
912

1013
# Coverage
1114
coverage==4.5.1

tests/conftest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
21
import pytest
2+
from flask import Flask
3+
from flask.testing import FlaskClient
34

45
from time_tracker_api import create_app
56

7+
68
@pytest.fixture(scope='session')
7-
def app():
9+
def app() -> Flask:
810
"""An instance of the app for tests"""
911
return create_app()
1012

13+
1114
@pytest.fixture
12-
def client(app):
15+
def client(app: Flask) -> FlaskClient:
1316
"""A test client for the app."""
1417
with app.test_client() as c:
15-
return c
18+
return c
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
from flask import json
2+
from flask.testing import FlaskClient
3+
from pytest_mock import MockFixture
24

3-
def test_list_should_return_empty_array(client):
5+
6+
def test_list_should_return_empty_array(mocker: MockFixture, client: FlaskClient):
7+
from time_tracker_api.time_entries.time_entries_namespace import model
48
"""Should return an empty array"""
9+
model_mock = mocker.patch.object(model, 'find_all', return_value=[])
10+
511
response = client.get("/time-entries", follow_redirects=True)
612

713
assert 200 == response.status_code
814

915
json_data = json.loads(response.data)
1016
assert [] == json_data
17+
model_mock.assert_called_once()

time_tracker_api/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
2+
23
from flask import Flask
34

45

5-
def create_app(config_path='time_tracker_api.config.WhateverDevelopConfig',
6+
def create_app(config_path='time_tracker_api.config.DefaultConfig',
67
config_data=None):
78
flask_app = Flask(__name__)
89

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

1415

15-
def init_app_config(app, config_path, config_data=None):
16+
def init_app_config(app: Flask, config_path: str, config_data: dict = None):
1617
if config_path:
1718
app.config.from_object(config_path)
1819
else:

time_tracker_api/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ class WhateverDevelopConfig(Config):
77
FLASK_DEBUG = True
88
FLASK_ENV = "develop"
99
DATABASE = "whatever"
10+
11+
12+
DefaultConfig = WhateverDevelopConfig

time_tracker_api/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def create(model_name: str):
2525

2626
def use_whatever():
2727
from time_tracker_api import whatever_repository
28-
return whatever_repository
28+
return whatever_repository

0 commit comments

Comments
 (0)