From feaa253a012c6a0ae879880b723bba05bce7823c Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 9 Jan 2024 20:53:55 +0000 Subject: [PATCH] Created better environment for testing --- controllers/database/database.py | 11 ++++++++++- docker-compose.yml | 2 ++ scripts/create_database.py | 27 ++++++++++++++++++++++++--- scripts/create_tables.sql | 17 ----------------- scripts/test_data.sql | 13 +++++++++++++ 5 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 scripts/test_data.sql diff --git a/controllers/database/database.py b/controllers/database/database.py index 69a120c..6bc410d 100644 --- a/controllers/database/database.py +++ b/controllers/database/database.py @@ -1,9 +1,18 @@ from abc import ABC, abstractmethod from typing import Mapping, Any import sqlite3 +import os class DatabaseController(ABC): - __sqlitefile = "./data/wmgzon.db" + __data_dir = "./data/" + __db_name = "wmgzon.db" + + # Use test file if necessary + if os.environ.get("ENVIRON") == "test": + __db_name = "test_" + __db_name + + __sqlitefile = __data_dir + __db_name + def __init__(self): self._conn = None diff --git a/docker-compose.yml b/docker-compose.yml index dd084ea..f4c6974 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,8 @@ services: build: . environment: - APPSECRET=test + - ENVIRON=test + # - ENVIRON=prod ports: - "5000:5000" volumes: diff --git a/scripts/create_database.py b/scripts/create_database.py index adcc2b9..b6a1aef 100644 --- a/scripts/create_database.py +++ b/scripts/create_database.py @@ -16,9 +16,15 @@ def create_connection(path: str, filename: str): # Execute creation scripts sql = open("scripts/create_tables.sql", "r"); conn.executescript(sql.read()) - - print("SQLite Version: " + sqlite3.version) + print("Table creation complete") + + # Populate with test data if we are in Test Mode + if os.environ.get("ENVIRON") != "test": + return + + sql = open("scripts/test_data.sql", "r"); + conn.executescript(sql.read()) except sqlite3.Error as e: print(e) finally: @@ -32,5 +38,20 @@ def create_directory(dir: str): except FileExistsError: pass +def remove_file(dir: str): + try: + os.remove(dir) + except FileNotFoundError: + pass if __name__ == '__main__': - create_connection(r"./data/", r"wmgzon.db") + dir = r"./data/" + db_name = r"wmgzon.db" + + # 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/scripts/create_tables.sql b/scripts/create_tables.sql index 25bc3ce..b9e1ec9 100644 --- a/scripts/create_tables.sql +++ b/scripts/create_tables.sql @@ -9,8 +9,6 @@ CREATE TABLE IF NOT EXISTS Users ( role TEXT NOT NULL ); -INSERT INTO Users (first_name, last_name, username, email, phone, password, role) VALUES ("Luke", "Else", "lukejelse04", "test@test.com", "07498 289321", "test213", "Customer"); - CREATE TABLE IF NOT EXISTS Categories ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE @@ -23,9 +21,6 @@ INSERT INTO Categories (name) VALUES ("Books"); INSERT INTO Categories (name) VALUES ("Phones"); INSERT INTO Categories (name) VALUES ("Music"); - - - CREATE TABLE IF NOT EXISTS Products ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, @@ -42,18 +37,6 @@ CREATE TABLE IF NOT EXISTS Products ( ON UPDATE NO ACTION ); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 1); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 1); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 1); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 2); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 2); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 3); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 4); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 4); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 4); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 6); -INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 6); - CREATE TABLE IF NOT EXISTS Orders ( id INTEGER PRIMARY KEY, sellerID TEXT NOT NULL diff --git a/scripts/test_data.sql b/scripts/test_data.sql new file mode 100644 index 0000000..a99cea0 --- /dev/null +++ b/scripts/test_data.sql @@ -0,0 +1,13 @@ +INSERT INTO Users (first_name, last_name, username, email, phone, password, role) VALUES ("Luke", "Else", "lukejelse04", "test@test.com", "07498 289321", "test213", "Customer"); + +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 1); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 1); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 1); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 2); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 2); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 3); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 4); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 4); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 4); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 6); +INSERT INTO Products (name, image, description, cost, sellerID, categoryID) VALUES ("test", "assets/img/wmgzon.png", "this is a product", 20.99, 1, 6); \ No newline at end of file