From bcde471f33fd9caedc6643ac030421ead94fcab2 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 23 Feb 2024 18:24:57 +0000 Subject: [PATCH] #11 Starting on the creation of an end to end test environment --- requirements.txt | Bin 491 -> 1128 bytes scripts/create_database.py | 23 ++++++++++++++--------- tests/__init__.py | 17 +++++++++++++++++ tests/endtoend/__init__.py | 0 tests/endtoend/use_cases_test.py | 8 ++++++++ tests/functional/__init__.py | 16 ---------------- tests/functional/test_homepage.py | 2 +- tests/unit/database/__init__.py | 3 +-- 8 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 tests/endtoend/__init__.py create mode 100644 tests/endtoend/use_cases_test.py diff --git a/requirements.txt b/requirements.txt index 361fe5c828949c6d16a86b580d673780ebfc8dc4..1ea46d7948919644f9a9696dc3ae3d4548ff1fb5 100644 GIT binary patch literal 1128 zcmYk5O;5sL5QO(^;!i2zTReF6VB*PxXRv^LwWOsG{PF5DyS$|#Mbn*~ota(!e&^QM z$x1ugl4oWwe1_JuKF`=5?ZFmQ0=2Of=)Kk88_;7%k3kN+XLQ5wZDWP)=&Kwt0P*CU z;`@SO!}rc>Gw)VCY9-Z@zPTNEq$ZT~Z0d-d-WtW?ShV$#PY^@SPFyAm0j*%nj z9Nsue=8L6rrEAMPgUn}ED9h=*+j65qbGD?sy2Oa4`Pvm&?$T* zCTzpxH5*ku)ZMFBPcuiq_IU-}bs}_{d1aDvU*FW~9;Tf+x|m?m9NnuB$;;P@^b;P0a<~n`J@?l?Vql1XFvSau7pT; HAL;)CR2!wy literal 491 zcmYjNyKciU4BQR)Q!Mz=Hp!s1LxD~mIxCW_GL}U_k`uUJUs49zjz=Ev!K;U`*d*m3 zPNHuc?LjY6o&{k-=rO5PN!g1h@zOR1a`A9wJTbw99QX2tLCTRBBTZ1}k|4OQV7IEY z7<|yS7sJ#xGkEKf4JN{3Xq#8Ba)mv+Lja9o2FG&8q-3LlvkAA%!+||+)?iyvyX2cK-C(u;DGi6#?sqDmQA;VgLVRhcoZ1`>Httbwyczd5(_W2OrC z6@#NA`on>X%|*%+)n<+Qh0f>L-7yIhxPII<8j7;}qcFMt#xYo`92FA75B*7xg#Z8m diff --git a/scripts/create_database.py b/scripts/create_database.py index 3a841eb..b8faef9 100644 --- a/scripts/create_database.py +++ b/scripts/create_database.py @@ -38,14 +38,19 @@ def create_connection(path: str, filename: str): # Ensure a directory is created given a path to it -dir = r"./data/" -db_name = r"wmgzon.db" +if __name__ == "__main__": + run() -# Check for test environ -if os.environ.get("ENVIRON") == "test": - # Remove the original test database - print("TEST ENVIRONMENT ACTIVE") - db_name = "test_" + db_name - remove_file(dir + db_name) +def run(): + """ Create the database for the application""" + dir = r"./data/" + db_name = r"wmgzon.db" -create_connection(dir, db_name) + # Check for test environ + if os.environ.get("ENVIRON") == "test": + # Remove the original test database + print("TEST ENVIRONMENT ACTIVE") + db_name = "test_" + db_name + remove_file(dir + db_name) + + create_connection(dir, db_name) diff --git a/tests/__init__.py b/tests/__init__.py index a6b29a5..59f2e74 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,5 +1,10 @@ +import pytest import os +from scripts.create_database import run +from app import app from dotenv import load_dotenv +from flask.testing import FlaskClient + # Setup test environment variables load_dotenv() @@ -8,3 +13,15 @@ load_dotenv() # Monitor current setting during tests old_env = os.environ.get("ENVIRON") os.environ["ENVIRON"] = "test" + +run() + +@pytest.fixture(scope="module") +def test_client() -> FlaskClient: + """ Enables tests to create requests to the web app + """ + os.environ['CONFIG_TYPE'] = 'config.TestingConfig' + + with app.test_client() as testing_client: + with app.app_context(): + yield testing_client diff --git a/tests/endtoend/__init__.py b/tests/endtoend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/endtoend/use_cases_test.py b/tests/endtoend/use_cases_test.py new file mode 100644 index 0000000..e9f315c --- /dev/null +++ b/tests/endtoend/use_cases_test.py @@ -0,0 +1,8 @@ +import pytest +from bs4 import BeautifulSoup +from tests import test_client +from flask.testing import FlaskClient + +# def test_use_case(test_client: FlaskClient): +# response = test_client.get('/products/Car Parts') +# assert response.status_code == 200 \ No newline at end of file diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py index 06bf919..e69de29 100644 --- a/tests/functional/__init__.py +++ b/tests/functional/__init__.py @@ -1,16 +0,0 @@ -from app import app -from flask.testing import FlaskClient -import pytest -import os - - -@pytest.fixture(scope="module") -def test_client() -> FlaskClient: - """ Test that required environment variables are set - ahead of runtime - """ - os.environ['CONFIG_TYPE'] = 'config.TestingConfig' - - with app.test_client() as testing_client: - with app.app_context(): - yield testing_client diff --git a/tests/functional/test_homepage.py b/tests/functional/test_homepage.py index d570fc2..b2803cb 100644 --- a/tests/functional/test_homepage.py +++ b/tests/functional/test_homepage.py @@ -1,5 +1,5 @@ from flask.testing import FlaskClient -from tests.functional import test_client +from tests import test_client import pytest diff --git a/tests/unit/database/__init__.py b/tests/unit/database/__init__.py index 9bd5abe..2257c58 100644 --- a/tests/unit/database/__init__.py +++ b/tests/unit/database/__init__.py @@ -1,3 +1,2 @@ # Ensure test environment is set before using -# Runs the database creation scripts -import scripts.create_database +# Runs the database creation scripts \ No newline at end of file