From 8cb06ad2e9015f51b84595249fc1d6a045e0aa1e Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 27 Oct 2017 22:50:06 +0200
Subject: [PATCH] unit tests + unit tests organisation

---
 smash/web/api_views/worker.py                   |  1 -
 smash/web/models/subject.py                     | 10 ----------
 .../test_appointment.py}                        |  0
 .../test_appointment_type.py}                   |  0
 .../test_configuration_item.py}                 |  4 ++--
 .../test_location.py}                           |  0
 smash/web/tests/api_views/test_subject.py       |  2 --
 smash/web/tests/forms/__init__.py               |  0
 .../{ => forms}/test_AppointmentAddForm.py      |  4 ++--
 .../{ => forms}/test_AppointmentEditForm.py     |  2 +-
 .../{ => forms}/test_AvailabilityAddForm.py     |  6 +++---
 .../{ => forms}/test_AvailabilityEditForm.py    |  6 +++---
 .../tests/{ => forms}/test_SubjectAddForm.py    |  2 +-
 .../tests/{ => forms}/test_SubjectEditForm.py   |  4 ++--
 .../web/tests/{ => forms}/test_VisitAddForm.py  |  4 ++--
 .../test_appointment.py}                        |  2 +-
 .../test_configuration_item.py}                 |  0
 .../test_mail_template.py}                      |  4 ++--
 smash/web/tests/models/test_visit.py            |  4 ++++
 smash/web/tests/models/test_worker.py           | 17 +++++++++++++++++
 .../test_configuration.py}                      |  2 +-
 .../test_contact_attempt.py}                    |  4 ++--
 .../test_export.py}                             |  4 ++--
 .../test_functions.py}                          |  2 +-
 .../test_kit_request.py}                        |  4 ++--
 .../{test_view_login.py => view/test_login.py}  |  2 +-
 .../test_notifications.py}                      |  8 ++++----
 .../test_statistics.py}                         |  2 +-
 .../{test_view_visit.py => view/test_visit.py}  |  4 ++--
 29 files changed, 56 insertions(+), 48 deletions(-)
 rename smash/web/tests/{test_api_appointment.py => api_views/test_appointment.py} (100%)
 rename smash/web/tests/{test_api_appointment_type.py => api_views/test_appointment_type.py} (100%)
 rename smash/web/tests/{test_api_configuration_item.py => api_views/test_configuration_item.py} (98%)
 rename smash/web/tests/{test_api_location.py => api_views/test_location.py} (100%)
 create mode 100644 smash/web/tests/forms/__init__.py
 rename smash/web/tests/{ => forms}/test_AppointmentAddForm.py (89%)
 rename smash/web/tests/{ => forms}/test_AppointmentEditForm.py (94%)
 rename smash/web/tests/{ => forms}/test_AvailabilityAddForm.py (93%)
 rename smash/web/tests/{ => forms}/test_AvailabilityEditForm.py (89%)
 rename smash/web/tests/{ => forms}/test_SubjectAddForm.py (98%)
 rename smash/web/tests/{ => forms}/test_SubjectEditForm.py (97%)
 rename smash/web/tests/{ => forms}/test_VisitAddForm.py (92%)
 rename smash/web/tests/{test_model_appointment.py => models/test_appointment.py} (87%)
 rename smash/web/tests/{test_model_configuration_item.py => models/test_configuration_item.py} (100%)
 rename smash/web/tests/{test_model_mail_template.py => models/test_mail_template.py} (98%)
 rename smash/web/tests/{test_view_configuration.py => view/test_configuration.py} (87%)
 rename smash/web/tests/{test_view_contact_attempt.py => view/test_contact_attempt.py} (96%)
 rename smash/web/tests/{test_view_export.py => view/test_export.py} (90%)
 rename smash/web/tests/{test_view_functions.py => view/test_functions.py} (88%)
 rename smash/web/tests/{test_view_kit_request.py => view/test_kit_request.py} (97%)
 rename smash/web/tests/{test_view_login.py => view/test_login.py} (97%)
 rename smash/web/tests/{test_view_notifications.py => view/test_notifications.py} (98%)
 rename smash/web/tests/{test_view_statistics.py => view/test_statistics.py} (94%)
 rename smash/web/tests/{test_view_visit.py => view/test_visit.py} (96%)

