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

unit tests for filtering appointemnets api

parent 24ff6af3
No related branches found
No related tags found
1 merge request!111Resolve "Unfinished appointments"
Pipeline #
......@@ -7,7 +7,7 @@ from django.urls import reverse
from django.utils import timezone
from web.api_views.serialization_utils import serialize_datetime, location_to_str, flying_team_to_str, add_column, \
bool_to_yes_no
bool_to_yes_no, get_filters_for_data_table_request
from web.models import Appointment, Study, SubjectColumns, AppointmentColumns, AppointmentList
from web.models.appointment_list import APPOINTMENT_LIST_GENERIC, APPOINTMENT_LIST_UNFINISHED, \
APPOINTMENT_LIST_APPROACHING
......@@ -76,10 +76,10 @@ def get_appointments_order(appointments_to_be_ordered, order_column, order_direc
result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__subject__first_name')
elif order_column == "last_name":
result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__subject__last_name')
elif order_column == "default_location":
result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__default_location')
elif order_column == "location":
result = appointments_to_be_ordered.order_by(order_direction + 'location')
elif order_column == "flying_team":
result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__flying_team')
result = appointments_to_be_ordered.order_by(order_direction + 'flying_team')
elif order_column == "post_mail_sent":
result = appointments_to_be_ordered.order_by(order_direction + 'post_mail_sent')
elif order_column == "datetime_when":
......@@ -89,11 +89,6 @@ def get_appointments_order(appointments_to_be_ordered, order_column, order_direc
return result
def filter_by_appointment(appointments_to_filter, appointment_type):
result = appointments_to_filter.filter(appointment_types=appointment_type)
return result
def get_appointments_filtered(appointments_to_be_filtered, filters):
result = appointments_to_be_filtered
for row in filters:
......@@ -103,12 +98,12 @@ def get_appointments_filtered(appointments_to_be_filtered, filters):
result = result.filter(visit__subject__subject__first_name__icontains=value)
elif column == "last_name":
result = result.filter(visit__subject__subject__last_name__icontains=value)
elif column == "default_location":
result = result.filter(visit__subject__default_location=value)
elif column == "location":
result = result.filter(location=value)
elif column == "flying_team":
result = result.filter(visit__subject__flying_team=value)
result = result.filter(flying_team=value)
elif column == "appointment_types":
result = filter_by_appointment(result, value)
result = result.filter(appointment_types=value)
else:
message = "UNKNOWN filter: "
if column is None:
......@@ -135,13 +130,7 @@ def appointments(request, appointment_type):
min_date = request.GET.get("start_date", None)
max_date = request.GET.get("end_date", None)
filters = []
column_id = 0
while request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown") != "unknown":
val = request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown")
if val != "":
filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val])
column_id += 1
filters = get_filters_for_data_table_request(request)
if min_date is not None:
length = 1000000000
......
......@@ -59,3 +59,14 @@ def add_column(result, name, field_name, column_list, param, columns_used_in_stu
"visible": visible,
"sortable": sortable
})
def get_filters_for_data_table_request(request):
filters = []
column_id = 0
while request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown") != "unknown":
val = request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown")
if val != "":
filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val])
column_id += 1
return filters
......@@ -6,7 +6,7 @@ from django.db.models import Q
from django.http import JsonResponse
from web.api_views.serialization_utils import bool_to_yes_no, flying_team_to_str, location_to_str, add_column, \
serialize_date, serialize_datetime
serialize_date, serialize_datetime, get_filters_for_data_table_request
from web.models import StudySubject, Visit, Appointment, Subject, SubjectColumns, StudyColumns, Study, ContactAttempt
from web.models.constants import SUBJECT_TYPE_CHOICES, GLOBAL_STUDY_ID
from web.models.study_subject_list import SUBJECT_LIST_GENERIC, SUBJECT_LIST_NO_VISIT, SUBJECT_LIST_REQUIRE_CONTACT, \
......@@ -260,13 +260,7 @@ def subjects(request, type):
order_dir = request.GET.get("order[0][dir]", "asc")
order_column = request.GET.get("columns[" + str(order) + "][data]", "last_name")
filters = []
column_id = 0
while request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown") != "unknown":
val = request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown")
if val != "":
filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val])
column_id += 1
filters = get_filters_for_data_table_request(request)
all_subjects = get_subjects(request, type)
......
......@@ -5,7 +5,7 @@ from django.db.models import Q
from django.http import JsonResponse
from web.api_views.serialization_utils import bool_to_yes_no, flying_team_to_str, location_to_str, add_column, \
serialize_date
serialize_date, get_filters_for_data_table_request
from web.models import AppointmentType, Appointment
from web.models import SubjectColumns
from web.models import Visit, Study, VisitColumns, StudyVisitList, StudyColumns
......@@ -182,13 +182,7 @@ def visits(request, visit_list_type):
order_dir = request.GET.get("order[0][dir]", "asc")
order_column = request.GET.get("columns[" + str(order) + "][data]", "last_name")
filters = []
column_id = 0
while request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown") != "unknown":
val = request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown")
if val != "":
filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val])
column_id += 1
filters = get_filters_for_data_table_request(request)
all_visits = get_visits(request, visit_list_type)
......
......@@ -5,10 +5,11 @@ import json
from django.urls import reverse
from web.models import AppointmentTypeLink, AppointmentList
from web.api_views.appointment import get_appointments_filtered, get_appointments_order
from web.models import AppointmentTypeLink, AppointmentList, Appointment
from web.tests import LoggedInWithWorkerTestCase
from web.tests.functions import create_study_subject, create_visit, create_appointment, \
create_appointment_type, create_get_suffix, create_flying_team
create_appointment_type, create_get_suffix, create_flying_team, create_location
from web.views.appointment import APPOINTMENT_LIST_GENERIC, APPOINTMENT_LIST_APPROACHING, APPOINTMENT_LIST_UNFINISHED
from web.views.notifications import get_today_midnight_date
......@@ -141,3 +142,149 @@ class TestAppointmentApi(LoggedInWithWorkerTestCase):
columns = json.loads(response.content)['columns']
self.assertTrue(len(columns) > 0)
def check_appointment_filtered(self, filters, result):
appointments = get_appointments_filtered(Appointment.objects.all(), filters)
self.assertEqual(len(result), appointments.count())
for index in range(len(result)):
self.assertEqual(result[index], appointments[index])
def check_appointment_ordered(self, order, result):
appointments = get_appointments_order(Appointment.objects.all(), order, "asc")
self.assertEqual(len(result), appointments.count())
for index in range(len(result)):
self.assertEqual(result[index], appointments[index])
appointments = get_appointments_order(Appointment.objects.all(), order, "desc")
length = len(result)
self.assertEqual(length, appointments.count())
for index in range(length):
self.assertEqual(result[length - index - 1], appointments[index])
def test_appointment_sort_first_name(self):
subject = self.study_subject
subject.subject.first_name = "PPP"
subject.subject.save()
subject2 = create_study_subject(2)
subject2.subject.first_name = "QQQ"
subject2.subject.save()
appointment = create_appointment(create_visit(subject))
appointment2 = create_appointment(create_visit(subject2))
self.check_appointment_ordered("first_name", [appointment, appointment2])
def test_appointment_sort_last_name(self):
subject = self.study_subject
subject.subject.last_name = "PPP"
subject.subject.save()
subject2 = create_study_subject(2)
subject2.subject.last_name = "QQQ"
subject2.subject.save()
appointment = create_appointment(create_visit(subject))
appointment2 = create_appointment(create_visit(subject2))
self.check_appointment_ordered("last_name", [appointment, appointment2])
def test_appointment_sort_default_location(self):
subject = self.study_subject
subject2 = create_study_subject(2)
appointment = create_appointment(create_visit(subject))
appointment.location = create_location(name="x")
appointment.save()
appointment2 = create_appointment(create_visit(subject2))
appointment2.location = create_location(name="y")
appointment2.save()
self.check_appointment_ordered("location", [appointment, appointment2])
def test_appointment_sort_flying_team(self):
subject = self.study_subject
subject2 = create_study_subject(2)
appointment = create_appointment(create_visit(subject))
appointment.flying_team = create_flying_team(place="x")
appointment.save()
appointment2 = create_appointment(create_visit(subject2))
appointment2.flying_team = create_flying_team(place="y")
appointment2.save()
self.check_appointment_ordered("flying_team", [appointment, appointment2])
def test_appointment_sort_post_mail_sent(self):
appointment = create_appointment(create_visit(self.study_subject))
self.check_appointment_ordered("post_mail_sent", [appointment])
def test_appointment_sort_datetime_when(self):
subject = self.study_subject
subject2 = create_study_subject(2)
appointment = create_appointment(create_visit(subject))
appointment.datetime_when = "2017-10-10"
appointment.save()
appointment2 = create_appointment(create_visit(subject2))
appointment2.datetime_when = "2017-10-12"
appointment2.save()
self.check_appointment_ordered("datetime_when", [appointment, appointment2])
def test_appointment_sort_unknown(self):
appointment = create_appointment(create_visit(self.study_subject))
self.check_appointment_ordered("some_unknown", [appointment])
self.check_appointment_ordered("", [appointment])
self.check_appointment_ordered(None, [appointment])
def test_subjects_filter_first_name(self):
subject = self.study_subject.subject
subject.first_name = "QQ"
subject.save()
appointment = create_appointment(create_visit(self.study_subject))
self.check_appointment_filtered([["first_name", "QQ"]], [appointment])
self.check_appointment_filtered([["first_name", "PP"]], [])
def test_subjects_filter_last_name(self):
subject = self.study_subject.subject
subject.last_name = "QQ"
subject.save()
appointment = create_appointment(create_visit(self.study_subject))
self.check_appointment_filtered([["last_name", "QQ"]], [appointment])
self.check_appointment_filtered([["last_name", "PP"]], [])
def test_subjects_filter_location(self):
appointment = create_appointment(create_visit(self.study_subject))
self.check_appointment_filtered([["location", str(appointment.location.id)]], [appointment])
self.check_appointment_filtered([["location", "-1"]], [])
def test_subjects_filter_flying_team(self):
appointment = create_appointment(create_visit(self.study_subject))
appointment.flying_team = create_flying_team()
appointment.save()
self.check_appointment_filtered([["flying_team", str(appointment.flying_team.id)]], [appointment])
self.check_appointment_filtered([["flying_team", "-1"]], [])
def test_subjects_filter_appointment_types(self):
appointment = create_appointment(create_visit(self.study_subject))
appointment_type = create_appointment_type()
AppointmentTypeLink.objects.create(appointment_type=appointment_type, appointment=appointment)
self.check_appointment_filtered([["appointment_types", str(appointment_type.id)]], [appointment])
self.check_appointment_filtered([["appointment_types", "-1"]], [])
def test_appointment_filter_unknown(self):
appointment = create_appointment(create_visit(self.study_subject))
self.check_appointment_filtered([["some_unknown", "unk"]], [appointment])
self.check_appointment_filtered([["", ""]], [appointment])
self.check_appointment_filtered([[None, None]], [appointment])
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