Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #46 from uktrade/feature/fixture-fixup
Browse files Browse the repository at this point in the history
Feature/fixture fixup
  • Loading branch information
NickFitz authored May 25, 2021
2 parents 35c6dd7 + fa29e6e commit 359b8f4
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 7 deletions.
8 changes: 1 addition & 7 deletions cypress/fixtures/strategicActionUpdates.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@
"content": "MU Form Test 1",
"implementation_rag_rating": "GREEN",
"reason_for_delays": "",
"changed_value_for_target_completion_date": null,
"reason_for_completion_date_change": "",
"changed_value_for_is_ongoing": false,
"user": "8ffdf4f4-ea0d-4727-947c-f741f0ac6959",
"strategic_action": "223f148b-7fa8-4b55-9f94-41e812a2b285",
"supply_chain": "a69d5624-1316-49a6-8a1a-4ac935ad13e3",
Expand All @@ -103,14 +100,11 @@
"pk": "1c18c53b-2f7a-4539-8b34-6a12d7ba371e",
"fields": {
"status": "submitted",
"submission_date": "2021-04-28",
"submission_date": "2021-04-22",
"date_created": "2021-04-07",
"content": "MU Form Test 0",
"implementation_rag_rating": "GREEN",
"reason_for_delays": "",
"changed_value_for_target_completion_date": null,
"reason_for_completion_date_change": "",
"changed_value_for_is_ongoing": false,
"user": "8ffdf4f4-ea0d-4727-947c-f741f0ac6959",
"strategic_action": "668be471-acd3-4eee-9a37-8debdff786a1",
"supply_chain": "a69d5624-1316-49a6-8a1a-4ac935ad13e3",
Expand Down
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions defend_data_capture/supply_chains/management/commands/datafixup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from datetime import date

from dateutil.relativedelta import relativedelta
from django.core.management import BaseCommand

from supply_chains.models import SupplyChain, StrategicActionUpdate


class Command(BaseCommand):
BASE_DATE = date(year=2021, month=5, day=1)

def handle(self, *args, **options):
months_to_add = relativedelta(months=self.calculate_months_to_add())
updates = StrategicActionUpdate.objects.all()
self.update_submission_and_created_dates(updates, months_to_add)
print("Fixtures fixed")

def update_submission_and_created_dates(self, updates, months_to_add):
updated_updates = []
updated_supply_chains = []
for update in updates:
if update.status == StrategicActionUpdate.Status.SUBMITTED:
if update.submission_date is not None:
update.submission_date += months_to_add
update.date_created += months_to_add
update.supply_chain.last_submission_date = update.submission_date
updated_updates.append(update)
updated_supply_chains.append(update.supply_chain)
StrategicActionUpdate.objects.bulk_update(
updated_updates, ["submission_date", "date_created"]
)
SupplyChain.objects.bulk_update(updated_supply_chains, ["last_submission_date"])

def calculate_months_to_add(self):
today = date.today()
month_difference = today.month - self.BASE_DATE.month
year_difference = today.year - self.BASE_DATE.year
months_to_add = month_difference + (year_difference * 12)
return months_to_add
184 changes: 184 additions & 0 deletions defend_data_capture/supply_chains/test/test_fixture_fixup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
from io import StringIO
from unittest import mock
from uuid import UUID
from datetime import date
from dateutil.relativedelta import relativedelta

import pytest
from django.core.management import call_command
from django.conf import settings

from supply_chains.models import StrategicActionUpdate
from supply_chains.management.commands.datafixup import Command

pytestmark = pytest.mark.django_db


@pytest.fixture(scope="function")
def django_db_setup(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
call_command("datafixup")


class TestFixtureFixup:
ROOT_DIR = settings.BASE_DIR.parent
fixtures = [
f"{ROOT_DIR}/cypress/fixtures/govDepartment.json",
f"{ROOT_DIR}/cypress/fixtures/user.json",
f"{ROOT_DIR}/cypress/fixtures/supplyChains.json",
f"{ROOT_DIR}/cypress/fixtures/strategicActions.json",
f"{ROOT_DIR}/cypress/fixtures/strategicActionUpdates.json",
]

def call_command(self, *args, **kwargs):
out = StringIO()
call_command("datafixup", *args, stdout=out, stderr=StringIO(), **kwargs)
return out.getvalue()

def test_updates_in_month_of_base_date(self):
""" Should leave the dates unaltered. """
base_date = Command.BASE_DATE
expected_submission_dates = {
UUID("3ac4b5ac-bba3-47e5-9964-7613aa2fcd88"): None,
UUID("7c18ca98-5947-4ea1-9e2d-2a834462a453"): None,
UUID("c9325de0-8233-4758-a908-b31042b0cb66"): None,
UUID("f49fddb2-02e1-4ea2-a3bb-e8a20a107fc9"): None,
UUID("e3fcd971-1d5a-4eab-a891-ba634d9703f3"): date(
year=2021, month=5, day=1
),
UUID("1303cbce-82c5-4cb9-b1d6-d96519f40882"): date(
year=2021, month=4, day=22
),
UUID("1c18c53b-2f7a-4539-8b34-6a12d7ba371e"): date(
year=2021, month=4, day=22
),
}
with mock.patch(
"supply_chains.management.commands.datafixup.date",
mock.Mock(today=mock.Mock(return_value=base_date)),
):
out = self.call_command()
updated_submission_dates = {
sau.pk: sau.submission_date
for sau in StrategicActionUpdate.objects.all()
}
updated_last_submission_dates = {
sau.pk: sau.supply_chain.last_submission_date
for sau in StrategicActionUpdate.objects.all()
}
for pk, updated_date in updated_submission_dates.items():
assert updated_date == expected_submission_dates[pk]
for pk, updated_date in updated_last_submission_dates.items():
if expected_submission_dates[pk]:
assert updated_date == expected_submission_dates[pk]

def test_updates_one_month_after_base_date(self):
""" Should set the dates one month later. """
base_date = Command.BASE_DATE + relativedelta(months=1)
expected_submission_dates = {
UUID("3ac4b5ac-bba3-47e5-9964-7613aa2fcd88"): None,
UUID("7c18ca98-5947-4ea1-9e2d-2a834462a453"): None,
UUID("c9325de0-8233-4758-a908-b31042b0cb66"): None,
UUID("f49fddb2-02e1-4ea2-a3bb-e8a20a107fc9"): None,
UUID("e3fcd971-1d5a-4eab-a891-ba634d9703f3"): date(
year=2021, month=6, day=1
),
UUID("1303cbce-82c5-4cb9-b1d6-d96519f40882"): date(
year=2021, month=5, day=22
),
UUID("1c18c53b-2f7a-4539-8b34-6a12d7ba371e"): date(
year=2021, month=5, day=22
),
}
with mock.patch(
"supply_chains.management.commands.datafixup.date",
mock.Mock(today=mock.Mock(return_value=base_date)),
):
out = self.call_command()
updated_submission_dates = {
sau.pk: sau.submission_date
for sau in StrategicActionUpdate.objects.all()
}
updated_last_submission_dates = {
sau.pk: sau.supply_chain.last_submission_date
for sau in StrategicActionUpdate.objects.all()
}
for pk, updated_date in updated_submission_dates.items():
assert updated_date == expected_submission_dates[pk]
for pk, updated_date in updated_last_submission_dates.items():
if expected_submission_dates[pk]:
assert updated_date == expected_submission_dates[pk]

def test_updates_one_year_after_base_date(self):
""" Should set the dates one month later. """
base_date = Command.BASE_DATE + relativedelta(years=1)
expected_submission_dates = {
UUID("3ac4b5ac-bba3-47e5-9964-7613aa2fcd88"): None,
UUID("7c18ca98-5947-4ea1-9e2d-2a834462a453"): None,
UUID("c9325de0-8233-4758-a908-b31042b0cb66"): None,
UUID("f49fddb2-02e1-4ea2-a3bb-e8a20a107fc9"): None,
UUID("e3fcd971-1d5a-4eab-a891-ba634d9703f3"): date(
year=2022, month=5, day=1
),
UUID("1303cbce-82c5-4cb9-b1d6-d96519f40882"): date(
year=2022, month=4, day=22
),
UUID("1c18c53b-2f7a-4539-8b34-6a12d7ba371e"): date(
year=2022, month=4, day=22
),
}
with mock.patch(
"supply_chains.management.commands.datafixup.date",
mock.Mock(today=mock.Mock(return_value=base_date)),
):
out = self.call_command()
updated_submission_dates = {
sau.pk: sau.submission_date
for sau in StrategicActionUpdate.objects.all()
}
updated_last_submission_dates = {
sau.pk: sau.supply_chain.last_submission_date
for sau in StrategicActionUpdate.objects.all()
}
for pk, updated_date in updated_submission_dates.items():
assert updated_date == expected_submission_dates[pk]
for pk, updated_date in updated_last_submission_dates.items():
if expected_submission_dates[pk]:
assert updated_date == expected_submission_dates[pk]

def test_updates_one_year_before_base_date(self):
""" Should set the dates one month later. """
base_date = Command.BASE_DATE + relativedelta(years=-1)
expected_submission_dates = {
UUID("3ac4b5ac-bba3-47e5-9964-7613aa2fcd88"): None,
UUID("7c18ca98-5947-4ea1-9e2d-2a834462a453"): None,
UUID("c9325de0-8233-4758-a908-b31042b0cb66"): None,
UUID("f49fddb2-02e1-4ea2-a3bb-e8a20a107fc9"): None,
UUID("e3fcd971-1d5a-4eab-a891-ba634d9703f3"): date(
year=2020, month=5, day=1
),
UUID("1303cbce-82c5-4cb9-b1d6-d96519f40882"): date(
year=2020, month=4, day=22
),
UUID("1c18c53b-2f7a-4539-8b34-6a12d7ba371e"): date(
year=2020, month=4, day=22
),
}
with mock.patch(
"supply_chains.management.commands.datafixup.date",
mock.Mock(today=mock.Mock(return_value=base_date)),
):
out = self.call_command()
updated_submission_dates = {
sau.pk: sau.submission_date
for sau in StrategicActionUpdate.objects.all()
}
updated_last_submission_dates = {
sau.pk: sau.supply_chain.last_submission_date
for sau in StrategicActionUpdate.objects.all()
}
for pk, updated_date in updated_submission_dates.items():
assert updated_date == expected_submission_dates[pk]
for pk, updated_date in updated_last_submission_dates.items():
if expected_submission_dates[pk]:
assert updated_date == expected_submission_dates[pk]
1 change: 1 addition & 0 deletions run_functional_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ python defend_data_capture/manage.py testserver \
cypress/fixtures/supplyChains.json cypress/fixtures/strategicActions.json \
cypress/fixtures/strategicActionUpdates.json \
& echo $! > backend.pid \
& defend_data_capture/manage.py datafixup \
& (sleep 5 && npx cypress run --headless --browser chrome)

cypress_failed=$?
Expand Down

0 comments on commit 359b8f4

Please sign in to comment.