WMGZON/tests/unit/database/test_products.py

66 lines
2.0 KiB
Python
Raw Normal View History

from tests.unit.database.database_test_class import TestDatabase
from datetime import datetime
from controllers.database.product import ProductController
from models.products.product import Product
product = Product(
"product",
"brake-disks.bmp",
"description",
10.00,
1,
1,
datetime.now(),
1
)
class TestProduct(TestDatabase):
""" Class that controls the
testing of the WMGZON database
"""
def test_create_product(self):
"""Tests a new product can be created"""
db = ProductController()
db.create(product)
def test_duplicate_product(self):
""" Tests the database maintains integrity when we try
and add a product with the same detail
"""
self.test_create_product()
def test_search_category(self):
""" Tests that products can be refined by category """
db = ProductController()
# Check each category for correct amount of test products
assert len(db.read_all("Car Parts")) == 9 + \
2 # Added in previous tests
assert len(db.read_all("Books")) == 9
assert db.read_all("Phones") is None
def test_search_term(self):
""" Tests that products can be refined by search term"""
db = ProductController()
# Check each search term for correct amount of test products
assert len(db.read_all(search_term="Alloy")) == 9
assert len(db.read_all("Car Parts", "tur")) == 2
assert len(db.read_all(search_term="fold")) == 8
assert db.read_all(search_term="Twin") is None
def test_read_product(self):
""" Test we the same product details get returned from the database """
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__