diff --git a/smash/web/api_views/worker.py b/smash/web/api_views/worker.py
index ece5fc05..5183e3cd 100644
--- a/smash/web/api_views/worker.py
+++ b/smash/web/api_views/worker.py
@@ -24,7 +24,6 @@ def units(request):
 @login_required
 def workers_for_daily_planning(request):
     workers = get_workers_for_daily_planning(request)
-    print workers
     workers_list_for_json = []
     for worker in workers:
         worker_dict_for_json = {
diff --git a/smash/web/models/subject.py b/smash/web/models/subject.py
index c0a0b4b8..756cb960 100644
--- a/smash/web/models/subject.py
+++ b/smash/web/models/subject.py
@@ -168,16 +168,6 @@ class Subject(models.Model):
         editable=True
     )
 
-    def latest_visit(self):
-        visits = self.visit_set.all()
-        if len(visits) == 0:
-            return None
-        result = visits[0]
-        for visit in visits:
-            if visit.datetime_begin > result.datetime_begin:
-                result = visit
-        return result
-
     def __str__(self):
         return "%s %s" % (self.first_name, self.last_name)
 
diff --git a/smash/web/tests/test_api_appointment.py b/smash/web/tests/api_views/test_appointment.py
similarity index 100%
rename from smash/web/tests/test_api_appointment.py
rename to smash/web/tests/api_views/test_appointment.py
diff --git a/smash/web/tests/test_api_appointment_type.py b/smash/web/tests/api_views/test_appointment_type.py
similarity index 100%
rename from smash/web/tests/test_api_appointment_type.py
rename to smash/web/tests/api_views/test_appointment_type.py
diff --git a/smash/web/tests/test_api_configuration_item.py b/smash/web/tests/api_views/test_configuration_item.py
similarity index 98%
rename from smash/web/tests/test_api_configuration_item.py
rename to smash/web/tests/api_views/test_configuration_item.py
index 102e3a5b..d4becf7c 100644
--- a/smash/web/tests/test_api_configuration_item.py
+++ b/smash/web/tests/api_views/test_configuration_item.py
@@ -4,9 +4,9 @@
 from django.urls import reverse
 
 from web.models import ConfigurationItem
-from web.tests.functions import create_configuration_item
-from . import LoggedInTestCase
 from web.models.constants import CANCELLED_APPOINTMENT_COLOR_CONFIGURATION_TYPE
+from web.tests import LoggedInTestCase
+from web.tests.functions import create_configuration_item
 
 
 class TestConfigurationItemApi(LoggedInTestCase):
diff --git a/smash/web/tests/test_api_location.py b/smash/web/tests/api_views/test_location.py
similarity index 100%
rename from smash/web/tests/test_api_location.py
rename to smash/web/tests/api_views/test_location.py
diff --git a/smash/web/tests/api_views/test_subject.py b/smash/web/tests/api_views/test_subject.py
index 18dea89e..b96c694f 100644
--- a/smash/web/tests/api_views/test_subject.py
+++ b/smash/web/tests/api_views/test_subject.py
@@ -415,8 +415,6 @@ class TestApi(TestCase):
         appointment.save()
         visit.mark_as_finished()
 
-        print Visit.objects.all()
-
         self.check_subject_filtered([["visit_1", "DONE"]], [subject])
         self.check_subject_filtered([["visit_2", "UPCOMING"]], [subject])
 
diff --git a/smash/web/tests/forms/__init__.py b/smash/web/tests/forms/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/smash/web/tests/test_AppointmentAddForm.py b/smash/web/tests/forms/test_AppointmentAddForm.py
similarity index 89%
rename from smash/web/tests/test_AppointmentAddForm.py
rename to smash/web/tests/forms/test_AppointmentAddForm.py
index 7861e12f..a63675e5 100644
--- a/smash/web/tests/test_AppointmentAddForm.py
+++ b/smash/web/tests/forms/test_AppointmentAddForm.py
@@ -1,9 +1,9 @@
 from django.test import TestCase
 
