diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-22 14:25:07 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-22 14:25:07 +0100 |
commit | 9a7d578b12ca6f869f7561f11ee0837a12b58a84 (patch) | |
tree | e5e645af0b6858bc603caa2f3f95335bf4e44050 | |
parent | 113f9483a2b2863f3e7e176285e9031593843eda (diff) |
failing tests but lots of progress on html builders
-rw-r--r-- | ctrack/register/css.py | 116 | ||||
-rw-r--r-- | ctrack/register/tests/test_css.py | 126 |
2 files changed, 132 insertions, 110 deletions
diff --git a/ctrack/register/css.py b/ctrack/register/css.py index 0c49fcd..7567e9d 100644 --- a/ctrack/register/css.py +++ b/ctrack/register/css.py @@ -1,7 +1,6 @@ -from dataclasses import dataclass from typing import NamedTuple, List -from ctrack.register.models import EventType, EventBase, CAFSingleDateEvent +from ctrack.register.models import EventType, EventBase class TagAttributes(NamedTuple): @@ -9,73 +8,62 @@ class TagAttributes(NamedTuple): id_str: str -def tag_attrs(event) -> TagAttributes: - if event.type_descriptor == EventType.CAF_INITIAL_CAF_RECEIVED.name: - return TagAttributes( - inline_style='style="background-color: green; color: white;"', - id_str="caf-initial-received-event", - ) - if event.type_descriptor == EventType.CAF_INITIAL_REVIEW_COMPLETE.name: - return TagAttributes( - inline_style='style="background-color: green; color: white;"', - id_str="caf-initial-review-complete-event", - ) - - -template = ( - "<tr>\n" - " <td>{0}</td>\n" - " {1}\n" - " {2}\n" - " <td>OES Revisions Submitted</td>\n" - " <td>Validation Agreed</td>\n" - " <td>Improvement Plan Submitted</td>\n" - " <td>Improvement Plan Review</td>\n" - "</tr>\n" -) - - class Swimlane: def __init__(self, org_name: str, events: List[EventBase]): self.events = events + self.attrs_added = [] self.org_name = org_name - self.slots = CAFSwimlaneSlots(*events) # type: CAFSwimlaneSlots + self._process_args() - @property - def tr(self): - initial_submitted_str = "".join( - [ - "<td ", - tag_attrs(self.slots.initial_submitted).inline_style, - ">", - self.slots.initial_submitted.type_descriptor, - "</td>" - ] - ) - revision_completed_str = "".join( - [ - "<td ", - tag_attrs(self.slots.reviewed).inline_style, - ">", - self.slots.reviewed.type_descriptor, - "</td>" - ] - ) - return template.format( - self.org_name, initial_submitted_str, revision_completed_str - ) + def tag_attrs(self, event) -> TagAttributes: + if event.type_descriptor == EventType.CAF_INITIAL_CAF_RECEIVED.name: + try: + self.attrs_added.pop( + self.attrs_added.index(EventType.CAF_INITIAL_CAF_RECEIVED.name) + ) + except ValueError: + pass + return TagAttributes( + inline_style=' style="background-color: green; color: white;"', + id_str="caf-initial-received-event", + ) + if event.type_descriptor == EventType.CAF_INITIAL_REVIEW_COMPLETE.name: + try: + self.attrs_added.pop( + self.attrs_added.index(EventType.CAF_INITIAL_REVIEW_COMPLETE.name) + ) + except ValueError: + pass + return TagAttributes( + inline_style=' style="background-color: green; color: white;"', + id_str="caf-initial-review-complete-event", + ) + def table_row_builder(self): + if len(self.events) == 0: + raise ValueError("Cannot handle an empty list") + tmpl = "<td{0}>{1}</td>" + org = self.events[0].related_caf.organisation.name + _tds = [ + tmpl.format(self.tag_attrs(e).inline_style, e.type_descriptor) + for e in self.events + ] + empties = [ + tmpl.format("", e) + for e in self.attrs_added + if e[:3] == "CAF" + ] + tds = "\n".join(_tds) + empties_strs = "\n".join(empties) + return "".join(["<tr>\n", f"<td>{org}</td>\n", tds, "\n", empties_strs, "\n", "</tr>"]) -@dataclass(frozen=True) -class CAFSwimlaneSlots: - """ - The pre-compliance stages we expect. - """ + def _process_args(self): + for v in EventType: + setattr(self, v.name, None) + self.attrs_added.append(v.name) + for e in self.events: + setattr(self, str(e.type_descriptor), e) - initial_submitted: CAFSingleDateEvent = None - reviewed: CAFSingleDateEvent = None - revisions_submitted: CAFSingleDateEvent = None - validation_agreed: CAFSingleDateEvent = None - improvement_plan_submitted: CAFSingleDateEvent = None - improvement_plan_reviewed: CAFSingleDateEvent = None - improvement_plan_agreed: CAFSingleDateEvent = None + @property + def tr(self): + return self.table_row_builder() diff --git a/ctrack/register/tests/test_css.py b/ctrack/register/tests/test_css.py index 8c9f77a..301c715 100644 --- a/ctrack/register/tests/test_css.py +++ b/ctrack/register/tests/test_css.py @@ -1,6 +1,6 @@ import pytest -from ctrack.register.css import tag_attrs, Swimlane, CAFSwimlaneSlots +from ctrack.register.css import Swimlane from ctrack.register.models import CAFSingleDateEvent, EventType pytestmark = pytest.mark.django_db @@ -11,54 +11,39 @@ pytestmark = pytest.mark.django_db [ ( EventType.CAF_INITIAL_CAF_RECEIVED.name, - 'style="background-color: green; color: white;"', + ' style="background-color: green; color: white;"', "caf-initial-received-event", ), ( EventType.CAF_INITIAL_REVIEW_COMPLETE.name, - 'style="background-color: green; color: white;"', + ' style="background-color: green; color: white;"', "caf-initial-review-complete-event", - ) + ), ], ) def test_can_get_class_string(caf, user, e_type, css_str, id_str): + org_name = caf.organisation.name event = CAFSingleDateEvent.objects.create( type_descriptor=e_type, related_caf=caf, date="2020-10-20", user=user ) - assert tag_attrs(event).inline_style == css_str - assert tag_attrs(event).id_str == id_str - - -class _TestEvent(CAFSingleDateEvent): - def __init__(self, event): - self.type_descriptor = event - - -def test_swimlane_slots(): - slots = CAFSwimlaneSlots( - initial_submitted=_TestEvent("CAF Initial Submitted"), - # reviewed=_TestEvent("CAF Reviewed"), - # revisions_submitted=_TestEvent("CAF Revisions Submitted"), - # validation_agreed=_TestEvent("CAF Validation Agreed"), - # improvement_plan_submitted=_TestEvent("Improvement Plan Submitted"), - # improvement_plan_reviewed=_TestEvent("Improvement Plan Reviewed"), - # improvement_plan_agreed=_TestEvent("Improvement Plan Agreed"), - ) - assert slots.initial_submitted.type_descriptor == "CAF Initial Submitted" + sl = Swimlane(org_name, [event]) + assert sl.tag_attrs(event).inline_style == css_str + assert sl.tag_attrs(event).id_str == id_str def test_progress_chart_css_initial_review_only(caf, user): - accept = ( - "<tr>\n" - " <td>{}</td>\n" - ' <td style="background-color: green; color: white;">CAF_INITIAL_CAF_RECEIVED</td>\n' - " <td>CAF Reviewed</td>\n" - " <td>OES Revisions Submitted</td>\n" - " <td>Validation Agreed</td>\n" - " <td>Improvement Plan Submitted</td>\n" - " <td>Improvement Plan Review</td>\n" - "</tr>\n" - ) + accept = ("<tr>\n" + "<td>{}</td>\n" + "<td style=\"background-color: green; color: white;\">CAF_INITIAL_CAF_RECEIVED</td>\n" + "<td>CAF_INITIAL_REVIEW_COMPLETE</td>\n" + "<td>CAF_FEEDBACK_EMAILED_OES</td>\n" + "<td>CAF_RECEIVED</td>\n" + "<td>CAF_EMAILED_ROSA</td>\n" + "<td>CAF_VALIDATION_SIGN_OFF</td>\n" + "<td>CAF_VALIDATION_RECORD_EMAILED_TO_OES</td>\n" + "<td>CAF_PEER_REVIEW_PERIOD</td>\n" + "<td>CAF_VALIDATION_PERIOD</td>\n" + "</tr>") org_name = caf.organisation.name caf_initial = CAFSingleDateEvent.objects.create( type_descriptor=EventType.CAF_INITIAL_CAF_RECEIVED.name, @@ -71,17 +56,18 @@ def test_progress_chart_css_initial_review_only(caf, user): def test_progress_chart_css_initial_two_events(caf, user): - accept = ( - "<tr>\n" - " <td>{}</td>\n" - " <td style=\"background-color: green; color: white;\">CAF_INITIAL_CAF_RECEIVED</td>\n" - " <td style=\"background-color: green; color: white;\">CAF_INITIAL_REVIEW_COMPLETE</td>\n" - " <td>OES Revisions Submitted</td>\n" - " <td>Validation Agreed</td>\n" - " <td>Improvement Plan Submitted</td>\n" - " <td>Improvement Plan Review</td>\n" - "</tr>\n" - ) + accept = ("<tr>\n" + "<td>{}</td>\n" + "<td style=\"background-color: green; color: white;\">CAF_INITIAL_CAF_RECEIVED</td>\n" + "<td style=\"background-color: green; color: white;\">CAF_INITIAL_REVIEW_COMPLETE</td>\n" + "<td>CAF_FEEDBACK_EMAILED_OES</td>\n" + "<td>CAF_RECEIVED</td>\n" + "<td>CAF_EMAILED_ROSA</td>\n" + "<td>CAF_VALIDATION_SIGN_OFF</td>\n" + "<td>CAF_VALIDATION_RECORD_EMAILED_TO_OES</td>\n" + "<td>CAF_PEER_REVIEW_PERIOD</td>\n" + "<td>CAF_VALIDATION_PERIOD</td>\n" + "</tr>") org_name = caf.organisation.name caf_initial = CAFSingleDateEvent.objects.create( type_descriptor=EventType.CAF_INITIAL_CAF_RECEIVED.name, @@ -97,3 +83,51 @@ def test_progress_chart_css_initial_two_events(caf, user): ) output = Swimlane(org_name, [caf_initial, caf_reviewed]) assert output.tr == accept.format(org_name) + + +def test_progress_chart_css_second_event(caf, user): + accept = ("<tr>\n" + "<td>{}</td>\n" + "<td>CAF_INITIAL_CAF_RECEIVED</td>\n" + "<td style=\"background-color: green; color: white;\">CAF_INITIAL_REVIEW_COMPLETE</td>\n" + "<td>CAF_FEEDBACK_EMAILED_OES</td>\n" + "<td>CAF_RECEIVED</td>\n" + "<td>CAF_EMAILED_ROSA</td>\n" + "<td>CAF_VALIDATION_SIGN_OFF</td>\n" + "<td>CAF_VALIDATION_RECORD_EMAILED_TO_OES</td>\n" + "<td>CAF_PEER_REVIEW_PERIOD</td>\n" + "<td>CAF_VALIDATION_PERIOD</td>\n" + "</tr>") + org_name = caf.organisation.name + caf_reviewed = CAFSingleDateEvent.objects.create( + type_descriptor=EventType.CAF_INITIAL_REVIEW_COMPLETE.name, + related_caf=caf, + date="2020-10-20", + user=user, + ) + output = Swimlane(org_name, [caf_reviewed]) + assert output.tr == accept.format(org_name) + + +def test_table_row_builder(user, caf): + e1 = CAFSingleDateEvent.objects.create( + type_descriptor=EventType.CAF_INITIAL_CAF_RECEIVED.name, + related_caf=caf, + date="2020-10-20", + user=user, + ) + e2 = CAFSingleDateEvent.objects.create( + type_descriptor=EventType.CAF_INITIAL_REVIEW_COMPLETE.name, + related_caf=caf, + date="2020-10-20", + user=user, + ) + org_name = caf.organisation.name + sl = Swimlane(org_name, [e1, e2]) + assert sl.table_row_builder() == ( + "<tr>\n" + f"<td>{caf.organisation.name}</td>\n" + '<td style="background-color: green; color: white;">CAF_INITIAL_CAF_RECEIVED</td>\n' + '<td style="background-color: green; color: white;">CAF_INITIAL_REVIEW_COMPLETE</td>\n' + "</tr>" + ) |