From 75e7ad5994e5d3af3561350c3c17c6a3660a27cf Mon Sep 17 00:00:00 2001 From: Luke Else Date: Wed, 10 Jan 2024 23:44:59 +0000 Subject: [PATCH] Added product tests --- controllers/database/database.py | 4 +++- controllers/database/product.py | 6 ++--- models/products/product.py | 19 ++++++++++++++- scripts/create_tables.sql | 4 +++- tests/database/test_products.py | 40 ++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 tests/database/test_products.py diff --git a/controllers/database/database.py b/controllers/database/database.py index 6bc410d..b43e1b1 100644 --- a/controllers/database/database.py +++ b/controllers/database/database.py @@ -17,7 +17,9 @@ class DatabaseController(ABC): def __init__(self): self._conn = None 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: # Close the connection if still open if self._conn: diff --git a/controllers/database/product.py b/controllers/database/product.py index 81cd436..8a8c97e 100644 --- a/controllers/database/product.py +++ b/controllers/database/product.py @@ -2,7 +2,7 @@ from .database import DatabaseController from models.products.product import Product 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): super().__init__() @@ -20,7 +20,7 @@ class ProductController(DatabaseController): ] 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 ) self._conn.commit() @@ -46,7 +46,7 @@ class ProductController(DatabaseController): for product in rows: params = dict(zip(self.FIELDS, product)) obj = self.new_instance(Product, params) - products.push(obj) + products.append(obj) return products diff --git a/models/products/product.py b/models/products/product.py index 34aaea8..ed65af6 100644 --- a/models/products/product.py +++ b/models/products/product.py @@ -10,8 +10,25 @@ class Product: self.image = "/static/assets/wmgzon.png" self.description = "" self.cost = 0.0 - self.category = "" + self.category = 0 self.sellerID = 0 self.postedDate = datetime.now() 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 \ No newline at end of file diff --git a/scripts/create_tables.sql b/scripts/create_tables.sql index b9e1ec9..5a582c2 100644 --- a/scripts/create_tables.sql +++ b/scripts/create_tables.sql @@ -34,7 +34,9 @@ CREATE TABLE IF NOT EXISTS Products ( categoryID INTEGER NOT NULL REFERENCES Categories (id) ON DELETE CASCADE - ON UPDATE NO ACTION + ON UPDATE NO ACTION, + postedDate TIMESTAMP, + quantityAvailable INTEGER DEFAULT 0 ); CREATE TABLE IF NOT EXISTS Orders ( diff --git a/tests/database/test_products.py b/tests/database/test_products.py new file mode 100644 index 0000000..84c8ab2 --- /dev/null +++ b/tests/database/test_products.py @@ -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__