-from functions import create_user, create_visit, create_location
-from functions import get_test_location
 from web.forms import AppointmentAddForm
 from web.models import Worker
+from web.tests.functions import create_user, create_visit, create_location
+from web.tests.functions import get_test_location
 
 
 class AppointmentAddFormTests(TestCase):
diff --git a/smash/web/tests/test_AppointmentEditForm.py b/smash/web/tests/forms/test_AppointmentEditForm.py
similarity index 94%
rename from smash/web/tests/test_AppointmentEditForm.py
rename to smash/web/tests/forms/test_AppointmentEditForm.py
index eba5c6d0..0a60144e 100644
--- a/smash/web/tests/test_AppointmentEditForm.py
+++ b/smash/web/tests/forms/test_AppointmentEditForm.py
@@ -1,9 +1,9 @@
 from django.test import TestCase
 
-from functions import get_test_location, create_user, create_visit, create_location
 from web.forms import AppointmentAddForm
 from web.forms import AppointmentEditForm
 from web.models import Appointment, Worker
+from web.tests.functions import get_test_location, create_user, create_visit, create_location
 
 
 class AppointmentEditFormTests(TestCase):
diff --git a/smash/web/tests/test_AvailabilityAddForm.py b/smash/web/tests/forms/test_AvailabilityAddForm.py
similarity index 93%
rename from smash/web/tests/test_AvailabilityAddForm.py
rename to smash/web/tests/forms/test_AvailabilityAddForm.py
index 85987100..0d8f1bee 100644
--- a/smash/web/tests/test_AvailabilityAddForm.py
+++ b/smash/web/tests/forms/test_AvailabilityAddForm.py
@@ -1,10 +1,10 @@
 from django.test import TestCase
 
-from functions import create_user, create_visit
-from functions import get_test_location
-from web.models.constants import MONDAY_AS_DAY_OF_WEEK
 from web.forms import AvailabilityAddForm
 from web.models import Worker
+from web.models.constants import MONDAY_AS_DAY_OF_WEEK
+from web.tests.functions import create_user, create_visit
+from web.tests.functions import get_test_location
 
 
 class AvailabilityAddFormTests(TestCase):
diff --git a/smash/web/tests/test_AvailabilityEditForm.py b/smash/web/tests/forms/test_AvailabilityEditForm.py
similarity index 89%
rename from smash/web/tests/test_AvailabilityEditForm.py
rename to smash/web/tests/forms/test_AvailabilityEditForm.py
index 9d2d3bd1..0779eabe 100644
--- a/smash/web/tests/test_AvailabilityEditForm.py
+++ b/smash/web/tests/forms/test_AvailabilityEditForm.py
@@ -1,10 +1,10 @@
 from django.test import TestCase
 
-from functions import create_user, create_visit
-from functions import get_test_location
-from web.models.constants import MONDAY_AS_DAY_OF_WEEK
 from web.forms import AvailabilityEditForm, AvailabilityAddForm
 from web.models import Worker
+from web.models.constants import MONDAY_AS_DAY_OF_WEEK
+from web.tests.functions import create_user, create_visit
+from web.tests.functions import get_test_location
 
 
 class AvailabilityEditFormTests(TestCase):
diff --git a/smash/web/tests/test_SubjectAddForm.py b/smash/web/tests/forms/test_SubjectAddForm.py
similarity index 98%
rename from smash/web/tests/test_SubjectAddForm.py
rename to smash/web/tests/forms/test_SubjectAddForm.py
index 77e9c6d6..c22fee47 100644
--- a/smash/web/tests/test_SubjectAddForm.py
+++ b/smash/web/tests/forms/test_SubjectAddForm.py
@@ -2,9 +2,9 @@ from django.contrib.auth.models import User
 from django.test import Client
 from django.test import TestCase
 
-from functions import get_test_location, create_subject, create_worker
 from web.forms import SubjectAddForm, get_new_screening_number
 from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL
+from web.tests.functions import get_test_location, create_subject, create_worker
 
 
 class SubjectAddFormTests(TestCase):
