def test_evaluation_form_same_name(self): """ Test whether giving an evaluation the same name as another evaluation in the same course in the evaluation edit form is invalid. """ course = mommy.make(Course, degrees=[mommy.make(Degree)]) evaluation1 = mommy.make(Evaluation, course=course, name_de="Evaluierung 1", name_en="Evaluation 1") evaluation2 = mommy.make(Evaluation, course=course, name_de="Evaluierung 2", name_en="Evaluation 2") evaluation1.general_contribution.questionnaires.set( [mommy.make(Questionnaire)]) evaluation2.general_contribution.questionnaires.set( [mommy.make(Questionnaire)]) form_data = get_form_data_from_instance(EvaluationForm, evaluation1) form_data[ "vote_start_datetime"] = "2098-01-01" # needed to fix the form form_data["vote_end_date"] = "2099-01-01" # needed to fix the form form = EvaluationForm(form_data, instance=evaluation1, semester=evaluation1.course.semester) self.assertTrue(form.is_valid()) form_data['name_de'] = evaluation2.name_de form = EvaluationForm(form_data, instance=evaluation1, semester=evaluation1.course.semester) self.assertFalse(form.is_valid())
def test_locked_questionnaire(self): """ Asserts that locked (general) questionnaires can be changed by staff. """ questionnaire = baker.make(Questionnaire, type=Questionnaire.Type.TOP, is_locked=False, visibility=Questionnaire.Visibility.EDITORS) locked_questionnaire = baker.make(Questionnaire, type=Questionnaire.Type.TOP, is_locked=True, visibility=Questionnaire.Visibility.EDITORS) semester = baker.make(Semester) evaluation = baker.make(Evaluation, course=baker.make(Course, semester=semester)) evaluation.general_contribution.questionnaires.add(questionnaire) form_data = get_form_data_from_instance(EvaluationForm, evaluation, semester=semester) form_data["general_questionnaires"] = [questionnaire.pk, locked_questionnaire.pk] # add locked questionnaire form = EvaluationForm(form_data, instance=evaluation, semester=semester) # Assert form is valid and locked questionnaire is added form.save() self.assertEqual({questionnaire, locked_questionnaire}, set(evaluation.general_contribution.questionnaires.all())) form_data = get_form_data_from_instance(EvaluationForm, evaluation, semester=semester) form_data["general_questionnaires"] = [questionnaire.pk] # remove locked questionnaire form = EvaluationForm(form_data, instance=evaluation, semester=semester) # Assert form is valid and locked questionnaire is removed form.save() self.assertEqual({questionnaire}, set(evaluation.general_contribution.questionnaires.all()))
def test_course_change_updates_cache(self): semester = baker.make(Semester) course1 = baker.make(Course, semester=semester) course2 = baker.make(Course, semester=semester) evaluation = baker.make(Evaluation, course=course1) evaluation.general_contribution.questionnaires.set([baker.make(Questionnaire)]) form_data = get_form_data_from_instance(EvaluationForm, evaluation) form = EvaluationForm(form_data, instance=evaluation, semester=semester) self.assertTrue(form.is_valid()) with patch('evap.results.views._delete_course_template_cache_impl') as delete_call, patch('evap.results.views.warm_up_template_cache') as warmup_call: # save without changes form.save() self.assertEqual(Evaluation.objects.get(pk=evaluation.pk).course, course1) self.assertEqual(delete_call.call_count, 0) self.assertEqual(warmup_call.call_count, 0) # change course and save form_data = get_form_data_from_instance(EvaluationForm, evaluation) form_data["course"] = course2.pk form = EvaluationForm(form_data, instance=evaluation, semester=semester) self.assertTrue(form.is_valid()) form.save() self.assertEqual(Evaluation.objects.get(pk=evaluation.pk).course, course2) self.assertEqual(delete_call.call_count, 2) self.assertEqual(warmup_call.call_count, 2)
def test_unused_questionnaire_visibility(self): evaluation = baker.make(Evaluation) questionnaire = baker.make(Questionnaire, visibility=Questionnaire.Visibility.HIDDEN, type=Questionnaire.Type.TOP) form = EvaluationForm(instance=evaluation, semester=evaluation.course.semester) self.assertNotIn(questionnaire, form.fields["general_questionnaires"].queryset) evaluation.general_contribution.questionnaires.add(questionnaire) form = EvaluationForm(instance=evaluation, semester=evaluation.course.semester) self.assertIn(questionnaire, form.fields["general_questionnaires"].queryset)
def test_uniqueness_constraint_error_shown(self): """ Tests whether errors being caused by a uniqueness constraint are shown in the form """ course = mommy.make(Course) evaluation1 = mommy.make(Evaluation, course=course, name_de="Evaluierung 1", name_en="Evaluation 1") evaluation2 = mommy.make(Evaluation, course=course, name_de="Evaluierung 2", name_en="Evaluation 2") form_data = get_form_data_from_instance(EvaluationForm, evaluation2) form_data["name_de"] = evaluation1.name_de form = EvaluationForm(form_data, instance=evaluation2, semester=evaluation2.course.semester) self.assertFalse(form.is_valid()) self.assertIn('name_de', form.errors) self.assertEqual( form.errors['name_de'], ['Evaluation with this Course and Name (german) already exists.'])
def test_voter_cannot_be_removed_from_evaluation(self): student = baker.make(UserProfile) evaluation = baker.make(Evaluation, course=baker.make(Course, degrees=[baker.make(Degree)]), participants=[student], voters=[student]) evaluation.general_contribution.questionnaires.set([baker.make(Questionnaire)]) form_data = get_form_data_from_instance(EvaluationForm, evaluation) form_data["participants"] = [] form = EvaluationForm(form_data, instance=evaluation, semester=evaluation.course.semester) self.assertFalse(form.is_valid()) self.assertIn('participants', form.errors) self.assertIn("Participants who already voted for the evaluation can't be removed", form.errors['participants'][0])
def helper_date_validation(self, EvaluationFormClass, start_date, end_date, expected_result): evaluation = Evaluation.objects.get() form_data = get_form_data_from_instance(EvaluationFormClass, evaluation) form_data["vote_start_datetime"] = start_date form_data["vote_end_date"] = end_date if EvaluationFormClass == EvaluationForm: form = EvaluationForm(form_data, instance=evaluation, semester=evaluation.course.semester) else: form = EvaluationFormClass(form_data, instance=evaluation) self.assertEqual(form.is_valid(), expected_result)