From bb264c09967eb601111efa08cbd99963ff8f7eb9 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 9 Nov 2020 13:32:35 +0100
Subject: [PATCH] test visibility of field from different study on the subject

---
 smash/web/tests/functions.py                 | 22 ++++++++++--------
 smash/web/tests/models/test_study_subject.py | 24 +++++++++++++++++++-
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/smash/web/tests/functions.py b/smash/web/tests/functions.py
index 12abed19..02966240 100644
--- a/smash/web/tests/functions.py
+++ b/smash/web/tests/functions.py
@@ -2,6 +2,7 @@ import datetime
 import logging
 import os
 
+from django.contrib.auth.models import Permission
 from django.contrib.auth.models import User
 from django.utils.timezone import make_aware, is_aware
 
@@ -14,7 +15,6 @@ from web.models.constants import REDCAP_TOKEN_CONFIGURATION_TYPE, REDCAP_BASE_UR
 from web.models.worker_study_role import ROLE_CHOICES_DOCTOR, WORKER_VOUCHER_PARTNER
 from web.redcap_connector import RedcapSubject
 from web.views.notifications import get_today_midnight_date
-from django.contrib.auth.models import Permission
 
 logger = logging.getLogger(__name__)
 
@@ -70,7 +70,8 @@ def create_study(name="test"):
     study_columns = StudyColumns.objects.create()
     notification_parameters = StudyNotificationParameters.objects.create()
     redcap_columns = StudyRedCapColumns.objects.create()
-    return Study.objects.create(name=name, columns=study_columns, notification_parameters=notification_parameters, redcap_columns=redcap_columns)
+    return Study.objects.create(name=name, columns=study_columns, notification_parameters=notification_parameters,
+                                redcap_columns=redcap_columns)
 
 
 TEST_ID_COUNTER = 0
@@ -191,7 +192,7 @@ def create_subject():
     )
 
 
-def create_study_subject(subject_id=1, subject=None, nd_number='ND0001'):
+def create_study_subject(subject_id=1, subject=None, nd_number='ND0001') -> StudySubject:
     if subject is None:
         subject = create_subject()
     study_subject = StudySubject.objects.create(
@@ -201,7 +202,7 @@ def create_study_subject(subject_id=1, subject=None, nd_number='ND0001'):
         study=get_test_study(),
         subject=subject
     )
-    if nd_number is not None: #null value in column "nd_number" violates not-null constraint
+    if nd_number is not None:  # null value in column "nd_number" violates not-null constraint
         study_subject.nd_number = nd_number
         study_subject.save()
 
@@ -242,12 +243,14 @@ def create_user(username=None, password=None):
     create_worker(user)
     return user
 
+
 def add_permissions_to_worker(worker, codenames):
-    roles  = WorkerStudyRole.objects.filter(worker=worker, study_id=GLOBAL_STUDY_ID)
-    perms  = Permission.objects.filter(codename__in=codenames).all()
+    roles = WorkerStudyRole.objects.filter(worker=worker, study_id=GLOBAL_STUDY_ID)
+    perms = Permission.objects.filter(codename__in=codenames).all()
     roles[0].permissions.set(perms)
     roles[0].save()
 
+
 def create_worker(user=None, with_test_location=False):
     worker = Worker.objects.create(
         first_name='piotr',
@@ -321,7 +324,7 @@ def create_appointment(visit=None, when=None, length=30):
             make_aware(when_datetime)
     else:
         when_datetime = when
-    
+
     return Appointment.objects.create(
         visit=visit,
         length=length,
@@ -427,6 +430,5 @@ def datetimeify_date(date, timezone=datetime.timezone.utc):
         """
 
     actual_type = str(type(date))
-    raise TypeError("Date should be either a subclass of 'datetime.date', string or bytes! But is: {} instead".format(actual_type))
-
-
+    raise TypeError(
+        "Date should be either a subclass of 'datetime.date', string or bytes! But is: {} instead".format(actual_type))
diff --git a/smash/web/tests/models/test_study_subject.py b/smash/web/tests/models/test_study_subject.py
index 4412dc79..11878f57 100644
--- a/smash/web/tests/models/test_study_subject.py
+++ b/smash/web/tests/models/test_study_subject.py
@@ -3,7 +3,7 @@ from django.test import TestCase
 from web.models import Appointment, Visit, StudySubject, Study
 from web.models.constants import CUSTOM_FIELD_TYPE_TEXT
 from web.models.custom_data import CustomStudySubjectField
-from web.tests.functions import create_visit
+from web.tests.functions import create_visit, create_study
 from web.tests.functions import get_test_study, create_study_subject, create_appointment, \
     create_study_subject_with_multiple_screening_numbers
 
@@ -191,3 +191,25 @@ class SubjectModelTests(TestCase):
         field.delete()
 
         self.assertEqual(0, len(subject.custom_data_values))
+
+    def test_subject_with_field_from_different_study(self):
+        field = CustomStudySubjectField.objects.create(study=create_study('bla'), type=CUSTOM_FIELD_TYPE_TEXT)
+
+        subject = create_study_subject()
+        self.assertIsNone(subject.get_custom_data_value(field))
+
+    def test_status(self):
+        subject = create_study_subject()
+        # noinspection PySetFunctionToLiteral
+        statuses = set([subject.status, ])
+        subject.endpoint_reached = True
+        statuses.add(subject.status)
+        subject.excluded = True
+        statuses.add(subject.status)
+        subject.resigned = True
+        statuses.add(subject.status)
+        subject.subject.dead = True
+        statuses.add(subject.status)
+
+        print(statuses)
+        self.assertEqual(5, len(statuses))
-- 
GitLab