This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from uktrade/feature/fixture-fixup
Feature/fixture fixup
- Loading branch information
Showing
6 changed files
with
225 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions
39
defend_data_capture/supply_chains/management/commands/datafixup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
184
defend_data_capture/supply_chains/test/test_fixture_fixup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters