From 9a7d578b12ca6f869f7561f11ee0837a12b58a84 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Thu, 22 Oct 2020 14:25:07 +0100 Subject: failing tests but lots of progress on html builders --- ctrack/register/css.py | 116 ++++++++++++++++------------------- ctrack/register/tests/test_css.py | 126 ++++++++++++++++++++++++-------------- 2 files changed, 132 insertions(+), 110 deletions(-) (limited to 'ctrack') 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 = ( - "\n" - " {0}\n" - " {1}\n" - " {2}\n" - " OES Revisions Submitted\n" - " Validation Agreed\n" - " Improvement Plan Submitted\n" - " Improvement Plan Review\n" - "\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( - [ - "", - self.slots.initial_submitted.type_descriptor, - "" - ] - ) - revision_completed_str = "".join( - [ - "", - self.slots.reviewed.type_descriptor, - "" - ] - ) - 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 = "{1}" + 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(["\n", f"{org}\n", tds, "\n", empties_strs, "\n", ""]) -@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 = ( - "\n" - " {}\n" - ' CAF_INITIAL_CAF_RECEIVED\n' - " CAF Reviewed\n" - " OES Revisions Submitted\n" - " Validation Agreed\n" - " Improvement Plan Submitted\n" - " Improvement Plan Review\n" - "\n" - ) + accept = ("\n" + "{}\n" + "CAF_INITIAL_CAF_RECEIVED\n" + "CAF_INITIAL_REVIEW_COMPLETE\n" + "CAF_FEEDBACK_EMAILED_OES\n" + "CAF_RECEIVED\n" + "CAF_EMAILED_ROSA\n" + "CAF_VALIDATION_SIGN_OFF\n" + "CAF_VALIDATION_RECORD_EMAILED_TO_OES\n" + "CAF_PEER_REVIEW_PERIOD\n" + "CAF_VALIDATION_PERIOD\n" + "") 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 = ( - "\n" - " {}\n" - " CAF_INITIAL_CAF_RECEIVED\n" - " CAF_INITIAL_REVIEW_COMPLETE\n" - " OES Revisions Submitted\n" - " Validation Agreed\n" - " Improvement Plan Submitted\n" - " Improvement Plan Review\n" - "\n" - ) + accept = ("\n" + "{}\n" + "CAF_INITIAL_CAF_RECEIVED\n" + "CAF_INITIAL_REVIEW_COMPLETE\n" + "CAF_FEEDBACK_EMAILED_OES\n" + "CAF_RECEIVED\n" + "CAF_EMAILED_ROSA\n" + "CAF_VALIDATION_SIGN_OFF\n" + "CAF_VALIDATION_RECORD_EMAILED_TO_OES\n" + "CAF_PEER_REVIEW_PERIOD\n" + "CAF_VALIDATION_PERIOD\n" + "") 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 = ("\n" + "{}\n" + "CAF_INITIAL_CAF_RECEIVED\n" + "CAF_INITIAL_REVIEW_COMPLETE\n" + "CAF_FEEDBACK_EMAILED_OES\n" + "CAF_RECEIVED\n" + "CAF_EMAILED_ROSA\n" + "CAF_VALIDATION_SIGN_OFF\n" + "CAF_VALIDATION_RECORD_EMAILED_TO_OES\n" + "CAF_PEER_REVIEW_PERIOD\n" + "CAF_VALIDATION_PERIOD\n" + "") + 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() == ( + "\n" + f"{caf.organisation.name}\n" + 'CAF_INITIAL_CAF_RECEIVED\n' + 'CAF_INITIAL_REVIEW_COMPLETE\n' + "" + ) -- cgit v1.2.3