From f1f93b08952a4c94eb729be38aba519d49dbe140 Mon Sep 17 00:00:00 2001 From: Shubham Saxena Date: Thu, 19 May 2022 22:12:08 +0530 Subject: [PATCH 1/2] Refactor and fix tests --- taketwo-webapi/{test => testing}/__init__.py | 0 taketwo-webapi/testing/integration/__init__.py | 0 .../testing/integration/test_mark.py | 6 +++--- .../testing/integration/util/__init__.py | 0 .../testing}/integration/util/assert_util.py | 0 .../testing/test_post_headers.py | 16 +++++++--------- taketwo-webapi/{test => testing}/test_webui.py | 0 taketwo-webapi/testing/unit/__init__.py | 0 .../testing}/unit/test_analyse_text.py | 4 ++-- .../testing}/unit/test_check_words.py | 4 ++-- taketwo-webapi/testing/unit/util/__init__.py | 0 .../testing}/unit/util/mock_db_util.py | 0 12 files changed, 14 insertions(+), 16 deletions(-) rename taketwo-webapi/{test => testing}/__init__.py (100%) create mode 100644 taketwo-webapi/testing/integration/__init__.py rename testing/integration/test.py => taketwo-webapi/testing/integration/test_mark.py (90%) create mode 100644 taketwo-webapi/testing/integration/util/__init__.py rename {testing => taketwo-webapi/testing}/integration/util/assert_util.py (100%) rename testing/test.py => taketwo-webapi/testing/test_post_headers.py (61%) rename taketwo-webapi/{test => testing}/test_webui.py (100%) create mode 100644 taketwo-webapi/testing/unit/__init__.py rename {testing => taketwo-webapi/testing}/unit/test_analyse_text.py (95%) rename {testing => taketwo-webapi/testing}/unit/test_check_words.py (96%) create mode 100644 taketwo-webapi/testing/unit/util/__init__.py rename {testing => taketwo-webapi/testing}/unit/util/mock_db_util.py (100%) diff --git a/taketwo-webapi/test/__init__.py b/taketwo-webapi/testing/__init__.py similarity index 100% rename from taketwo-webapi/test/__init__.py rename to taketwo-webapi/testing/__init__.py diff --git a/taketwo-webapi/testing/integration/__init__.py b/taketwo-webapi/testing/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/integration/test.py b/taketwo-webapi/testing/integration/test_mark.py similarity index 90% rename from testing/integration/test.py rename to taketwo-webapi/testing/integration/test_mark.py index db0c155..a0ccbcc 100644 --- a/testing/integration/test.py +++ b/taketwo-webapi/testing/integration/test_mark.py @@ -3,13 +3,13 @@ import json import sys -sys.path.append('../../taketwo-webapi') -sys.path.append('../util') +#sys.path.append('../../taketwo-webapi') +#sys.path.append('../util') from main import app from main import validate from main import getDb from fastapi.testclient import TestClient -from util.assert_util import comparePayloads +from testing.integration.util.assert_util import comparePayloads def override_validate(): return {"sub": 'test'} diff --git a/taketwo-webapi/testing/integration/util/__init__.py b/taketwo-webapi/testing/integration/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/integration/util/assert_util.py b/taketwo-webapi/testing/integration/util/assert_util.py similarity index 100% rename from testing/integration/util/assert_util.py rename to taketwo-webapi/testing/integration/util/assert_util.py diff --git a/testing/test.py b/taketwo-webapi/testing/test_post_headers.py similarity index 61% rename from testing/test.py rename to taketwo-webapi/testing/test_post_headers.py index 15eb629..0cfe247 100644 --- a/testing/test.py +++ b/taketwo-webapi/testing/test_post_headers.py @@ -1,13 +1,12 @@ -import requests -# example api test - +from fastapi.testclient import TestClient +from main import app import json -apiurl = 'https://localhost:8000' +client = TestClient(app) def test_post_headers_body_json(): - url = apiurl + '/mark' + url = '/mark' # Additional headers. headers = {'Content-Type': 'application/json'} @@ -16,13 +15,12 @@ def test_post_headers_body_json(): payload = {'user_id': "test", 'flagged_string': "test string", 'category': "stereotyping", 'info': "none", 'url': "example.com"} - # convert dict to json by json.dumps() for body data. - resp = requests.post(url, headers=headers, data=json.dumps(payload, indent=4)) + resp = client.post(url, headers=headers, json=payload) # Validate response headers and body contents, e.g. status code. assert resp.status_code == 200 resp_body = resp.json() - assert resp_body['url'] == url + assert resp_body['url'] == 'example.com' # print response full body as text - print(resp.text) \ No newline at end of file + #print(resp.text) \ No newline at end of file diff --git a/taketwo-webapi/test/test_webui.py b/taketwo-webapi/testing/test_webui.py similarity index 100% rename from taketwo-webapi/test/test_webui.py rename to taketwo-webapi/testing/test_webui.py diff --git a/taketwo-webapi/testing/unit/__init__.py b/taketwo-webapi/testing/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/unit/test_analyse_text.py b/taketwo-webapi/testing/unit/test_analyse_text.py similarity index 95% rename from testing/unit/test_analyse_text.py rename to taketwo-webapi/testing/unit/test_analyse_text.py index 0836ebb..0a1c0e3 100644 --- a/testing/unit/test_analyse_text.py +++ b/taketwo-webapi/testing/unit/test_analyse_text.py @@ -8,8 +8,8 @@ from main import analyse_text from main import Text -from util.mock_db_util import getRow -from util.mock_db_util import setupMocks +from testing.unit.util.mock_db_util import getRow +from testing.unit.util.mock_db_util import setupMocks class TestAnalyseText(unittest.TestCase): diff --git a/testing/unit/test_check_words.py b/taketwo-webapi/testing/unit/test_check_words.py similarity index 96% rename from testing/unit/test_check_words.py rename to taketwo-webapi/testing/unit/test_check_words.py index c4afaf8..841f3dc 100644 --- a/testing/unit/test_check_words.py +++ b/taketwo-webapi/testing/unit/test_check_words.py @@ -8,8 +8,8 @@ from main import check_words from main import Text -from util.mock_db_util import getRow -from util.mock_db_util import setupMocks +from testing.unit.util.mock_db_util import getRow +from testing.unit.util.mock_db_util import setupMocks class TestCheckWords(unittest.TestCase): diff --git a/taketwo-webapi/testing/unit/util/__init__.py b/taketwo-webapi/testing/unit/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testing/unit/util/mock_db_util.py b/taketwo-webapi/testing/unit/util/mock_db_util.py similarity index 100% rename from testing/unit/util/mock_db_util.py rename to taketwo-webapi/testing/unit/util/mock_db_util.py From 90e4bf79bb6fe5a7509db5a51c3fa5c0659f4c15 Mon Sep 17 00:00:00 2001 From: Shubham Saxena Date: Thu, 19 May 2022 22:22:30 +0530 Subject: [PATCH 2/2] Refactor tests; remove unused tests Signed-off-by: Shubham Saxena --- .../testing/integration/test_mark.py | 4 --- .../testing/{ => integration}/test_webui.py | 0 taketwo-webapi/testing/test_post_headers.py | 26 ------------------- 3 files changed, 30 deletions(-) rename taketwo-webapi/testing/{ => integration}/test_webui.py (100%) delete mode 100644 taketwo-webapi/testing/test_post_headers.py diff --git a/taketwo-webapi/testing/integration/test_mark.py b/taketwo-webapi/testing/integration/test_mark.py index a0ccbcc..72927f6 100644 --- a/taketwo-webapi/testing/integration/test_mark.py +++ b/taketwo-webapi/testing/integration/test_mark.py @@ -1,10 +1,6 @@ import requests -#example api test - import json import sys -#sys.path.append('../../taketwo-webapi') -#sys.path.append('../util') from main import app from main import validate from main import getDb diff --git a/taketwo-webapi/testing/test_webui.py b/taketwo-webapi/testing/integration/test_webui.py similarity index 100% rename from taketwo-webapi/testing/test_webui.py rename to taketwo-webapi/testing/integration/test_webui.py diff --git a/taketwo-webapi/testing/test_post_headers.py b/taketwo-webapi/testing/test_post_headers.py deleted file mode 100644 index 0cfe247..0000000 --- a/taketwo-webapi/testing/test_post_headers.py +++ /dev/null @@ -1,26 +0,0 @@ -from fastapi.testclient import TestClient -from main import app -import json - -client = TestClient(app) - - -def test_post_headers_body_json(): - url = '/mark' - - # Additional headers. - headers = {'Content-Type': 'application/json'} - - # Body - payload = {'user_id': "test", 'flagged_string': "test string", 'category': "stereotyping", 'info': "none", - 'url': "example.com"} - - resp = client.post(url, headers=headers, json=payload) - - # Validate response headers and body contents, e.g. status code. - assert resp.status_code == 200 - resp_body = resp.json() - assert resp_body['url'] == 'example.com' - - # print response full body as text - #print(resp.text) \ No newline at end of file