#2 Added ability for user to login as seller and added test to verify functionality
This commit is contained in:
parent
e7ca108b7f
commit
19165220fa
@ -26,6 +26,10 @@ class DatabaseController(ABC):
|
|||||||
self._conn.close()
|
self._conn.close()
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self._conn != None:
|
||||||
|
self._conn.close()
|
||||||
|
|
||||||
""" Takes a dictionary of fields and returns the object
|
""" Takes a dictionary of fields and returns the object
|
||||||
with those fields populated """
|
with those fields populated """
|
||||||
def new_instance(self, of: type, with_fields: Mapping[str, Any]):
|
def new_instance(self, of: type, with_fields: Mapping[str, Any]):
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from .database import DatabaseController
|
from .database import DatabaseController
|
||||||
from models.users.user import User
|
from models.users.user import User
|
||||||
from models.users.customer import Customer
|
from models.users.customer import Customer
|
||||||
|
from models.users.seller import Seller
|
||||||
|
|
||||||
class UserController(DatabaseController):
|
class UserController(DatabaseController):
|
||||||
FIELDS = ['id', 'username', 'password', 'firstName', 'lastName', 'email', 'phone', 'role']
|
FIELDS = ['id', 'username', 'password', 'firstName', 'lastName', 'email', 'phone', 'role']
|
||||||
@ -38,7 +39,13 @@ class UserController(DatabaseController):
|
|||||||
|
|
||||||
if row != None:
|
if row != None:
|
||||||
params = dict(zip(self.FIELDS, row))
|
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 obj
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
@ -2,9 +2,10 @@ import pytest
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
from controllers.database.user import UserController
|
from controllers.database.user import UserController
|
||||||
from models.users.customer import Customer
|
from models.users.customer import Customer
|
||||||
|
from models.users.seller import Seller
|
||||||
|
|
||||||
customer = Customer(
|
customer = Customer(
|
||||||
"testuser",
|
"testcustomer",
|
||||||
"Password1",
|
"Password1",
|
||||||
"firstname",
|
"firstname",
|
||||||
"lastname",
|
"lastname",
|
||||||
@ -12,6 +13,15 @@ customer = Customer(
|
|||||||
"123456789"
|
"123456789"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
seller = Seller(
|
||||||
|
"testseller",
|
||||||
|
"Password1",
|
||||||
|
"firstname",
|
||||||
|
"lastname",
|
||||||
|
"seller@seller",
|
||||||
|
"987654321"
|
||||||
|
)
|
||||||
|
|
||||||
# Tests a new user can be created
|
# Tests a new user can be created
|
||||||
def test_create_user():
|
def test_create_user():
|
||||||
db = UserController()
|
db = UserController()
|
||||||
@ -28,9 +38,28 @@ def test_read_user():
|
|||||||
db = UserController()
|
db = UserController()
|
||||||
|
|
||||||
# Test the same user is returned
|
# Test the same user is returned
|
||||||
user = db.read("testuser")
|
user = db.read(customer.username)
|
||||||
assert isinstance(user, Customer)
|
assert isinstance(user, Customer)
|
||||||
|
|
||||||
# Update the ID on the item as database assigns new id
|
# Update the ID on the item as database assigns new id
|
||||||
customer.id = user.id
|
customer.id = user.id
|
||||||
assert user.__dict__ == customer.__dict__
|
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__
|
||||||
|
Loading…
Reference in New Issue
Block a user