Added Database tests
This commit is contained in:
parent
feaa253a01
commit
b7dd1b9ef0
@ -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)
|
||||||
|
@ -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"
|
||||||
)
|
)
|
||||||
|
@ -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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -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 = ""
|
||||||
|
@ -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
0
scripts/__init__.py
Normal 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");
|
||||||
|
conn.executescript(sql.read())
|
||||||
|
|
||||||
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:
|
||||||
@ -43,15 +42,17 @@ def remove_file(dir: str):
|
|||||||
os.remove(dir)
|
os.remove(dir)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
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)
|
||||||
|
8
tests/database/__init__.py
Normal file
8
tests/database/__init__.py
Normal 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
|
36
tests/database/test_users.py
Normal file
36
tests/database/test_users.py
Normal 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__
|
Loading…
Reference in New Issue
Block a user