From 005d161fa8219f59d7429b9b5463a52c720d2c27 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 17 Apr 2020 10:21:05 +0200
Subject: [PATCH] don't add appointment link to existing appointments

---
 smash/web/importer/csv_tns_visit_import_reader.py      |  8 ++++----
 smash/web/tests/importer/test_exporter.py              |  6 +++---
 smash/web/tests/importer/test_exporter_cron_job.py     |  6 +++---
 .../tests/importer/test_tns_csv_visit_import_reader.py | 10 ++++++++++
 4 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/smash/web/importer/csv_tns_visit_import_reader.py b/smash/web/importer/csv_tns_visit_import_reader.py
index c8b46e74..9032daf2 100644
--- a/smash/web/importer/csv_tns_visit_import_reader.py
+++ b/smash/web/importer/csv_tns_visit_import_reader.py
@@ -1,10 +1,10 @@
 # coding=utf-8
+import codecs
 import csv
 import datetime
 import logging
 import sys
 import traceback
-import codecs
 
 import pytz
 from django.conf import settings
@@ -100,9 +100,9 @@ class TnsCsvVisitImportReader:
                     else:
                         appointment = Appointment.objects.create(visit=visit, length=60, datetime_when=date,
                                                                  location=location)
-                    if self.appointment_type is not None:
-                        AppointmentTypeLink.objects.create(appointment_id=appointment.id,
-                                                           appointment_type=self.appointment_type)
+                        if self.appointment_type is not None:
+                            AppointmentTypeLink.objects.create(appointment_id=appointment.id,
+                                                               appointment_type=self.appointment_type)
                     self.processed_count += 1
                 except:
                     self.problematic_count += 1
diff --git a/smash/web/tests/importer/test_exporter.py b/smash/web/tests/importer/test_exporter.py
index 3a552443..91eb81a9 100644
--- a/smash/web/tests/importer/test_exporter.py
+++ b/smash/web/tests/importer/test_exporter.py
@@ -6,7 +6,7 @@ import logging
 from django.test import TestCase
 
 from web.tests.functions import create_study_subject
-from web.importer import Exporter
+from web.importer import SubjectExporter
 from web.models import Subject, StudySubject, Study, Provenance
 from web.models.constants import GLOBAL_STUDY_ID
 
@@ -21,7 +21,7 @@ class TestExporter(TestCase):
     def test_export_not_excluded(self):
         create_study_subject()
 
-        exporter = Exporter(filename="empty.csv")
+        exporter = SubjectExporter(filename="empty.csv")
         exporter.execute()
 
         self.assertEqual(0, exporter.exported_count)
@@ -32,7 +32,7 @@ class TestExporter(TestCase):
         subject.excluded=True
         subject.save()
 
-        exporter = Exporter(filename="empty.csv")
+        exporter = SubjectExporter(filename="empty.csv")
         exporter.execute()
 
         self.assertEqual(1, exporter.exported_count)
diff --git a/smash/web/tests/importer/test_exporter_cron_job.py b/smash/web/tests/importer/test_exporter_cron_job.py
index 95c09253..7bb15fc9 100644
--- a/smash/web/tests/importer/test_exporter_cron_job.py
+++ b/smash/web/tests/importer/test_exporter_cron_job.py
@@ -6,7 +6,7 @@ import tempfile
 from django.conf import settings
 from django.test import TestCase
 
-from web.importer import ExporterCronJob
+from web.importer import SubjectExporterCronJob
 
 logger = logging.getLogger(__name__)
 from django.core import mail
@@ -24,7 +24,7 @@ class TestCronJobExporter(TestCase):
     def test_import_without_configuration(self):
         CronJobLog.objects.all().delete()
 
-        job = ExporterCronJob()
+        job = SubjectExporterCronJob()
 
         status = job.do()
 
@@ -37,7 +37,7 @@ class TestCronJobExporter(TestCase):
         setattr(settings, "DAILY_SUBJECT_EXPORT_FILE", tmp)
         CronJobLog.objects.all().delete()
 
-        job = ExporterCronJob()
+        job = SubjectExporterCronJob()
 
         status = job.do()
 
diff --git a/smash/web/tests/importer/test_tns_csv_visit_import_reader.py b/smash/web/tests/importer/test_tns_csv_visit_import_reader.py
index f8ee5ce7..c1540557 100644
--- a/smash/web/tests/importer/test_tns_csv_visit_import_reader.py
+++ b/smash/web/tests/importer/test_tns_csv_visit_import_reader.py
@@ -151,6 +151,16 @@ class TestTnsCsvVisitReader(TestCase):
 
         self.assertEquals(3, self.get_warnings_count())
 
+    def test_dont_add_links_for_existing_appointments(self):
+        filename = get_resource_path('tns_vouchers_import.csv')
+        TnsCsvVisitImportReader().load_data(filename)
+        links = AppointmentTypeLink.objects.all().count()
+
+        TnsCsvVisitImportReader().load_data(filename)
+        self.assertEquals(links, AppointmentTypeLink.objects.all().count())
+
+        self.assertEquals(0, self.get_warnings_count())
+
     def get_warnings_count(self):
         if "WARNING" in self.warning_counter.level2count:
             return self.warning_counter.level2count["WARNING"]
-- 
GitLab