def test_reviews_have_confirmed_dates_in_the_past_emails_are_created( make_review): target = make_review( consultation_start=get_today() - timedelta(days=1), dates_confirmed=True, add_stakeholders=True, ) make_review( consultation_start=get_today() + timedelta(days=1), dates_confirmed=True, add_stakeholders=True, ) send_open_review_notifications() target_contacts = Contact.objects.filter( stakeholder__reviews=target).distinct() assert (Email.objects.count() == len(target_contacts) + 1 ) # add 1 for the phe comms notification assert (Email.objects.filter( address=target_contacts[0].email, status=Email.STATUS.pending, context=target.get_email_context( **{"recipient name": target_contacts[0].name}), template_id=settings.NOTIFY_TEMPLATE_CONSULTATION_OPEN, ).count() == len(target_contacts)) assert Email.objects.filter( address=settings.PHE_COMMUNICATIONS_EMAIL, status=Email.STATUS.pending, context=target.get_email_context( **{"recipient name": settings.PHE_COMMUNICATIONS_NAME}), template_id=settings.NOTIFY_TEMPLATE_CONSULTATION_OPEN, ).exists()
def test_no_reviews_have_confirmed_dates_emails_are_not_created(make_review): make_review(consultation_start=get_today() - timedelta(days=1), dates_confirmed=False) make_review(consultation_start=get_today() - timedelta(days=2), dates_confirmed=False) send_open_review_notifications() assert not Email.objects.exists()
def test_yesterday_is_overdue(make_policy): """ Test the queryset overdue() method does not include policies with a next review date of today. """ make_policy(next_review=get_today()) instance = make_policy(next_review=get_today() - relativedelta(days=1)) actual = [obj.pk for obj in Policy.objects.overdue()] assert [instance.pk] == actual
def test_no_reviews_have_open_consultation_time_in_the_past_no_emails_are_not_created( make_review, ): make_review(consultation_start=get_today() + timedelta(days=1), dates_confirmed=True) make_review(consultation_start=get_today() + timedelta(days=2), dates_confirmed=True) send_open_review_notifications() assert not Email.objects.exists()
def test_today_is_upcoming(make_policy): """ Test the queryset upcoming() method includes policies with a next review date of today. """ make_policy(next_review=get_today()) yesterday = get_today() - relativedelta(days=1) expected = [obj.pk for obj in Policy.objects.filter(next_review__gt=yesterday)] actual = [obj.pk for obj in Policy.objects.upcoming()] assert expected == actual
def test_consultation_end_must_come_after_consultation_start(): with freeze_time(): today = get_today() tomorrow = get_today() + relativedelta(days=+1) review = baker.make(Review, consultation_start=tomorrow, consultation_end=today) data = form_for_review(review) assert not ReviewDatesForm(instance=review, data=data).is_valid()
def test_search_on_open_for_comment(django_app_form): """ Test the list of policies can be filtered by whether the policy is under review and currently open for the public to comment. """ tomorrow = get_today() + relativedelta(days=+1) later = get_today() + relativedelta(months=+3) policy = baker.make(Policy, name="name") review = baker.make(Review, consultation_start=tomorrow, consultation_end=later) policy.reviews.add(review) response = django_app_form(condition_list_url, comments="open") assert not response.context["object_list"]
def test_nsc_meeting_date_must_be_valid(): today = get_today() soon = get_today() + relativedelta(months=+3) review = baker.make(Review, consultation_start=today, consultation_end=soon) data = form_for_review(review) key = random.choice(["nsc_meeting_date_month", "nsc_meeting_date_year"]) data[key] = "0" assert not ReviewDatesForm(instance=review, data=data).is_valid()
def test_opening_consultation_initializes_dates_starting_today(): with freeze_time(): start = get_today() end = get_today() + relativedelta(months=+3) form = ReviewDatesForm(instance=baker.make(Review)) assert form.initial["consultation_start_day"] == start.day assert form.initial["consultation_start_month"] == start.month assert form.initial["consultation_start_year"] == start.year assert form.initial["consultation_end_day"] == end.day assert form.initial["consultation_end_month"] == end.month assert form.initial["consultation_end_year"] == end.year
def test_nsc_meeting_must_be_in_the_same_month_as_consultation_end_or_later(): with freeze_time(): today = get_today() next_month = get_today() + relativedelta(months=+1) review = baker.make( Review, consultation_start=today, consultation_end=next_month, nsc_meeting_date=today, ) data = form_for_review(review) assert not ReviewDatesForm(instance=review, data=data).is_valid()
def test_nsc_meeting_date_set_from_fields(): with freeze_time(): today = get_today() tomorrow = get_today() + relativedelta(days=+1) review = baker.make(Review, nsc_meeting_date=today) data = form_for_review(review) data["nsc_meeting_date_month"] = tomorrow.month form = ReviewDatesForm(instance=review, data=data) assert form.is_valid() assert form.cleaned_data["nsc_meeting_date"] == tomorrow.replace(day=1)
def test_review_start_is_updated(): """ Test the review_start field can be set when a Review is created. """ tomorrow = get_today() + relativedelta(days=+1) instance = baker.make(Review, review_start=tomorrow) assert instance.review_start == tomorrow
def clean_name(self): name = self.cleaned_data.get("name") if not name and getattr(self.policy_formset, "cleaned_data", {}): policies = [ e.get("policy") for e in self.policy_formset.cleaned_data ] if not policies: return "" joined_policy_names_front = ", ".join(e.name for e in policies[:-1]) joined_policy_names_back = policies[-1].name joined_policy_names_template = ( "{front} and {back}" if joined_policy_names_front and joined_policy_names_back else "{back}") joined_policy_names = joined_policy_names_template.format( front=joined_policy_names_front, back=joined_policy_names_back, ) name = _("%s %d review" % (joined_policy_names, get_today().year)) slug = slugify(name) if Review.objects.filter(slug=slug).exists(): raise ValidationError(_("A review with this name already exists")) return name
def get_initial(self): initial = super().get_initial() start = self.object.consultation_start end = self.object.consultation_end meeting = self.object.nsc_meeting_date if start is None: initial["consultation_open"] = None else: initial["consultation_open"] = start == get_today() initial["consultation_start_day"] = start.day initial["consultation_start_month"] = start.month initial["consultation_start_year"] = start.year if end: initial["consultation_end_day"] = end.day initial["consultation_end_month"] = end.month initial["consultation_end_year"] = end.year if meeting: initial["nsc_meeting_date_day"] = meeting.day initial["nsc_meeting_date_month"] = meeting.month initial["nsc_meeting_date_year"] = meeting.year return initial
def test_policy_is_closed(django_app): """ Test a policy is not annotated with the current review outside of the public consultation period """ tomorrow = get_today() + relativedelta(days=+1) later = get_today() + relativedelta(months=+1) policy = baker.make(Policy) review = baker.make(Review, consultation_start=tomorrow, consultation_end=later) policy.reviews.add(review) response = django_app.get(condition_list_url) policies = response.context["object_list"] assert not policies.first().reviews_in_consultation assert "OPEN" not in response.text
def test_if_dates_are_already_confirmed_error_is_not_raised(): with freeze_time(): today = get_today() next_month = get_today() + relativedelta(months=+1) review = baker.make( Review, consultation_start=today, consultation_end=next_month, nsc_meeting_date=next_month, dates_confirmed=True, ) data = form_for_review(review) form = ReviewDatesForm(instance=review, data=data) assert form.is_valid()
def test_both_start_and_end_dates_must_be_blank_together(): with freeze_time(): review = baker.make(Review, consultation_start=None, consultation_end=get_today()) data = form_for_review(review) assert not ReviewDatesForm(instance=review, data=data).is_valid()
def test_no_review_is_overdue(make_policy): """ Test the queryset overdue() method includes records where next_review is not set. """ make_policy(next_review=get_today()) instance = make_policy(next_review=None) actual = [obj.pk for obj in Policy.objects.overdue()] assert [instance.pk] == actual
def test_today_is_not_overdue(make_policy): """ Test the queryset overdue() method does not include policies with a next review date of today. """ make_policy(next_review=get_today()) actual = [obj.pk for obj in Policy.objects.overdue()] assert [] == actual
def review_in_pre_consultation(make_review): review_start = get_today() - relativedelta(months=2) review = make_review(name="Evidence Review", review_start=review_start) baker.make(Document, name="document", document_type="evidence_review", review=review) return review
def upcoming(self): """ Get the policies due for a review in the next 12 month, ordered by the next review date, soonest, first. """ today = get_today() next_year = today + relativedelta(months=12) return self.filter(next_review__gte=today, next_review__lt=next_year)
def next_review_display(self): today = get_today() if self.next_review is None: return _("No review has been scheduled") if self.next_review < today: return _("Overdue") else: return self.next_review.strftime("%B %Y")
def test_policy_is_open(django_app): """ Test a policy is annotated with the current review when it is open for public comment """ today = get_today() later = get_today() + relativedelta(months=+3) policy = baker.make(Policy) review = baker.make(Review, consultation_start=today, consultation_end=later) policy.reviews.add(review) response = django_app.get(condition_list_url) policies = response.context["object_list"] assert policy in policies assert policies.first().reviews_in_consultation[0].pk == review.pk assert "OPEN" in response.text
def test_review_start_is_not_updated_later(): """ Test the review_start field is not changed if set already. """ tomorrow = get_today() + relativedelta(days=+1) instance = baker.make(Review, review_start=tomorrow) instance.save() assert instance.review_start == tomorrow
def test_yesterday_is_not_upcoming(make_policy): """ Test the queryset upcoming() method excludd policies with a next review date in the past. """ make_policy(next_review=get_today() - relativedelta(days=1)) actual = [obj.pk for obj in Policy.objects.upcoming()] assert [] == actual
def test_consultation_start_set_from_fields(): with freeze_time(): start = get_today() tomorrow = get_today() + relativedelta(days=+1) review = baker.make(Review, consultation_start=start, consultation_end=tomorrow) data = form_for_review(review) data["consultation_start_day"] = tomorrow.day data["consultation_start_month"] = tomorrow.month data["consultation_start_year"] = tomorrow.year form = ReviewDatesForm(instance=review, data=data) assert form.is_valid() assert form.cleaned_data["consultation_start"] == tomorrow
def test_overdue(make_policy): """ Test the queryset overdue() method includes records where next_review is in the past. """ make_policy(next_review=relative_date(months=+1)) make_policy(next_review=relative_date(months=-1)) expected = [obj.pk for obj in Policy.objects.filter(next_review__lt=get_today())] actual = [obj.pk for obj in Policy.objects.overdue()] assert expected == actual
def test_response_is_yes_next_review_is_updated( erm_user, make_review, make_policy, make_summary_draft, django_app ): first_policy, second_policy = make_policy(_quantity=2, next_review=get_today()) review = make_review(policies=[first_policy, second_policy]) response = django_app.get( reverse("review:publish", kwargs={"slug": review.slug}), user=erm_user ) form = response.forms[1] form["published"] = True form.submit() first_policy.refresh_from_db() second_policy.refresh_from_db() assert first_policy.next_review.year == get_today().year + 3 assert second_policy.next_review.year == get_today().year + 3
def test_add_recommendation_view__updated(policy, erm_user, django_app): summary = django_app.get(reverse("policy:add:recommendation", args=(policy.slug, )), user=erm_user) summary_form = summary.forms[1] summary_form["recommendation"] = True summary_form["next_review"] = get_today().year result = summary_form.submit() assert result.status == "302 Found" assert result.url == reverse("review:add") + f"?policy={policy.slug}" policy.refresh_from_db() assert policy.recommendation assert policy.next_review == datetime.date(get_today().year, 1, 1) assert policy.is_active
def get_initial(self): initial = super().get_initial() slug = self.request.GET.get("policy", None) if slug: policy = Policy.objects.filter(slug=slug).first() if policy: initial["policies"] = [policy.pk] initial["name"] = _("%s %d review" % (policy.name, get_today().year)) return initial