Started on making functional unit tests for endpoints
This commit is contained in:
parent
e45ec4b217
commit
ee33965baf
7
app.py
7
app.py
@ -3,12 +3,12 @@ from os import environ
|
|||||||
from controllers.web.endpoints import blueprint
|
from controllers.web.endpoints import blueprint
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Main entrypoint for Flask application.
|
Main entrypoint for Flask application.
|
||||||
Initialises any components that are needed at runtime such as the
|
Initialises any components that are needed at runtime such as the
|
||||||
Database manager...
|
Database manager...
|
||||||
'''
|
'''
|
||||||
|
|
||||||
app = Flask(__name__)
|
app: Flask = Flask(__name__)
|
||||||
|
|
||||||
# Set app secret key to sign session cookies
|
# Set app secret key to sign session cookies
|
||||||
secret_key = environ.get("APPSECRET")
|
secret_key = environ.get("APPSECRET")
|
||||||
@ -24,4 +24,3 @@ app.register_blueprint(blueprint)
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True, host="0.0.0.0", port=8080)
|
app.run(debug=True, host="0.0.0.0", port=8080)
|
||||||
|
|
||||||
|
0
tests/functional/__init__.py
Normal file
0
tests/functional/__init__.py
Normal file
41
tests/functional/test_homepage.py
Normal file
41
tests/functional/test_homepage.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
from app import app
|
||||||
|
from flask.testing import FlaskClient
|
||||||
|
import pytest
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def test_client() -> FlaskClient:
|
||||||
|
""" Test that required environment variables are set
|
||||||
|
ahead of runtime
|
||||||
|
"""
|
||||||
|
os.environ['CONFIG_TYPE'] = 'config.TestingConfig'
|
||||||
|
|
||||||
|
with app.test_client() as testing_client:
|
||||||
|
with app.app_context():
|
||||||
|
yield testing_client
|
||||||
|
|
||||||
|
|
||||||
|
def test_homepage(test_client: FlaskClient):
|
||||||
|
""" Tests that the main homepage loads correctly
|
||||||
|
once the '/' endpoint is hit
|
||||||
|
"""
|
||||||
|
response = test_client.get('/')
|
||||||
|
assert response.status_code == 302
|
||||||
|
|
||||||
|
response = test_client.get('/products')
|
||||||
|
assert response.status_code == 308
|
||||||
|
|
||||||
|
|
||||||
|
def test_products(test_client: FlaskClient):
|
||||||
|
""" Tests that a product page is displayed when
|
||||||
|
hitting one of the product endpoints
|
||||||
|
"""
|
||||||
|
response = test_client.get('/products/2')
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
response = test_client.get('/products/50')
|
||||||
|
assert response.status_code == 302
|
||||||
|
|
||||||
|
response = test_client.get('/products/Books')
|
||||||
|
assert response.status_code == 200
|
0
tests/unit/__init__.py
Normal file
0
tests/unit/__init__.py
Normal file
@ -1,3 +1,3 @@
|
|||||||
# Ensure test environment is set before using
|
# Ensure test environment is set before using
|
||||||
# Runs the database creation scripts
|
# Runs the database creation scripts
|
||||||
import scripts.create_database
|
import scripts.create_database
|
@ -1,67 +1,67 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from controllers.database.product import ProductController
|
from controllers.database.product import ProductController
|
||||||
from models.products.product import Product
|
from models.products.product import Product
|
||||||
|
|
||||||
product = Product(
|
product = Product(
|
||||||
"product",
|
"product",
|
||||||
"brake-disks.bmp",
|
"brake-disks.bmp",
|
||||||
"description",
|
"description",
|
||||||
10.00,
|
10.00,
|
||||||
1,
|
1,
|
||||||
1,
|
1,
|
||||||
datetime.now(),
|
datetime.now(),
|
||||||
1
|
1
|
||||||
)
|
)
|
||||||
|
|
||||||
# Tests a new product can be created
|
# Tests a new product can be created
|
||||||
|
|
||||||
|
|
||||||
def test_create_product():
|
def test_create_product():
|
||||||
db = ProductController()
|
db = ProductController()
|
||||||
db.create(product)
|
db.create(product)
|
||||||
|
|
||||||
# Tests the database maintains integrity when we try
|
# Tests the database maintains integrity when we try
|
||||||
# and add a product with the same details
|
# and add a product with the same details
|
||||||
|
|
||||||
|
|
||||||
def test_duplicate_product():
|
def test_duplicate_product():
|
||||||
test_create_product()
|
test_create_product()
|
||||||
|
|
||||||
# Tests that products can be refined by category
|
# Tests that products can be refined by category
|
||||||
|
|
||||||
|
|
||||||
def test_search_category():
|
def test_search_category():
|
||||||
db = ProductController()
|
db = ProductController()
|
||||||
|
|
||||||
# Check each category for correct amount of test products
|
# 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("Car Parts")) == 9 + 2 # Added in previous tests
|
||||||
assert len(db.read_all("Books")) == 9
|
assert len(db.read_all("Books")) == 9
|
||||||
assert db.read_all("Phones") is None
|
assert db.read_all("Phones") is None
|
||||||
|
|
||||||
|
|
||||||
# Tests that products can be refined by search term
|
# Tests that products can be refined by search term
|
||||||
def test_search_term():
|
def test_search_term():
|
||||||
db = ProductController()
|
db = ProductController()
|
||||||
|
|
||||||
# Check each search term for correct amount of test products
|
# Check each search term for correct amount of test products
|
||||||
assert len(db.read_all(search_term="Alloy")) == 9
|
assert len(db.read_all(search_term="Alloy")) == 9
|
||||||
assert len(db.read_all("Car Parts", "tur")) == 2
|
assert len(db.read_all("Car Parts", "tur")) == 2
|
||||||
assert len(db.read_all(search_term="fold")) == 8
|
assert len(db.read_all(search_term="fold")) == 8
|
||||||
assert db.read_all(search_term="Twin") is None
|
assert db.read_all(search_term="Twin") is None
|
||||||
|
|
||||||
# Test we the same product details get returned from the database
|
# Test we the same product details get returned from the database
|
||||||
|
|
||||||
|
|
||||||
def test_read_product():
|
def test_read_product():
|
||||||
db = ProductController()
|
db = ProductController()
|
||||||
|
|
||||||
# Test the same product is returned
|
# Test the same product is returned
|
||||||
new_product = db.read("product")
|
new_product = db.read("product")
|
||||||
assert isinstance(new_product, list)
|
assert isinstance(new_product, list)
|
||||||
assert isinstance(new_product[0], Product)
|
assert isinstance(new_product[0], Product)
|
||||||
|
|
||||||
# Update the ID on the item as database assigns new id
|
# Update the ID on the item as database assigns new id
|
||||||
product.id = new_product[0].id
|
product.id = new_product[0].id
|
||||||
assert new_product[0].__dict__ == product.__dict__
|
assert new_product[0].__dict__ == product.__dict__
|
@ -1,74 +1,74 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from controllers.database.user import UserController
|
from controllers.database.user import UserController
|
||||||
from models.users.customer import Customer
|
from models.users.customer import Customer
|
||||||
from models.users.seller import Seller
|
from models.users.seller import Seller
|
||||||
|
|
||||||
customer = Customer(
|
customer = Customer(
|
||||||
"testcustomer",
|
"testcustomer",
|
||||||
"Password1",
|
"Password1",
|
||||||
"firstname",
|
"firstname",
|
||||||
"lastname",
|
"lastname",
|
||||||
"test@test",
|
"test@test",
|
||||||
"123456789"
|
"123456789"
|
||||||
)
|
)
|
||||||
|
|
||||||
seller = Seller(
|
seller = Seller(
|
||||||
"testseller",
|
"testseller",
|
||||||
"Password1",
|
"Password1",
|
||||||
"firstname",
|
"firstname",
|
||||||
"lastname",
|
"lastname",
|
||||||
"seller@seller",
|
"seller@seller",
|
||||||
"987654321"
|
"987654321"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Tests a new user can be created
|
# Tests a new user can be created
|
||||||
|
|
||||||
|
|
||||||
def test_create_user():
|
def test_create_user():
|
||||||
db = UserController()
|
db = UserController()
|
||||||
db.create(customer)
|
db.create(customer)
|
||||||
|
|
||||||
# Tests the database maintains integrity when we try
|
# Tests the database maintains integrity when we try
|
||||||
# and add a user with the same details
|
# and add a user with the same details
|
||||||
|
|
||||||
|
|
||||||
def test_duplicate_user():
|
def test_duplicate_user():
|
||||||
db = UserController()
|
db = UserController()
|
||||||
with pytest.raises(sqlite3.IntegrityError):
|
with pytest.raises(sqlite3.IntegrityError):
|
||||||
db.create(customer)
|
db.create(customer)
|
||||||
|
|
||||||
# Test we the same user details get returned from the database
|
# Test we the same user details get returned from the database
|
||||||
|
|
||||||
|
|
||||||
def test_read_user():
|
def test_read_user():
|
||||||
db = UserController()
|
db = UserController()
|
||||||
|
|
||||||
# Test the same user is returned
|
# Test the same user is returned
|
||||||
user = db.read(customer.username)
|
user = db.read(customer.username)
|
||||||
assert isinstance(user, Customer)
|
assert isinstance(user, Customer)
|
||||||
|
|
||||||
# Update the ID on the item as database assigns new id
|
# Update the ID on the item as database assigns new id
|
||||||
customer.id = user.id
|
customer.id = user.id
|
||||||
assert user.__dict__ == customer.__dict__
|
assert user.__dict__ == customer.__dict__
|
||||||
|
|
||||||
|
|
||||||
# Tests a new seller can be created
|
# Tests a new seller can be created
|
||||||
def test_create_seller():
|
def test_create_seller():
|
||||||
db = UserController()
|
db = UserController()
|
||||||
db.create(seller)
|
db.create(seller)
|
||||||
|
|
||||||
# Test that the same seller details get returned from the database
|
# Test that the same seller details get returned from the database
|
||||||
|
|
||||||
|
|
||||||
def test_read_seller():
|
def test_read_seller():
|
||||||
db = UserController()
|
db = UserController()
|
||||||
|
|
||||||
# Test the same user is returned
|
# Test the same user is returned
|
||||||
user = db.read(seller.username)
|
user = db.read(seller.username)
|
||||||
assert isinstance(user, Seller)
|
assert isinstance(user, Seller)
|
||||||
|
|
||||||
# Update the ID on the item as database assigns new id
|
# Update the ID on the item as database assigns new id
|
||||||
seller.id = user.id
|
seller.id = user.id
|
||||||
user.store = ""
|
user.store = ""
|
||||||
assert user.__dict__ == seller.__dict__
|
assert user.__dict__ == seller.__dict__
|
@ -1,11 +1,11 @@
|
|||||||
import pycodestyle
|
import pycodestyle
|
||||||
|
|
||||||
# Tests files to ensure they conform to pep8 standards
|
# Tests files to ensure they conform to pep8 standards
|
||||||
|
|
||||||
|
|
||||||
def test_pep8_conformance():
|
def test_pep8_conformance():
|
||||||
"""Test that we conform to PEP8."""
|
"""Test that we conform to PEP8."""
|
||||||
pep8style = pycodestyle.StyleGuide()
|
pep8style = pycodestyle.StyleGuide()
|
||||||
dirs = ["./controllers", "./models", "./scripts", "./tests", "./utils"]
|
dirs = ["./controllers", "./models", "./scripts", "./tests", "./utils"]
|
||||||
result = pep8style.check_files(dirs)
|
result = pep8style.check_files(dirs)
|
||||||
assert result.total_errors == 0
|
assert result.total_errors == 0
|
Loading…
Reference in New Issue
Block a user