Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test_StudySubjectEditForm.py 2.06 KiB
import logging

from web.forms import StudySubjectEditForm
from web.models import StudySubject
from web.tests import LoggedInWithWorkerTestCase
from web.tests.functions import create_study_subject

logger = logging.getLogger(__name__)


class StudySubjectEditFormTests(LoggedInWithWorkerTestCase):
    def setUp(self):
        super(StudySubjectEditFormTests, self).setUp()
        self.study_subject = create_study_subject()

        location = self.worker.locations.all()[0]
        self.sample_data = {
            'type': self.study_subject.type,
            'default_location': location.id,
            'screening_number': self.study_subject.screening_number,
            'nd_number': self.study_subject.nd_number,
            'subject': self.study_subject.subject.id,
            'id': self.study_subject.id
        }

    def tearDown(self):
        StudySubject.objects.all().delete()

    def test_validation(self):
        edit_form = StudySubjectEditForm(self.sample_data)
        save_status = edit_form.is_valid()
        self.assertTrue(save_status)

    def test_invalid_nd_number_edit(self):
        study_subject2 = create_study_subject(124)
        study_subject2.nd_number = "ND0124"
        study_subject2.screening_number = "124"
        study_subject2.save()

        self.sample_data['nd_number'] = "ND0124"
        edit_form = StudySubjectEditForm(self.sample_data)

        save_status = edit_form.is_valid()
        self.assertTrue("nd_number" in edit_form.errors)
        self.assertFalse(save_status)

    def test_invalid_mpower_id_edit(self):
        self.study_subject.mpower_id = "mpower_002"
        self.study_subject.nd_number = "ND0002"
        self.study_subject.screening_number = "002"
        self.study_subject.save()

        self.sample_data['mpower_id'] = "mpower_002"
        self.sample_data['nd_number'] = "ND0001"
        self.sample_data['screening_number'] = "001"
        edit_form = StudySubjectEditForm(self.sample_data)

        save_status = edit_form.is_valid()
        self.assertTrue("mpower_id" in edit_form.errors)
        self.assertFalse(save_status)