Created better environment for testing
This commit is contained in:
parent
1df7ea856c
commit
feaa253a01
@ -1,9 +1,18 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Mapping, Any
|
from typing import Mapping, Any
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import os
|
||||||
|
|
||||||
class DatabaseController(ABC):
|
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):
|
def __init__(self):
|
||||||
self._conn = None
|
self._conn = None
|
||||||
|
@ -6,6 +6,8 @@ services:
|
|||||||
build: .
|
build: .
|
||||||
environment:
|
environment:
|
||||||
- APPSECRET=test
|
- APPSECRET=test
|
||||||
|
- ENVIRON=test
|
||||||
|
# - ENVIRON=prod
|
||||||
ports:
|
ports:
|
||||||
- "5000:5000"
|
- "5000:5000"
|
||||||
volumes:
|
volumes:
|
||||||
|
@ -16,9 +16,15 @@ def create_connection(path: str, filename: str):
|
|||||||
# Execute creation scripts
|
# Execute creation scripts
|
||||||
sql = open("scripts/create_tables.sql", "r");
|
sql = open("scripts/create_tables.sql", "r");
|
||||||
conn.executescript(sql.read())
|
conn.executescript(sql.read())
|
||||||
|
|
||||||
print("SQLite Version: " + sqlite3.version)
|
|
||||||
print("Table creation complete")
|
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:
|
except sqlite3.Error as e:
|
||||||
print(e)
|
print(e)
|
||||||
finally:
|
finally:
|
||||||
@ -32,5 +38,20 @@ def create_directory(dir: str):
|
|||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def remove_file(dir: str):
|
||||||
|
try:
|
||||||
|
os.remove(dir)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
if __name__ == '__main__':
|
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)
|
||||||
|
@ -9,8 +9,6 @@ CREATE TABLE IF NOT EXISTS Users (
|
|||||||
role TEXT NOT NULL
|
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 (
|
CREATE TABLE IF NOT EXISTS Categories (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
name TEXT NOT NULL UNIQUE
|
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 ("Phones");
|
||||||
INSERT INTO Categories (name) VALUES ("Music");
|
INSERT INTO Categories (name) VALUES ("Music");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS Products (
|
CREATE TABLE IF NOT EXISTS Products (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
@ -42,18 +37,6 @@ CREATE TABLE IF NOT EXISTS Products (
|
|||||||
ON UPDATE NO ACTION
|
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 (
|
CREATE TABLE IF NOT EXISTS Orders (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
sellerID TEXT NOT NULL
|
sellerID TEXT NOT NULL
|
||||||
|
13
scripts/test_data.sql
Normal file
13
scripts/test_data.sql
Normal file
@ -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);
|
Loading…
Reference in New Issue
Block a user