Added product tests

This commit is contained in:
Luke Else 2024-01-10 23:44:59 +00:00
parent babc1d6471
commit 75e7ad5994
5 changed files with 67 additions and 6 deletions

View File

@ -17,7 +17,9 @@ class DatabaseController(ABC):
def __init__(self): def __init__(self):
self._conn = None self._conn = None
try: try:
self._conn = sqlite3.connect(self.__sqlitefile) # Creates a connection and specifies a flag to parse all types back down into
# Python declared types e.g. date & time
self._conn = sqlite3.connect(self.__sqlitefile, detect_types=sqlite3.PARSE_DECLTYPES)
except sqlite3.Error as e: except sqlite3.Error as e:
# Close the connection if still open # Close the connection if still open
if self._conn: if self._conn:

View File

@ -2,7 +2,7 @@ from .database import DatabaseController
from models.products.product import Product from models.products.product import Product
class ProductController(DatabaseController): class ProductController(DatabaseController):
FIELDS = ['id', 'name', 'image', 'description', 'cost', 'category', 'sellerID', 'postedDate', 'quantity'] FIELDS = ['id', 'name', 'image', 'description', 'cost', 'category', 'sellerID', 'postedDate', 'quantityAvailable']
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -20,7 +20,7 @@ class ProductController(DatabaseController):
] ]
self._conn.execute( self._conn.execute(
"INSERT INTO Products (name, cost, image, description, category, sellerID, postedDate, quantityAvailable) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", "INSERT INTO Products (name, image, description, cost, categoryID, sellerID, postedDate, quantityAvailable) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
params params
) )
self._conn.commit() self._conn.commit()
@ -46,7 +46,7 @@ class ProductController(DatabaseController):
for product in rows: for product in rows:
params = dict(zip(self.FIELDS, product)) params = dict(zip(self.FIELDS, product))
obj = self.new_instance(Product, params) obj = self.new_instance(Product, params)
products.push(obj) products.append(obj)
return products return products

View File

@ -10,8 +10,25 @@ class Product:
self.image = "/static/assets/wmgzon.png" self.image = "/static/assets/wmgzon.png"
self.description = "" self.description = ""
self.cost = 0.0 self.cost = 0.0
self.category = "" self.category = 0
self.sellerID = 0 self.sellerID = 0
self.postedDate = datetime.now() self.postedDate = datetime.now()
self.quantityAvailable = 0 self.quantityAvailable = 0
'''
Class constructor to instatiate a customer object
No additional properties are assigned to the customer
'''
def __init__(self, name: str, image: str, description: str, cost: float, category: int,
seller_id: int, posted_date: datetime, quantity_available: int):
self.id = 0
self.name = name
self.image = image
self.description = description
self.cost = cost
self.category = category
self.sellerID = seller_id
self.postedDate = posted_date
self.quantityAvailable = quantity_available

View File

@ -34,7 +34,9 @@ CREATE TABLE IF NOT EXISTS Products (
categoryID INTEGER NOT NULL categoryID INTEGER NOT NULL
REFERENCES Categories (id) REFERENCES Categories (id)
ON DELETE CASCADE ON DELETE CASCADE
ON UPDATE NO ACTION ON UPDATE NO ACTION,
postedDate TIMESTAMP,
quantityAvailable INTEGER DEFAULT 0
); );
CREATE TABLE IF NOT EXISTS Orders ( CREATE TABLE IF NOT EXISTS Orders (

View File

@ -0,0 +1,40 @@
import pytest
import sqlite3
from datetime import datetime
from controllers.database.product import ProductController
from models.products.product import Product
product = Product(
"product",
"image.png",
"description",
10.00,
1,
1,
datetime.now(),
1
)
# Tests a new product can be created
def test_create_product():
db = ProductController()
db.create(product)
# Tests the database maintains integrity when we try and add a product with the same details
def test_duplicate_product():
db = ProductController()
with pytest.raises(sqlite3.IntegrityError):
db.create(product)
# Test we the same product details get returned from the database
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__