Added Database tests

This commit is contained in:
Luke Else 2024-01-09 22:20:32 +00:00
parent feaa253a01
commit b7dd1b9ef0
9 changed files with 72 additions and 28 deletions

View File

@ -61,8 +61,7 @@ def signup():
request.form['firstname'],
request.form['lastname'],
request.form['email'],
"123",
"Customer"
"123"
))
# Code 307 Preserves the original request (POST)

View File

@ -6,8 +6,8 @@ class Admin(User):
No additional properties are assigned to the admin
'''
def __init__(self, id: int, username: str, password: str, firstname: str,
lastname: str, email: str, phone: str, role: str):
def __init__(self, username: str, password: str, firstname: str,
lastname: str, email: str, phone: str):
super().__init__(
id, username, password, firstname, lastname, email, phone, role
username, password, firstname, lastname, email, phone, "Admin"
)

View File

@ -6,9 +6,9 @@ class Customer(User):
No additional properties are assigned to the customer
'''
def __init__(self, id: int, username: str, password: str, firstname: str,
lastname: str, email: str, phone: str, role: str):
def __init__(self, username: str, password: str, firstname: str,
lastname: str, email: str, phone: str):
super().__init__(
id, username, password, firstname, lastname, email, phone, role
username, password, firstname, lastname, email, phone, "Customer"
)

View File

@ -6,9 +6,9 @@ class Seller(User):
No additional properties are assigned to the customer
'''
def __init__(self, id: int, username: str, password: str, firstname: str,
lastname: str, email: str, phone: str, role: str):
def __init__(self, username: str, password: str, firstname: str,
lastname: str, email: str, phone: str):
super().__init__(
id, username, password, firstname, lastname, email, phone, role
id, username, password, firstname, lastname, email, phone, "Seller"
)
self.store = ""

View File

@ -3,9 +3,9 @@ from abc import ABC
class User(ABC):
""" Functional Class constructor to initialise all properties in the base object
with a value """
def __init__(self, id: int, username: str, password: str, firstname: str,
def __init__(self, username: str, password: str, firstname: str,
lastname: str, email: str, phone: str, role: str):
self.id = id
self.id = 0
self.username = username
self.password = password
self.firstName = firstname

0
scripts/__init__.py Normal file
View File

View File

@ -20,11 +20,10 @@ def create_connection(path: str, filename: str):
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())
if os.environ.get("ENVIRON") == "test":
sql = open("scripts/test_data.sql", "r");
conn.executescript(sql.read())
except sqlite3.Error as e:
print(e)
finally:
@ -43,15 +42,17 @@ def remove_file(dir: str):
os.remove(dir)
except FileNotFoundError:
pass
if __name__ == '__main__':
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)
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)

View File

@ -0,0 +1,8 @@
# Ensure test environment is set before using
import os
# Setup test environment variables
os.environ["ENVIRON"] = "test"
# Runs the database creation scripts
import scripts.create_database

View File

@ -0,0 +1,36 @@
import pytest
import sqlite3
from controllers.database.user import UserController
from models.users.customer import Customer
customer = Customer(
"testuser",
"Password1",
"firstname",
"lastname",
"test@test",
"123456789"
)
# Tests a new user can be created
def test_create_user():
db = UserController()
db.create(customer)
# Tests the database maintains integrity when we try and add a user with the same details
def test_duplicate_user():
db = UserController()
with pytest.raises(sqlite3.IntegrityError):
db.create(customer)
# Test we the same user details get returned from the database
def test_read_user():
db = UserController()
# Test the same user is returned
user = db.read("testuser")
assert isinstance(user, Customer)
# Update the ID on the item as database assigns new id
customer.id = user.id
assert user.__dict__ == customer.__dict__