#11 REFACTOR: Moved all tests into test classes
This commit is contained in:
8
tests/unit/database/database_test_class.py
Normal file
8
tests/unit/database/database_test_class.py
Normal file
@ -0,0 +1,8 @@
|
||||
from tests.base_test import TestBase
|
||||
|
||||
|
||||
class TestDatabase(TestBase):
|
||||
""" Class that controls the
|
||||
testing of the WMGZON database
|
||||
"""
|
||||
pass
|
@ -1,5 +1,4 @@
|
||||
import pytest
|
||||
import sqlite3
|
||||
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
|
||||
@ -15,53 +14,52 @@ product = Product(
|
||||
1
|
||||
)
|
||||
|
||||
# Tests a new product can be created
|
||||
|
||||
class TestProduct(TestDatabase):
|
||||
""" Class that controls the
|
||||
testing of the WMGZON database
|
||||
"""
|
||||
|
||||
def test_create_product():
|
||||
db = ProductController()
|
||||
db.create(product)
|
||||
def test_create_product(self):
|
||||
"""Tests a new product can be created"""
|
||||
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(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()
|
||||
|
||||
def test_duplicate_product():
|
||||
test_create_product()
|
||||
# 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
|
||||
|
||||
# Tests that products can be refined by category
|
||||
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_search_category():
|
||||
db = ProductController()
|
||||
def test_read_product(self):
|
||||
""" Test we the same product details get returned from the database """
|
||||
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
|
||||
# Test the same product is returned
|
||||
new_product = db.read("product")
|
||||
assert isinstance(new_product, list)
|
||||
assert isinstance(new_product[0], Product)
|
||||
|
||||
|
||||
# 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="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
|
||||
|
||||
# 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__
|
||||
# Update the ID on the item as database assigns new id
|
||||
product.id = new_product[0].id
|
||||
assert new_product[0].__dict__ == product.__dict__
|
||||
|
@ -1,5 +1,6 @@
|
||||
import pytest
|
||||
import sqlite3
|
||||
from tests.unit.database.database_test_class import TestDatabase
|
||||
from controllers.database.user import UserController
|
||||
from models.users.customer import Customer
|
||||
from models.users.seller import Seller
|
||||
@ -22,53 +23,53 @@ seller = Seller(
|
||||
"987654321"
|
||||
)
|
||||
|
||||
# Tests a new user can be created
|
||||
|
||||
class TestUsers(TestDatabase):
|
||||
""" Class to encapsulate all of the user
|
||||
datbase tests
|
||||
"""
|
||||
|
||||
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):
|
||||
def test_create_user(self):
|
||||
""" Tests a new user can be created """
|
||||
db = UserController()
|
||||
db.create(customer)
|
||||
|
||||
# Test we the same user details get returned from the database
|
||||
def test_duplicate_user(self):
|
||||
""" Tests the database maintains integrity when we try
|
||||
and add a user with the same details
|
||||
"""
|
||||
db = UserController()
|
||||
with pytest.raises(sqlite3.IntegrityError):
|
||||
db.create(customer)
|
||||
|
||||
def test_read_user(self):
|
||||
""" Test we the same user details get returned from the database """
|
||||
db = UserController()
|
||||
|
||||
def test_read_user():
|
||||
db = UserController()
|
||||
# Test the same user is returned
|
||||
user = db.read(customer.username)
|
||||
assert isinstance(user, Customer)
|
||||
|
||||
# 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__
|
||||
|
||||
# Update the ID on the item as database assigns new id
|
||||
customer.id = user.id
|
||||
assert user.__dict__ == customer.__dict__
|
||||
def test_create_seller(self):
|
||||
""" Tests a new seller can be created """
|
||||
db = UserController()
|
||||
db.create(seller)
|
||||
|
||||
def test_read_seller(self):
|
||||
""" Test that the same seller details get
|
||||
returned from the database
|
||||
"""
|
||||
db = UserController()
|
||||
|
||||
# Tests a new seller can be created
|
||||
def test_create_seller():
|
||||
db = UserController()
|
||||
db.create(seller)
|
||||
# Test the same user is returned
|
||||
user = db.read(seller.username)
|
||||
assert isinstance(user, 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__
|
||||
# Update the ID on the item as database assigns new id
|
||||
seller.id = user.id
|
||||
user.store = ""
|
||||
assert user.__dict__ == seller.__dict__
|
||||
|
Reference in New Issue
Block a user