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 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_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 setup_method(self, *args, **kwargs): 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 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 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_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 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 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 strategic_action_queryset(supply_chain, last_modified_times) -> StrategicActionQuerySet: with mock.patch("django.utils.timezone.now") as mock_now: for last_modified in last_modified_times: mock_now.return_value = last_modified StrategicActionFactory(supply_chain=supply_chain) return StrategicAction.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_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 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 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 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_pagination(self, logged_in_client, test_user, sc_name, num_sas, url, actions_returned): # Arrange sc = SupplyChainFactory(name=sc_name, gov_department=test_user.gov_department) StrategicActionFactory.create_batch(num_sas, name=f"{sc_name} 00", supply_chain=sc) # Act resp = logged_in_client.get(url) # Assert p = resp.context["view"].sa_updates assert resp.status_code == 200 assert len(p.object_list) == actions_returned
def test_archived_actions_dont_appear_in_task_list(self, logged_in_client, tasklist_stub): # Arrange sc = tasklist_stub["sc"] StrategicActionFactory.create_batch(5, is_archived=True, archived_reason="Reason", supply_chain=sc) # Act resp = logged_in_client.get(tasklist_stub["url"]) # Assert num_unarchived_actions = StrategicAction.objects.filter( supply_chain=sc, is_archived=False).count() v = resp.context["view"] assert v.total_sa == num_unarchived_actions
def test_strat_action_summary_page_success(logged_in_client, test_user): """Test authenticated request. When an authorised request is made to the strategic_action_summary URL, from a user linked to the same gov department as the strategic actions, the correct supply chain and only un_archived strategic actions are returned in context. """ sc = SupplyChainFactory(gov_department=test_user.gov_department) StrategicActionFactory.create_batch(5, supply_chain=sc) StrategicActionFactory.create_batch(2, supply_chain=sc, is_archived=True, archived_reason="A reason") response = logged_in_client.get( reverse("strategic-action-summary", args=[sc.slug])) assert response.status_code == 200 assert response.context["supply_chain"] == sc assert len(response.context["strategic_actions"]) == 5
def test_no_archived_date_save_strategic_action(): """ Test validation error is raised if an archived strategic action is saved without an archived_reason. """ with pytest.raises(ValidationError) as excifno: StrategicActionFactory(is_archived=True) assert ( "An archived_reason must be given when archiving a strategic action." in excifno.value.messages)
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 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 setup_method(self): supply_chain: SupplyChain = SupplyChainFactory() strategic_action: StrategicAction = StrategicActionFactory( supply_chain=supply_chain ) self.strategic_action_update: StrategicActionUpdate = ( StrategicActionUpdateFactory( strategic_action=strategic_action, supply_chain=strategic_action.supply_chain, ) )
def test_slug_init_without_factory(self): # Arrange strategic_action = StrategicActionFactory() # Act sau = StrategicActionUpdate.objects.create( status=StrategicActionUpdate.Status.IN_PROGRESS, supply_chain=strategic_action.supply_chain, strategic_action=strategic_action, ) # Assert assert date.today().strftime("%m-%Y") == sau.slug
def update_stub(test_user): sc_name = "Supply Chain 1" sa_name = "action 01" sa_completion = "2021-02-01" update_slug = "05-2021" sau_rag = "Amber" sau_reason = "Brexit negotiations" 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, target_completion_date=sa_completion) StrategicActionUpdateFactory( slug=update_slug, status=Status.SUBMITTED, submission_date=date.today(), strategic_action=sa, supply_chain=sc, implementation_rag_rating=sau_rag, reason_for_delays=sau_reason, ) yield { "sc": sc, "sa": sa, "sc_name": sc_name, "sa_name": sa_name, "sa_completion": sa_completion, "sau_rag": sau_rag, "sau_reason": sau_reason, "update_slug": update_slug, "url": reverse( "monthly-update-review", kwargs={ "supply_chain_slug": slugify(sc_name), "action_slug": slugify(sa_name), "update_slug": update_slug, }, ), }
def test_form_saves_the_content(self): supply_chain = SupplyChainFactory() strategic_action = StrategicActionFactory(supply_chain=supply_chain) strategic_action_update = StrategicActionUpdate.objects.create( supply_chain=supply_chain, strategic_action=strategic_action) assert strategic_action_update.content == "" form_data = {"content": "Now is the winter of our discontent"} form = MonthlyUpdateInfoForm(data=form_data, instance=strategic_action_update) assert form.is_valid() saved_instance = form.save() assert saved_instance.content == form_data["content"]
def model_stub(test_user): sc = SupplyChainFactory.create( name=sc_name, gov_department=test_user.gov_department, last_submission_date=date.today(), ) sa = StrategicActionFactory.create(supply_chain=sc, name=sa_name) StrategicActionUpdateFactory( status=Status.SUBMITTED, submission_date=date(day=1, month=1, year=2020), strategic_action=sa, supply_chain=sc, )
def test_dump_sa_data(self): # Arrange sc = SupplyChainFactory() StrategicActionFactory(supply_chain=sc) # Act self.invoke_dump(MODEL_STRAT_ACTION, self.data_file.name) rows = self.load_csv() # Assert assert len(rows) == 1 assert re.match(f"Strategic action ", rows[0]["name"]) assert rows[0]["supply_chain"] == str(sc.id)
def test_dump_sau_data(self): # Arrange sc = SupplyChainFactory() sa = StrategicActionFactory(supply_chain=sc) StrategicActionUpdateFactory(supply_chain=sc, strategic_action=sa) # Act self.invoke_dump(MODEL_STRAT_ACTION_UPDATE, self.data_file.name) rows = self.load_csv() # Assert assert len(rows) == 1 assert rows[0]["supply_chain"] == str(sc.id) assert rows[0]["strategic_action"] == str(sa.id)
def sau_stub(test_user): sc_name = "carbon" sa_name = "Source raw packaging" sc = SupplyChainFactory(name=sc_name, gov_department=test_user.gov_department) sa = StrategicActionFactory(name=sa_name, supply_chain=sc) yield { "user": test_user, "sc_name": sc_name, "sa_name": sa_name, "sc": sc, "sa": sa, }
def test_sc_homepage_summary_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, ) StrategicActionFactory.create_batch(5, supply_chain=active_sa) StrategicActionFactory.create_batch(3, supply_chain=sc, is_archived=True, archived_reason="Reason") # Act resp = logged_in_client.get(reverse("sc-home")) # Assert assert resp.context["update_complete"] == False assert resp.context["num_in_prog_supply_chains"] == 1 assert len(resp.context["supply_chains"]) == 2