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

unit test fixed

parent 2289e191
No related branches found
No related tags found
1 merge request!99separate patient data from study data
...@@ -5,7 +5,7 @@ from django import forms ...@@ -5,7 +5,7 @@ from django import forms
from django.forms import ModelForm, Form from django.forms import ModelForm, Form
from django.utils.dates import MONTHS from django.utils.dates import MONTHS
from web.models import StudySubject, Worker, Appointment, Visit, AppointmentType, ContactAttempt, AppointmentTypeLink, \ from web.models import Subject, StudySubject, Worker, Appointment, Visit, AppointmentType, ContactAttempt, AppointmentTypeLink, \
Availability, Holiday Availability, Holiday
from web.models.constants import SUBJECT_TYPE_CHOICES, SCREENING_NUMBER_PREFIXES_FOR_TYPE, COUNTRY_OTHER_ID from web.models.constants import SUBJECT_TYPE_CHOICES, SCREENING_NUMBER_PREFIXES_FOR_TYPE, COUNTRY_OTHER_ID
from web.views.notifications import get_filter_locations from web.views.notifications import get_filter_locations
...@@ -62,7 +62,7 @@ def validate_subject_mpower_number(self, cleaned_data): ...@@ -62,7 +62,7 @@ def validate_subject_mpower_number(self, cleaned_data):
self.add_error('mpower_id', "mPower number already in use") self.add_error('mpower_id', "mPower number already in use")
class SubjectAddForm(ModelForm): class StudySubjectAddForm(ModelForm):
date_born = forms.DateField(label="Date of birth", date_born = forms.DateField(label="Date of birth",
widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d"), widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d"),
required=False required=False
...@@ -98,7 +98,7 @@ class SubjectAddForm(ModelForm): ...@@ -98,7 +98,7 @@ class SubjectAddForm(ModelForm):
return screening_number return screening_number
def clean(self): def clean(self):
cleaned_data = super(SubjectAddForm, self).clean() cleaned_data = super(StudySubjectAddForm, self).clean()
screening_number = self.build_screening_number(cleaned_data) screening_number = self.build_screening_number(cleaned_data)
if screening_number is not None: if screening_number is not None:
cleaned_data['screening_number'] = screening_number cleaned_data['screening_number'] = screening_number
...@@ -521,3 +521,28 @@ class HolidayAddForm(ModelForm): ...@@ -521,3 +521,28 @@ class HolidayAddForm(ModelForm):
availabilities = worker.availability_set.all() availabilities = worker.availability_set.all()
for availability in availabilities: for availability in availabilities:
validate_availability_conflict(self, self.cleaned_data, availability) validate_availability_conflict(self, self.cleaned_data, availability)
class SubjectAddForm(ModelForm):
date_born = forms.DateField(label="Date of birth",
widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d"),
required=False
)
class Meta:
model = Subject
fields = '__all__'
exclude = ['dead']
def build_screening_number(self, cleaned_data):
screening_number = cleaned_data.get('screening_number', None)
if not screening_number:
prefix_screening_number = self.get_prefix_screening_number()
if prefix_screening_number is not None:
screening_number = get_new_screening_number(prefix_screening_number)
return screening_number
def clean(self):
cleaned_data = super(SubjectAddForm, self).clean()
validate_subject_country(self, cleaned_data)
return cleaned_data
...@@ -16,28 +16,28 @@ SELECT DISTINCT(rank() OVER (PARTITION BY subject_id ORDER BY datetime_begin)) A ...@@ -16,28 +16,28 @@ SELECT DISTINCT(rank() OVER (PARTITION BY subject_id ORDER BY datetime_begin)) A
""" """
QUERY_APPOINTMENTS_COUNT = """ QUERY_APPOINTMENTS_COUNT = """
SELECT count(*) FROM web_appointment LEFT JOIN (SELECT id, subject_id, rank() SELECT count(*) FROM web_appointment LEFT JOIN (SELECT id, subject_id as web_visit_subject_id, rank()
OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk FROM web_visit) OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk FROM web_visit)
a ON a.id = web_appointment.visit_id a ON a.id = web_appointment.visit_id
LEFT JOIN web_studysubject ON web_studysubject.id = subject_id LEFT JOIN web_studysubject ON web_studysubject.id = web_visit_subject_id
WHERE a.rnk = %s WHERE a.rnk = %s
AND EXTRACT(MONTH FROM web_appointment.datetime_when) = %s AND EXTRACT(MONTH FROM web_appointment.datetime_when) = %s
AND EXTRACT(YEAR FROM web_appointment.datetime_when) = %s AND EXTRACT(YEAR FROM web_appointment.datetime_when) = %s
""" """
QUERY_VISITS_ENDED_COUNT = """ QUERY_VISITS_ENDED_COUNT = """
SELECT count(*) FROM (SELECT id, subject_id, datetime_begin, datetime_end, rank() SELECT count(*) FROM (SELECT id, subject_id as web_visit_subject_id, datetime_begin, datetime_end, rank()
OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk FROM web_visit) a OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk FROM web_visit) a
LEFT JOIN web_studysubject ON web_studysubject.id = subject_id LEFT JOIN web_studysubject ON web_studysubject.id = web_visit_subject_id
WHERE a.rnk = %s AND WHERE a.rnk = %s AND
EXTRACT(MONTH FROM a.datetime_end) = %s AND EXTRACT(MONTH FROM a.datetime_end) = %s AND
EXTRACT(YEAR FROM a.datetime_end) = %s EXTRACT(YEAR FROM a.datetime_end) = %s
""" """
QUERY_VISITS_STARTED_COUNT = """ QUERY_VISITS_STARTED_COUNT = """
SELECT count(*) FROM (SELECT id, subject_id, datetime_begin, datetime_end, rank() SELECT count(*) FROM (SELECT id, subject_id as web_visit_subject_id, datetime_begin, datetime_end, rank()
OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk FROM web_visit) a OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk FROM web_visit) a
LEFT JOIN web_studysubject ON web_studysubject.id = subject_id LEFT JOIN web_studysubject ON web_studysubject.id = web_visit_subject_id
WHERE a.rnk = %s AND WHERE a.rnk = %s AND
EXTRACT(MONTH FROM a.datetime_begin) = %s AND EXTRACT(MONTH FROM a.datetime_begin) = %s AND
EXTRACT(YEAR FROM a.datetime_begin) = %s EXTRACT(YEAR FROM a.datetime_begin) = %s
...@@ -45,10 +45,10 @@ EXTRACT(YEAR FROM a.datetime_begin) = %s ...@@ -45,10 +45,10 @@ EXTRACT(YEAR FROM a.datetime_begin) = %s
QUERY_APPOINTMENTS = """ QUERY_APPOINTMENTS = """
SELECT types.appointment_type_id, web_appointment.status, count(*) FROM web_appointment SELECT types.appointment_type_id, web_appointment.status, count(*) FROM web_appointment
LEFT JOIN (SELECT id, subject_id, rank() OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk LEFT JOIN (SELECT id, subject_id as web_visit_subject_id, rank() OVER (PARTITION BY subject_id ORDER BY datetime_begin) AS rnk
FROM web_visit) a ON a.id = web_appointment.visit_id LEFT JOIN web_appointmenttypelink types FROM web_visit) a ON a.id = web_appointment.visit_id LEFT JOIN web_appointmenttypelink types
ON types.appointment_id = web_appointment.id ON types.appointment_id = web_appointment.id
LEFT JOIN web_studysubject ON web_studysubject.id = subject_id LEFT JOIN web_studysubject ON web_studysubject.id = web_visit_subject_id
WHERE a.rnk = %s WHERE a.rnk = %s
AND EXTRACT(MONTH FROM web_appointment.datetime_when) = %s AND EXTRACT(MONTH FROM web_appointment.datetime_when) = %s
AND EXTRACT(YEAR FROM web_appointment.datetime_when) = %s AND EXTRACT(YEAR FROM web_appointment.datetime_when) = %s
......
from web.forms import SubjectAddForm, get_new_screening_number import logging
from web.forms import StudySubjectAddForm, get_new_screening_number
from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, COUNTRY_AFGHANISTAN_ID from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, COUNTRY_AFGHANISTAN_ID
from web.tests import LoggedInWithWorkerTestCase from web.tests import LoggedInWithWorkerTestCase
from web.tests.functions import create_study_subject from web.tests.functions import create_study_subject, create_subject
logger = logging.getLogger(__name__)
class SubjectAddFormTests(LoggedInWithWorkerTestCase): class StudySubjectAddFormTests(LoggedInWithWorkerTestCase):
def setUp(self): def setUp(self):
super(SubjectAddFormTests, self).setUp() super(StudySubjectAddFormTests, self).setUp()
location = self.worker.locations.all()[0] location = self.worker.locations.all()[0]
self.subject = create_subject()
self.sample_data = {'first_name': 'name', self.sample_data = {'first_name': 'name',
'last_name': 'name', 'last_name': 'name',
'sex': SEX_CHOICES_MALE, 'sex': SEX_CHOICES_MALE,
...@@ -16,10 +21,11 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -16,10 +21,11 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase):
'default_location': location.id, 'default_location': location.id,
'screening_number': "123", 'screening_number': "123",
'country': COUNTRY_AFGHANISTAN_ID, 'country': COUNTRY_AFGHANISTAN_ID,
'subject': self.subject.id
} }
def test_validation(self): def test_validation(self):
form = SubjectAddForm(data=self.sample_data, user=self.user) form = StudySubjectAddForm(data=self.sample_data, user=self.user)
form.is_valid() form.is_valid()
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
...@@ -27,13 +33,13 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -27,13 +33,13 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase):
form_data = self.sample_data form_data = self.sample_data
form_data['screening_number'] = "123" form_data['screening_number'] = "123"
form = SubjectAddForm(data=form_data, user=self.user) form = StudySubjectAddForm(data=form_data, user=self.user)
form.is_valid() form.is_valid()
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
self.assertIsNone(form.fields['year_of_diagnosis'].initial) self.assertIsNone(form.fields['year_of_diagnosis'].initial)
form.save() form.save()
form2 = SubjectAddForm(data=form_data, user=self.user) form2 = StudySubjectAddForm(data=form_data, user=self.user)
validation_status = form2.is_valid() validation_status = form2.is_valid()
self.assertFalse(validation_status) self.assertFalse(validation_status)
self.assertTrue("screening_number" in form2.errors) self.assertTrue("screening_number" in form2.errors)
...@@ -42,13 +48,13 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -42,13 +48,13 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase):
form_data = self.sample_data form_data = self.sample_data
form_data['nd_number'] = "ND0123" form_data['nd_number'] = "ND0123"
form = SubjectAddForm(data=form_data, user=self.user) form = StudySubjectAddForm(data=form_data, user=self.user)
form.is_valid() form.is_valid()
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
form.save() form.save()
form_data['screening_number'] = "2" form_data['screening_number'] = "2"
form2 = SubjectAddForm(data=form_data, user=self.user) form2 = StudySubjectAddForm(data=form_data, user=self.user)
validation_status = form2.is_valid() validation_status = form2.is_valid()
self.assertFalse(validation_status) self.assertFalse(validation_status)
self.assertTrue("nd_number" in form2.errors) self.assertTrue("nd_number" in form2.errors)
...@@ -57,13 +63,13 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase): ...@@ -57,13 +63,13 @@ class SubjectAddFormTests(LoggedInWithWorkerTestCase):
form_data = self.sample_data form_data = self.sample_data
form_data['mpower_id'] = "123" form_data['mpower_id'] = "123"
form = SubjectAddForm(data=form_data, user=self.user) form = StudySubjectAddForm(data=form_data, user=self.user)
form.is_valid() form.is_valid()
self.assertTrue(form.is_valid()) self.assertTrue(form.is_valid())
form.save() form.save()
form_data['screening_number'] = "2" form_data['screening_number'] = "2"
form2 = SubjectAddForm(data=form_data, user=self.user) form2 = StudySubjectAddForm(data=form_data, user=self.user)
validation_status = form2.is_valid() validation_status = form2.is_valid()
self.assertFalse(validation_status) self.assertFalse(validation_status)
self.assertTrue("mpower_id" in form2.errors) self.assertTrue("mpower_id" in form2.errors)
......
from web.forms import SubjectAddForm from web.forms import StudySubjectAddForm
from web.forms import SubjectEditForm from web.forms import SubjectEditForm
from web.models import StudySubject from web.models import StudySubject
from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, COUNTRY_AFGHANISTAN_ID from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, COUNTRY_AFGHANISTAN_ID
from web.tests import LoggedInWithWorkerTestCase from web.tests import LoggedInWithWorkerTestCase
from web.tests.functions import create_subject
class SubjectEditFormTests(LoggedInWithWorkerTestCase): class SubjectEditFormTests(LoggedInWithWorkerTestCase):
def setUp(self): def setUp(self):
super(SubjectEditFormTests, self).setUp() super(SubjectEditFormTests, self).setUp()
self.subject = create_subject()
location = self.worker.locations.all()[0] location = self.worker.locations.all()[0]
self.sample_data = {'first_name': 'name', self.sample_data = {'first_name': 'name',
...@@ -17,14 +19,15 @@ class SubjectEditFormTests(LoggedInWithWorkerTestCase): ...@@ -17,14 +19,15 @@ class SubjectEditFormTests(LoggedInWithWorkerTestCase):
'default_location': location.id, 'default_location': location.id,
'country': COUNTRY_AFGHANISTAN_ID, 'country': COUNTRY_AFGHANISTAN_ID,
'screening_number': '123', 'screening_number': '123',
'nd_number': 'ND0123' 'nd_number': 'ND0123',
'subject': self.subject.id
} }
def tearDown(self): def tearDown(self):
StudySubject.objects.all().delete() StudySubject.objects.all().delete()
def test_validation(self): def test_validation(self):
add_form = SubjectAddForm(data=self.sample_data, user=self.user) add_form = StudySubjectAddForm(data=self.sample_data, user=self.user)
subject = add_form.save() subject = add_form.save()
self.sample_data['id'] = subject.id self.sample_data['id'] = subject.id
...@@ -33,12 +36,12 @@ class SubjectEditFormTests(LoggedInWithWorkerTestCase): ...@@ -33,12 +36,12 @@ class SubjectEditFormTests(LoggedInWithWorkerTestCase):
self.assertTrue(save_status) self.assertTrue(save_status)
def test_invalid_nd_number_edit(self): def test_invalid_nd_number_edit(self):
add_form = SubjectAddForm(data=self.sample_data, user=self.user) add_form = StudySubjectAddForm(data=self.sample_data, user=self.user)
add_form.save() add_form.save()
self.sample_data['nd_number'] = "ND0124" self.sample_data['nd_number'] = "ND0124"
self.sample_data['screening_number'] = "124" self.sample_data['screening_number'] = "124"
add_form = SubjectAddForm(data=self.sample_data, user=self.user) add_form = StudySubjectAddForm(data=self.sample_data, user=self.user)
subject = add_form.save() subject = add_form.save()
self.sample_data['id'] = subject.id self.sample_data['id'] = subject.id
...@@ -50,13 +53,13 @@ class SubjectEditFormTests(LoggedInWithWorkerTestCase): ...@@ -50,13 +53,13 @@ class SubjectEditFormTests(LoggedInWithWorkerTestCase):
def test_invalid_mpower_id_edit(self): def test_invalid_mpower_id_edit(self):
self.sample_data['screening_number'] = "001" self.sample_data['screening_number'] = "001"
add_form = SubjectAddForm(data=self.sample_data, user=self.user) add_form = StudySubjectAddForm(data=self.sample_data, user=self.user)
subject = add_form.save() subject = add_form.save()
self.sample_data['mpower_id'] = "mpower_002" self.sample_data['mpower_id'] = "mpower_002"
self.sample_data['nd_number'] = "ND0002" self.sample_data['nd_number'] = "ND0002"
self.sample_data['screening_number'] = "002" self.sample_data['screening_number'] = "002"
add_form = SubjectAddForm(data=self.sample_data, user=self.user) add_form = StudySubjectAddForm(data=self.sample_data, user=self.user)
add_form.save() add_form.save()
self.sample_data['id'] = subject.id self.sample_data['id'] = subject.id
......
...@@ -2,18 +2,20 @@ import datetime ...@@ -2,18 +2,20 @@ import datetime
from django.urls import reverse from django.urls import reverse
from web.forms import SubjectAddForm, SubjectEditForm from web.forms import StudySubjectAddForm, SubjectEditForm
from web.models import StudySubject from web.models import StudySubject
from web.models.constants import SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, SUBJECT_TYPE_CHOICES_PATIENT, \ 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
from web.tests import LoggedInWithWorkerTestCase 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_subject
from web.views.notifications import get_today_midnight_date from web.views.notifications import get_today_midnight_date
class SubjectsViewTests(LoggedInWithWorkerTestCase): class SubjectsViewTests(LoggedInWithWorkerTestCase):
def setUp(self): def setUp(self):
super(SubjectsViewTests, self).setUp() super(SubjectsViewTests, self).setUp()
self.subject = create_subject()
def test_subjects_add(self): def test_subjects_add(self):
self.worker.save() self.worker.save()
...@@ -80,8 +82,8 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase): ...@@ -80,8 +82,8 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase):
def test_subjects_add_2(self): def test_subjects_add_2(self):
form = SubjectAddForm(user=self.user) form = StudySubjectAddForm(user=self.user)
form_data = {} form_data = {"subject": self.subject.id}
for key, value in form.initial.items(): for key, value in form.initial.items():
if value is not None: if value is not None:
form_data[key] = value form_data[key] = value
...@@ -99,17 +101,17 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase): ...@@ -99,17 +101,17 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase):
"prefix should start by L" + "prefix should start by L" +
" as default location prefix is not defined and subject type is control") " as default location prefix is not defined and subject type is control")
@staticmethod def add_valid_form_data_for_subject_add(self, form_data):
def add_valid_form_data_for_subject_add(form_data):
form_data["country"] = COUNTRY_AFGHANISTAN_ID form_data["country"] = COUNTRY_AFGHANISTAN_ID
form_data["first_name"] = "John" form_data["first_name"] = "John"
form_data["last_name"] = "Doe" form_data["last_name"] = "Doe"
form_data["sex"] = SEX_CHOICES_MALE form_data["sex"] = SEX_CHOICES_MALE
form_data["type"] = SUBJECT_TYPE_CHOICES_PATIENT form_data["type"] = SUBJECT_TYPE_CHOICES_PATIENT
form_data["subject"] = self.subject.id
def test_subjects_add_patient(self): def test_subjects_add_patient(self):
form = SubjectAddForm(user=self.user) form = StudySubjectAddForm(user=self.user)
form_data = {} form_data = {}
for key, value in form.initial.items(): for key, value in form.initial.items():
if value is not None: if value is not None:
...@@ -129,7 +131,7 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase): ...@@ -129,7 +131,7 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase):
def test_subjects_add_invalid(self): def test_subjects_add_invalid(self):
form = SubjectAddForm(user=self.user) form = StudySubjectAddForm(user=self.user)
form_data = {} form_data = {}
for key, value in form.initial.items(): for key, value in form.initial.items():
if value is not None: if value is not None:
...@@ -143,7 +145,7 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase): ...@@ -143,7 +145,7 @@ class SubjectsViewTests(LoggedInWithWorkerTestCase):
def test_subjects_add_with_prefixed_location(self): def test_subjects_add_with_prefixed_location(self):
form = SubjectAddForm(user=self.user) form = StudySubjectAddForm(user=self.user)
form_data = {} form_data = {}
for key, value in form.initial.items(): for key, value in form.initial.items():
if value is not None: if value is not None:
......
...@@ -3,7 +3,7 @@ from django.contrib import messages ...@@ -3,7 +3,7 @@ from django.contrib import messages
from django.shortcuts import redirect, get_object_or_404 from django.shortcuts import redirect, get_object_or_404
from . import wrap_response from . import wrap_response
from ..forms import SubjectAddForm, SubjectEditForm, VisitDetailForm from ..forms import StudySubjectAddForm, SubjectEditForm, VisitDetailForm
from ..models import StudySubject, MailTemplate, Worker from ..models import StudySubject, MailTemplate, Worker
SUBJECT_LIST_GENERIC = "GENERIC" SUBJECT_LIST_GENERIC = "GENERIC"
...@@ -21,7 +21,7 @@ def subjects(request): ...@@ -21,7 +21,7 @@ def subjects(request):
def subject_add(request): def subject_add(request):
if request.method == 'POST': if request.method == 'POST':
form = SubjectAddForm(request.POST, request.FILES, user=request.user) form = StudySubjectAddForm(request.POST, request.FILES, user=request.user)
if form.is_valid(): if form.is_valid():
form.save() form.save()
messages.add_message(request, messages.SUCCESS, 'Subject created') messages.add_message(request, messages.SUCCESS, 'Subject created')
...@@ -30,7 +30,7 @@ def subject_add(request): ...@@ -30,7 +30,7 @@ def subject_add(request):
messages.add_message(request, messages.ERROR, 'Invalid data. Please fix data and try again.') messages.add_message(request, messages.ERROR, 'Invalid data. Please fix data and try again.')
else: else:
form = SubjectAddForm(user=request.user) form = StudySubjectAddForm(user=request.user)
return wrap_response(request, 'subjects/add.html', {'form': form}) return wrap_response(request, 'subjects/add.html', {'form': form})
......
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