import pytest
import sqlite3
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"
)

# 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(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__
