From 007fd07151cf5e6697f0374cce82f9b4b1958cff Mon Sep 17 00:00:00 2001 From: nboyse Date: Tue, 6 Feb 2024 13:44:47 +0000 Subject: [PATCH] add unit test for eu data --- reports/reports/eu_data.py | 2 - reports/reports/quotas_cannot_be_used.py | 1 - reports/tests/test_eu_data_report.py | 180 +++++++++++++++++++++++ reports/views.py | 18 ++- 4 files changed, 190 insertions(+), 11 deletions(-) create mode 100644 reports/tests/test_eu_data_report.py diff --git a/reports/reports/eu_data.py b/reports/reports/eu_data.py index e4a274c5cf..e966f43835 100644 --- a/reports/reports/eu_data.py +++ b/reports/reports/eu_data.py @@ -39,7 +39,6 @@ def headers(self) -> [dict]: for header in self.headers_list ] - def row(self, row) -> [dict]: return { field.replace("_", " ").capitalize(): str(getattr(row, field, None)) @@ -55,4 +54,3 @@ def rows(self) -> [dict]: def query(self): return EUDataModel.objects.all().order_by("goods_code") - diff --git a/reports/reports/quotas_cannot_be_used.py b/reports/reports/quotas_cannot_be_used.py index 7a19dd9547..97521ef155 100644 --- a/reports/reports/quotas_cannot_be_used.py +++ b/reports/reports/quotas_cannot_be_used.py @@ -1,6 +1,5 @@ import datetime -from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.db.models import Exists, Q from reports.reports.base_table import ReportBaseTable diff --git a/reports/tests/test_eu_data_report.py b/reports/tests/test_eu_data_report.py new file mode 100644 index 0000000000..0a88332879 --- /dev/null +++ b/reports/tests/test_eu_data_report.py @@ -0,0 +1,180 @@ +import pytest +from reports.models import EUDataModel +from reports.reports.eu_data import Report as EUDataReport + + +@pytest.fixture +def eu_data_report(): + return EUDataReport() + + +@pytest.fixture +def mock_data(): + return [ + EUDataModel( + goods_code="123", + add_code="456", + order_no=None, + start_date=None, + end_date=None, + red_ind=None, + origin=None, + measure_type=None, + legal_base=None, + duty=None, + origin_code=None, + meas_type_code=None, + goods_nomenclature_exists=None, + geographical_area_exists=None, + measure_type_exists=None, + measure_exists=None, + ), + EUDataModel( + goods_code="789", + add_code="012", + order_no=None, + start_date=None, + end_date=None, + red_ind=None, + origin=None, + measure_type=None, + legal_base=None, + duty=None, + origin_code=None, + meas_type_code=None, + goods_nomenclature_exists=None, + geographical_area_exists=None, + measure_type_exists=None, + measure_exists=None, + ), + ] + + +def test_report_name(eu_data_report): + assert eu_data_report.name == "Table report of EU data" + + +def test_report_description(eu_data_report): + assert ( + eu_data_report.description + == "Imported data from the EU tariff using the upload CSV feature" + ) + + +def test_report_enabled(eu_data_report): + assert eu_data_report.enabled + + +def test_report_headers(eu_data_report): + expected_headers = [ + {"field": field, "filter": "agTextColumnFilter"} + for field in [ + "Add code", + "Duty", + "End date", + "Geographical area exists", + "Goods code", + "Goods nomenclature exists", + "Legal base", + "Meas type code", + "Measure exists", + "Measure type", + "Measure type exists", + "Order no", + "Origin", + "Origin code", + "Red ind", + "Start date", + ] + ] + assert eu_data_report.headers() == expected_headers + + +@pytest.mark.parametrize( + "mock_row, expected_row", + [ + ( + EUDataModel( + goods_code="123", + add_code="456", + order_no=None, + start_date=None, + end_date=None, + red_ind=None, + origin=None, + measure_type=None, + legal_base=None, + duty=None, + origin_code=None, + meas_type_code=None, + goods_nomenclature_exists=None, + geographical_area_exists=None, + measure_type_exists=None, + measure_exists=None, + ), + { + "Goods code": "123", + "Add code": "456", + "Duty": "None", + "End date": "None", + "Geographical area exists": "None", + "Goods nomenclature exists": "None", + "Legal base": "None", + "Meas type code": "None", + "Measure exists": "None", + "Measure type": "None", + "Measure type exists": "None", + "Order no": "None", + "Origin": "None", + "Origin code": "None", + "Red ind": "None", + "Start date": "None", + }, + ), + ], +) +def test_report_row(eu_data_report, mock_row, expected_row): + assert eu_data_report.row(mock_row) == expected_row + + +def test_report_rows(eu_data_report, mock_data): + eu_data_report.query = lambda: mock_data + expected_rows = [ + { + "Goods code": "123", + "Add code": "456", + "Duty": "None", + "End date": "None", + "Geographical area exists": "None", + "Goods nomenclature exists": "None", + "Legal base": "None", + "Meas type code": "None", + "Measure exists": "None", + "Measure type": "None", + "Measure type exists": "None", + "Order no": "None", + "Origin": "None", + "Origin code": "None", + "Red ind": "None", + "Start date": "None", + }, + { + "Add code": "012", + "Duty": "None", + "End date": "None", + "Geographical area exists": "None", + "Goods code": "789", + "Goods nomenclature exists": "None", + "Legal base": "None", + "Meas type code": "None", + "Measure exists": "None", + "Measure type": "None", + "Measure type exists": "None", + "Order no": "None", + "Origin": "None", + "Origin code": "None", + "Red ind": "None", + "Start date": "None", + }, + ] + assert eu_data_report.rows() == expected_rows diff --git a/reports/views.py b/reports/views.py index 5b10aac02f..77cf51d0c4 100644 --- a/reports/views.py +++ b/reports/views.py @@ -89,20 +89,22 @@ def export_report_to_csv(request, report_slug, current_tab=None): headers = ( report_instance.headers() if hasattr(report_instance, "headers") else None ) - rows = ( - report_instance.rows() - if hasattr(report_instance, "rows") - else None - ) + rows = report_instance.rows() if hasattr(report_instance, "rows") else None writer = csv.writer(response) # For ag grid reports if hasattr(report_instance, "headers_list"): - header_row = [header.replace("_", " ").capitalize() for header in report_instance.headers_list] + header_row = [ + header.replace("_", " ").capitalize() + for header in report_instance.headers_list + ] writer.writerow(header_row) for row in report_instance.rows(): - data_row = [str(row[header.replace("_", " ").capitalize()]) for header in report_instance.headers_list] + data_row = [ + str(row[header.replace("_", " ").capitalize()]) + for header in report_instance.headers_list + ] writer.writerow(data_row) # For govuk table reports @@ -110,7 +112,7 @@ def export_report_to_csv(request, report_slug, current_tab=None): writer.writerow([header.get("text", None) for header in headers]) for row in rows: writer.writerow([column["text"] for column in row]) - + # For charts else: writer.writerow(["Date", "Data"])