From 17adf12bab3c7a45d6e3d2e8674a9736a33729c4 Mon Sep 17 00:00:00 2001 From: Lauren Qurashi Date: Thu, 28 Sep 2023 12:01:27 +0100 Subject: [PATCH] Add tests for Sort by --- measures/tests/test_views.py | 95 +++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 7 deletions(-) diff --git a/measures/tests/test_views.py b/measures/tests/test_views.py index 344e90735..7371f898f 100644 --- a/measures/tests/test_views.py +++ b/measures/tests/test_views.py @@ -26,6 +26,7 @@ from common.validators import UpdateType from common.views import TamatoListView from common.views import TrackedModelDetailMixin +from geo_areas.validators import AreaCode from measures.business_rules import ME70 from measures.constants import START from measures.constants import MeasureEditSteps @@ -1307,10 +1308,9 @@ def test_measure_form_wizard_create_measures( # Create measures returns a list of created measures measure_data = wizard.create_measures(form_data) measures = Measure.objects.filter(goods_nomenclature__in=[commodity1, commodity2]) - """ - In this implementation goods_nomenclature is a FK of Measure, so there is one measure - for each commodity specified in formset-commodities. + In this implementation goods_nomenclature is a FK of Measure, so there is + one measure for each commodity specified in formset-commodities. Verify that the expected measures were created. """ @@ -1535,10 +1535,7 @@ def test_measure_form_wizard_create_measures_with_tariff_suspension_action( # Create measures returns a list of created measures measure_data = wizard.create_measures(form_data) measures = Measure.objects.filter(goods_nomenclature=commodity1) - - """ - Verify that the expected measures were created. - """ + """Verify that the expected measures were created.""" assert len(measure_data) == 1 # Each created measure contains the supplied condition codes where DELETE=False @@ -2385,3 +2382,87 @@ def test_measure_list_redirects_to_search_with_no_params(valid_user_client): def test_measure_search_200(valid_user_client): response = valid_user_client.get(reverse("measure-ui-search")) assert response.status_code == 200 + + +def test_measures_list_sorting(valid_user_client, date_ranges): + # make measure types + type1 = factories.MeasureTypeFactory.create(sid="111") + type2 = factories.MeasureTypeFactory.create(sid="222") + type3 = factories.MeasureTypeFactory.create(sid="333") + + # Make geo_groups + # Erga Omnes + area_1 = factories.GeographicalAreaFactory.create( + area_code=AreaCode.GROUP, + area_id="1011", + ) + # North America + area_2 = factories.GeographicalAreaFactory.create( + area_code=AreaCode.GROUP, + area_id="2200", + ) + # European Union + area_3 = factories.GeographicalAreaFactory.create( + area_code=AreaCode.GROUP, + area_id="1013", + ) + + # make measures + measure1 = factories.MeasureFactory.create( + measure_type=type1, + geographical_area=area_2, + valid_between=date_ranges.no_end, + ) + measure2 = factories.MeasureFactory.create( + measure_type=type2, + geographical_area=area_3, + valid_between=date_ranges.earlier, + ) + measure3 = factories.MeasureFactory.create( + measure_type=type3, + geographical_area=area_1, + valid_between=date_ranges.later, + ) + + sort_by_list = [ + "?sort_by=sid&ordered=desc", + "?sort_by=sid&ordered=asc", + "?sort_by=measure_type&ordered=desc", + "?sort_by=measure_type&ordered=asc", + "?sort_by=start_date&ordered=desc", + "?sort_by=start_date&ordered=asc", + "?sort_by=end_date&ordered=desc", + "?sort_by=end_date&ordered=asc", + "?sort_by=geo_area&ordered=desc", + "?sort_by=geo_area&ordered=asc", + ] + + expected_order_list = [ + # sid desc, asc + [measure3.sid, measure2.sid, measure1.sid], + [measure1.sid, measure2.sid, measure3.sid], + # type desc, asc + [measure3.sid, measure2.sid, measure1.sid], + [measure1.sid, measure2.sid, measure3.sid], + # start date desc, asc + [measure3.sid, measure1.sid, measure2.sid], + [measure2.sid, measure1.sid, measure3.sid], + # end date desc, asc + [measure1.sid, measure3.sid, measure2.sid], + [measure2.sid, measure3.sid, measure1.sid], + # geo area desc, asc + [measure1.sid, measure2.sid, measure3.sid], + [measure3.sid, measure2.sid, measure1.sid], + ] + + for index, item in enumerate(sort_by_list): + url = reverse("measure-ui-list") + item + response = valid_user_client.get(url) + + assert response.status_code == 200 + + soup = BeautifulSoup(response.content.decode(response.charset), "html.parser") + measure_sids = [ + int(el.text) for el in soup.select(".govuk-table tbody tr td:nth-child(2)") + ] + assert measure_sids == expected_order_list[index]