From 0434d85ddbcdfe247c0584f2f6ab1557b918388f Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 23 Feb 2024 22:07:57 +0000 Subject: [PATCH] #11 Created product filter test :) --- tests/endtoend/use_cases_test.py | 62 ++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/tests/endtoend/use_cases_test.py b/tests/endtoend/use_cases_test.py index d62a04f..614e6a4 100644 --- a/tests/endtoend/use_cases_test.py +++ b/tests/endtoend/use_cases_test.py @@ -4,6 +4,64 @@ from tests.base_test import TestBase class TestFunctionalRequirements(TestBase): - def test_use_case(self, test_client: FlaskClient): - response = test_client.get('/products/Car Parts') + def test_product_filters(self, test_client: FlaskClient): + 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 + + # 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)