From c64a6d2c203c91344e4fdf57e77cec6f897bd1d8 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 11 Mar 2021 14:12:04 +0100
Subject: [PATCH] tests fixed

---
 smash/db_scripts/create_dummy_data.py         | 20 ++++++++-----------
 smash/web/importer/importer.py                |  4 +++-
 ...4_migrate_subject_type_to_new_structure.py |  5 +++--
 smash/web/models/study_subject.py             |  4 ++--
 smash/web/models/visit.py                     |  9 ++++-----
 smash/web/tests/api_views/test_subject.py     |  6 +++---
 .../test_csv_subject_import_reader.py         |  3 ---
 7 files changed, 23 insertions(+), 28 deletions(-)

diff --git a/smash/db_scripts/create_dummy_data.py b/smash/db_scripts/create_dummy_data.py
index dadf16b1..f7128dce 100644
--- a/smash/db_scripts/create_dummy_data.py
+++ b/smash/db_scripts/create_dummy_data.py
@@ -1,10 +1,8 @@
 # coding=utf-8
-import os, sys
+import os
+import sys
+
 sys.path.append(sys.path.append(os.path.join(os.path.dirname(__file__), '..'))) #run script as it was on parent folder
-from django.conf import settings
-from django.core.files import File  # you need this somewhere
-import urllib.request, urllib.parse, urllib.error
-from django.core.files.uploadedfile import SimpleUploadedFile
 import django
 import datetime
 from django.utils import timezone
@@ -14,23 +12,21 @@ from django.contrib.auth.models import User
 # models (please add in both lines)
 from web.models import StudySubject, Availability, Visit, Appointment, AppointmentType, AppointmentTypeLink, Study, Subject, Worker, Location, Language, Country, WorkerStudyRole, Item, FlyingTeam, Room, MailTemplate
 from smash.local_settings import MEDIA_ROOT
-from web.models.constants import REDCAP_TOKEN_CONFIGURATION_TYPE, REDCAP_BASE_URL_CONFIGURATION_TYPE, \
-    SEX_CHOICES_MALE, SEX_CHOICES_FEMALE, SUBJECT_TYPE_CHOICES_CONTROL, SUBJECT_TYPE_CHOICES_PATIENT, CONTACT_TYPES_PHONE, \
-    MONDAY_AS_DAY_OF_WEEK, COUNTRY_AFGHANISTAN_ID, VOUCHER_STATUS_NEW, GLOBAL_STUDY_ID, DEFAULT_LOCALE_NAME
+from web.models.constants import SEX_CHOICES_MALE, SEX_CHOICES_FEMALE, SUBJECT_TYPE_CHOICES_CONTROL, SUBJECT_TYPE_CHOICES_PATIENT, \
+    COUNTRY_AFGHANISTAN_ID, GLOBAL_STUDY_ID, DEFAULT_LOCALE_NAME
 from web.models.constants import MAIL_TEMPLATE_CONTEXT_APPOINTMENT, MAIL_TEMPLATE_CONTEXT_VISIT, \
     MAIL_TEMPLATE_CONTEXT_SUBJECT, MAIL_TEMPLATE_CONTEXT_VOUCHER
-from web.models.worker_study_role import ROLE_CHOICES_PROJECT_MANAGER, ROLE_CHOICES_SECRETARY, ROLE_CHOICES_DOCTOR, WORKER_VOUCHER_PARTNER, ROLE_CHOICES_TECHNICIAN, ROLE_CHOICES_PSYCHOLOGIST, ROLE_CHOICES_NURSE
+from web.models.worker_study_role import ROLE_CHOICES_PROJECT_MANAGER, ROLE_CHOICES_SECRETARY, ROLE_CHOICES_DOCTOR, \
+    ROLE_CHOICES_TECHNICIAN, ROLE_CHOICES_PSYCHOLOGIST, ROLE_CHOICES_NURSE
 
 from collections import defaultdict
 import logging
 logger = logging.getLogger(__name__)
 
 from web.utils import get_today_midnight_date
-from faker.providers import BaseProvider, color
+from faker.providers import BaseProvider
 from numpy.random import choice
 from faker import Faker
-import platform
-import tempfile
 from shutil import copyfile
 
 
diff --git a/smash/web/importer/importer.py b/smash/web/importer/importer.py
index 9cb1b299..69e06c87 100644
--- a/smash/web/importer/importer.py
+++ b/smash/web/importer/importer.py
@@ -3,7 +3,7 @@ import logging
 import sys
 import traceback
 
-from web.models import StudySubject, Subject, Language
+from web.models import StudySubject, Subject, Language, SubjectType
 from .etl_common import EtlCommon
 from .subject_import_reader import SubjectImportReader
 from .warning_counter import MsgCounterHandler
@@ -76,6 +76,8 @@ class Importer(EtlCommon):
 
             self.merged_count += 1
         else:
