Skip to content

Commit

Permalink
Add tests for mi scope report
Browse files Browse the repository at this point in the history
  • Loading branch information
said-moj committed Sep 9, 2024
1 parent c18e8bb commit adf64b4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 136 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ repos:
rev: v2.1.0
hooks:
- id: flake8
# Pre-commit fails to install on apple chips as install pyyaml 5.4
additional_dependencies: ['PyYAML==5.3.1']
language_version: python2.7
args: ['--config=setup.cfg', '--exclude=./cla_backend/settings/*,./docs/*']
- repo: https://github.com/jazzband/pip-tools
Expand Down
25 changes: 10 additions & 15 deletions cla_backend/apps/reports/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ def get_rows(self):
yield row

def __iter__(self):
try:
yield self.get_headers()
for row in self.get_rows():
yield row
except Exception as e:
print "ERROR: %s" % e
yield self.get_headers()
for row in self.get_rows():
yield row

def get_output(self):
return list(self)
Expand Down Expand Up @@ -1081,19 +1078,17 @@ def _get_col_index(self, column_name):

@staticmethod
def notes_to_dict(notes):
parts = notes.split("User selected:\nWhat do you need help with?:")
ret = {"user problem": "", "categories": [], "scope": ""}
parts = notes.split("User selected:\nWhat do you need help with?: ")
if not notes:
return ret

