#11 Created product filter test :)

This commit is contained in:
Luke Else 2024-02-23 22:07:57 +00:00
parent 7a9bc61d04
commit 0434d85ddb

View File

@ -4,6 +4,64 @@ from tests.base_test import TestBase
class TestFunctionalRequirements(TestBase): class TestFunctionalRequirements(TestBase):
def test_use_case(self, test_client: FlaskClient): def test_product_filters(self, test_client: FlaskClient):
response = test_client.get('/products/Car Parts') base_url = '/products/Car Part?filter={{FILTER}}'
# Make get request for all products in car parts
response = test_client.get(
base_url.replace("{{FILTER}}", "Price: High -> Low")
)
assert response.status_code == 200 assert response.status_code == 200
# Extract first and last product
first, last = self.get_first_and_last_product(response.data)
# Get Prices of each
first_cost = float(self.get_tag_value(
str(first), "product-price").replace('£', '')
)
last_cost = float(self.get_tag_value(
str(last), "product-price").replace('£', '')
)
# Check filter is working
assert first_cost >= last_cost
# =============================================
# Test the reverse of the previous filter
# Get html data
response = test_client.get(
base_url.replace("{{FILTER}}", "Price: Low -> High")
)
assert response.status_code == 200
# Extract first and last product
first, last = self.get_first_and_last_product(response.data)
# Get Prices of each
first_cost = float(self.get_tag_value(
str(first), "product-price").replace('£', '')
)
last_cost = float(self.get_tag_value(
str(last), "product-price").replace('£', '')
)
# Check filter is working
assert first_cost <= last_cost
def get_tag_value(self, html: str, class_name: str) -> str:
""" Returns the value of a given tag in a html element """
soup = BeautifulSoup(html, features='html.parser')
return str(soup.find("div", {'class': class_name}).contents[0])
def get_first_and_last_product(self, html: str) -> (str, str):
""" Returns the first and last product on the page """
soup = BeautifulSoup(html, features='html.parser')
products = soup.findAll("a", {"class": "product"})
assert len(products) > 0
first = products[0]
last = products[-1]
return (first, last)