WMGZON/tests/database/test_users.py

75 lines
1.6 KiB
Python
Raw Normal View History

2024-01-09 22:20:32 +00:00
import pytest
import sqlite3
from controllers.database.user import UserController
from models.users.customer import Customer
from models.users.seller import Seller
2024-01-09 22:20:32 +00:00
customer = Customer(
2024-01-21 22:06:06 +00:00
"testcustomer",
"Password1",
"firstname",
"lastname",
"test@test",
"123456789"
2024-01-09 22:20:32 +00:00
)
seller = Seller(
2024-01-21 22:06:06 +00:00
"testseller",
"Password1",
"firstname",
"lastname",
"seller@seller",
"987654321"
)
2024-01-09 22:20:32 +00:00
# Tests a new user can be created
2024-01-21 22:06:06 +00:00
2024-01-09 22:20:32 +00:00
def test_create_user():
db = UserController()
db.create(customer)
2024-01-21 22:22:29 +00:00
# Tests the database maintains integrity when we try
# and add a user with the same details
2024-01-21 22:06:06 +00:00
2024-01-09 22:20:32 +00:00
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
2024-01-21 22:06:06 +00:00
2024-01-09 22:20:32 +00:00
def test_read_user():
db = UserController()
# Test the same user is returned
user = db.read(customer.username)
2024-01-09 22:20:32 +00:00
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
2024-01-21 22:06:06 +00:00
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__