Skip to content

Commit

Permalink
Merge pull request #2112 from uktrade/LTD-5265_Add_and_populate_new_U…
Browse files Browse the repository at this point in the history
…UID_field_on_DenialReason_objects

Add and populate new UUID field on DenialReason objects
  • Loading branch information
hnryjmes authored Aug 8, 2024
2 parents 1ef94c0 + 60433a7 commit fade281
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
28 changes: 28 additions & 0 deletions api/staticdata/denial_reasons/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
DENIAL_REASON_ID_TO_UUID_MAP = {
"1": "f8e2cd45-071b-4bf2-bc01-155d9c83c460", # /PS-IGNORE
"1a": "6e96160f-de24-4f3e-a2f7-056f96012efa", # /PS-IGNORE
"1b": "4a86755a-25c4-4b0d-8874-c337ecbd0d67", # /PS-IGNORE
"1c": "04a7314f-d2d0-480b-947b-4f9332749b32", # /PS-IGNORE
"1d": "c947c434-4987-4a26-9277-62ddd9048ae1", # /PS-IGNORE
"1e": "5d59ca98-a606-4b33-8a3b-252135da939a", # /PS-IGNORE
"1f": "06a0bc03-064d-47a8-9e88-26db89cdd224", # /PS-IGNORE
"2": "6b570188-634a-4aa7-91be-3f10c0dab2f8", # /PS-IGNORE
"2a": "815d0d95-448f-4514-a428-37bf9a6a4b9b", # /PS-IGNORE
"2b": "8335b8c0-643a-4a1b-9ca6-a2e5b65a57d8", # /PS-IGNORE
"2c": "03beb66a-7b46-4d84-9616-949045626ea6", # /PS-IGNORE
"3": "27cbd803-d2ac-4fed-af58-8990438ca3e0", # /PS-IGNORE
"4": "7205638b-8f9b-4260-bb27-0c41de2208ee", # /PS-IGNORE
"5": "73e063e8-ede0-43aa-b8c5-a9b7e546d67c", # /PS-IGNORE
"5a": "49203257-8fca-4f7f-9f86-b8a6fa1dfa58", # /PS-IGNORE
"5b": "aecd9fd0-287e-4b39-9f6a-4531a2e5848e", # /PS-IGNORE
"5c": "913d7cb9-f04d-488a-b68c-554c8bfe6711", # /PS-IGNORE
"6": "b6b0ce7c-8494-4c16-9dec-f2aa770d77ad", # /PS-IGNORE
"6a": "df28a06c-c2c6-4a07-bd7e-1959bee1e7a8", # /PS-IGNORE
"6b": "f45133c5-3695-4f12-a6c3-ae9897ce48d8", # /PS-IGNORE
"7": "ec3794dc-6387-4e8a-bec8-293f1598a710", # /PS-IGNORE
"8": "2674ef18-ff2b-4739-bde9-7b4a53e7aa49", # /PS-IGNORE
"IRAN": "56b8649f-59d7-4fdf-848a-488b0cb4d52e", # /PS-IGNORE
"MEND": "932fc377-3adb-4d2e-9eb6-0c19abdc0bc1", # /PS-IGNORE
"OF": "aa20a4be-5ee8-4de3-8cea-5a5ba9142ed4", # /PS-IGNORE
"WMD": "85411aed-22a6-46bf-b513-b901e1959c36", # /PS-IGNORE
}
19 changes: 19 additions & 0 deletions api/staticdata/denial_reasons/migrations/0005_denialreason_uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.14 on 2024-07-30 15:58

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
("denial_reasons", "0004_denial_reasons_update"),
]

operations = [
migrations.AddField(
model_name="denialreason",
name="uuid",
field=models.UUIDField(default=uuid.uuid4, editable=False),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import uuid

from django.db import migrations

from api.staticdata.denial_reasons.constants import DENIAL_REASON_ID_TO_UUID_MAP


def populate_uuid_field(apps, schema_editor):
DenialReason = apps.get_model("denial_reasons", "DenialReason")
for denial_reason in DenialReason.objects.all():
denial_reason.uuid = uuid.UUID(DENIAL_REASON_ID_TO_UUID_MAP[denial_reason.id])
denial_reason.save()


class Migration(migrations.Migration):
dependencies = [
("denial_reasons", "0005_denialreason_uuid"),
]

operations = [
migrations.RunPython(populate_uuid_field, migrations.RunPython.noop),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
import uuid

from django_test_migrations.migrator import Migrator

from api.staticdata.denial_reasons.constants import DENIAL_REASON_ID_TO_UUID_MAP


@pytest.mark.django_db()
def test_populate_uuid_field():
migrator = Migrator(database="default")

old_state = migrator.apply_initial_migration(("denial_reasons", "0005_denialreason_uuid"))

new_state = migrator.apply_tested_migration(("denial_reasons", "0006_populate_uuid_field"))
DenialReason = new_state.apps.get_model("denial_reasons", "DenialReason")
for denial_reason in DenialReason.objects.all():
assert denial_reason.uuid is not None
assert type(denial_reason.uuid) is uuid.UUID
assert str(denial_reason.uuid) == DENIAL_REASON_ID_TO_UUID_MAP[denial_reason.id]

expected_uuids = set(DENIAL_REASON_ID_TO_UUID_MAP.values())
actual_uuids = set([str(denial_reason.uuid) for denial_reason in DenialReason.objects.all()])
assert expected_uuids == actual_uuids
3 changes: 3 additions & 0 deletions api/staticdata/denial_reasons/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import uuid

from django.db import models


class DenialReason(models.Model):
id = models.TextField(primary_key=True, editable=False)
uuid = models.UUIDField(primary_key=False, default=uuid.uuid4, editable=False)
deprecated = models.BooleanField(default=False, null=False, blank=False)
description = models.TextField(default="")
display_value = models.TextField(default="")
Expand Down
6 changes: 5 additions & 1 deletion api/staticdata/management/commands/seeddenialreasons.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import uuid

from django.db import transaction

from api.staticdata.denial_reasons.models import DenialReason
from api.staticdata.management.SeedCommand import SeedCommand
from api.staticdata.denial_reasons.constants import DENIAL_REASON_ID_TO_UUID_MAP

DENIAL_REASONS_FILE = "lite_content/lite_api/denial_reasons.csv"
DENIAL_REASONS_FILE = "lite_content/lite_api/denial_reasons_update.csv"


class Command(SeedCommand):
Expand All @@ -21,6 +24,7 @@ def operation(self, *args, **options):
filtered_csv = [
{
"id": row["id"],
"uuid": uuid.UUID(DENIAL_REASON_ID_TO_UUID_MAP[row["id"]]),
"display_value": row["display_value"],
"deprecated": row["deprecated"],
"description": row["description"],
Expand Down

0 comments on commit fade281

Please sign in to comment.