Skip to content

Commit

Permalink
add unit test for eu data
Browse files Browse the repository at this point in the history
  • Loading branch information
nboyse committed Feb 6, 2024
1 parent a04c688 commit 007fd07
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 11 deletions.
2 changes: 0 additions & 2 deletions reports/reports/eu_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -55,4 +54,3 @@ def rows(self) -> [dict]:

def query(self):
return EUDataModel.objects.all().order_by("goods_code")

1 change: 0 additions & 1 deletion reports/reports/quotas_cannot_be_used.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
180 changes: 180 additions & 0 deletions reports/tests/test_eu_data_report.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 10 additions & 8 deletions reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,30 @@ 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
elif hasattr(report_instance, "headers"):
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"])
Expand Down

0 comments on commit 007fd07

Please sign in to comment.