Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

from .base import BaseModel
from .response_set import ResponseSet
Expand All @@ -12,3 +14,13 @@ class FamilyHistoryLungCancerValues(models.TextChoices):
class FamilyHistoryLungCancerResponse(BaseModel):
response_set = models.OneToOneField(ResponseSet, on_delete=models.CASCADE, related_name='family_history_lung_cancer')
value = models.CharField(max_length=1, choices=FamilyHistoryLungCancerValues.choices)


@receiver(post_save, sender=FamilyHistoryLungCancerResponse)
def remove_relatives_age_when_diagnosed_if_not_yes(sender, instance, **kwargs):
if (
instance.value != FamilyHistoryLungCancerValues.YES
and instance.response_set
and hasattr(instance.response_set, "relatives_age_when_diagnosed")
):
instance.response_set.relatives_age_when_diagnosed.delete()
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from django.test import TestCase, tag

from ...factories.response_set_factory import ResponseSetFactory
from ...factories.family_history_lung_cancer_response_factory import FamilyHistoryLungCancerResponseFactory, FamilyHistoryLungCancerValues
from ...factories.family_history_lung_cancer_response_factory import (
FamilyHistoryLungCancerResponseFactory,
)
from ...factories.relatives_age_when_diagnosed_response_factory import (
RelativesAgeWhenDiagnosedResponseFactory
)

from ....models.family_history_lung_cancer_response import FamilyHistoryLungCancerResponse
from ....models.family_history_lung_cancer_response import (
FamilyHistoryLungCancerResponse,
FamilyHistoryLungCancerValues,
)

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


def test_has_response_set_as_foreign_key(self):
response_set = ResponseSetFactory()
response = FamilyHistoryLungCancerResponse.objects.create(
response_set=response_set,
response_set=self.response_set,
value=FamilyHistoryLungCancerValues.NO
)

self.assertEqual(response.response_set, response_set)
self.assertEqual(response.response_set, self.response_set)

def test_has_value_as_string(self):
response_set = ResponseSetFactory()
response = FamilyHistoryLungCancerResponse.objects.create(
response_set=response_set,
response_set=self.response_set,
value=FamilyHistoryLungCancerValues.NO
)

self.assertIsInstance(response.value, str)


def test_deletes_family_age_when_diagnosed_if_response_is_no(self):
RelativesAgeWhenDiagnosedResponseFactory.create(response_set=self.response_set)

self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))

FamilyHistoryLungCancerResponse.objects.create(
response_set=self.response_set,
value=FamilyHistoryLungCancerValues.NO
)

self.response_set.refresh_from_db()
self.assertFalse(hasattr(self.response_set, "relatives_age_when_diagnosed"))


def test_deletes_family_age_when_diagnosed_if_response_is_unknown(self):
RelativesAgeWhenDiagnosedResponseFactory.create(response_set=self.response_set)

self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))

FamilyHistoryLungCancerResponse.objects.create(
response_set=self.response_set,
value=FamilyHistoryLungCancerValues.UNKNOWN
)

self.response_set.refresh_from_db()
self.assertFalse(hasattr(self.response_set, "relatives_age_when_diagnosed"))


def test_does_not_delete_family_age_when_diagnosed_if_response_is_yes(self):
RelativesAgeWhenDiagnosedResponseFactory.create(response_set=self.response_set)

self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))

FamilyHistoryLungCancerResponse.objects.create(
response_set=self.response_set,
value=FamilyHistoryLungCancerValues.YES
)

self.response_set.refresh_from_db()
self.assertTrue(hasattr(self.response_set, "relatives_age_when_diagnosed"))