diff --git a/smash/web/tests/test_SubjectEditForm.py b/smash/web/tests/forms/test_SubjectEditForm.py
similarity index 97%
rename from smash/web/tests/test_SubjectEditForm.py
rename to smash/web/tests/forms/test_SubjectEditForm.py
index bf9fb731..25f2047a 100644
--- a/smash/web/tests/test_SubjectEditForm.py
+++ b/smash/web/tests/forms/test_SubjectEditForm.py
@@ -1,12 +1,12 @@
 from django.contrib.auth.models import User
-from django.test import TestCase
 from django.test import Client
+from django.test import TestCase
 
-from functions import get_test_location, create_worker
 from web.forms import SubjectAddForm
 from web.forms import SubjectEditForm
 from web.models import Subject
 from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL
+from web.tests.functions import get_test_location, create_worker
 
 
 class SubjectEditFormTests(TestCase):
diff --git a/smash/web/tests/test_VisitAddForm.py b/smash/web/tests/forms/test_VisitAddForm.py
similarity index 92%
rename from smash/web/tests/test_VisitAddForm.py
rename to smash/web/tests/forms/test_VisitAddForm.py
index 2efa428b..b6a0d817 100644
--- a/smash/web/tests/test_VisitAddForm.py
+++ b/smash/web/tests/forms/test_VisitAddForm.py
@@ -2,9 +2,9 @@ from __future__ import print_function
 
 from django.test import TestCase
 
-from functions import create_subject
-from functions import get_test_location
 from web.forms import VisitAddForm
+from web.tests.functions import create_subject
+from web.tests.functions import get_test_location
 
 
 class VisitAddFormTests(TestCase):
diff --git a/smash/web/tests/test_model_appointment.py b/smash/web/tests/models/test_appointment.py
similarity index 87%
rename from smash/web/tests/test_model_appointment.py
rename to smash/web/tests/models/test_appointment.py
index 36525f58..a781e69c 100644
--- a/smash/web/tests/test_model_appointment.py
+++ b/smash/web/tests/models/test_appointment.py
@@ -1,6 +1,6 @@
 from django.test import TestCase
 
-from functions import create_appointment
+from web.tests.functions import create_appointment
 
 
 class AppointmentModelTests(TestCase):
diff --git a/smash/web/tests/test_model_configuration_item.py b/smash/web/tests/models/test_configuration_item.py
similarity index 100%
rename from smash/web/tests/test_model_configuration_item.py
rename to smash/web/tests/models/test_configuration_item.py
diff --git a/smash/web/tests/test_model_mail_template.py b/smash/web/tests/models/test_mail_template.py
similarity index 98%
rename from smash/web/tests/test_model_mail_template.py
rename to smash/web/tests/models/test_mail_template.py
index 04603143..df6611fa 100644
--- a/smash/web/tests/test_model_mail_template.py
+++ b/smash/web/tests/models/test_mail_template.py
@@ -3,12 +3,12 @@ import StringIO
 from django.test import TestCase
 from docx import Document
 
-from functions import create_language, get_resource_path, create_appointment, create_user, create_subject, \
-    create_visit
 from web.models import MailTemplate
 from web.models.constants import MAIL_TEMPLATE_CONTEXT_APPOINTMENT, MAIL_TEMPLATE_CONTEXT_VISIT, \
     MAIL_TEMPLATE_CONTEXT_SUBJECT
 from web.models.mail_template import DATE_FORMAT_SHORT
+from web.tests.functions import create_language, get_resource_path, create_appointment, create_user, create_subject, \
+    create_visit
 
 
 class MailTemplateModelTests(TestCase):
diff --git a/smash/web/tests/models/test_visit.py b/smash/web/tests/models/test_visit.py
index 3b302837..835b7a66 100644
--- a/smash/web/tests/models/test_visit.py
+++ b/smash/web/tests/models/test_visit.py
@@ -35,3 +35,7 @@ class VisitModelTests(TestCase):
 
         visit_count = Visit.objects.filter(subject=subject).count()
         self.assertEquals(1, visit_count)
+
+    def test_visit_to_string(self):
+        visit = create_visit(create_subject())
+        self.assertIsNotNone(str(visit))
diff --git a/smash/web/tests/models/test_worker.py b/smash/web/tests/models/test_worker.py
index be91b963..9b193326 100644
--- a/smash/web/tests/models/test_worker.py
+++ b/smash/web/tests/models/test_worker.py
@@ -2,6 +2,7 @@ from django.contrib.auth.models import User
 from django.test import Client
 from django.test import TestCase
 
