Skip to content

Commit

Permalink
Fixed various tests, but still encountering issues with untouched tests?
Browse files Browse the repository at this point in the history
  • Loading branch information
lambeb committed Nov 15, 2024
1 parent f47fd15 commit 67cf7db
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 26 deletions.
4 changes: 3 additions & 1 deletion services/deliver_mi_hub_reports_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
import logging
from dataclasses import fields

from functions.csv_functions import write_csv
from functions.zip_functions import create_zip

Expand All @@ -21,7 +23,7 @@ def upload_mi_hub_reports_to_gcp(
else:
logging.warning(f"No call history for {questionnaire_name}")

if mi_hub_respondent_data:
if mi_hub_respondent_data and all(getattr(mi_hub_respondent_data[0], field.name) != "" for field in fields(mi_hub_respondent_data[0])):
respondent_data_csv = write_csv(mi_hub_respondent_data)
zip_data.append(
{"filename": "respondent_data.csv", "content": respondent_data_csv}
Expand Down
57 changes: 48 additions & 9 deletions tests/features/run_mi_hub_reports.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Feature: Generate Reports
| QHousehold.QHHold.Person[1].tmpDoB| 2-11-2022_3:08 |
| QHousehold.QHHold.Person[1].DVAge | 2 |
| DateTimeStamp | 2-11-2022_9:20 |
Then data is present in all fields
And data is present in all fields
When the report generation is triggered
Then the report should be generated and delivered
Then the report should be generated and delivered with the available fields
And no warnings should be logged
Examples:
| field_name |
Expand All @@ -27,16 +27,12 @@ Scenario Outline: Some fields are missing and all data is available
| QHAdmin.HOut | 310 |
| QHAdmin.Interviewer[1] | testuser |
| Mode | testmode |
| QDataBag.PostCode | |
| QHousehold.QHHold.Person[1].Sex | Male |
| QHousehold.QHHold.Person[1].tmpDoB| |
| QHousehold.QHHold.Person[1].DVAge | 2 |
| DateTimeStamp | 2-11-2022_9:20 |
Then data is present in all fields
Then the field "postcode" is missing
And the field "date_of_birth" is missing
And the field "postcode" is missing
When the report generation is triggered
Then the report should be generated and delivered
Then the report should be generated and delivered with the available fields
And "Done - LMS2222Z" is logged as an information message
Examples:
| field_name |
Expand All @@ -56,8 +52,51 @@ Scenario Outline: Some fields are missing and all data is available
| QHousehold.QHHold.Person[1].DVAge |
| DateTimeStamp |
When the report generation is triggered
Then the report should be generated and delivered
Then the report should not be generated
And "No respondent data for LMS2222Z" is logged as an warning message
Examples:
| field_name |
| 0 |


Scenario Outline: All fields are available but some data is missing
Given all of the following fields are available for the Respondent Data report
| field_name | value |
| QID.Serial_Number | 900001 |
| QHAdmin.HOut | 310 |
| QHAdmin.Interviewer[1] | testuser |
| Mode | testmode |
| QDataBag.PostCode | |
| QHousehold.QHHold.Person[1].Sex | Male |
| QHousehold.QHHold.Person[1].tmpDoB| |
| QHousehold.QHHold.Person[1].DVAge | 2 |
| DateTimeStamp | 2-11-2022_9:20 |
And data is present in all fields
And there is no data present in "postcode"
When the report generation is triggered
Then the report should be generated and delivered with the available fields
And "Done - LMS2222Z" is logged as an information message
Examples:
| field_name |
| 0 |


Scenario Outline: All fields are available but no data is present
Given all of the following fields are available for the Respondent Data report
| field_name | value |
| QID.Serial_Number | |
| QHAdmin.HOut | |
| QHAdmin.Interviewer[1] | |
| Mode | |
| QDataBag.PostCode | |
| QHousehold.QHHold.Person[1].Sex | |
| QHousehold.QHHold.Person[1].tmpDoB| |
| QHousehold.QHHold.Person[1].DVAge | |
| DateTimeStamp | |
And there is no data present in any of the fields
When the report generation is triggered
Then the report should not be generated
And "No respondent data for LMS2222Z" is logged as an warning message
Examples:
| field_name |
| 0 |
67 changes: 51 additions & 16 deletions tests/features/steps/steps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# type: ignore[no-redef]

import logging
from dataclasses import fields

from behave import given, then, when
from models.mi_hub_respondent_data_model import MiHubRespondentData
from services.deliver_mi_hub_reports_service import DeliverMiHubReportsService
Expand All @@ -11,17 +13,19 @@ def step_impl(context):
if context.table:
fields = {row["field_name"]: row["value"] for row in context.table}

context.mi_hub_respondent_data = [MiHubRespondentData(
serial_number=str(fields["QID.Serial_Number"]),
outcome_code=str(fields["QHAdmin.HOut"]),
date_completed=str(fields["DateTimeStamp"]),
interviewer=str(fields["QHAdmin.Interviewer[1]"]),
mode=str(fields["Mode"]),
postcode=str(fields["QDataBag.PostCode"]),
gender=str(fields["QHousehold.QHHold.Person[1].Sex"]),
date_of_birth=str(fields["QHousehold.QHHold.Person[1].tmpDoB"]),
age=str(fields["QHousehold.QHHold.Person[1].DVAge"]),
)]
context.mi_hub_respondent_data = [
MiHubRespondentData(
serial_number=fields.get("QID.Serial_Number", None),
outcome_code=fields.get("QHAdmin.HOut", None),
date_completed=fields.get("DateTimeStamp", None),
interviewer=fields.get("QHAdmin.Interviewer[1]", None),
mode=fields.get("Mode", None),
postcode=fields.get("QDataBag.PostCode", None),
gender=fields.get("QHousehold.QHHold.Person[1].Sex", None),
date_of_birth=fields.get("QHousehold.QHHold.Person[1].tmpDoB", None),
age=fields.get("QHousehold.QHHold.Person[1].DVAge", None),
)
]


@when('the report generation is triggered')
Expand All @@ -36,28 +40,47 @@ def step_impl(context):
context.response = response


@then('data is present in all fields')
@given('data is present in all fields')
def step_impl(context):
for respondent_data in context.mi_hub_respondent_data:
assert (
not all(value in ("", None) for value in vars(respondent_data).values())
not all(value is None for value in vars(respondent_data).values())
), f"Fields are not all present"


@then('the field "{field}" is missing')
@given('the field "{field}" is missing')
def step_impl(context, field):
for respondent_data in context.mi_hub_respondent_data:
assert (
getattr(respondent_data, field) == ""
getattr(respondent_data, field) is None
), f"Field {field} is present"


@given('there is no data present in "{field}"')
def step_impl(context, field):
for respondent_data in context.mi_hub_respondent_data:
assert (
getattr(respondent_data, field) == ""
), f"Field {field} has data present"


@given('there is no data present in any of the fields')
def step_impl(context):
for respondent_data in context.mi_hub_respondent_data:
for field in fields(respondent_data):
field_name = field.name
field_value = getattr(respondent_data, field_name)
assert (
field_value == ""
), f"Field {field_value} has data present"


@given('none of the following fields are available for the report')
def step_impl(context):
context.mi_hub_respondent_data = []


@then('the report should be generated and delivered')
@then('the report should be generated and delivered with the available fields')
def step_impl(context):
assert (
context.response == "Done - " + context.questionnaire_name
Expand All @@ -71,6 +94,18 @@ def step_impl(context):
), f"Warnings should not be generated"


@then('the report should not be generated')
def step_impl(context):
messages = [
(record.levelno, record.getMessage()) for record in context.log_capture.buffer
]
mappings = {"information": logging.INFO, "error": logging.ERROR, "warning": logging.WARNING}
assert (
mappings["warning"],
"No respondent data for LMS2222Z",
) in messages, f"Could not find warning, No respondent data for LMS2222Z in {messages}"


@then('"{message}" is logged as an {error_level} message')
def step_impl(context, message, error_level):
messages = [
Expand Down

0 comments on commit 67cf7db

Please sign in to comment.