Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmills-DIT committed Nov 13, 2024
1 parent 5e44da3 commit e308050
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
35 changes: 14 additions & 21 deletions exporter/quotas/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def normalise_loglevel(loglevel):


class QuotaExport:
"""
Runs the export command against TAP data to extract quota CSV data
"""
"""Runs the export command against TAP data to extract quota CSV data."""

def __init__(self, target_file: NamedTemporaryFile):
# self.rows = []
Expand All @@ -43,11 +41,10 @@ def __init__(self, target_file: NamedTemporaryFile):
@staticmethod
def csv_headers():
"""
produces a list of headers for the CSV
Produces a list of headers for the CSV.
Returns:
list: list of header names
"""
quota_headers = [
"quota_definition__sid",
Expand Down Expand Up @@ -75,11 +72,10 @@ def csv_headers():

def run(self):
"""
produces data for the quota export CSV, from the TAP database.
Produces data for the quota export CSV, from the TAP database.
Returns:
None: Operations performed and stored within the NamedTemporaryFile
"""

quotas = QuotaDefinition.objects.latest_approved().filter(
Expand Down Expand Up @@ -120,7 +116,8 @@ def run(self):
@staticmethod
def get_geographical_areas_and_exclusions(quota):
"""
returns a tuple of geographical areas and exclusions associated with a Quota
Returns a tuple of geographical areas and exclusions associated with a
Quota.
Args:
quota: the quota to be queried
Expand Down Expand Up @@ -163,14 +160,13 @@ def get_geographical_areas_and_exclusions(quota):
@staticmethod
def get_monetary_unit(quota):
"""
returns the monetary unit associated with a Quota as a string
Returns the monetary unit associated with a Quota as a string.
Args:
quota: the quota to be queried
Returns:
str or None: Monetary unit as string or None
"""
monetary_unit = None
if quota.monetary_unit:
Expand All @@ -182,14 +178,13 @@ def get_monetary_unit(quota):
@staticmethod
def get_measurement_unit(quota):
"""
returns the measurement unit associated with a Quota as a string
Returns the measurement unit associated with a Quota as a string.
Args:
quota: the quota to be queried
Returns:
str or None: Measurement unit as string or None
"""
if quota.measurement_unit:
measurement_unit_description = f"{quota.measurement_unit.description}"
Expand All @@ -204,7 +199,7 @@ def get_measurement_unit(quota):
@staticmethod
def get_api_query_date(quota):
"""
returns the most appropriate date for querying the HMRC API
Returns the most appropriate date for querying the HMRC API.
Dates are checked against current date and collected, the oldest of
the dates is used as the API query date. Typically, this wil be today's date
Expand All @@ -218,7 +213,6 @@ def get_api_query_date(quota):
Returns:
date or none: a date if available or none if quota is in the future
"""
api_query_dates = []

Expand Down Expand Up @@ -261,15 +255,14 @@ def get_api_query_date(quota):
@staticmethod
def get_associated_measures(quota):
"""
returns associated measures for the quota
Returns associated measures for the quota.
Args:
quota: The quota to be queried
Returns:
TrackedModelQuerySet(Measures): A set of measures associated with the
provided quota
"""
measures = (
Measure.objects.latest_approved()
Expand All @@ -291,15 +284,14 @@ def get_associated_measures(quota):

def get_goods_nomenclature_item_ids(self, quota):
"""
collects associated item_ids for a quota
Collects associated item_ids for a quota.
Args:
quota: The quota to be queried
Returns:
list(str): list of strings each containing the associated item_id for a
measure
"""
item_ids = []
for measure in self.get_associated_measures(quota):
Expand All @@ -309,8 +301,9 @@ def get_goods_nomenclature_item_ids(self, quota):

def get_goods_nomenclature_headings(self, item_ids: List[str]):
"""
returns a string representing the headings and descriptions for measures
associated with a quota. Headings are at the 4 digit level, e.g. 1234000000
Returns a string representing the headings and descriptions for measures
associated with a quota. Headings are at the 4 digit level, e.g.
1234000000.
Args:
item_ids: list(str) : a list of strings representing item_ids
Expand Down Expand Up @@ -340,7 +333,7 @@ def get_goods_nomenclature_headings(self, item_ids: List[str]):
@staticmethod
def get_goods_nomenclature_description(item_id):
"""
returns the description associated with an item_id
Returns the description associated with an item_id.
Args:
item_id: the item_id to be queried
Expand Down
36 changes: 18 additions & 18 deletions exporter/tests/quotas_export/test_quotas_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def test_get_geographical_areas_and_exclusions(self):
)
for origin in quota.order_number.quotaordernumberorigin_set.all():
assert (
origin.geographical_area.descriptions.all().last().description
in geo_areas
origin.geographical_area.descriptions.all().last().description
in geo_areas
)
assert (
exclusion.excluded_geographical_area.descriptions.all().last().description
in geo_area_exclusions
exclusion.excluded_geographical_area.descriptions.all().last().description
in geo_area_exclusions
)

def test_get_monetary_unit_populated(self):
Expand All @@ -66,8 +66,8 @@ def test_get_monetary_unit_populated(self):

target = self.get_target()
assert (
target.get_monetary_unit(quota)
== f"{quota.monetary_unit.description} ({quota.monetary_unit.code})"
target.get_monetary_unit(quota)
== f"{quota.monetary_unit.description} ({quota.monetary_unit.code})"
)

def test_get_monetary_unit_blank(self):
Expand Down Expand Up @@ -296,18 +296,18 @@ def test_run(self):
content = open(ntf.name, "r").read()

headers_str = (
"quota_definition__sid,quota__order_number," +
"quota__geographical_areas," +
"quota__geographical_area_exclusions," +
"quota__headings," +
"quota__commodities," +
"quota__measurement_unit," +
"quota__monetary_unit," +
"quota_definition__description," +
"quota_definition__validity_start_date," +
"quota_definition__validity_end_date," +
"quota_definition__initial_volume," +
"api_query_date"
"quota_definition__sid,quota__order_number,"
+ "quota__geographical_areas,"
+ "quota__geographical_area_exclusions,"
+ "quota__headings,"
+ "quota__commodities,"
+ "quota__measurement_unit,"
+ "quota__monetary_unit,"
+ "quota_definition__description,"
+ "quota_definition__validity_start_date,"
+ "quota_definition__validity_end_date,"
+ "quota_definition__initial_volume,"
+ "api_query_date"
)

assert headers_str in content
Expand Down

0 comments on commit e308050

Please sign in to comment.