+            if study_subject.type is None:
+                study_subject.type = SubjectType.objects.all().first()
             study_subject.subject.save()
             study_subject.subject = Subject.objects.get(pk=study_subject.subject.id)
             study_subject.save()
diff --git a/smash/web/migrations/0194_migrate_subject_type_to_new_structure.py b/smash/web/migrations/0194_migrate_subject_type_to_new_structure.py
index a348877c..8e1bdab8 100644
--- a/smash/web/migrations/0194_migrate_subject_type_to_new_structure.py
+++ b/smash/web/migrations/0194_migrate_subject_type_to_new_structure.py
@@ -52,8 +52,9 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='studysubject',
             name='new_type',
-            field=models.ForeignKey(default=patient_type_id, on_delete=django.db.models.deletion.CASCADE,
-                                    to='web.subjecttype', verbose_name='Type')
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
+                                    to='web.subjecttype', verbose_name='Type',
+                                    blank=True, null=True)
         ),
         migrations.RunSQL("update web_studysubject " +
                           " set new_type_id = " + str(patient_type_id) + " where type = 'P'"),
diff --git a/smash/web/models/study_subject.py b/smash/web/models/study_subject.py
index 4f224593..c81add33 100644
--- a/smash/web/models/study_subject.py
+++ b/smash/web/models/study_subject.py
@@ -74,8 +74,8 @@ class StudySubject(models.Model):
     )
 
     type = models.ForeignKey("web.SubjectType",
-                             null=False,
-                             blank=False,
+                             null=True,
+                             blank=True,
                              on_delete=models.CASCADE,
                              verbose_name='Type'
                              )
diff --git a/smash/web/models/visit.py b/smash/web/models/visit.py
index c335cceb..eea63ff0 100644
--- a/smash/web/models/visit.py
+++ b/smash/web/models/visit.py
@@ -1,18 +1,17 @@
 # coding=utf-8
-import datetime
-from dateutil.relativedelta import relativedelta
+import logging
 
+from dateutil.relativedelta import relativedelta
 from django.db import models
+from django.db import transaction
 from django.db.models.signals import post_save
 from django.dispatch import receiver
-from django.db import transaction
 
 from web.models.constants import BOOL_CHOICES, SUBJECT_TYPE_CHOICES_CONTROL
-from web.models import Study
 
-import logging
 logger = logging.getLogger(__name__)
 
+
 class Visit(models.Model):
 
     class Meta:
diff --git a/smash/web/tests/api_views/test_subject.py b/smash/web/tests/api_views/test_subject.py
index 36596aca..3f2b213c 100644
--- a/smash/web/tests/api_views/test_subject.py
+++ b/smash/web/tests/api_views/test_subject.py
@@ -4,15 +4,15 @@ import json
 import logging
 
 from django.urls import reverse
-from parameterized import parameterized
 from django.utils import timezone
+from parameterized import parameterized
 from six import ensure_str
 
 from web.api_views.subject import get_subjects_order, get_subjects_filtered, serialize_subject, get_subject_columns
 from web.importer.warning_counter import MsgCounterHandler
 from web.models import StudySubject, Appointment, Study, Worker, SubjectColumns, StudyColumns
-from web.models.constants import GLOBAL_STUDY_ID, SUBJECT_TYPE_CHOICES_PATIENT, SUBJECT_TYPE_CHOICES_CONTROL, \
-    CUSTOM_FIELD_TYPE_TEXT, CUSTOM_FIELD_TYPE_BOOLEAN, CUSTOM_FIELD_TYPE_INTEGER, CUSTOM_FIELD_TYPE_DOUBLE, \
+from web.models.constants import GLOBAL_STUDY_ID, CUSTOM_FIELD_TYPE_TEXT, CUSTOM_FIELD_TYPE_BOOLEAN, \
+    CUSTOM_FIELD_TYPE_INTEGER, CUSTOM_FIELD_TYPE_DOUBLE, \
     CUSTOM_FIELD_TYPE_DATE, CUSTOM_FIELD_TYPE_SELECT_LIST, CUSTOM_FIELD_TYPE_FILE
 from web.models.custom_data import CustomStudySubjectField
 from web.models.custom_data.custom_study_subject_field import get_study_subject_field_id
diff --git a/smash/web/tests/importer/test_csv_subject_import_reader.py b/smash/web/tests/importer/test_csv_subject_import_reader.py
index 792331b8..963e895e 100644
--- a/smash/web/tests/importer/test_csv_subject_import_reader.py
+++ b/smash/web/tests/importer/test_csv_subject_import_reader.py
@@ -54,9 +54,6 @@ class TestCsvReader(TestCase):
     def test_load_language(self):
         self.subject_import_data.filename = get_resource_path('import_language.csv')
         study_subjects = CsvSubjectImportReader(self.subject_import_data).load_data()
-        for study_subject in study_subjects:
-            study_subject.subject.save()
-            study_subject.save()
 
         self.assertEqual(3, len(study_subjects))
         self.assertIsNotNone(study_subjects[0].subject.default_written_communication_language)
-- 
GitLab