From b7dd1b9ef0f6b6a4f1b494dc0f777645acc77ed3 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 9 Jan 2024 22:20:32 +0000 Subject: [PATCH] Added Database tests --- controllers/web/user.py | 3 +-- models/users/admin.py | 6 +++--- models/users/customer.py | 6 +++--- models/users/seller.py | 6 +++--- models/users/user.py | 4 ++-- scripts/__init__.py | 0 scripts/create_database.py | 31 ++++++++++++++++--------------- tests/database/__init__.py | 8 ++++++++ tests/database/test_users.py | 36 ++++++++++++++++++++++++++++++++++++ 9 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 scripts/__init__.py create mode 100644 tests/database/__init__.py create mode 100644 tests/database/test_users.py diff --git a/controllers/web/user.py b/controllers/web/user.py index 0c5458e..24f577b 100644 --- a/controllers/web/user.py +++ b/controllers/web/user.py @@ -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) diff --git a/models/users/admin.py b/models/users/admin.py index 917907f..946a53e 100644 --- a/models/users/admin.py +++ b/models/users/admin.py @@ -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" ) diff --git a/models/users/customer.py b/models/users/customer.py index 34a5e64..0c571ad 100644 --- a/models/users/customer.py +++ b/models/users/customer.py @@ -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" ) diff --git a/models/users/seller.py b/models/users/seller.py index 57473ac..28bb3b1 100644 --- a/models/users/seller.py +++ b/models/users/seller.py @@ -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 = "" diff --git a/models/users/user.py b/models/users/user.py index e282500..95d3206 100644 --- a/models/users/user.py +++ b/models/users/user.py @@ -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 diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scripts/create_database.py b/scripts/create_database.py index b6a1aef..442c5d5 100644 --- a/scripts/create_database.py +++ b/scripts/create_database.py @@ -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) diff --git a/tests/database/__init__.py b/tests/database/__init__.py new file mode 100644 index 0000000..3c3cf69 --- /dev/null +++ b/tests/database/__init__.py @@ -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 \ No newline at end of file diff --git a/tests/database/test_users.py b/tests/database/test_users.py new file mode 100644 index 0000000..95281f8 --- /dev/null +++ b/tests/database/test_users.py @@ -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__