if len(parts) == 1:
user_problem = ""
categories = parts[0]
else:
user_problem = parts[0].split("User problem:\n")[1]
ret["user problem"] = parts[0].split("User problem:\n")[1]
categories = parts[1]
categories, scope = categories.split("Outcome: ")
ret = {
"user problem": user_problem,
"categories": [],
"scope": scope
}
categories, ret["scope"] = categories.split("Outcome: ")
for category in categories.split("\n\n"):
if "?: " in category:
category = category.split("?: ")[1]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
WITH log_changed_category as (
SELECT log_created.case_id, (log_changed_category.patch #>> '{}')::json#>>'{backwards,0,value}' as "diagnosis_category"
FROM cla_eventlog_log as log_created
JOIN cla_eventlog_log as log_changed_category ON log_created.case_id=log_changed_category.case_id
AND log_created.notes = 'Case created digitally' AND log_changed_category.notes LIKE 'Changed category to%%'
),
log_mi_oos_outcome_code as (
WITH log_mi_oos_outcome_code as (
SELECT case_id, code
FROM cla_eventlog_log
WHERE code = 'MIS-OOS'
GROUP BY case_id, code
)

SELECT
c.personal_details_id as "Person ID"
DISTINCT c.personal_details_id as "Person ID"
,c.reference as "Case Id"
,to_char(c.created, 'YYYY-MM-DD') as "Created"
,to_char(c.modified, 'YYYY-MM-DD') as "Modified"
Expand All @@ -32,7 +26,7 @@ SELECT
WHEN ec.state IS NOT NULL AND provider_assigned_at IS NULL THEN 'Operator'
WHEN c.provider_viewed IS NOT NULL AND log_mi_oos_outcome_code.code IS NULL THEN 'Read and approved by SP'
WHEN c.provider_viewed IS NOT NULL AND log_mi_oos_outcome_code.code IS NOT NULL THEN 'Read and NOT approved by SP'
ELSE 'Provider has not viewed'
ELSE 'NOT read by SP'
END as "Workflow status"
,c.outcome_code as "CHS case outcome code"
,c.provider_notes as "Provider Notes"
Expand All @@ -54,9 +48,10 @@ FROM legalaid_case as c
LEFT OUTER JOIN legalaid_eligibilitycheck as ec on c.eligibility_check_id = ec.id
LEFT OUTER JOIN legalaid_category as category on ec.category_id = category.id
LEFT OUTER JOIN legalaid_adaptationdetails as adapt on c.adaptation_details_id = adapt.id
LEFT OUTER JOIN log_changed_category ON log_changed_category.case_id = c.id
LEFT OUTER JOIN legalaid_mattertype as mt1 on mt1.id = c.matter_type1_id
LEFT OUTER JOIN legalaid_mattertype as mt2 on mt2.id = c.matter_type2_id
LEFT OUTER JOIN diagnosis_diagnosistraversal as diagnosis on c.diagnosis_id = diagnosis.id
LEFT OUTER JOIN log_mi_oos_outcome_code ON log_mi_oos_outcome_code.case_id = c.id
ORDER BY c.modified DESC
WHERE source IN ('WEB')
AND c.modified >= %(from_date)s AND c.modified < %(to_date)s
ORDER BY to_char(c.modified, 'YYYY-MM-DD') DESC
177 changes: 67 additions & 110 deletions cla_backend/apps/reports/tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,110 +532,15 @@ def test_callback_time_slots(self, _):
self.assertDictEqual(row_dict, callbacks[row_dict["Interval"]])


class TestScopeReportTwo(TestCase):
CALLBACK_TIME_SLOT = "checker.callback_time_slot"
LEGALAID_CASE = "legalaid.case"

def get_report(self, date_range):
with mock.patch("reports.forms.MIScopeReport.date_range", date_range):
report = reports.forms.MIScopeReport()
rows = report.get_rows()
headers = report.get_headers()
data = []
for row in rows:
data.append(zip(headers, row))

return data

@mock.patch("cla_common.call_centre_availability.OpeningHours.available", return_value=True)
def test_callback_time_slots(self, _):
tomorrow = datetime.datetime(2024, 1, 2)
overmorrow = tomorrow + datetime.timedelta(days=1)
date_format = "%d/%m/%Y"
# Create callback time slots with capacity
callbacks = {
"0900": {
"Date": tomorrow.strftime(date_format),
"Interval": u"0900",
"Total capacity": 4,
"Used capacity": 1,
"Remaining capacity": 3,
"% Remaining capacity": "75",
},
"1000": {
"Date": tomorrow.strftime(date_format),
"Interval": u"1000",
"Total capacity": 9,
"Used capacity": 3,
"Remaining capacity": 6,
"% Remaining capacity": "66.66",
},
"1100": {
"Date": tomorrow.strftime(date_format),
"Interval": u"1100",
"Total capacity": 0,
"Used capacity": 0,
"Remaining capacity": 0,
"% Remaining capacity": "0",
},
"1200": {
"Date": tomorrow.strftime(date_format),
"Interval": u"1200",
"Total capacity": 1,
"Used capacity": 1,
"Remaining capacity": 0,
"% Remaining capacity": "0",
},
"1300": {
"Date": tomorrow.strftime(date_format),
"Interval": u"1300",
"Total capacity": 1,
"Used capacity": 0,
"Remaining capacity": 1,
"% Remaining capacity": "100",
},
"1400": {
"Date": overmorrow.strftime(date_format),
"Interval": u"1400",
"Total capacity": 1,
"Used capacity": 0,
"Remaining capacity": 1,
"% Remaining capacity": "100",
},
}
for interval, callback in callbacks.iteritems():
# Create callback time slots
dt = datetime.datetime.strptime(callback["Date"], date_format)
make_recipe(self.CALLBACK_TIME_SLOT, capacity=callback["Total capacity"], date=dt, time=interval)
if callback["Used capacity"] > 0:
hour = int(interval[0:2])
minutes = int(interval[2:])
requires_action_at = datetime.datetime.combine(dt, datetime.time(hour=hour, minute=minutes))
# Create callbacks
make_recipe(
self.LEGALAID_CASE,
requires_action_at=requires_action_at,
_quantity=callback["Used capacity"],
eligibility_check=None,
callback_type=CALLBACK_TYPES.CHECKER_SELF,
notes=interval,
source="WEB"
)

date_range = (tomorrow, tomorrow)
report = self.get_report(date_range)


for row in report:
row_dict = dict(row)
self.assertEqual(row_dict["Date"], tomorrow.strftime(date_format))
self.assertDictEqual(row_dict, callbacks[row_dict["Interval"]])

class TestMIScopeReport(TestCase):
LEGALAID_CASE = "legalaid.case"
today = datetime.date.today()

def get_report(self, date_range):
with mock.patch("reports.forms.MIScopeReport.date_range", date_range):
def get_report(self):
date_from = self.today - datetime.timedelta(days=1)
date_to = self.today + datetime.timedelta(days=1)

with mock.patch("reports.forms.MIScopeReport.date_range", (date_from, date_to)):
report = reports.forms.MIScopeReport()
rows = report.get_rows()
headers = report.get_headers()
Expand All @@ -645,19 +550,71 @@ def get_report(self, date_range):

return data

def test_report(self):
eligible_case = make_recipe("legalaid.case", source="WEB")
def test_report_client_notes(self):
eligible_case = make_recipe("legalaid.eligible_case", source="WEB")
eligible_case.eligibility_check.notes = self.get_notes()
eligible_case.eligibility_check.save()
eligible_case.save()
# self.assertEqual(eligible_case.eligibility_check.state, "yes")
# self.assertEqual(eligible_case.diagnosis.state, "INSCOPE")
# self.assertEqual(eligible_case.source, "WEB")

today = datetime.date.today()
report = self.get_report((today, today))
self.assertEqual(eligible_case.eligibility_check.state, "yes")
self.assertEqual(eligible_case.diagnosis.state, "INSCOPE")
self.assertEqual(eligible_case.source, "WEB")

report = self.get_report()
expected = {
"Web diagnosis category 1": "Discrimination",
"Web diagnosis category 2": "Age",
"Web diagnosis category 3": "18 or over",
"Web diagnosis category 4": "At work",
"Web diagnosis category 5": "",
"Web diagnosis category 6": "",
"Web scope state": "INSCOPE",
"Client notes": "This is a free text field\nI can type whatever I want\nYes\nNo\nDiscrimination\n\n",
"Workflow status": "Operator",
}
self.assertDictContainsSubset(expected, dict(report[0]))

def test_report_workflow_status_pending(self):
eligible_case = make_recipe("legalaid.case", source="WEB")
self.assertEqual(eligible_case.source, "WEB")
report = self.get_report()
expected = {"Workflow status": "Pending"}
self.assertDictContainsSubset(expected, dict(report[0]))

def test_report_workflow_status_operator(self):
eligible_case = make_recipe("legalaid.eligible_case", source="WEB")
self.assertEqual(eligible_case.source, "WEB")
report = self.get_report()
expected = {"Workflow status": "Operator"}
self.assertDictContainsSubset(expected, dict(report[0]))

def test_report_workflow_status_read_approved_by_SP(self):
eligible_case = make_recipe(
"legalaid.eligible_case", source="WEB", provider_viewed=self.today, provider_assigned_at=self.today
)
make_recipe("cla_eventlog.log", case=eligible_case, code="COI")
self.assertEqual(eligible_case.source, "WEB")
report = self.get_report()
expected = {"Workflow status": "Read and approved by SP"}
self.assertDictContainsSubset(expected, dict(report[0]))

def test_report_workflow_status_read_NOT_approved_by_SP(self):
eligible_case = make_recipe(
"legalaid.eligible_case", source="WEB", provider_viewed=self.today, provider_assigned_at=self.today
)
make_recipe("cla_eventlog.log", case=eligible_case, code="MIS-OOS")

self.assertEqual(eligible_case.source, "WEB")

report = self.get_report()
expected = {"Workflow status": "Read and NOT approved by SP"}
self.assertDictContainsSubset(expected, dict(report[0]))

import pdb;pdb.set_trace()
def test_report_workflow_status_NOT_read_by_SP(self):
eligible_case = make_recipe("legalaid.eligible_case", source="WEB", provider_assigned_at=self.today)
self.assertEqual(eligible_case.source, "WEB")
report = self.get_report()
expected = {"Workflow status": "NOT read by SP"}
self.assertDictContainsSubset(expected, dict(report[0]))

def get_notes(self):
return """User problem:
Expand Down

0 comments on commit adf64b4

Please sign in to comment.