Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TP2000-1503 Auto end-date measures #1345

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
mattjamc committed Dec 2, 2024
commit 6321fa03d1edfb9050c4f66fdd77ebaf89159671
4 changes: 2 additions & 2 deletions workbaskets/jinja2/workbaskets/auto_end_date_measures.jinja
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@
{{ table_rows.append([
{"text": measure_link},
{"html": commodity_link if measure.goods_nomenclature else "-"},
{"text": "{:%d %b %Y}".format(measure.goods_nomenclature.valid_between.upper) if measure.goods_nomenclature.valid_between.upper else "-" },
{"text": "{:%d %b %Y}".format(measure.goods_nomenclature.version_at(workbasket.transactions.last()).valid_between.upper) if measure.goods_nomenclature.version_at(workbasket.transactions.last()).valid_between.upper else "-" },
{"text": "{:%d %b %Y}".format(measure.valid_between.lower) },
{"text": action},
]) or "" }}
@@ -45,7 +45,7 @@
{% endset %}

{% if object_list %}
<p>The following measures' commodity codes have been end-dated in your workbasket. Click submit to end-date or delete these measures.</p>
<p>The following measures are linked to commodity codes that have been end-dated in your workbasket. Click submit to automatically end-date or delete these measures.</p>
{{ govukTable({
"head": [
{"text": "Measure SID"},
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
{% from "components/panel/macro.njk" import govukPanel %}
{% from "components/button/macro.njk" import govukButton %}

{% set page_title = "Measures ended" %}
{% set page_title = "Measures submitted to be ended" %}

{% block breadcrumb %}
{{ govukBreadcrumbs({
@@ -22,10 +22,11 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
{{ govukPanel({
"titleText": page_title,
"text": "You have end dated or deleted measures linked to commodities end dated in workbasket " ~ request.user.current_workbasket.id,
"titleText": "X measures will be ended",
"text": "You have successfully submitted X measures to be end-dated or deleted and they will be added to workbasket " ~ request.user.current_workbasket.id ~ " when complete.",
"classes": "govuk-!-margin-bottom-7"
}) }}
<h2 class="govuk-heading-m">Next steps</h2>
<a href="{{ url('workbaskets:edit-workbasket') }}" class="govuk-button govuk-button--primary">Continue to workbasket</a>
</div>
</div>
11 changes: 7 additions & 4 deletions workbaskets/tasks.py
Original file line number Diff line number Diff line change
@@ -105,10 +105,13 @@ def end_measures(measures, workbasket):
workbasket=workbasket,
workbasket__status=WorkflowStatus.EDITING,
).order_by("order")

commodity = GoodsNomenclature.objects.all().get(
pk=measure.goods_nomenclature_id,
transaction__workbasket=workbasket,
commodity = (
GoodsNomenclature.objects.all()
.filter(
sid=measure.goods_nomenclature.sid,
transaction__workbasket=workbasket,
)
.last()
)
if measure.valid_between.lower > commodity.valid_between.upper:
continue
101 changes: 99 additions & 2 deletions workbaskets/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
from checks.tests.factories import TrackedModelCheckFactory
from common.inspect_tap_tasks import CeleryTask
from common.inspect_tap_tasks import TAPTasks
from common.models.trackedmodel import TrackedModel
from common.models.utils import override_current_transaction
from common.tests import factories
from common.tests.util import date_post_data
@@ -28,6 +29,7 @@
from tasks.models import Comment
from tasks.models import UserAssignment
from workbaskets import models
from workbaskets.tasks import call_end_measures
from workbaskets.tasks import check_workbasket_sync
from workbaskets.validators import WorkflowStatus
from workbaskets.views import ui
@@ -2608,7 +2610,9 @@ def test_current_tasks_is_called(valid_user_client):


def test_remove_all_workbasket_changes_button_only_shown_to_superusers(
client, user_workbasket, superuser
client,
user_workbasket,
superuser,
):
url = reverse(
"workbaskets:workbasket-ui-changes",
@@ -2626,7 +2630,8 @@ def test_remove_all_workbasket_changes_button_only_shown_to_superusers(


def test_remove_all_workbasket_changes_button_not_shown_to_users_without_permision(
valid_user_client, user_workbasket
valid_user_client,
user_workbasket,
):
url = reverse(
"workbaskets:workbasket-ui-changes",
@@ -2639,3 +2644,95 @@ def test_remove_all_workbasket_changes_button_not_shown_to_users_without_permisi

remove_all_button = page.find("button", value="remove-all")
assert not remove_all_button


@pytest.fixture
def commodity_with_measures(workbasket, date_ranges):
commodity = factories.GoodsNomenclatureFactory.create(
valid_between=date_ranges.no_end,
)
measures = factories.MeasureFactory.create_batch(10, goods_nomenclature=commodity)
future_measures = factories.MeasureFactory.create_batch(
2,
goods_nomenclature=commodity,
valid_between=date_ranges.future,
)
return commodity


def test_auto_end_measures_renders(
valid_user_client,
user_workbasket,
commodity_with_measures,
date_ranges,
):
"""Test that the list of measures to be ended renders correctly."""
commodity_with_measures.new_version(
workbasket=user_workbasket,
valid_between=date_ranges.big,
)
url = reverse(
"workbaskets:workbasket-ui-auto-end-date-measures",
kwargs={"wb_pk": user_workbasket.pk},
)
response = valid_user_client.get(url)
assert response.status_code == 200
page = BeautifulSoup(str(response.content), "html.parser")
rows = page.find_all("tr", {"class": "govuk-table__row"})
text = page.get_text()
assert text.count("To be end-dated") == 10
assert text.count("To be deleted") == 2
assert len(rows) == 13


@patch("workbaskets.tasks.call_end_measures.apply_async")
def test_auto_end_measures_post(
call_check_end_measures,
valid_user_client,
commodity_with_measures,
user_workbasket,
date_ranges,
):
"""Test that posting the auto end measures form results in the end_measures
Celery task being called and redirects to the confirmation page."""
commodity_with_measures.new_version(
workbasket=user_workbasket,
valid_between=date_ranges.big,
)
url = reverse(
"workbaskets:workbasket-ui-auto-end-date-measures",
kwargs={"wb_pk": user_workbasket.pk},
)
response = valid_user_client.post(url, {"action": "auto-end-date-measures"})
confirmation_url = reverse(
"workbaskets:workbasket-ui-auto-end-date-measures-confirm",
kwargs={"pk": user_workbasket.pk},
)
assert response.status_code == 302
assert response.url == confirmation_url
assert call_check_end_measures.called


def test_auto_end_measures(commodity_with_measures, user_workbasket, date_ranges):
"""Test that the call_end_measures correctly ends measures and reorders them
in the workbasket."""
new_commodity = commodity_with_measures.new_version(
workbasket=user_workbasket,
valid_between=date_ranges.big,
)
measure_pks = [measure.pk for measure in commodity_with_measures.measures.all()]
call_end_measures(measure_pks, user_workbasket.pk)
measures = user_workbasket.measures
assert len(measures) == 12
for measure in measures[:10]:
assert measure.valid_between.upper == new_commodity.valid_between.upper
update_types = [measure.update_type for measure in measures]
assert update_types.count(UpdateType.UPDATE) == 10
assert update_types.count(UpdateType.DELETE) == 2
first_12_items = (
TrackedModel.objects.all()
.filter(transaction__workbasket=user_workbasket)
.order_by("transaction__order")[:12]
)
for item in first_12_items:
assert isinstance(item, Measure)
4 changes: 2 additions & 2 deletions workbaskets/views/ui.py
Original file line number Diff line number Diff line change
@@ -1807,14 +1807,14 @@ def commodities(self):
transaction__workbasket=self.workbasket,
valid_between__upper_inf=False,
)
.values_list("pk")
.values_list("sid")
)

@property
def measures(self):
# Should I only be filtering for measures without an end date?
return Measure.objects.current().filter(
goods_nomenclature__id__in=self.commodities,
goods_nomenclature__sid__in=self.commodities,
)

def workbasket_transactions(self):
Loading