2024-01-10 23:44:59 +00:00
|
|
|
import pytest
|
|
|
|
import sqlite3
|
|
|
|
from datetime import datetime
|
|
|
|
from controllers.database.product import ProductController
|
|
|
|
from models.products.product import Product
|
|
|
|
|
|
|
|
product = Product(
|
2024-01-21 22:06:06 +00:00
|
|
|
"product",
|
|
|
|
"image.png",
|
|
|
|
"description",
|
|
|
|
10.00,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
datetime.now(),
|
|
|
|
1
|
2024-01-10 23:44:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Tests a new product can be created
|
2024-01-21 22:06:06 +00:00
|
|
|
|
|
|
|
|
2024-01-10 23:44:59 +00:00
|
|
|
def test_create_product():
|
|
|
|
db = ProductController()
|
|
|
|
db.create(product)
|
|
|
|
|
2024-01-21 22:22:29 +00:00
|
|
|
# Tests the database maintains integrity when we try
|
|
|
|
# and add a product with the same details
|
2024-01-21 22:06:06 +00:00
|
|
|
|
|
|
|
|
2024-01-15 18:34:06 +00:00
|
|
|
@pytest.mark.skip
|
2024-01-10 23:44:59 +00:00
|
|
|
def test_duplicate_product():
|
|
|
|
db = ProductController()
|
|
|
|
with pytest.raises(sqlite3.IntegrityError):
|
|
|
|
db.create(product)
|
|
|
|
|
2024-01-15 21:47:18 +00:00
|
|
|
# Tests that products can be refined by category
|
2024-01-21 22:06:06 +00:00
|
|
|
|
|
|
|
|
2024-01-15 21:47:18 +00:00
|
|
|
def test_search_category():
|
|
|
|
db = ProductController()
|
|
|
|
|
2024-01-21 22:06:06 +00:00
|
|
|
# Check each category for correct amount of test products
|
|
|
|
assert len(db.read_all("Car Parts")) == 9 + 1 # Added in previous test
|
2024-01-15 21:47:18 +00:00
|
|
|
assert len(db.read_all("Books")) == 9
|
2024-01-21 22:22:29 +00:00
|
|
|
assert db.read_all("Phones") is None
|
2024-01-15 21:47:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Tests that products can be refined by search term
|
|
|
|
def test_search_term():
|
|
|
|
db = ProductController()
|
|
|
|
|
|
|
|
# Check each search term for correct amount of test products
|
|
|
|
assert len(db.read_all(search_term="test")) == 33
|
|
|
|
assert len(db.read_all("Car Parts", "test")) == 9
|
|
|
|
assert len(db.read_all(search_term="product")) == 1
|
2024-01-21 22:22:29 +00:00
|
|
|
assert db.read_all(search_term="not_test") is None
|
2024-01-15 21:47:18 +00:00
|
|
|
|
2024-01-10 23:44:59 +00:00
|
|
|
# Test we the same product details get returned from the database
|
2024-01-21 22:06:06 +00:00
|
|
|
|
|
|
|
|
2024-01-10 23:44:59 +00:00
|
|
|
def test_read_product():
|
|
|
|
db = ProductController()
|
|
|
|
|
|
|
|
# Test the same product is returned
|
|
|
|
new_product = db.read("product")
|
|
|
|
assert isinstance(new_product, list)
|
|
|
|
assert isinstance(new_product[0], Product)
|
|
|
|
|
|
|
|
# Update the ID on the item as database assigns new id
|
|
|
|
product.id = new_product[0].id
|
|
|
|
assert new_product[0].__dict__ == product.__dict__
|