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['firstname'],
request.form['lastname'], request.form['lastname'],
request.form['email'], request.form['email'],
"123", "123"
"Customer"
)) ))
# Code 307 Preserves the original request (POST) # Code 307 Preserves the original request (POST)

View File

@ -6,8 +6,8 @@ class Admin(User):
No additional properties are assigned to the admin No additional properties are assigned to the admin
''' '''
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): lastname: str, email: str, phone: str):
super().__init__( 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 No additional properties are assigned to the customer
''' '''
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): lastname: str, email: str, phone: str):
super().__init__( 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 No additional properties are assigned to the customer
''' '''
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): lastname: str, email: str, phone: str):
super().__init__( super().__init__(
id, username, password, firstname, lastname, email, phone, role id, username, password, firstname, lastname, email, phone, "Seller"
) )
self.store = "" self.store = ""

View File

@ -3,9 +3,9 @@ from abc import ABC
class User(ABC): class User(ABC):
""" Functional Class constructor to initialise all properties in the base object """ Functional Class constructor to initialise all properties in the base object
with a value """ 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): lastname: str, email: str, phone: str, role: str):
self.id = id self.id = 0
self.username = username self.username = username
self.password = password self.password = password
self.firstName = firstname 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") print("Table creation complete")
# Populate with test data if we are in Test Mode # Populate with test data if we are in Test Mode
if os.environ.get("ENVIRON") != "test": if os.environ.get("ENVIRON") == "test":
return
sql = open("scripts/test_data.sql", "r"); sql = open("scripts/test_data.sql", "r");
conn.executescript(sql.read()) conn.executescript(sql.read())
except sqlite3.Error as e: except sqlite3.Error as e:
print(e) print(e)
finally: finally:
@ -43,7 +42,9 @@ def remove_file(dir: str):
os.remove(dir) os.remove(dir)
except FileNotFoundError: except FileNotFoundError:
pass pass
if __name__ == '__main__':
dir = r"./data/" dir = r"./data/"
db_name = r"wmgzon.db" db_name = r"wmgzon.db"

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__