Skip to content

Commit b644e1f

Browse files
committed
PPHA-409: Cleanup family age when diagnosed when answering family history
1 parent 1c358cf commit b644e1f

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

lung_cancer_screening/questions/models/family_history_lung_cancer_response.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from django.db import models
2+
from django.db.models.signals import post_save
3+
from django.dispatch import receiver
24

35
from .base import BaseModel
46
from .response_set import ResponseSet
@@ -12,3 +14,13 @@ class FamilyHistoryLungCancerValues(models.TextChoices):
1214
class FamilyHistoryLungCancerResponse(BaseModel):
1315
response_set = models.OneToOneField(ResponseSet, on_delete=models.CASCADE, related_name='family_history_lung_cancer')
1416
value = models.CharField(max_length=1, choices=FamilyHistoryLungCancerValues.choices)
17+
18+
19+
@receiver(post_save, sender=FamilyHistoryLungCancerResponse)
20+
def remove_relatives_age_when_diagnosed_if_not_yes(sender, instance, **kwargs):
21+
if (
22+
instance.value != FamilyHistoryLungCancerValues.YES
23+
and instance.response_set
24+
and hasattr(instance.response_set, "relatives_age_when_diagnosed")
25+
):
26+
instance.response_set.relatives_age_when_diagnosed.delete()
Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
from django.test import TestCase, tag
22

33
from ...factories.response_set_factory import ResponseSetFactory
4-
from ...factories.family_history_lung_cancer_response_factory import FamilyHistoryLungCancerResponseFactory, FamilyHistoryLungCancerValues
4+
from ...factories.family_history_lung_cancer_response_factory import (
5+
FamilyHistoryLungCancerResponseFactory,
6+
)
7+
from ...factories.relatives_age_when_diagnosed_response_factory import (
8+
RelativesAgeWhenDiagnosedResponseFactory
9+
)
510

6-
from ....models.family_history_lung_cancer_response import FamilyHistoryLungCancerResponse
11+
from ....models.family_history_lung_cancer_response import (
12+
FamilyHistoryLungCancerResponse,
13+
FamilyHistoryLungCancerValues,
14+
)
715

816
@tag("FamilyHistoryLungCancer")
917
class TestFamilyHistoryLungCancerResponse(TestCase):
@@ -16,19 +24,59 @@ def test_has_a_valid_factory(self):
1624

1725

1826
def test_has_response_set_as_foreign_key(self):
19-
response_set = ResponseSetFactory()
2027
response = FamilyHistoryLungCancerResponse.objects.create(
21-
response_set=response_set,
28+
response_set=self.response_set,
2229
value=FamilyHistoryLungCancerValues.NO
2330
)
2431

25-
self.assertEqual(response.response_set, response_set)
32+
self.assertEqual(response.response_set, self.response_set)
2633

2734
def test_has_value_as_string(self):
28-
response_set = ResponseSetFactory()
2935
response = FamilyHistoryLungCancerResponse.objects.create(
30-
response_set=response_set,
36+
response_set=self.response_set,
3137
value=FamilyHistoryLungCancerValues.NO
3238
)
3339

3440
self.assertIsInstance(response.value, str)
41+
42+
43+
def test_deletes_family_age_when_diagnosed_if_response_is_no(self):
44+
RelativesAgeWhenDiagnosedResponseFactory.create(response_set=self.response_set)
45+
46+
self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))
47+
48+
FamilyHistoryLungCancerResponse.objects.create(
49+
response_set=self.response_set,
50+
value=FamilyHistoryLungCancerValues.NO
51+
)
52+
53+
self.response_set.refresh_from_db()
54+
self.assertFalse(hasattr(self.response_set, "relatives_age_when_diagnosed"))
55+
56+
57+
def test_deletes_family_age_when_diagnosed_if_response_is_unknown(self):
58+
RelativesAgeWhenDiagnosedResponseFactory.create(response_set=self.response_set)
59+
60+
self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))
61+
62+
FamilyHistoryLungCancerResponse.objects.create(
63+
response_set=self.response_set,
64+
value=FamilyHistoryLungCancerValues.UNKNOWN
65+
)
66+
67+
self.response_set.refresh_from_db()
68+
self.assertFalse(hasattr(self.response_set, "relatives_age_when_diagnosed"))
69+
70+
71+
def test_does_not_delete_family_age_when_diagnosed_if_response_is_yes(self):
72+
RelativesAgeWhenDiagnosedResponseFactory.create(response_set=self.response_set)
73+
74+
self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))
75+
76+
FamilyHistoryLungCancerResponse.objects.create(
77+
response_set=self.response_set,
78+
value=FamilyHistoryLungCancerValues.YES
79+
)
80+
81+
self.response_set.refresh_from_db()
82+
self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))

0 commit comments

Comments
 (0)