From 33b378ceb590b092de66cacfe54b505bf61eaf7c Mon Sep 17 00:00:00 2001 From: Dave Nathanael Date: Mon, 18 May 2020 16:26:50 +0700 Subject: [PATCH 1/3] [RED] Add test for `is_positive` field --- .../test_units/test_investigation_cases.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/apps/cases/tests/test_units/test_investigation_cases.py b/apps/cases/tests/test_units/test_investigation_cases.py index dbf8ff4..5459f85 100644 --- a/apps/cases/tests/test_units/test_investigation_cases.py +++ b/apps/cases/tests/test_units/test_investigation_cases.py @@ -34,6 +34,7 @@ class InvestigationCaseViewTest(APITestCase): case_subject=cls.case_subject, author=cls.author, outcome="BTA+", + is_positive=True, ) cls.case = InvestigationCaseFactory( author=cls.author, @@ -209,6 +210,40 @@ class InvestigationCaseViewTest(APITestCase): investigation_case_history_current_count, investigation_case_history_prev_count + 1 ) + def test_edit_investigation_to_positive_success(self): + url = self.BASE_URL + str(self.case.id) + "/" + + data = { + "case_subject": str(self.case_subject.id), + "reference_case": str(self.reference_case.id), + "case_relation": self.case.case_relation, + "medical_symptoms": self.case.medical_symptoms, + "risk_factors": self.case.risk_factors, + "is_referral_needed": not self.case.is_referral_needed, + "medical_facility_reference": self.case.medical_facility_reference, + "outcome": "BTA+", + } + + investigation_case_prev_count = InvestigationCase.objects.all().count() + positive_case_prev_count = InvestigationCase.objects.filter(is_positive=True).count() + + response = self.client.put(path=url, data=data, format="json",) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual( + response.data["is_referral_needed"], not self.case.is_referral_needed + ) + + investigation_case_current_count = InvestigationCase.objects.all().count() + positive_case_current_count = InvestigationCase.objects.filter(is_positive=True).count() + + self.assertEqual( + investigation_case_current_count, investigation_case_prev_count + ) + + self.assertEqual( + positive_case_current_count, positive_case_prev_count + 1 + ) + def test_edit_investigation_case_fails_with_incomplete_data(self): url = self.BASE_URL + str(self.case.id) + "/" -- GitLab From fff0cf622aa08e0acafc3937f3bd81893806a99a Mon Sep 17 00:00:00 2001 From: Dave Nathanael Date: Mon, 18 May 2020 16:27:47 +0700 Subject: [PATCH 2/3] [GREEN] Implement new `is_positive` field for InvestigationCase --- apps/cases/filters.py | 9 ++++++++ .../migrations/0012_auto_20200518_1532.py | 23 +++++++++++++++++++ apps/cases/models.py | 2 ++ apps/cases/serializers.py | 9 +++++++- apps/cases/views.py | 5 ++-- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 apps/cases/migrations/0012_auto_20200518_1532.py diff --git a/apps/cases/filters.py b/apps/cases/filters.py index 4fc53df..3b1d2c3 100644 --- a/apps/cases/filters.py +++ b/apps/cases/filters.py @@ -45,12 +45,14 @@ INVESTIGATION_CASE_FILTERSET_FIELDS = ( "reference_case__is_referral_needed", "reference_case__medical_facility_reference", "reference_case__outcome", + "reference_case__is_positive", "case_relation", "medical_symptoms", "risk_factors", "is_referral_needed", "medical_facility_reference", "outcome", + "is_positive", "author__name", "created_at", ) @@ -67,12 +69,14 @@ INVESTIGATION_CASE_SEARCH_FIELDS = ( "reference_case__risk_factors", "reference_case__medical_facility_reference", "reference_case__outcome", + "reference_case__is_positive", "reference_case__created_at", "case_relation", "medical_symptoms", "risk_factors", "medical_facility_reference", "outcome", + "is_positive", "author__name", ) @@ -90,6 +94,7 @@ INVESTIGATION_CASE_ORDERING_FIELDS = ( "reference_case__is_referral_needed", "reference_case__medical_facility_reference", "reference_case__outcome", + "reference_case__is_positive", "reference_case__created_at", "case_relation", "medical_symptoms", @@ -97,6 +102,7 @@ INVESTIGATION_CASE_ORDERING_FIELDS = ( "is_referral_needed", "medical_facility_reference", "outcome", + "is_positive", "author__name", "created_at", ) @@ -115,6 +121,7 @@ MONITORING_CASE_FILTERSET_FIELDS = ( "investigation_case__reference_case__is_referral_needed", "investigation_case__reference_case__medical_facility_reference", "investigation_case__reference_case__outcome", + "investigation_case__reference_case__is_positive", "is_referred", "is_checked", "is_medicated", @@ -138,6 +145,7 @@ MONITORING_CASE_SEARCH_FIELDS = ( "investigation_case__reference_case__risk_factors", "investigation_case__reference_case__medical_facility_reference", "investigation_case__reference_case__outcome", + "investigation_case__reference_case__is_positive", "investigation_case__reference_case__created_at", "is_referred", "is_checked", @@ -163,6 +171,7 @@ MONITORING_CASE_ORDERING_FIELDS = ( "investigation_case__reference_case__is_referral_needed", "investigation_case__reference_case__medical_facility_reference", "investigation_case__reference_case__outcome", + "investigation_case__reference_case__is_positive", "investigation_case__reference_case__created_at", "is_referred", "is_checked", diff --git a/apps/cases/migrations/0012_auto_20200518_1532.py b/apps/cases/migrations/0012_auto_20200518_1532.py new file mode 100644 index 0000000..2625d81 --- /dev/null +++ b/apps/cases/migrations/0012_auto_20200518_1532.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.1 on 2020-05-18 08:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cases', '0011_monitoring_checking_date'), + ] + + operations = [ + migrations.AddField( + model_name='investigationcase', + name='is_positive', + field=models.BooleanField(db_index=True, null=True), + ), + migrations.AddField( + model_name='investigationcasehistory', + name='is_positive', + field=models.BooleanField(db_index=True, null=True), + ), + ] diff --git a/apps/cases/models.py b/apps/cases/models.py index c045a52..804e029 100644 --- a/apps/cases/models.py +++ b/apps/cases/models.py @@ -79,6 +79,7 @@ class InvestigationCaseHistory(HistoryModel): is_referral_needed = models.BooleanField(db_index=True) medical_facility_reference = models.CharField(max_length=128, blank=True) outcome = models.CharField(max_length=256, blank=True, null=True) + is_positive = models.BooleanField(db_index=True, null=True) author = models.ForeignKey( Account, blank=True, @@ -129,6 +130,7 @@ class InvestigationCase(HistoryEnabledModel): is_referral_needed = models.BooleanField(db_index=True) medical_facility_reference = models.CharField(max_length=128, blank=True) outcome = models.CharField(max_length=256, blank=True, null=True) + is_positive = models.BooleanField(db_index=True, null=True) author = models.ForeignKey( Account, blank=True, diff --git a/apps/cases/serializers.py b/apps/cases/serializers.py index 038cd46..610ce15 100644 --- a/apps/cases/serializers.py +++ b/apps/cases/serializers.py @@ -63,6 +63,7 @@ class InvestigationCaseSummarySerializer(serializers.ModelSerializer): "is_referral_needed", "medical_facility_reference", "outcome", + "is_positive", "author", "created_at", ] @@ -88,7 +89,13 @@ class InvestigationCaseSerializer(serializers.ModelSerializer): def save(self): account = self.context.get('request').user.account - super(InvestigationCaseSerializer, self).save(author=account) + + outcome = self.validated_data.get('outcome', None) + is_positive = None + if outcome is not None: + is_positive = True if "+" in outcome else False + + super(InvestigationCaseSerializer, self).save(author=account, is_positive=is_positive) def to_representation(self, instance): serializer = InvestigationCaseSummarySerializer(instance) diff --git a/apps/cases/views.py b/apps/cases/views.py index 541c12d..aa5179f 100644 --- a/apps/cases/views.py +++ b/apps/cases/views.py @@ -71,7 +71,7 @@ class InvestigationCaseViewSet(viewsets.ModelViewSet): query_include_positive = self.request.query_params.get('include_positive', None) if query_include_positive is not None: return self.queryset - return self.queryset.filter(Q(outcome__isnull=True) | Q(outcome__icontains="-")) + return self.queryset.exclude(is_positive=True) return self.queryset def get_serializer_class(self): @@ -85,8 +85,7 @@ class InvestigationCaseViewSet(viewsets.ModelViewSet): class InvestigationPositiveCaseAPIView(ListAPIView): serializer_class = InvestigationCaseSummarySerializer - queryset = InvestigationCase.objects.filter( - outcome__isnull=False).exclude(outcome__icontains="-") + queryset = InvestigationCase.objects.filter(is_positive=True) filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) permission_classes = (IsAuthenticated,) pagination_class = PageNumberPagination -- GitLab From 08d72f3dd5516fb9491ad361d235db9e8ddf79ec Mon Sep 17 00:00:00 2001 From: Dave Nathanael Date: Mon, 18 May 2020 16:28:17 +0700 Subject: [PATCH 3/3] [CHORES] Refactor code using auto-formatter --- apps/accounts/migrations/0005_auto_20200515_0038.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/accounts/migrations/0005_auto_20200515_0038.py b/apps/accounts/migrations/0005_auto_20200515_0038.py index 06ad976..744e184 100644 --- a/apps/accounts/migrations/0005_auto_20200515_0038.py +++ b/apps/accounts/migrations/0005_auto_20200515_0038.py @@ -14,6 +14,11 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='account', name='phone_number', - field=models.CharField(max_length=64, validators=[django.core.validators.RegexValidator(message='Phone number has 9-15 digits, allowed to have + prefix', regex='^\\+?\\d{9,15}$')]), + field=models.CharField( + max_length=64, + validators=[ + django.core.validators.RegexValidator( + message='Phone number has 9-15 digits, allowed to have + prefix', + regex='^\\+?\\d{9,15}$')]), ), ] -- GitLab