From b226256187b60bb20545d565f66efd79a00496ed Mon Sep 17 00:00:00 2001 From: marcsello Date: Thu, 14 May 2020 13:54:19 +0200 Subject: [PATCH 1/2] fixed tests not running --- .coveragerc | 2 ++ consumer_api/tests/__init__.py | 1 - consumer_api/tests/consumers_view_test.py | 24 +++++++++++----------- consumer_api/tests/log_view_test.py | 22 ++++++++++---------- consumer_api/tests/sync_view_test.py | 25 ++++++++++++----------- consumer_api/views/consumers_view.py | 2 +- requirements_dev.txt | 4 ++++ 7 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 .coveragerc create mode 100644 requirements_dev.txt diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..cd03256 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[run] +omit=venv/* \ No newline at end of file diff --git a/consumer_api/tests/__init__.py b/consumer_api/tests/__init__.py index 883d9c7..95f505d 100644 --- a/consumer_api/tests/__init__.py +++ b/consumer_api/tests/__init__.py @@ -1,6 +1,5 @@ import os # Setup environment variables for testing -os.environ["INITIAL_SERVERS"] = "192.168.111.22" os.environ["LOCAL_UUID"] = "d8b2e5e2-f675-4194-9324-af58e4b70c54" os.environ["REDIS_URL"] = "redis://192.168.111.121/0" diff --git a/consumer_api/tests/consumers_view_test.py b/consumer_api/tests/consumers_view_test.py index 82087e9..8f8043e 100644 --- a/consumer_api/tests/consumers_view_test.py +++ b/consumer_api/tests/consumers_view_test.py @@ -1,21 +1,21 @@ -import json -import os +import db import pytest -from flask import current_app + @pytest.fixture -def client(): - current_app.config['TESTING'] = True +def client(mocker): + mocker.patch("db.redis_client") + db.redis_client.get.side_effect=lambda a: None - with current_app.test_client() as client: + from app import app + + app.config['TESTING'] = True + + with app.test_client() as client: yield client def test_response_length(client): - data = { - "uuid": os.environ["LOCAL_UUID"], - "message": "Hello There!" - } - r = client.get('/consumers', data = json.dumps(data)) - assert len(r) == 0 + r = client.get('/consumers') + diff --git a/consumer_api/tests/log_view_test.py b/consumer_api/tests/log_view_test.py index 195be38..70bb3fa 100644 --- a/consumer_api/tests/log_view_test.py +++ b/consumer_api/tests/log_view_test.py @@ -1,16 +1,16 @@ -import json -import os - +import db import pytest -from flask import current_app - - @pytest.fixture -def client(): - current_app.config['TESTING'] = True +def client(mocker): + mocker.patch("db.redis_client") + db.redis_client.get.side_effect=lambda a: None - with current_app.test_client() as client: + from app import app + + app.config['TESTING'] = True + + with app.test_client() as client: yield client @@ -22,9 +22,9 @@ def test_log_code_get(client): def test_log_code_post(client): data = { - "uuid": os.environ["LOCAL_UUID"], + "uuid": "asdasdasd", "message": "Hello There!" } - r = client.post('/log', data = json.dumps(data)) + r = client.post('/log', json=data) assert r.status_code == 204 diff --git a/consumer_api/tests/sync_view_test.py b/consumer_api/tests/sync_view_test.py index 7f9552c..7e940b3 100644 --- a/consumer_api/tests/sync_view_test.py +++ b/consumer_api/tests/sync_view_test.py @@ -1,19 +1,20 @@ -import json -import os - +import db import pytest -from flask import current_app - - @pytest.fixture -def client(): - current_app.config['TESTING'] = True +def client(mocker): + mocker.patch("db.redis_client") + db.redis_client.get.side_effect=lambda a: None - with current_app.test_client() as client: + from app import app + + app.config['TESTING'] = True + + with app.test_client() as client: yield client + def test_log_code_get(client): r = client.get('/sync') @@ -22,8 +23,8 @@ def test_log_code_get(client): def test_log_code_post(client): data = { - "uuid": os.environ["LOCAL_UUID"] + "uuid": "tesuuid" } - r = client.post('/sync', data = json.dumps(data)) + r = client.post('/sync', json=data) - assert r.status_code == 204 + assert r.status_code == 200 diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index cba9382..1876507 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -1,7 +1,7 @@ import json from db import redis_client -from flask import jsonify, current_app +from flask import jsonify from flask_classful import FlaskView diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..cd4f820 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,4 @@ +pytest +pytest-mock +mock +coverage \ No newline at end of file -- 2.45.2 From 858e18362fd33bf2bdd01481f274b270754db7cb Mon Sep 17 00:00:00 2001 From: marcsello Date: Thu, 14 May 2020 13:55:20 +0200 Subject: [PATCH 2/2] Added a single assert --- consumer_api/tests/consumers_view_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/consumer_api/tests/consumers_view_test.py b/consumer_api/tests/consumers_view_test.py index 8f8043e..03f3019 100644 --- a/consumer_api/tests/consumers_view_test.py +++ b/consumer_api/tests/consumers_view_test.py @@ -19,3 +19,4 @@ def client(mocker): def test_response_length(client): r = client.get('/consumers') + assert r.status_code == 200 -- 2.45.2