+from web.models import Worker
 from web.tests.functions import create_worker
 
 
@@ -29,3 +30,19 @@ class WorkerModelTests(TestCase):
     def test_is_active_for_invalid(self):
         worker = create_worker()
         self.assertFalse(worker.is_active())
+
+    def test_get_by_user_for_None(self):
+        worker = Worker.get_by_user(None)
+        self.assertIsNone(worker)
+
+    def test_get_by_user_for_Worker(self):
+        worker = create_worker()
+        worker_2 = Worker.get_by_user(worker)
+        self.assertEqual(worker, worker_2)
+
+    def test_get_by_user_for_invalid(self):
+        try:
+            Worker.get_by_user("invalid object")
+            self.fail("Exception expected")
+        except TypeError:
+            pass
diff --git a/smash/web/tests/test_view_configuration.py b/smash/web/tests/view/test_configuration.py
similarity index 87%
rename from smash/web/tests/test_view_configuration.py
rename to smash/web/tests/view/test_configuration.py
index 4b9bbccd..23a14779 100644
--- a/smash/web/tests/test_view_configuration.py
+++ b/smash/web/tests/view/test_configuration.py
@@ -2,7 +2,7 @@ import datetime
 
 from django.urls import reverse
 
-from . import LoggedInTestCase
+from web.tests import LoggedInTestCase
 
 
 class ConfigurationViewTests(LoggedInTestCase):
diff --git a/smash/web/tests/test_view_contact_attempt.py b/smash/web/tests/view/test_contact_attempt.py
similarity index 96%
rename from smash/web/tests/test_view_contact_attempt.py
rename to smash/web/tests/view/test_contact_attempt.py
index 6a36fdaa..9bb40d71 100644
--- a/smash/web/tests/test_view_contact_attempt.py
+++ b/smash/web/tests/view/test_contact_attempt.py
@@ -4,10 +4,10 @@ from django.urls import reverse
 from django.utils import timezone
 
 from web.forms import ContactAttemptEditForm
-from functions import create_subject, create_contact_attempt, format_form_field
 from web.models import ContactAttempt
 from web.models.constants import CONTACT_TYPES_EMAIL
-from . import LoggedInWithWorkerTestCase
+from web.tests import LoggedInWithWorkerTestCase
+from web.tests.functions import create_subject, create_contact_attempt, format_form_field
 
 
 class ContactAttemptViewTests(LoggedInWithWorkerTestCase):
diff --git a/smash/web/tests/test_view_export.py b/smash/web/tests/view/test_export.py
similarity index 90%
rename from smash/web/tests/test_view_export.py
rename to smash/web/tests/view/test_export.py
index f15c2cf4..f2c7850f 100644
--- a/smash/web/tests/test_view_export.py
+++ b/smash/web/tests/view/test_export.py
@@ -1,8 +1,8 @@
 # coding=utf-8
 from django.urls import reverse
 
-from functions import create_subject, create_appointment
-from . import LoggedInTestCase
+from web.tests import LoggedInTestCase
+from web.tests.functions import create_subject, create_appointment
 
 
 class TestExportView(LoggedInTestCase):
diff --git a/smash/web/tests/test_view_functions.py b/smash/web/tests/view/test_functions.py
similarity index 88%
rename from smash/web/tests/test_view_functions.py
rename to smash/web/tests/view/test_functions.py
index 7df3e0a0..d03ab059 100644
--- a/smash/web/tests/test_view_functions.py
+++ b/smash/web/tests/view/test_functions.py
@@ -1,6 +1,6 @@
 from django.test import TestCase
 
-from functions import create_location, create_user, create_worker, get_test_location
+from web.tests.functions import create_location, create_user, create_worker, get_test_location
 from web.views.notifications import get_filter_locations
 
 
