Started on making functional unit tests for endpoints
This commit is contained in:
0
tests/unit/__init__.py
Normal file
0
tests/unit/__init__.py
Normal file
3
tests/unit/database/__init__.py
Normal file
3
tests/unit/database/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
# Ensure test environment is set before using
|
||||
# Runs the database creation scripts
|
||||
import scripts.create_database
|
67
tests/unit/database/test_products.py
Normal file
67
tests/unit/database/test_products.py
Normal file
@ -0,0 +1,67 @@
|
||||
import pytest
|
||||
import sqlite3
|
||||
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
|
||||
)
|
||||
|
||||
# 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():
|
||||
test_create_product()
|
||||
|
||||
# Tests that products can be refined by category
|
||||
|
||||
|
||||
def test_search_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
|
||||
|
||||
|
||||
# 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__
|
74
tests/unit/database/test_users.py
Normal file
74
tests/unit/database/test_users.py
Normal file
@ -0,0 +1,74 @@
|
||||
import pytest
|
||||
import sqlite3
|
||||
from controllers.database.user import UserController
|
||||
from models.users.customer import Customer
|
||||
from models.users.seller import Seller
|
||||
|
||||
customer = Customer(
|
||||
"testcustomer",
|
||||
"Password1",
|
||||
"firstname",
|
||||
"lastname",
|
||||
"test@test",
|
||||
"123456789"
|
||||
)
|
||||
|
||||
seller = Seller(
|
||||
"testseller",
|
||||
"Password1",
|
||||
"firstname",
|
||||
"lastname",
|
||||
"seller@seller",
|
||||
"987654321"
|
||||
)
|
||||
|
||||
# Tests a new user can be created
|
||||
|
||||
|
||||
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):
|
||||
db.create(customer)
|
||||
|
||||
# Test we the same user details get returned from the database
|
||||
|
||||
|
||||
def test_read_user():
|
||||
db = UserController()
|
||||
|
||||
# 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__
|
||||
|
||||
|
||||
# Tests a new seller can be created
|
||||
def test_create_seller():
|
||||
db = UserController()
|
||||
db.create(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__
|
31
tests/unit/general/test_env.py
Normal file
31
tests/unit/general/test_env.py
Normal file
@ -0,0 +1,31 @@
|
||||
from os import environ
|
||||
from warnings import warn
|
||||
from tests import old_env
|
||||
|
||||
# Tests environment variables used within the projects domain are
|
||||
# set in the correct environment
|
||||
|
||||
VARS = ['ENVIRON', 'APPSECRET', 'FILESTORE']
|
||||
|
||||
ENV_STATES = ['test', 'prod']
|
||||
|
||||
|
||||
def test_env_vars():
|
||||
""" Test that required environment variables are set
|
||||
ahead of runtime
|
||||
"""
|
||||
for var in VARS:
|
||||
env = environ.get(var)
|
||||
|
||||
# Check to see what variable we are comparing
|
||||
if env is None:
|
||||
warn(f"Variable {var} is not set!")
|
||||
|
||||
|
||||
def test_environment_var_state():
|
||||
""" Tests that the 'ENVIRON' Environment variable
|
||||
is in a correct state
|
||||
"""
|
||||
var = old_env
|
||||
assert var is not None
|
||||
assert (var in ENV_STATES)
|
11
tests/unit/general/test_pep8.py
Normal file
11
tests/unit/general/test_pep8.py
Normal file
@ -0,0 +1,11 @@
|
||||
import pycodestyle
|
||||
|
||||
# Tests files to ensure they conform to pep8 standards
|
||||
|
||||
|
||||
def test_pep8_conformance():
|
||||
"""Test that we conform to PEP8."""
|
||||
pep8style = pycodestyle.StyleGuide()
|
||||
dirs = ["./controllers", "./models", "./scripts", "./tests", "./utils"]
|
||||
result = pep8style.check_files(dirs)
|
||||
assert result.total_errors == 0
|
Reference in New Issue
Block a user