Skip to content

Commit

Permalink
RAS-1382 Write test for get_survey_list_details_for_party in frontsta…
Browse files Browse the repository at this point in the history
…ge (#1010)

* RAS-1382 Write test for get_survey_list_details_for_party in frontstage
  • Loading branch information
LJBabbage authored Nov 27, 2024
1 parent a09208c commit 8777844
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 2 deletions.
4 changes: 2 additions & 2 deletions _infra/helm/frontstage/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 2.5.31
version: 2.5.32

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 2.5.31
appVersion: 2.5.32
277 changes: 277 additions & 0 deletions tests/unit/controllers/test_party_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from collections import namedtuple
from unittest.mock import patch

import responses

Expand All @@ -9,6 +10,7 @@
from frontstage.controllers.party_controller import (
display_button,
get_respondent_enrolments_for_started_collex,
get_survey_list_details_for_party,
)
from frontstage.exceptions.exceptions import ApiError
from tests.integration.mocked_services import (
Expand Down Expand Up @@ -244,3 +246,278 @@ def test_display_button(self):
]
for combination in combinations:
self.assertEqual(display_button(combination.status, combination.ci_type), combination.expected)

@patch("frontstage.controllers.party_controller.RedisCache.get_collection_instrument")
@patch("frontstage.controllers.party_controller.RedisCache.get_survey")
@patch("frontstage.controllers.party_controller.RedisCache.get_business_party")
def test_get_survey_list_details_for_party_todo(self, get_business_party, get_survey, get_collection_instrument):
# Given party, survey, collection instrument, collection exercise (lower down) and case (lower down) are mocked
get_business_party.side_effect = self._business_details_side_effect()
get_survey.side_effect = self._survey_details_side_effect()
get_collection_instrument.side_effect = [{"type": "SEFT"}, {"type": "EQ"}, {"type": "EQ"}, {"type": "EQ"}]
respondent = self.respondent_details()

expected_response = [
{
"case_id": "e05e3cd3-6707-4751-befa-aaa29481a626",
"status": "Downloaded",
"collection_instrument_type": "SEFT",
"survey_id": "41320b22-b425-4fba-a90e-718898f718ce",
"survey_long_name": "Annual Inward Foreign Direct Investment Survey",
"survey_short_name": "AIFDI",
"survey_ref": "062",
"business_party_id": "bebee450-46da-4f8b-a7a6-d4632087f2a3",
"business_name": "Test Business 1",
"trading_as": "Trading as Test Business 1",
"business_ref": "49910000014",
"period": "3001",
"submit_by": "01 Jan 2030",
"formatted_submit_by": "1st January 2030",
"due_in": "Due in over 3 months",
"collection_exercise_ref": "3001",
"collection_exercise_id": "6bea2aa8-53a9-4d68-8160-8cbbabfe2d20",
"added_survey": None,
"display_button": True,
},
{
"case_id": "000d3115-2e95-4033-8307-0daa8b0a3123",
"status": "Not started",
"collection_instrument_type": "EQ",
"survey_id": "02b9c366-7397-42f7-942a-76dc5876d86d",
"survey_long_name": "Quarterly Business Survey",
"survey_short_name": "QBS",
"survey_ref": "139",
"business_party_id": "fd4d0444-d40a-4c47-996a-de6f5f20658b",
"business_name": "Test Business 2",
"trading_as": "Trading as Test Business 2",
"business_ref": "49900000005",
"period": "December",
"submit_by": "29 Nov 2024",
"formatted_submit_by": "29th November 2024",
"due_in": "Due in 3 days",
"collection_exercise_ref": "1912",
"collection_exercise_id": "09286c27-7bba-4b94-8ec1-248c60711ebc",
"added_survey": None,
"display_button": True,
},
{
"case_id": "e11d8652-bd92-46ca-a984-a586966c7c16",
"status": "In progress",
"collection_instrument_type": "EQ",
"survey_id": "02b9c366-7397-42f7-942a-76dc5876d86d",
"survey_long_name": "Quarterly Business Survey",
"survey_short_name": "QBS",
"survey_ref": "139",
"business_party_id": "3ab241f7-b5cc-4cab-a7e6-b6ad6283cbe1",
"business_name": "Test Business 3",
"trading_as": "Trading as Test Business 3",
"business_ref": "49900000004",
"period": "December",
"submit_by": "29 Nov 2024",
"formatted_submit_by": "29th November 2024",
"due_in": "Due in 3 days",
"collection_exercise_ref": "1912",
"collection_exercise_id": "09286c27-7bba-4b94-8ec1-248c60711ebc",
"added_survey": None,
"display_button": True,
},
]

with app.app_context(): # the 2 patches are inside the context to capture the args
with patch(
"frontstage.controllers.party_controller.RedisCache.get_collection_exercises_by_survey",
_get_ces_return_value_by_survey_id,
):
with patch(
"frontstage.controllers.case_controller.get_cases_for_list_type_by_party_id",
_get_case_return_value_by_business_id,
):
# when get_survey_list_details_for_party is called
survey_list_details_for_party = get_survey_list_details_for_party(respondent, "todo", None, None)

# Then the correct list is returned
self.assertEqual(list(survey_list_details_for_party), expected_response)

@staticmethod
def respondent_details():
return {
"associations": [
{
"enrolments": [{"enrolmentStatus": "ENABLED", "surveyId": "41320b22-b425-4fba-a90e-718898f718ce"}],
"partyId": "bebee450-46da-4f8b-a7a6-d4632087f2a3",
},
{
"enrolments": [{"enrolmentStatus": "ENABLED", "surveyId": "02b9c366-7397-42f7-942a-76dc5876d86d"}],
"partyId": "fd4d0444-d40a-4c47-996a-de6f5f20658b",
},
{
"enrolments": [{"enrolmentStatus": "ENABLED", "surveyId": "02b9c366-7397-42f7-942a-76dc5876d86d"}],
"partyId": "3ab241f7-b5cc-4cab-a7e6-b6ad6283cbe1",
},
{
"enrolments": [{"enrolmentStatus": "ENABLED", "surveyId": "02b9c366-7397-42f7-942a-76dc5876d86d"}],
"partyId": "4865ad73-684e-4c2c-ba00-aece24f1f27e",
},
],
"emailAddress": "[email protected]",
"firstName": "john",
"id": "da457118-b048-403a-99d6-472fad2726fa",
"lastName": "doe",
"sampleUnitType": "BI",
"status": "ACTIVE",
"telephone": "07777000000",
}

@staticmethod
def _survey_details_side_effect():
# This isn't a mistake the code calls out to QBS 3 times, but Redis prevents 3 calls to the service.
return [
{
"id": "41320b22-b425-4fba-a90e-718898f718ce",
"shortName": "AIFDI",
"longName": "Annual Inward Foreign Direct Investment Survey",
"surveyRef": "062",
"surveyMode": "SEFT",
},
{
"id": "02b9c366-7397-42f7-942a-76dc5876d86d",
"shortName": "QBS",
"longName": "Quarterly Business Survey",
"surveyRef": "139",
"surveyMode": "EQ",
},
{
"id": "02b9c366-7397-42f7-942a-76dc5876d86d",
"shortName": "QBS",
"longName": "Quarterly Business Survey",
"surveyRef": "139",
"surveyMode": "EQ",
},
{
"id": "02b9c366-7397-42f7-942a-76dc5876d86d",
"shortName": "QBS",
"longName": "Quarterly Business Survey",
"surveyRef": "139",
"surveyMode": "EQ",
},
]

@staticmethod
def _business_details_side_effect():
return [
{
"id": "bebee450-46da-4f8b-a7a6-d4632087f2a3",
"name": "Test Business 1",
"sampleUnitRef": "49910000014",
"trading_as": "Trading as Test Business 1",
},
{
"id": "fd4d0444-d40a-4c47-996a-de6f5f20658b",
"name": "Test Business 2",
"sampleUnitRef": "49900000005",
"trading_as": "Trading as Test Business 2",
},
{
"id": "3ab241f7-b5cc-4cab-a7e6-b6ad6283cbe1",
"name": "Test Business 3",
"sampleUnitRef": "49900000004",
"trading_as": "Trading as Test Business 3",
},
{
"id": "4865ad73-684e-4c2c-ba00-aece24f1f27e",
"name": "Test Business 4",
"sampleUnitRef": "49900000001",
"trading_as": "Trading as Test Business 4",
},
]


def _get_case_return_value_by_business_id(*args):
"""returns the correct case details based on the business_id used in the patched called"""

business_id = args[0]
if business_id == "bebee450-46da-4f8b-a7a6-d4632087f2a3":
return [
{
"id": "e05e3cd3-6707-4751-befa-aaa29481a626",
"collectionInstrumentId": "37cffd1b-b7e2-4ce0-b4d1-1e4f932189e2",
"partyId": "bebee450-46da-4f8b-a7a6-d4632087f2a3",
"caseGroup": {
"collectionExerciseId": "6bea2aa8-53a9-4d68-8160-8cbbabfe2d20",
"caseGroupStatus": "INPROGRESS",
},
},
{
"id": "6e6b49ae-76ae-458d-9dd2-447bc6ff37de",
"collectionInstrumentId": "04a1a6f6-fe98-4fea-a2ab-d452d75b2a53",
"partyId": "bebee450-46da-4f8b-a7a6-d4632087f2a3",
"caseGroup": {
"collectionExerciseId": "398cf4c5-7f3b-4bfe-a8b9-2d51a0b598a4",
"caseGroupStatus": "NOTSTARTED",
},
},
]

elif business_id == "3ab241f7-b5cc-4cab-a7e6-b6ad6283cbe1":
return [
{
"id": "e11d8652-bd92-46ca-a984-a586966c7c16",
"collectionInstrumentId": "07b96b43-ab74-40d5-aaac-786c92e5dce2",
"partyId": "3ab241f7-b5cc-4cab-a7e6-b6ad6283cbe1",
"caseGroup": {
"collectionExerciseId": "09286c27-7bba-4b94-8ec1-248c60711ebc",
"caseGroupStatus": "INPROGRESS",
},
}
]
elif business_id == "fd4d0444-d40a-4c47-996a-de6f5f20658b":
return [
{
"id": "000d3115-2e95-4033-8307-0daa8b0a3123",
"collectionInstrumentId": "07b96b43-ab74-40d5-aaac-786c92e5dce2",
"partyId": "fd4d0444-d40a-4c47-996a-de6f5f20658b",
"caseGroup": {
"collectionExerciseId": "09286c27-7bba-4b94-8ec1-248c60711ebc",
"caseGroupStatus": "NOTSTARTED",
},
}
]
else:
return []


def _get_ces_return_value_by_survey_id(*args):
"""returns the correct collection exercise details based on the survey_id used in the patched called"""
survey_id = args[1]
if survey_id == "41320b22-b425-4fba-a90e-718898f718ce":
return [
{
"id": "6bea2aa8-53a9-4d68-8160-8cbbabfe2d20",
"surveyId": "41320b22-b425-4fba-a90e-718898f718ce",
"exerciseRef": "3001",
"userDescription": "3001",
"events": {
"return_by": {
"date": "01 Jan 2030",
"formatted_date": "1st January 2030",
"due_time": "Due in over 3 months",
}
},
}
]
else:
return [
{
"id": "09286c27-7bba-4b94-8ec1-248c60711ebc",
"surveyId": "02b9c366-7397-42f7-942a-76dc5876d86d",
"exerciseRef": "1912",
"userDescription": "December",
"events": {
"return_by": {
"date": "29 Nov 2024",
"formatted_date": "29th November 2024",
"due_time": "Due in 3 days",
}
},
}
]

0 comments on commit 8777844

Please sign in to comment.