diff --git a/smash/web/tests/test_view_kit_request.py b/smash/web/tests/view/test_kit_request.py
similarity index 97%
rename from smash/web/tests/test_view_kit_request.py
rename to smash/web/tests/view/test_kit_request.py
index cc82b543..f0d199b6 100644
--- a/smash/web/tests/test_view_kit_request.py
+++ b/smash/web/tests/view/test_kit_request.py
@@ -3,11 +3,11 @@ import datetime
 from django.core import mail
 from django.urls import reverse
 
-from functions import create_appointment_type, create_appointment, create_visit
 from web.models import Item, Appointment, AppointmentTypeLink
+from web.tests import LoggedInTestCase
+from web.tests.functions import create_appointment_type, create_appointment, create_visit
 from web.views.kit import get_kit_requests
 from web.views.notifications import get_today_midnight_date
-from . import LoggedInTestCase
 
 
 class ViewFunctionsTests(LoggedInTestCase):
diff --git a/smash/web/tests/test_view_login.py b/smash/web/tests/view/test_login.py
similarity index 97%
rename from smash/web/tests/test_view_login.py
rename to smash/web/tests/view/test_login.py
index f3fa9b05..f66d376e 100644
--- a/smash/web/tests/test_view_login.py
+++ b/smash/web/tests/view/test_login.py
@@ -5,8 +5,8 @@ from django.test import Client
 from django.test import TestCase
 from django.urls import reverse
 
-from functions import create_user
 from web.models import Worker
+from web.tests.functions import create_user
 
 
 class TestLoginView(TestCase):
diff --git a/smash/web/tests/test_view_notifications.py b/smash/web/tests/view/test_notifications.py
similarity index 98%
rename from smash/web/tests/test_view_notifications.py
rename to smash/web/tests/view/test_notifications.py
index 9c79dd8c..c2586124 100644
--- a/smash/web/tests/test_view_notifications.py
+++ b/smash/web/tests/view/test_notifications.py
@@ -2,10 +2,11 @@ import datetime
 
 from django.contrib.auth.models import AnonymousUser
 
-from functions import create_appointment, create_location, create_worker, create_appointment_type
-from functions import create_subject
-from functions import create_visit
 from web.models import Appointment, Location, AppointmentTypeLink
+from web.tests import LoggedInTestCase
+from web.tests.functions import create_appointment, create_location, create_worker, create_appointment_type
+from web.tests.functions import create_subject
+from web.tests.functions import create_visit
 from web.views.notifications import \
     get_approaching_visits_for_mail_contact, \
     get_approaching_visits_for_mail_contact_count, \
@@ -22,7 +23,6 @@ from web.views.notifications import \
     get_unfinished_appointments, \
     get_unfinished_appointments_count, \
     get_unfinished_visits
-from . import LoggedInTestCase
 
 
 class NotificationViewTests(LoggedInTestCase):
diff --git a/smash/web/tests/test_view_statistics.py b/smash/web/tests/view/test_statistics.py
similarity index 94%
rename from smash/web/tests/test_view_statistics.py
rename to smash/web/tests/view/test_statistics.py
index eac349b6..795a1ed8 100644
--- a/smash/web/tests/test_view_statistics.py
+++ b/smash/web/tests/view/test_statistics.py
@@ -3,7 +3,7 @@ from datetime import datetime
 
 from django.urls import reverse
 
-from . import LoggedInTestCase
+from web.tests import LoggedInTestCase
 
 __author__ = 'Valentin Grouès'
 
diff --git a/smash/web/tests/test_view_visit.py b/smash/web/tests/view/test_visit.py
similarity index 96%
rename from smash/web/tests/test_view_visit.py
rename to smash/web/tests/view/test_visit.py
index b8c0a286..217475e6 100644
--- a/smash/web/tests/test_view_visit.py
+++ b/smash/web/tests/view/test_visit.py
@@ -2,11 +2,11 @@ import datetime
 
 from django.urls import reverse
 
-from functions import create_subject, create_visit, create_appointment, create_appointment_type
 from web.forms import VisitDetailForm, VisitAddForm
 from web.models import Visit
+from web.tests import LoggedInTestCase
+from web.tests.functions import create_subject, create_visit, create_appointment, create_appointment_type
 from web.views.notifications import get_today_midnight_date
-from . import LoggedInTestCase
 
 
 class VisitViewTests(LoggedInTestCase):
-- 
GitLab