def test_sc_homepage_summary_complete_with_archived_SAs( logged_in_client, test_user): # Arrange active_sa = SupplyChainFactory( name="Medical", gov_department=test_user.gov_department, ) sc = SupplyChainFactory( name="carbon", gov_department=test_user.gov_department, ) sa = StrategicActionFactory(supply_chain=active_sa) StrategicActionFactory.create_batch(3, supply_chain=sc, is_archived=True, archived_reason="Reason") StrategicActionUpdateFactory() dummy_qs = QuerySet(model=StrategicActionUpdate) # Act with mock.patch( "supply_chains.models.SupplyChainQuerySet.submitted_since", return_value=dummy_qs, ): resp = logged_in_client.get(reverse("sc-home")) # Assert assert resp.context["update_complete"] assert resp.context["num_in_prog_supply_chains"] == 0 assert len(resp.context["supply_chains"]) == 2
def taskcomp_stub(test_user): sc_name = "Supply Chain 1" sc = SupplyChainFactory.create( name=sc_name, gov_department=test_user.gov_department, last_submission_date=date.today(), ) scs = SupplyChainFactory.create_batch( 2, gov_department=test_user.gov_department) sa = StrategicActionFactory.create(supply_chain=sc) StrategicActionUpdateFactory( status=Status.SUBMITTED, submission_date=date.today(), strategic_action=sa, supply_chain=sc, ) yield { "sc_name": sc_name, "url": reverse( "supply-chain-update-complete", kwargs={"supply_chain_slug": slugify(sc_name)}, ), }
def test_command_execution_imports_scenario_assessments(self): supply_chain_one: SupplyChain = SupplyChainFactory( name="Supply Chain One") supply_chain_two: SupplyChain = SupplyChainFactory( name="Supply Chain Two") call_command("ingestscenarioassessments", "no_value_needed_due_to_mock_open") assert supply_chain_one.scenario_assessment assert supply_chain_two.scenario_assessment scenario_assessment_one: ScenarioAssessment = ( supply_chain_one.scenario_assessment) assert (scenario_assessment_one.ports_blocked_rag_rating == NullableRAGRating.NONE) assert (scenario_assessment_one.storage_full_impact == "One storage full implication") assert scenario_assessment_one.labour_shortage_is_critical == False assert scenario_assessment_one.demand_spike_critical_scenario == "" scenario_assessment_two: ScenarioAssessment = ( supply_chain_two.scenario_assessment) assert (scenario_assessment_two.ports_blocked_rag_rating == NullableRAGRating.AMBER) assert (scenario_assessment_two.raw_material_shortage_impact == "Two raw material shortage implication") assert scenario_assessment_two.labour_shortage_is_critical == True assert (scenario_assessment_two.demand_spike_critical_scenario == "Two demand spike critical scenario")
def test_sc_homepage_update_incomplete(logged_in_client, test_user): """Test update_complete is False. 'update_complete' should be False in context passed to homepage when not all supply chains linked to the user's gov department have a last_submission date after last deadline. """ # Arrange for _ in range(3): sc = SupplyChainFactory(gov_department=test_user.gov_department, last_submission_date=date.today()) StrategicActionFactory(supply_chain=sc) sc2 = SupplyChainFactory( gov_department=test_user.gov_department, last_submission_date=date.today() - timedelta(35), ) StrategicActionFactory(supply_chain=sc2) # Act response = logged_in_client.get("/supply-chains/") # Assert assert response.status_code == 200 assert not response.context["update_complete"] assert response.context["num_updated_supply_chains"] == 3 assert response.context["num_in_prog_supply_chains"] == 3 assert "Complete your monthly update" not in response.rendered_content assert ("Select a supply chain to provide your regular monthly update" in response.rendered_content) assert "Your monthly update is complete" not in response.rendered_content assert ("All supply chains have been completed for this month" not in response.rendered_content)
def test_sc_summary_multiple(self, logged_in_client, test_user): # Arrange SupplyChainFactory.create_batch( 4, gov_department=test_user.gov_department) # Act resp = logged_in_client.get(reverse("supply-chain-summary")) # Assert assert resp.status_code == 200 assert resp.context["supply_chains"].paginator.count == 4
def test_sc_homepage_pagination(num_supply_chains, logged_in_client, test_user, url, num_supply_chains_returned): """Test pagination of supply chains on homepage. Check the correct number of supply chains are passed to homepage via context depending on total number linked to the user's department, and that the supply chains are annotated with 'strategic_action_count'. """ SupplyChainFactory.create_batch(num_supply_chains, gov_department=test_user.gov_department) response = logged_in_client.get(url) assert response.status_code == 200 assert response.context[ "gov_department_name"] == test_user.gov_department.name assert len(response.context["supply_chains"]) == num_supply_chains_returned
def test_last_monthly_update(): supply_chain = SupplyChainFactory() strategic_action = StrategicActionFactory(supply_chain=supply_chain) march_strategic_action_update: StrategicActionUpdate = StrategicActionUpdateFactory( strategic_action=strategic_action, supply_chain=strategic_action.supply_chain, submission_date=date(year=2021, month=3, day=31), status=StrategicActionUpdate.Status.SUBMITTED, ) february_strategic_action_update: StrategicActionUpdate = ( StrategicActionUpdateFactory( strategic_action=strategic_action, supply_chain=strategic_action.supply_chain, submission_date=date(year=2021, month=2, day=17), status=StrategicActionUpdate.Status.SUBMITTED, )) january_strategic_action_update: StrategicActionUpdate = ( StrategicActionUpdateFactory( strategic_action=strategic_action, supply_chain=strategic_action.supply_chain, submission_date=date(year=2021, month=1, day=1), status=StrategicActionUpdate.Status.SUBMITTED, )) last_submitted: StrategicActionUpdate = strategic_action.last_submitted_update( ) assert (last_submitted.submission_date == march_strategic_action_update.submission_date)
def test_dump_sc_data_multiple(self): # Arrange SupplyChainFactory.create_batch(5) # Act self.invoke_dump(MODEL_SUPPLY_CHAIN, self.data_file.name) rows = self.load_csv() # Assert assert len(rows) == 5 names = [x["name"] for x in rows] assert all([x.startswith("Product ") for x in names]) ids = [x["id"] for x in rows] assert len(ids) == len(set(ids))
def test_update_details_ongoing(self, logged_in_client, test_user): # Arrange sc_name = "ceramics" sa_name = "action 01" update_slug = "05-2021" sc = SupplyChainFactory.create( name=sc_name, gov_department=test_user.gov_department, last_submission_date=date.today(), ) sa = StrategicActionFactory.create(name=sa_name, supply_chain=sc, is_ongoing=True) StrategicActionUpdateFactory( slug=update_slug, status=Status.SUBMITTED, submission_date=date.today(), strategic_action=sa, supply_chain=sc, ) # Act resp = logged_in_client.get( reverse( "monthly-update-review", kwargs={ "supply_chain_slug": sc_name, "action_slug": slugify(sa_name), "update_slug": update_slug, }, )) # Assert assert resp.context["supply_chain_name"] == sc_name assert resp.context["completion_estimation"] == "Ongoing"
def scenario_assessment_queryset( last_modified_times) -> ScenarioAssessmentQuerySet: supply_chain = SupplyChainFactory(name="SA OneToOne Test Supply Chain") with mock.patch("django.utils.timezone.now") as mock_now: mock_now.return_value = last_modified_times[0] ScenarioAssessmentFactory(supply_chain=supply_chain) return ScenarioAssessment.objects.all()
def bit_of_everything_queryset(bit_of_everything_item_count, bit_of_everything_last_modified_times): """ Generate a bunch of SCs, SA, and SAUs so we can be sure there's a good mix for the union queryset. Doing them this rather odd way helps check that the ordering works as expected by interleaving the last_modified values among the different models, such that simply concatenating the instances from each queryset would not be ordered by last_modified. To get an idea of what comes out, run the following at the console: ['sc' if i % 8 == 0 else 'sa' if i % 4 == 1 else 'sau' for i in range(0, 23)] """ supply_chain_in_use = None strategic_action_in_use = None gov_department: GovDepartment = GovDepartment.objects.first() user = UserFactory(gov_department=gov_department) with mock.patch("django.utils.timezone.now") as mock_now: for i, last_modified in enumerate( bit_of_everything_last_modified_times): mock_now.return_value = last_modified if i % 8 == 0: supply_chain_in_use = SupplyChainFactory( gov_department=gov_department) continue if i % 5 == 1: strategic_action_in_use = StrategicActionFactory( supply_chain=supply_chain_in_use) continue StrategicActionUpdateFactory( supply_chain=supply_chain_in_use, strategic_action=strategic_action_in_use, user=user, status=StrategicActionUpdate.Status.SUBMITTED, )
def test_auth_no_perm(self, logged_in_client): # Arrange sc_name = "ceramics" dep = GovDepartmentFactory() sc = SupplyChainFactory(gov_department=dep, name=sc_name) sa = StrategicActionFactory.create(supply_chain=sc) sau = StrategicActionUpdateFactory( status=Status.SUBMITTED, submission_date=date.today(), strategic_action=sa, supply_chain=sc, ) # Act resp = logged_in_client.get( reverse( "monthly-update-review", kwargs={ "supply_chain_slug": slugify(sc_name), "action_slug": sa.slug, "update_slug": sau.slug, }, )) # Assert assert resp.status_code == 403
def test_dump_sau_data_multiple(self): # Arrange exp_sc_ids = list() exp_sa_ids = list() for _ in range(4): sc = SupplyChainFactory() sa = StrategicActionFactory(supply_chain=sc) StrategicActionUpdateFactory(supply_chain=sc, strategic_action=sa) exp_sc_ids.append(str(sc.id)) exp_sa_ids.append(str(sa.id)) # Act self.invoke_dump(MODEL_STRAT_ACTION_UPDATE, self.data_file.name) rows = self.load_csv() # Assert assert len(rows) == 4 ids = [x["id"] for x in rows] assert len(ids) == len(set(ids)) sc_ids = [x["supply_chain"] for x in rows] assert all( [a == b for a, b in zip(sorted(sc_ids), sorted(exp_sc_ids))]) sa_ids = [x["strategic_action"] for x in rows] assert all( [a == b for a, b in zip(sorted(sa_ids), sorted(exp_sa_ids))])
def tasklist_stub(test_user): sc_name = "Supply Chain 1" sa_description = "1234567890qweertyuiodfsfgfgggsf" sa_name = "SA 00" sc = SupplyChainFactory(name=sc_name, gov_department=test_user.gov_department) sa = StrategicActionFactory.create_batch(4, name=sa_name, description=sa_description, supply_chain=sc) yield { "sc_name": sc_name, "sa_description": sa_description, "sa_name": sa_name, "url": reverse("supply-chain-task-list", kwargs={"supply_chain_slug": slugify(sc_name)}), "sc": sc, "sa": sa, }
def test_SC_complete_summary_with_archived(self, logged_in_client, taskcomp_stub, test_user): # Arrange SupplyChainFactory.create( is_archived=True, archived_reason="reason", gov_department=test_user.gov_department, ) # Act resp = logged_in_client.get(taskcomp_stub["url"]) # Assert assert SupplyChain.objects.all().count() == 4 assert resp.context["view"].sum_of_supply_chains == 3 assert resp.context["view"].num_updated_supply_chains == 1
def test_since_with_filter(self): # Arrange sc = SupplyChainFactory.create(name="Supply Chain 1") sa = StrategicActionFactory.create(name="SA 01", supply_chain=sc) sau = StrategicActionUpdateFactory.create( status=StrategicActionUpdate.Status.IN_PROGRESS, supply_chain=sc, strategic_action=sa, ) # Act sau_prog = StrategicActionUpdate.objects.since( deadline=date.today() - timedelta(days=1), supply_chain=sc, status=StrategicActionUpdate.Status.IN_PROGRESS, ) sau_comp = StrategicActionUpdate.objects.since( deadline=date.today() - timedelta(days=1), supply_chain=sc, status=StrategicActionUpdate.Status.READY_TO_SUBMIT, ) # Assert assert sau_prog[0] == sau assert sau_comp.count() == 0
def test_SC_no_umbrella(self, test_user): # Arrange # Act sc = SupplyChainFactory.build(gov_department=test_user.gov_department, supply_chain_umbrella=None) # Assert self.validate(sc, {}, objects_saved=1)
def test_sc_homepage_filters_out_archived_supply_chains( logged_in_client, test_user): gov_department = test_user.gov_department # Create archived supply chains SupplyChainFactory.create_batch(5, gov_department=gov_department, is_archived=True, archived_reason="Reason") # Create non archived supply chains SupplyChainFactory.create_batch(5, gov_department=gov_department) num_unarchived_supply_chains = SupplyChain.objects.filter( is_archived=False).count() response = logged_in_client.get(reverse("sc-home")) assert len( response.context["supply_chains"]) == num_unarchived_supply_chains
def setup_method(self): self.supply_chain = SupplyChainFactory() self.strategic_action = StrategicActionFactory( supply_chain=self.supply_chain) self.strategic_action_update = StrategicActionUpdateFactory( strategic_action=self.strategic_action, supply_chain=self.strategic_action.supply_chain, )
def strategic_action_with_is_ongoing(test_user): supply_chain: SupplyChain = SupplyChainFactory() strategic_action: StrategicAction = StrategicActionFactory( supply_chain=supply_chain, target_completion_date=None, is_ongoing=True ) test_user.gov_department = supply_chain.gov_department test_user.save() return strategic_action
def test_strat_action_summary_page_pagination(logged_in_client, test_user): """Test pagination of strategic actions returned in context.""" sc = SupplyChainFactory(gov_department=test_user.gov_department) StrategicActionFactory.create_batch(14, supply_chain=sc) response = logged_in_client.get( "%s?page=3" % reverse("strategic-action-summary", args=[sc.slug])) assert response.status_code == 200 assert len(response.context["strategic_actions"]) == 4
def setup_method(self): supply_chain = SupplyChainFactory() self.strategic_action = StrategicActionFactory( supply_chain=supply_chain, ) self.strategic_action_update: StrategicActionUpdate = ( StrategicActionUpdate.objects.create( supply_chain=supply_chain, strategic_action=self.strategic_action))
def setup_method(self): supply_chain = SupplyChainFactory() self.strategic_action: StrategicAction = StrategicActionFactory( supply_chain=supply_chain, target_completion_date=self.current_completion_date, ) self.strategic_action_update: StrategicActionUpdate = ( StrategicActionUpdate.objects.create( supply_chain=supply_chain, strategic_action=self.strategic_action))
def test_source_redundant_transition_period_scenario_ignored(self): SupplyChainFactory(name="Supply Chain Two") command = ingestscenarioassessments.Command() assessments = list(command.source_assessments) assessment = assessments[0] transition_period_values = [ key for key in assessment.keys() if key.startswith("End_of_transition") ] assert len(transition_period_values) == 0
def test_auth_wo_perm(self, logged_in_client): # Arrange dep = GovDepartmentFactory() sc = SupplyChainFactory(gov_department=dep, name="ceramics") # Act resp = logged_in_client.get(reverse("privacy")) # Assert assert resp.status_code == 200
def test_update_on_umbrella(slef, logged_in_client, test_user): # Arrange u_name = "houseware" sc_name = "ceramics" u = SupplyChainUmbrellaFactory.create(name=u_name) SupplyChainFactory.create( name=sc_name, gov_department=test_user.gov_department, supply_chain_umbrella=u, ) # Act resp = logged_in_client.get( reverse("supply-chain-task-list", kwargs={"supply_chain_slug": slugify(u_name)}), ) # Assert assert resp.context["view"].supply_chain_name == u_name
def strategic_action_with_completion_date(test_user): supply_chain: SupplyChain = SupplyChainFactory() strategic_action: StrategicAction = StrategicActionFactory( supply_chain=supply_chain, target_completion_date=date.today() + relativedelta(months=2), is_ongoing=False, ) test_user.gov_department = supply_chain.gov_department test_user.save() return strategic_action
def test_sc_risk_status(): # Arrange sc_name = "optional" # Act sc = SupplyChainFactory.create(name=sc_name) # Assert assert SupplyChain.objects.count() == 1 assert SupplyChain.objects.get(name=sc_name).risk_severity_status == ""
def test_archived_no_reason(self, test_user): # Arrange # Act sc = SupplyChainFactory.build( gov_department=test_user.gov_department, is_archived=True, ) # Assert self.validate(sc, {ArcReason: ERROR_MSGS[ArcReason]}, objects_saved=0)
def test_dump_sc_data(self): # Arrange SupplyChainFactory() # Act self.invoke_dump(MODEL_SUPPLY_CHAIN, self.data_file.name) rows = self.load_csv() # Assert assert len(rows) == 1 assert re.match(f"Product ", rows[0]["name"])