#2 Added ability for user to login as seller and added test to verify functionality

This commit is contained in:
Luke Else 2024-01-18 21:11:01 +00:00
parent e7ca108b7f
commit 19165220fa
3 changed files with 43 additions and 3 deletions

View File

@ -26,6 +26,10 @@ class DatabaseController(ABC):
self._conn.close()
print(e)
def __del__(self):
if self._conn != None:
self._conn.close()
""" Takes a dictionary of fields and returns the object
with those fields populated """
def new_instance(self, of: type, with_fields: Mapping[str, Any]):

View File

@ -1,6 +1,7 @@
from .database import DatabaseController
from models.users.user import User
from models.users.customer import Customer
from models.users.seller import Seller
class UserController(DatabaseController):
FIELDS = ['id', 'username', 'password', 'firstName', 'lastName', 'email', 'phone', 'role']
@ -38,7 +39,13 @@ class UserController(DatabaseController):
if row != None:
params = dict(zip(self.FIELDS, row))
obj = self.new_instance(Customer, params)
# Is user a seller
type = Customer
if row[7] == "Seller":
type = Seller
obj = self.new_instance(type, params)
return obj
return None

View File

@ -2,9 +2,10 @@ import pytest
import sqlite3
from controllers.database.user import UserController
from models.users.customer import Customer
from models.users.seller import Seller
customer = Customer(
"testuser",
"testcustomer",
"Password1",
"firstname",
"lastname",
@ -12,6 +13,15 @@ customer = Customer(
"123456789"
)
seller = Seller(
"testseller",
"Password1",
"firstname",
"lastname",
"seller@seller",
"987654321"
)
# Tests a new user can be created
def test_create_user():
db = UserController()
@ -28,9 +38,28 @@ def test_read_user():
db = UserController()
# Test the same user is returned
user = db.read("testuser")
user = db.read(customer.username)
assert isinstance(user, Customer)
# Update the ID on the item as database assigns new id
customer.id = user.id
assert user.__dict__ == customer.__dict__
# Tests a new seller can be created
def test_create_seller():
db = UserController()
db.create(seller)
# Test that the same seller details get returned from the database
def test_read_seller():
db = UserController()
# Test the same user is returned
user = db.read(seller.username)
assert isinstance(user, Seller)
# Update the ID on the item as database assigns new id
seller.id = user.id
user.store = ""
assert user.__dict__ == seller.__dict__