2024-02-05 19:19:29 +00:00
|
|
|
import pytest
|
|
|
|
import sqlite3
|
2024-02-23 21:15:06 +00:00
|
|
|
from tests.unit.database.database_test_class import TestDatabase
|
2024-02-05 19:19:29 +00:00
|
|
|
from controllers.database.user import UserController
|
|
|
|
from models.users.customer import Customer
|
|
|
|
from models.users.seller import Seller
|
|
|
|
|
|
|
|
customer = Customer(
|
|
|
|
"testcustomer",
|
|
|
|
"Password1",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"test@test",
|
|
|
|
"123456789"
|
|
|
|
)
|
|
|
|
|
|
|
|
seller = Seller(
|
|
|
|
"testseller",
|
|
|
|
"Password1",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"seller@seller",
|
|
|
|
"987654321"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-23 21:15:06 +00:00
|
|
|
class TestUsers(TestDatabase):
|
|
|
|
""" Class to encapsulate all of the user
|
|
|
|
datbase tests
|
|
|
|
"""
|
2024-02-05 19:19:29 +00:00
|
|
|
|
2024-02-23 21:15:06 +00:00
|
|
|
def test_create_user(self):
|
|
|
|
""" Tests a new user can be created """
|
|
|
|
db = UserController()
|
2024-02-05 19:19:29 +00:00
|
|
|
db.create(customer)
|
|
|
|
|
2024-02-23 21:15:06 +00:00
|
|
|
def test_duplicate_user(self):
|
|
|
|
""" Tests the database maintains integrity when we try
|
|
|
|
and add a user with the same details
|
|
|
|
"""
|
|
|
|
db = UserController()
|
|
|
|
with pytest.raises(sqlite3.IntegrityError):
|
|
|
|
db.create(customer)
|
|
|
|
|
|
|
|
def test_read_user(self):
|
|
|
|
""" Test we the same user details get returned from the database """
|
|
|
|
db = UserController()
|
|
|
|
|
|
|
|
# Test the same user is returned
|
|
|
|
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__
|
|
|
|
|
|
|
|
def test_create_seller(self):
|
|
|
|
""" Tests a new seller can be created """
|
|
|
|
db = UserController()
|
|
|
|
db.create(seller)
|
|
|
|
|
|
|
|
def test_read_seller(self):
|
|
|
|
""" Test that the same seller details get
|
|
|
|
returned from the database
|
|
|
|
"""
|
|
|
|
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__
|