Skip to content
Snippets Groups Projects
Commit bc8bdfe4 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch 'test-coverage-unit-test' into 'master'

Test coverage unit tests

See merge request NCER-PD/scheduling-system!100
parents 415e5d44 53363a23
No related branches found
No related tags found
1 merge request!100Test coverage unit tests
Pipeline #
Showing
with 290 additions and 89 deletions
......@@ -5,7 +5,7 @@ from django.db.models import Count, Case, When, Min
from django.db.models import Q
from django.http import JsonResponse
from web.models import Subject, StudySubject, Visit, Appointment
from web.models import StudySubject, Visit, Appointment, Subject
from web.models.constants import SUBJECT_TYPE_CHOICES
from web.views import e500_error
from web.views.notifications import get_subjects_with_no_visit, get_subjects_with_reminder, get_today_midnight_date
......@@ -16,14 +16,10 @@ logger = logging.getLogger(__name__)
@login_required
def cities(request):
try:
result_subjects = Subject.objects.filter(city__isnull=False).values_list('city').distinct()
return JsonResponse({
"cities": [x[0] for x in result_subjects]
})
except Exception as e:
logger.error(e, exc_info=True)
return e500_error(request)
result_subjects = Subject.objects.filter(city__isnull=False).values_list('city').distinct()
return JsonResponse({
"cities": [x[0] for x in result_subjects]
})
@login_required
......@@ -82,27 +78,18 @@ def get_subjects_order(subjects_to_be_ordered, order_column, order_direction):
result = subjects_to_be_ordered.order_by(order_direction + 'id')
elif order_column == "date_born":
result = subjects_to_be_ordered.order_by(order_direction + 'subject__date_born')
elif order_column == "visit_1":
result = order_by_visit(subjects_to_be_ordered, order_direction, 1)
elif order_column == "visit_2":
result = order_by_visit(subjects_to_be_ordered, order_direction, 2)
elif order_column == "visit_3":
result = order_by_visit(subjects_to_be_ordered, order_direction, 3)
elif order_column == "visit_4":
result = order_by_visit(subjects_to_be_ordered, order_direction, 4)
elif order_column == "visit_5":
result = order_by_visit(subjects_to_be_ordered, order_direction, 5)
elif order_column == "visit_6":
result = order_by_visit(subjects_to_be_ordered, order_direction, 6)
elif order_column == "visit_7":
result = order_by_visit(subjects_to_be_ordered, order_direction, 7)
elif order_column == "visit_8":
result = order_by_visit(subjects_to_be_ordered, order_direction, 8)
elif str(order_column).startswith("visit_"):
visit_number = get_visit_number_from_visit_x_string(order_column)
result = order_by_visit(subjects_to_be_ordered, order_direction, visit_number)
else:
logger.warn("Unknown sort column: " + order_column)
logger.warn("Unknown sort column: " + str(order_column))
return result
def get_visit_number_from_visit_x_string(order_column):
return int(str(order_column).split("_")[1])
def filter_by_visit(result, visit_number, visit_type):
# we need to give custom names for filtering params that contain visit_number in it
# because we might want to filter by few visits and they shouldn't collide
......@@ -189,22 +176,9 @@ def get_subjects_filtered(subjects_to_be_filtered, filters):
result = result.filter(default_location=value)
elif column == "type":
result = result.filter(type=value)
elif column == "visit_1":
result = filter_by_visit(result, 1, value)
elif column == "visit_2":
result = filter_by_visit(result, 2, value)
elif column == "visit_3":
result = filter_by_visit(result, 3, value)
elif column == "visit_4":
result = filter_by_visit(result, 4, value)
elif column == "visit_5":
result = filter_by_visit(result, 5, value)
elif column == "visit_6":
result = filter_by_visit(result, 6, value)
elif column == "visit_7":
result = filter_by_visit(result, 7, value)
elif column == "visit_8":
result = filter_by_visit(result, 8, value)
elif str(column).startswith("visit_"):
visit_number = get_visit_number_from_visit_x_string(column)
result = filter_by_visit(result, visit_number, value)
elif column == "":
pass
else:
......@@ -281,12 +255,6 @@ def get_yes_no(val):
return "NO"
def serialize_subject_visit(visit):
status = "---"
appointments = visit.appointment_set.filter()
pass
def serialize_subject(study_subject):
location = ""
if study_subject.default_location is not None:
......
......@@ -24,7 +24,16 @@ class Availability(models.Model):
)
def __str__(self):
return "%d %s %s" % (self.day_number, self.person.last_name, self.person.first_name)
day_of_week = self.get_day_of_week_as_string()
return "%s %s %s" % (day_of_week, self.person.last_name, self.person.first_name)
def __unicode__(self):
return "%d %s %s" % (self.day_number, self.person.last_name, self.person.first_name)
day_of_week = self.get_day_of_week_as_string()
return "%s %s %s" % (day_of_week, self.person.last_name, self.person.first_name)
def get_day_of_week_as_string(self):
day_of_week = "N/A"
for row in WEEKDAY_CHOICES:
if row[0] == self.day_number:
day_of_week = row[1]
return day_of_week
......@@ -6,27 +6,6 @@ class FlyingTeam(models.Model):
class Meta:
app_label = 'web'
# doctor = models.ForeignKey(Worker, related_name='FlyingTeamDoctor',
# verbose_name='Doctor'
# )
# nurse = models.ForeignKey(Worker, related_name='FlyingTeamNurse',
# verbose_name='Nurse'
# )
# psychologist = models.ForeignKey(Worker, related_name='FlyingTeamPsychologist',
# verbose_name='Psychologist'
# )
# datetime_called = models.DateTimeField(
# verbose_name='Created on'
# )
# datetime_until = models.DateTimeField(
# verbose_name='Disbanded on'
# )
#
# def __str__(self):
# return "%s %s %s" % (self.doctor.last_name, self.nurse.last_name, self.psychologist.last_name)
#
# def __unicode__(self):
# return "%s %s %s" % (self.doctor.last_name, self.nurse.last_name, self.psychologist.last_name)
place = models.CharField(max_length=255, verbose_name='Place')
def __str__(self):
......
......@@ -9,7 +9,7 @@ from django.urls import reverse
from web.models import AppointmentTypeLink
from web.tests.functions import create_study_subject, create_worker, create_visit, create_appointment, \
create_appointment_type, create_get_suffix
create_appointment_type, create_get_suffix, create_flying_team
from web.views.appointment import APPOINTMENT_LIST_GENERIC, APPOINTMENT_LIST_APPROACHING, APPOINTMENT_LIST_UNFINISHED
from web.views.notifications import get_today_midnight_date
......@@ -30,11 +30,14 @@ class TestAppointmentApi(TestCase):
self.assertEqual(response.status_code, 500)
def test_appointments_valid(self):
place = "Some new flying team location"
name = "Piotrek"
self.study_subject.subject.first_name = name
self.study_subject.subject.save()
visit = create_visit(self.study_subject)
create_appointment(visit)
appointment = create_appointment(visit)
appointment.flying_team = create_flying_team(place=place)
appointment.save()
appointment2 = create_appointment(visit, get_today_midnight_date())
appointment2.visit = None
......@@ -47,6 +50,20 @@ class TestAppointmentApi(TestCase):
self.assertEqual(response.status_code, 200)
self.assertTrue(name in response.content)
self.assertTrue(place in response.content)
def test_flying_team_location_in_appointments(self):
place = "Some new flying team location"
visit = create_visit(self.study_subject)
appointment = create_appointment(visit)
appointment.flying_team = create_flying_team(place=place)
appointment.save()
url = reverse('web.api.appointments', kwargs={'type': APPOINTMENT_LIST_GENERIC})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTrue(place in response.content)
def test_appointments_approaching(self):
name = "Piotrek"
......
......@@ -368,7 +368,7 @@ class TestApi(TestCase):
def test_subjects_filter_visit_1_SHOULD_BE_IN_PROGRESS(self):
subject = self.study_subject
visit = create_visit(subject)
create_visit(subject)
self.check_subject_filtered([["visit_1", "SHOULD_BE_IN_PROGRESS"]], [subject])
self.check_subject_filtered([["visit_2", "SHOULD_BE_IN_PROGRESS"]], [])
......@@ -410,3 +410,17 @@ class TestApi(TestCase):
self.check_subject_filtered([["visit_1", "DONE"], ["visit_2", "UPCOMING"]], [subject])
self.check_subject_filtered([["visit_1", "UPCOMING"], ["visit_2", "DONE"]], [])
def test_subjects_ordered_by_visit_1(self):
subject = self.study_subject
subject2 = create_study_subject(2)
visit = create_visit(subject)
appointment = create_appointment(visit)
appointment.status = Appointment.APPOINTMENT_STATUS_FINISHED
appointment.save()
self.check_subject_ordered("visit_1", [subject, subject2])
def test_invalid_order(self):
self.check_subject_ordered(None, [self.study_subject])
smash/web/tests/data/PL.png

