diff options
author | Matthew Lemon <matt@matthewlemon.com> | 2021-05-16 19:52:28 +0100 |
---|---|---|
committer | Matthew Lemon <matt@matthewlemon.com> | 2021-05-16 19:52:28 +0100 |
commit | 2bb492949858b0753a68beba76fd70ace2a1492f (patch) | |
tree | 3fa6b50c7dbffb4a6c1cf37e1160b65b8b5e8776 | |
parent | 58e1ba750e34f876c5435ee519483ffd87913854 (diff) |
fixed broken test
-rw-r--r-- | datamaps/api/api.py | 2 | ||||
-rw-r--r-- | datamaps/plugins/dft/master.py | 94 | ||||
-rw-r--r-- | datamaps/tests/test_api.py | 19 |
3 files changed, 89 insertions, 26 deletions
diff --git a/datamaps/api/api.py b/datamaps/api/api.py index c9f9a64..6fcf4cf 100644 --- a/datamaps/api/api.py +++ b/datamaps/api/api.py @@ -35,5 +35,5 @@ def project_data_from_master_month_api(master_file: str, month: int, year: int): else: pass # TODO: raise exception here - m = Master(Quarter(quarter, year), master_file) + m = Master(Quarter(quarter, year), master_file, month) return m diff --git a/datamaps/plugins/dft/master.py b/datamaps/plugins/dft/master.py index 1eab1b7..ac893b8 100644 --- a/datamaps/plugins/dft/master.py +++ b/datamaps/plugins/dft/master.py @@ -11,13 +11,14 @@ from datamaps.core.temporal import Quarter from openpyxl import load_workbook -logger = logging.getLogger('bcompiler.utils') +logger = logging.getLogger("bcompiler.utils") class ProjectData: """ ProjectData class """ + def __init__(self, d: dict) -> None: """ :py:func:`OrderedDict` is easiest to get from project_data_from_master[x] @@ -37,7 +38,7 @@ class ProjectData: data = [item for item in self._data.items() if key in item[0]] if not data: raise KeyError("Sorry, there is no matching data") - return (data) + return data def pull_keys(self, input_iter: Iterable, flat=False) -> List[Tuple[Any, ...]]: """ @@ -46,19 +47,54 @@ class ProjectData: """ if flat is True: # search and replace troublesome EN DASH character - xs = [item for item in self._data.items() - for i in input_iter if item[0].strip().replace(unicodedata.lookup('EN DASH'), unicodedata.lookup('HYPHEN-MINUS')) == i] + xs = [ + item + for item in self._data.items() + for i in input_iter + if item[0] + .strip() + .replace( + unicodedata.lookup("EN DASH"), unicodedata.lookup("HYPHEN-MINUS") + ) + == i + ] xs = [_convert_str_date_to_object(x) for x in xs] - ts = sorted(xs, key=lambda x: input_iter.index(x[0].strip().replace(unicodedata.lookup('EN DASH'), unicodedata.lookup('HYPHEN-MINUS')))) + ts = sorted( + xs, + key=lambda x: input_iter.index( + x[0] + .strip() + .replace( + unicodedata.lookup("EN DASH"), + unicodedata.lookup("HYPHEN-MINUS"), + ) + ), + ) ts = [item[1] for item in ts] return ts else: - xs = [item for item in self._data.items() - for i in input_iter if item[0].replace(unicodedata.lookup('EN DASH'), unicodedata.lookup('HYPHEN-MINUS')) == i] - xs = [item for item in self._data.items() - for i in input_iter if item[0] == i] + xs = [ + item + for item in self._data.items() + for i in input_iter + if item[0].replace( + unicodedata.lookup("EN DASH"), unicodedata.lookup("HYPHEN-MINUS") + ) + == i + ] + xs = [ + item for item in self._data.items() for i in input_iter if item[0] == i + ] xs = [_convert_str_date_to_object(x) for x in xs] - ts = sorted(xs, key=lambda x: input_iter.index(x[0].replace(unicodedata.lookup('EN DASH'), unicodedata.lookup('HYPHEN-MINUS')))) + ts = sorted( + xs, + key=lambda x: input_iter.index( + x[0].replace( + unicodedata.lookup("EN DASH"), + unicodedata.lookup("HYPHEN-MINUS"), + ) + ), + ) return ts def __repr__(self): @@ -69,7 +105,7 @@ def _convert_str_date_to_object(d_str: tuple) -> Tuple[str, Optional[datetime.da try: if re.match(DATE_REGEX_4, d_str[1]): try: - ds = d_str[1].split('-') + ds = d_str[1].split("-") return (d_str[0], datetime.date(int(ds[0]), int(ds[1]), int(ds[2]))) except TypeError: return d_str @@ -114,8 +150,10 @@ class Master: filename = m1.filename ..etc """ - def __init__(self, quarter: Quarter, path: str) -> None: + + def __init__(self, quarter: Quarter, path: str, declared_month=None) -> None: self._quarter = quarter + self._declared_month = declared_month self.path = path self._data = project_data_from_master(self.path) self._project_titles = [item for item in self.data.keys()] @@ -162,21 +200,35 @@ class Master: """ Returns the ``Month`` object associated with the ``Master``. """ - breakpoint() - return [m.month for m in self.quarter.months] - + months = { + 1: "January", + 2: "February", + 3: "March", + 4: "April", + 5: "May", + 6: "June", + 7: "July", + 8: "August", + 9: "September", + 10: "October", + 11: "November", + 12: "December", + } + return [ + m.month + for m in self.quarter.months + if m.month == months[self._declared_month] + ][0] @property def filename(self): - """The filename of the master xlsx file, e.g. ``master_1_2017.xlsx``. - """ + """The filename of the master xlsx file, e.g. ``master_1_2017.xlsx``.""" p = Path(self.path) return p.name @property def projects(self): - """A list of project titles derived from the master xlsx. - """ + """A list of project titles derived from the master xlsx.""" return self._project_titles def duplicate_keys(self, to_log=None): @@ -203,7 +255,9 @@ class Master: dups.add(x) if to_log and len(dups) > 0: for x in dups: - logger.warning(f"{self.path} contains duplicate key: \"{x}\". Masters cannot contain duplicate keys. Rename them.") + logger.warning( + f'{self.path} contains duplicate key: "{x}". Masters cannot contain duplicate keys. Rename them.' + ) return True elif to_log and len(dups) == 0: logger.info(f"No duplicate keys in {self.path}") diff --git a/datamaps/tests/test_api.py b/datamaps/tests/test_api.py index 347ea4a..0ded406 100644 --- a/datamaps/tests/test_api.py +++ b/datamaps/tests/test_api.py @@ -15,13 +15,22 @@ def test_get_project_data(master): def test_get_project_data_using_month(master): - master = project_data_from_master_month(master, 7, 2021) + m = project_data_from_master_month(master, 7, 2021) + m2 = project_data_from_master_month(master, 8, 2021) + m3 = project_data_from_master_month(master, 9, 2021) + m4 = project_data_from_master_month(master, 10, 2021) assert ( - master["Chutney Bridge.xlsm"]["Project/Programme Name"] == "Chutney Bridge Ltd" + m["Chutney Bridge.xlsm"]["Project/Programme Name"] == "Chutney Bridge Ltd" ) - assert master.month == "July" - assert master.quarter.quarter == 2 - assert master.quarter.end_date == datetime.date(2021, 9, 30) + assert m.month == "July" + assert m2.month == "August" + assert m3.month == "September" + assert m4.month == "October" + assert m.quarter.quarter == 2 + assert m2.quarter.quarter == 2 + assert m3.quarter.quarter == 2 + assert m4.quarter.quarter == 3 + assert m.quarter.end_date == datetime.date(2021, 9, 30) def test_quarter_objects_have_months(): |