413 B

import logging
from django.test import TestCase
from web.tests.functions import create_appointment_type
logger = logging.getLogger(__name__)
class AppointmentTypeTests(TestCase):
def test_str(self):
appointment_type = create_appointment_type()
self.assertIsNotNone(str(appointment_type))
import logging
from django.test import TestCase
from web.models import AppointmentTypeLink
from web.tests.functions import create_appointment, create_appointment_type
from web.views.notifications import get_today_midnight_date
logger = logging.getLogger(__name__)
class AppointmentTypeLinkTests(TestCase):
def test_save(self):
link = AppointmentTypeLink(appointment=create_appointment(), appointment_type=create_appointment_type())
link.date_when = get_today_midnight_date()
link.save()
self.assertIsNone(link.date_when.tzname())
import logging
from django.test import TestCase
from web.models import Availability
from web.tests import create_worker
from web.models.constants import MONDAY_AS_DAY_OF_WEEK
logger = logging.getLogger(__name__)
class AvailabilityTests(TestCase):
def test_str(self):
availability = Availability(person=create_worker(), day_number=MONDAY_AS_DAY_OF_WEEK)
self.assertTrue("MONDAY" in str(availability))
self.assertTrue("MONDAY" in unicode(availability))
......@@ -2,7 +2,9 @@ from django.test import TestCase
from web.models import ConfigurationItem
from web.models.constants import CANCELLED_APPOINTMENT_COLOR_CONFIGURATION_TYPE, \
NO_SHOW_APPOINTMENT_COLOR_CONFIGURATION_TYPE
NO_SHOW_APPOINTMENT_COLOR_CONFIGURATION_TYPE, KIT_EMAIL_HOUR_CONFIGURATION_TYPE, \
KIT_EMAIL_DAY_OF_WEEK_CONFIGURATION_TYPE
from web.tests.functions import create_configuration_item
class ConfigurationItemModelTests(TestCase):
......@@ -12,3 +14,22 @@ class ConfigurationItemModelTests(TestCase):
items = ConfigurationItem.objects.filter(type=NO_SHOW_APPOINTMENT_COLOR_CONFIGURATION_TYPE)
self.assertTrue(len(items) > 0)
def test_str(self):
configuration_item = create_configuration_item()
self.assertIsNotNone(str(configuration_item))
self.assertIsNotNone(unicode(configuration_item))
def test_validate(self):
item = ConfigurationItem.objects.filter(type=KIT_EMAIL_HOUR_CONFIGURATION_TYPE)[0]
item.value = "09:00"
self.assertEqual("", ConfigurationItem.validation_error(item))
item.value = "text"
self.assertNotEqual("", ConfigurationItem.validation_error(item))
item = ConfigurationItem.objects.filter(type=KIT_EMAIL_DAY_OF_WEEK_CONFIGURATION_TYPE)[0]
item.value = "MONDAY"
self.assertEqual("", ConfigurationItem.validation_error(item))
item.value = "unknown day"
self.assertNotEqual("", ConfigurationItem.validation_error(item))
import logging
from django.test import TestCase
from web.tests.functions import create_study_subject
from web.models import ContactAttempt
from web.tests import create_worker
logger = logging.getLogger(__name__)
class ContactAttemptTests(TestCase):
def test_str(self):
contact_attempt = ContactAttempt(worker=create_worker(), subject=create_study_subject())
self.assertIsNotNone(str(contact_attempt))
self.assertIsNotNone(unicode(contact_attempt))
import logging
from django.test import TestCase
from web.models import FlyingTeam
logger = logging.getLogger(__name__)
class FlyingTeamTests(TestCase):
def test_image_img(self):
flying_team = FlyingTeam(place="xxx")
self.assertTrue("x" in str(flying_team))
import logging
from django.test import TestCase
from web.models import Holiday
from web.tests import create_worker
logger = logging.getLogger(__name__)
class HolidayTests(TestCase):
def test_str(self):
holiday = Holiday(person=create_worker())
self.assertIsNotNone(str(holiday))
self.assertIsNotNone(unicode(holiday))
import logging
from django.test import TestCase
from web.models import Item
logger = logging.getLogger(__name__)
class ItemTests(TestCase):
def test_str(self):
item = Item(name="test item")
self.assertIsNotNone(str(item))
self.assertIsNotNone(unicode(item))
import logging
from django.core.files.images import ImageFile
from django.test import TestCase
from web.models import Language
from web.tests.functions import get_resource_path
logger = logging.getLogger(__name__)
class LanguageTests(TestCase):
def test_image_img(self):
language = Language(image=ImageFile(open(get_resource_path("PL.png"), "rb")))
img_html_tag = language.image_img()
self.assertTrue("flag-icon" in img_html_tag)
import logging
from django.test import TestCase
from web.models import Room
logger = logging.getLogger(__name__)
class RoomTests(TestCase):
def test_str(self):
room = Room(room_number=4, address="some street 2/3", city="Luxembourg")
self.assertIsNotNone(str(room))
self.assertIsNotNone(unicode(room))
from django.test import TestCase
from web.models import Appointment
from web.models import Visit
from web.tests.functions import create_study_subject, create_appointment
from web.tests.functions import create_visit
from web.models import Appointment, Visit
from web.tests.functions import create_study_subject, create_appointment, create_subject, create_visit
class SubjectModelTests(TestCase):
......@@ -20,3 +18,9 @@ class SubjectModelTests(TestCase):
self.assertTrue(subject.dead)
self.assertTrue(visit_finished)
self.assertEquals(Appointment.APPOINTMENT_STATUS_CANCELLED, appointment_status)
def test_str(self):
subject = create_subject()
self.assertIsNotNone(str(subject))
self.assertIsNotNone(unicode(subject))
from django.contrib.auth.models import User
from django.test import Client
from django.test import TestCase
import datetime
from web.models import Worker
from django.contrib.auth.models import User, AnonymousUser
from django.test import Client, TestCase
from web.models import Holiday, Worker
from web.tests.functions import create_worker
from web.views.notifications import get_today_midnight_date
class WorkerModelTests(TestCase):
......@@ -46,3 +48,15 @@ class WorkerModelTests(TestCase):
self.fail("Exception expected")
except TypeError:
pass
def test_is_on_leave(self):
worker = create_worker()
self.assertFalse(worker.is_on_leave())
Holiday(person=worker,
datetime_start=get_today_midnight_date() + datetime.timedelta(days=-2),
datetime_end=get_today_midnight_date() + datetime.timedelta(days=2)).save()
self.assertTrue(worker.is_on_leave())
def test_get_by_user_for_anonymous(self):
self.assertIsNone(Worker.get_by_user(AnonymousUser()))
# coding=utf-8
from django.urls import reverse
from web.models import Appointment
from web.models import Appointment, AppointmentTypeLink
from web.tests import LoggedInTestCase
from web.tests.functions import create_study_subject, create_appointment, create_visit
from web.tests.functions import create_study_subject, create_appointment, create_visit, create_appointment_type
from web.views.export import subject_to_row_for_fields, DROP_OUT_FIELD
......@@ -13,6 +13,11 @@ class TestExportView(LoggedInTestCase):
response = self.client.get(reverse('web.views.export_to_csv', kwargs={'data_type': "subjects"}))
self.assertEqual(response.status_code, 200)
def test_render_export(self):
create_study_subject()
response = self.client.get(reverse('web.views.export'))
self.assertEqual(response.status_code, 200)
def test_export_appointments_to_csv(self):
create_appointment()
response = self.client.get(reverse('web.views.export_to_csv', kwargs={'data_type': "appointments"}))
......@@ -24,7 +29,11 @@ class TestExportView(LoggedInTestCase):
self.assertEqual(response.status_code, 200)
def test_export_appointments_to_excel(self):
create_appointment()
appointment = create_appointment()
appointment.visit = None
appointment.save()
AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=create_appointment_type())
response = self.client.get(reverse('web.views.export_to_excel', kwargs={'data_type': "appointments"}))
self.assertEqual(response.status_code, 200)
......
......@@ -4,11 +4,12 @@ import logging
from django.urls import reverse
from web.forms import StudySubjectAddForm, StudySubjectEditForm, SubjectEditForm, SubjectAddForm
from web.models import StudySubject
from web.models import MailTemplate, StudySubject
from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, SUBJECT_TYPE_CHOICES_PATIENT, \
COUNTRY_AFGHANISTAN_ID, COUNTRY_OTHER_ID
COUNTRY_AFGHANISTAN_ID, COUNTRY_OTHER_ID, MAIL_TEMPLATE_CONTEXT_SUBJECT
from web.tests import LoggedInWithWorkerTestCase
from web.tests.functions import create_study_subject, create_visit, create_appointment, get_test_location
from web.tests.functions import create_study_subject, create_visit, create_appointment, get_test_location, \
create_language, get_resource_path
from web.views.notifications import get_today_midnight_date
logger = logging.getLogger(__name__)
......@@ -29,6 +30,20 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase):
response = self.client.get(reverse('web.views.subject_edit', kwargs={'id': self.study_subject.id}))
self.assertEqual(response.status_code, 200)
def test_render_subject_edit_with_mail_templates(self):
language = create_language(name="German")
template_name = "german_template"
template_file = get_resource_path('upcoming_appointment_FR.docx')
self.study_subject.subject.default_written_communication_language = language
self.study_subject.subject.save()
MailTemplate(name=template_name, language=language, context=MAIL_TEMPLATE_CONTEXT_SUBJECT,
template_file=template_file).save()
response = self.client.get(reverse('web.views.subject_edit', kwargs={'id': self.study_subject.id}))
self.assertEqual(response.status_code, 200)
self.assertTrue(template_name in response.content)
def test_render_subject_visit_details(self):
visit = create_visit(self.study_subject)
create_appointment(visit)
......@@ -67,6 +82,16 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase):
self.assertTrue(updated_study_subject.subject.dead)
self.assertTrue(updated_study_subject.resigned)
def test_save_subject_edit_with_continue(self):
form_data = self.create_edit_form_data_for_study_subject()
form_data['_continue'] = True
response = self.client.post(reverse('web.views.subject_edit', kwargs={'id': self.study_subject.id}),
data=form_data)
self.assertEqual(response.status_code, 302)
self.assertTrue("edit" in response.url)
def create_edit_form_data_for_study_subject(self):
form_study_subject = StudySubjectEditForm(instance=self.study_subject, prefix="study_subject")
form_subject = SubjectEditForm(instance=self.study_subject.subject, prefix="subject")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment