def test_result_calculation_with_no_contributor_rating_question(self): evaluation = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, participants=[self.student1, self.student2], voters=[self.student1, self.student2], ) questionnaire_text = baker.make(Questionnaire) baker.make(Question, questionnaire=questionnaire_text, type=Question.TEXT) baker.make( Contribution, contributor=baker.make(UserProfile), evaluation=evaluation, questionnaires=[questionnaire_text], ) evaluation.general_contribution.questionnaires.set( [self.questionnaire]) make_rating_answer_counters(self.question_grade, evaluation.general_contribution, [1, 0, 0, 0, 0]) cache_results(evaluation) distribution = calculate_average_distribution(evaluation) self.assertEqual(distribution[0], 1)
def test_course_grade(self): degree = baker.make(Degree) course = baker.make(Course, degrees=[degree]) evaluations = baker.make( Evaluation, course=course, name_en=iter(["eval0", "eval1", "eval2"]), name_de=iter(["eval0", "eval1", "eval2"]), state=Evaluation.State.PUBLISHED, _voter_count=5, _participant_count=10, _quantity=3, ) grades_per_eval = [[1, 1, 0, 0, 0], [0, 1, 1, 0, 0], [1, 0, 1, 0, 0]] expected_average = 2.0 questionnaire = baker.make(Questionnaire) question = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire) for grades, e in zip(grades_per_eval, evaluations): make_rating_answer_counters(question, e.general_contribution, grades) e.general_contribution.questionnaires.set([questionnaire]) for evaluation in evaluations: cache_results(evaluation) sheet = self.get_export_sheet(course.semester, degree, [course.type.id]) self.assertEqual(sheet.row_values(12)[1], expected_average) self.assertEqual(sheet.row_values(12)[2], expected_average) self.assertEqual(sheet.row_values(12)[3], expected_average)
def test_results_cache_refreshed(self): contributor = baker.make(UserProfile, first_name="Peter") evaluation = baker.make(Evaluation, state="published") baker.make(Contribution, contributor=contributor, evaluation=evaluation) cache_results(evaluation) results_before = get_results(evaluation) form_data = get_form_data_from_instance(UserForm, contributor) form_data["first_name"] = "Patrick" form = UserForm(form_data, instance=contributor) form.save() results_after = get_results(evaluation) self.assertCountEqual( (result.contributor.first_name for result in results_before.contribution_results if result.contributor), ("Peter", ), ) self.assertCountEqual( (result.contributor.first_name for result in results_after.contribution_results if result.contributor), ("Patrick", ), )
def test_correct_grades_and_bottom_numbers(self): degree = baker.make(Degree) evaluation = baker.make( Evaluation, _voter_count=5, _participant_count=10, course__degrees=[degree], state=Evaluation.State.PUBLISHED, ) questionnaire1 = baker.make(Questionnaire, order=1) questionnaire2 = baker.make(Questionnaire, order=2) question1 = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire1) question2 = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire2) make_rating_answer_counters(question1, evaluation.general_contribution, [1, 0, 1, 0, 0]) make_rating_answer_counters(question2, evaluation.general_contribution, [0, 1, 0, 1, 0]) evaluation.general_contribution.questionnaires.set([questionnaire1, questionnaire2]) cache_results(evaluation) sheet = self.get_export_sheet(evaluation.course.semester, degree, [evaluation.course.type.id]) self.assertEqual(sheet.row_values(5)[1], 2.0) # question 1 average self.assertEqual(sheet.row_values(8)[1], 3.0) # question 2 average self.assertEqual(sheet.row_values(10)[1], 2.5) # Average grade self.assertEqual(sheet.row_values(11)[1], "5/10") # Voters / Participants self.assertEqual(sheet.row_values(12)[1], "50%") # Voter percentage
def test_calculation_unipolar_results(self): contributor1 = baker.make(UserProfile) student = baker.make(UserProfile) evaluation = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, participants=[student, contributor1], voters=[student, contributor1], ) questionnaire = baker.make(Questionnaire) question = baker.make(Question, questionnaire=questionnaire, type=Question.GRADE) contribution1 = baker.make(Contribution, contributor=contributor1, evaluation=evaluation, questionnaires=[questionnaire]) make_rating_answer_counters(question, contribution1, [5, 15, 40, 60, 30]) cache_results(evaluation) evaluation_results = get_results(evaluation) self.assertEqual(len(evaluation_results.questionnaire_results), 1) questionnaire_result = evaluation_results.questionnaire_results[0] self.assertEqual(len(questionnaire_result.question_results), 1) question_result = questionnaire_result.question_results[0] self.assertEqual(question_result.count_sum, 150) self.assertAlmostEqual(question_result.average, float(109) / 30) self.assertEqual(question_result.counts, (5, 15, 40, 60, 30))
def test_calculate_average_course_distribution(self): make_rating_answer_counters(self.question_grade, self.contribution1, [2, 0, 0, 0, 0]) course = self.evaluation.course single_result = baker.make( Evaluation, name_de="Single Result", name_en="Single Result", course=course, weight=3, is_single_result=True, vote_start_datetime=datetime.now(), vote_end_date=datetime.now().date(), state=Evaluation.State.PUBLISHED, ) single_result_questionnaire = Questionnaire.single_result_questionnaire( ) single_result_question = single_result_questionnaire.questions.first() contribution = baker.make(Contribution, evaluation=single_result, contributor=None, questionnaires=[single_result_questionnaire]) make_rating_answer_counters(single_result_question, contribution, [0, 1, 1, 0, 0]) cache_results(single_result) cache_results(self.evaluation) distribution = calculate_average_course_distribution(course) self.assertEqual(distribution[0], 0.25) self.assertEqual(distribution[1], 0.375) self.assertEqual(distribution[2], 0.375) self.assertEqual(distribution[3], 0) self.assertEqual(distribution[4], 0)
def test_results_cache_after_user_merge(self): """Asserts that merge_users leaves the results cache in a consistent state. Regression test for #907""" contributor = baker.make(UserProfile) main_user = baker.make(UserProfile) student = baker.make(UserProfile) evaluation = baker.make(Evaluation, state=Evaluation.State.PUBLISHED, participants=[student]) questionnaire = baker.make(Questionnaire) baker.make(Question, questionnaire=questionnaire, type=Question.GRADE) baker.make(Contribution, contributor=contributor, evaluation=evaluation, questionnaires=[questionnaire]) cache_results(evaluation) merge_users(main_user, contributor) evaluation_results = get_results(evaluation) for contribution_result in evaluation_results.contribution_results: self.assertTrue( Contribution.objects.filter( evaluation=evaluation, contributor=contribution_result.contributor).exists())
def test_view_excel_file_sorted(self): semester = baker.make(Semester) course_type = baker.make(CourseType) degree = baker.make(Degree) evaluation1 = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, course=baker.make(Course, degrees=[degree], type=course_type, semester=semester, name_de="A", name_en="B"), name_de="Evaluation1", name_en="Evaluation1", ) evaluation2 = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, course=baker.make(Course, degrees=[degree], type=course_type, semester=semester, name_de="B", name_en="A"), name_de="Evaluation2", name_en="Evaluation2", ) cache_results(evaluation1) cache_results(evaluation2) content_de = BytesIO() with translation.override("de"): ResultsExporter().export(content_de, [semester], [([degree.id], [course_type.id])], True, True) content_en = BytesIO() with translation.override("en"): ResultsExporter().export(content_en, [semester], [([degree.id], [course_type.id])], True, True) content_de.seek(0) content_en.seek(0) # Load responses as Excel files and check for correct sorting workbook = xlrd.open_workbook(file_contents=content_de.read()) self.assertEqual(workbook.sheets()[0].row_values(0)[1], "A – Evaluation1\n") self.assertEqual(workbook.sheets()[0].row_values(0)[2], "B – Evaluation2\n") workbook = xlrd.open_workbook(file_contents=content_en.read()) self.assertEqual(workbook.sheets()[0].row_values(0)[1], "A – Evaluation2\n") self.assertEqual(workbook.sheets()[0].row_values(0)[2], "B – Evaluation1\n")
def test_distribution_with_general_grade_question(self): baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution1, answer=1, count=1) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution1, answer=3, count=1) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution2, answer=4, count=1) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution2, answer=2, count=1) baker.make(RatingAnswerCounter, question=self.question_likert, contribution=self.contribution1, answer=3, count=3) baker.make(RatingAnswerCounter, question=self.question_likert, contribution=self.contribution1, answer=5, count=3) baker.make(RatingAnswerCounter, question=self.question_likert, contribution=self.general_contribution, answer=5, count=5) baker.make(RatingAnswerCounter, question=self.question_likert_2, contribution=self.general_contribution, answer=3, count=3) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.general_contribution, answer=2, count=10) cache_results(self.evaluation) # contributions and general_non_grade are as above # general_grade: (0, 1, 0, 0, 0) # total: 0.3 * (0.15, 0.125, 0.375, 0.125, 0.225) + 0.2 * (0, 1, 0, 0, 0) + 0.5 * (0, 0, 0.375, 0, 0.625) = (0.045, 0.2375, 0.3, 0.0375, 0.38) distribution = calculate_average_distribution(self.evaluation) self.assertAlmostEqual(distribution[0], 0.045) self.assertAlmostEqual(distribution[1], 0.2375) self.assertAlmostEqual(distribution[2], 0.3) self.assertAlmostEqual(distribution[3], 0.0375) self.assertAlmostEqual(distribution[4], 0.38)
def test_cache_results(self): evaluation = baker.make(Evaluation, state='published') self.assertIsNone(caches['results'].get(get_results_cache_key(evaluation))) cache_results(evaluation) self.assertIsNotNone(caches['results'].get(get_results_cache_key(evaluation)))
def test_average_grade(self): question_grade2 = baker.make(Question, questionnaire=self.questionnaire, type=Question.GRADE) make_rating_answer_counters(self.question_grade, self.contribution1, [0, 1, 0, 0, 0]) make_rating_answer_counters(self.question_grade, self.contribution2, [0, 0, 0, 2, 0]) make_rating_answer_counters(question_grade2, self.contribution1, [1, 0, 0, 0, 0]) make_rating_answer_counters(self.question_likert, self.contribution1, [0, 0, 4, 0, 0]) make_rating_answer_counters(self.question_likert, self.general_contribution, [0, 0, 0, 0, 5]) make_rating_answer_counters(self.question_likert_2, self.general_contribution, [0, 0, 3, 0, 0]) make_rating_answer_counters(self.question_bipolar, self.general_contribution, [0, 0, 0, 0, 0, 0, 2]) make_rating_answer_counters(self.question_bipolar_2, self.general_contribution, [0, 0, 4, 0, 0, 0, 0]) cache_results(self.evaluation) contributor_weights_sum = ( settings.CONTRIBUTOR_GRADE_QUESTIONS_WEIGHT + settings.CONTRIBUTOR_NON_GRADE_RATING_QUESTIONS_WEIGHT) contributor1_average = ( (settings.CONTRIBUTOR_GRADE_QUESTIONS_WEIGHT * ((2 * 1) + (1 * 1)) / (1 + 1)) + (settings.CONTRIBUTOR_NON_GRADE_RATING_QUESTIONS_WEIGHT * 3)) / contributor_weights_sum # 2.4 contributor2_average = 4 contributors_average = ( (4 * contributor1_average) + (2 * contributor2_average)) / (4 + 2) # 2.9333333 general_non_grade_average = ( (5 * 5) + (3 * 3) + (2 * 5) + (4 * 7 / 3)) / (5 + 3 + 2 + 4) # 3.80952380 contributors_percentage = settings.CONTRIBUTIONS_WEIGHT / ( settings.CONTRIBUTIONS_WEIGHT + settings.GENERAL_NON_GRADE_QUESTIONS_WEIGHT) # 0.375 general_non_grade_percentage = settings.GENERAL_NON_GRADE_QUESTIONS_WEIGHT / ( settings.CONTRIBUTIONS_WEIGHT + settings.GENERAL_NON_GRADE_QUESTIONS_WEIGHT) # 0.625 total_grade = (contributors_percentage * contributors_average + general_non_grade_percentage * general_non_grade_average ) # 1.1 + 2.38095238 = 3.48095238 average_grade = distribution_to_grade( calculate_average_distribution(self.evaluation)) self.assertAlmostEqual(average_grade, total_grade) self.assertAlmostEqual(average_grade, 3.48095238)
def test_heading_question_filtering(self): degree = baker.make(Degree) evaluation = baker.make(Evaluation, course=baker.make(Course, degrees=[degree]), state='published', _participant_count=2, _voter_count=2) contributor = baker.make(UserProfile) evaluation.general_contribution.questionnaires.set( [baker.make(Questionnaire)]) questionnaire = baker.make(Questionnaire) baker.make(Question, type=Question.HEADING, questionnaire=questionnaire, order=0) heading_question = baker.make(Question, type=Question.HEADING, questionnaire=questionnaire, order=1) likert_question = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire, order=2) baker.make(Question, type=Question.HEADING, questionnaire=questionnaire, order=3) contribution = baker.make(Contribution, evaluation=evaluation, questionnaires=[questionnaire], contributor=contributor) baker.make(RatingAnswerCounter, question=likert_question, contribution=contribution, answer=3, count=100) cache_results(evaluation) binary_content = BytesIO() ResultsExporter().export( binary_content, [evaluation.course.semester], [([ course_degree.id for course_degree in evaluation.course.degrees.all() ], [evaluation.course.type.id])], True, True) binary_content.seek(0) workbook = xlrd.open_workbook(file_contents=binary_content.read()) self.assertEqual(workbook.sheets()[0].row_values(4)[0], questionnaire.name) self.assertEqual(workbook.sheets()[0].row_values(5)[0], heading_question.text) self.assertEqual(workbook.sheets()[0].row_values(6)[0], likert_question.text) self.assertEqual(workbook.sheets()[0].row_values(7)[0], "")
def test_distribution_without_general_grade_question(self): baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution1, answer=1, count=1) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution1, answer=3, count=1) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution2, answer=4, count=1) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution2, answer=2, count=1) baker.make(RatingAnswerCounter, question=self.question_likert, contribution=self.contribution1, answer=3, count=3) baker.make(RatingAnswerCounter, question=self.question_likert, contribution=self.contribution1, answer=5, count=3) baker.make(RatingAnswerCounter, question=self.question_likert, contribution=self.general_contribution, answer=5, count=5) baker.make(RatingAnswerCounter, question=self.question_likert_2, contribution=self.general_contribution, answer=3, count=3) cache_results(self.evaluation) # contribution1: 0.4 * (0.5, 0, 0.5, 0, 0) + 0.6 * (0, 0, 0.5, 0, 0.5) = (0.2, 0, 0.5, 0, 0.3) # contribution2: (0, 0.5, 0, 0.5, 0) # contributions: (6 / 8) * (0.2, 0, 0.5, 0, 0.3) + (2 / 8) * (0, 0.5, 0, 0.5, 0) = (0.15, 0.125, 0.375, 0.125, 0.225) # general_non_grade: (0, 0, 0.375, 0, 0.625) # total: 0.375 * (0.15, 0.125, 0.375, 0.125, 0.225) + 0.625 * (0, 0, 0.375, 0, 0.625) = (0.05625, 0.046875, 0.375, 0.046875, 0.475) distribution = calculate_average_distribution(self.evaluation) self.assertAlmostEqual(distribution[0], 0.05625) self.assertAlmostEqual(distribution[1], 0.046875) self.assertAlmostEqual(distribution[2], 0.375) self.assertAlmostEqual(distribution[3], 0.046875) self.assertAlmostEqual(distribution[4], 0.475)
def test_default_view_is_public(self): cache_results(self.evaluation) random.seed(42) # use explicit seed to always choose the same "random" slogan page_without_get_parameter = self.app.get(self.url, user=self.manager) random.seed(42) page_with_get_parameter = self.app.get(self.url + '?view=public', user=self.manager) random.seed(42) page_with_random_get_parameter = self.app.get(self.url + '?view=asdf', user=self.manager) self.assertEqual(page_without_get_parameter.body, page_with_get_parameter.body) self.assertEqual(page_without_get_parameter.body, page_with_random_get_parameter.body)
def test_degree_course_type_name(self): degree = baker.make(Degree, name_en="Celsius") course_type = baker.make(CourseType, name_en="LetsPlay") evaluation = baker.make( Evaluation, course__degrees=[degree], course__type=course_type, state=Evaluation.State.PUBLISHED ) cache_results(evaluation) sheet = self.get_export_sheet(evaluation.course.semester, degree, [course_type.id]) self.assertEqual(sheet.col_values(1)[1:3], [degree.name, course_type.name])
def test_exclude_single_result(self): degree = baker.make(Degree) evaluation = baker.make( Evaluation, is_single_result=True, state=Evaluation.State.PUBLISHED, course__degrees=[degree] ) cache_results(evaluation) sheet = self.get_export_sheet(evaluation.course.semester, degree, [evaluation.course.type.id]) self.assertEqual( len(sheet.row_values(0)), 1, "There should be no column for the evaluation, only the row description" )
def test_cache_results(self): evaluation = baker.make(Evaluation, state=Evaluation.State.PUBLISHED) self.assertIsNone(caches["results"].get( get_results_cache_key(evaluation))) cache_results(evaluation) self.assertIsNotNone(caches["results"].get( get_results_cache_key(evaluation)))
def setUpTestData(cls): cls.reviewer = baker.make( UserProfile, email="*****@*****.**", groups=[Group.objects.get(name="Reviewer")], ) evaluation = baker.make(Evaluation, state='published') cache_results(evaluation) cls.url = f"/results/evaluation/{evaluation.id}/text_answers_export"
def test_course_type_ordering(self): degree = baker.make(Degree) course_type_1 = baker.make(CourseType, order=1) course_type_2 = baker.make(CourseType, order=2) semester = baker.make(Semester) evaluation_1 = baker.make( Evaluation, course=baker.make(Course, semester=semester, degrees=[degree], type=course_type_1), state=Evaluation.State.PUBLISHED, _participant_count=2, _voter_count=2, ) evaluation_2 = baker.make( Evaluation, course=baker.make(Course, semester=semester, degrees=[degree], type=course_type_2), state=Evaluation.State.PUBLISHED, _participant_count=2, _voter_count=2, ) cache_results(evaluation_1) cache_results(evaluation_2) questionnaire = baker.make(Questionnaire) question = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire) evaluation_1.general_contribution.questionnaires.set([questionnaire]) make_rating_answer_counters(question, evaluation_1.general_contribution) evaluation_2.general_contribution.questionnaires.set([questionnaire]) make_rating_answer_counters(question, evaluation_2.general_contribution) binary_content = BytesIO() ResultsExporter().export( binary_content, [semester], [([degree.id], [course_type_1.id, course_type_2.id])], True, True ) binary_content.seek(0) workbook = xlrd.open_workbook(file_contents=binary_content.read()) self.assertEqual(workbook.sheets()[0].row_values(0)[1], evaluation_1.full_name + "\n") self.assertEqual(workbook.sheets()[0].row_values(0)[2], evaluation_2.full_name + "\n") course_type_2.order = 0 course_type_2.save() binary_content = BytesIO() ResultsExporter().export( binary_content, [semester], [([degree.id], [course_type_1.id, course_type_2.id])], True, True ) binary_content.seek(0) workbook = xlrd.open_workbook(file_contents=binary_content.read()) self.assertEqual(workbook.sheets()[0].row_values(0)[1], evaluation_2.full_name + "\n") self.assertEqual(workbook.sheets()[0].row_values(0)[2], evaluation_1.full_name + "\n")
def test_calculation_unipolar_results(self): contributor1 = baker.make(UserProfile) student = baker.make(UserProfile) evaluation = baker.make(Evaluation, state='published', participants=[student, contributor1], voters=[student, contributor1]) questionnaire = baker.make(Questionnaire) question = baker.make(Question, questionnaire=questionnaire, type=Question.GRADE) contribution1 = baker.make(Contribution, contributor=contributor1, evaluation=evaluation, questionnaires=[questionnaire]) baker.make(RatingAnswerCounter, question=question, contribution=contribution1, answer=1, count=5) baker.make(RatingAnswerCounter, question=question, contribution=contribution1, answer=2, count=15) baker.make(RatingAnswerCounter, question=question, contribution=contribution1, answer=3, count=40) baker.make(RatingAnswerCounter, question=question, contribution=contribution1, answer=4, count=60) baker.make(RatingAnswerCounter, question=question, contribution=contribution1, answer=5, count=30) cache_results(evaluation) evaluation_results = get_results(evaluation) self.assertEqual(len(evaluation_results.questionnaire_results), 1) questionnaire_result = evaluation_results.questionnaire_results[0] self.assertEqual(len(questionnaire_result.question_results), 1) question_result = questionnaire_result.question_results[0] self.assertEqual(question_result.count_sum, 150) self.assertAlmostEqual(question_result.average, float(109) / 30) self.assertEqual(question_result.counts, (5, 15, 40, 60, 30))
def test_archiving_participations_does_not_change_results(self): cache_results(self.evaluation) distribution = calculate_average_distribution(self.evaluation) self.semester.archive() self.refresh_evaluation() caches["results"].clear() cache_results(self.evaluation) new_distribution = calculate_average_distribution(self.evaluation) self.assertEqual(new_distribution, distribution)
def test_result_calculation_with_no_contributor_rating_question(self): evaluation = baker.make(Evaluation, state='published', participants=[self.student1, self.student2], voters=[self.student1, self.student2]) questionnaire_text = baker.make(Questionnaire) baker.make(Question, questionnaire=questionnaire_text, type=Question.TEXT) baker.make(Contribution, contributor=baker.make(UserProfile), evaluation=evaluation, questionnaires=[questionnaire_text]) evaluation.general_contribution.questionnaires.set([self.questionnaire]) baker.make(RatingAnswerCounter, question=self.question_grade, contribution=evaluation.general_contribution, answer=1, count=1) cache_results(evaluation) distribution = calculate_average_distribution(evaluation) self.assertEqual(distribution[0], 1)
def test_questionnaire_ordering(self): degree = baker.make(Degree) evaluation = baker.make( Evaluation, course__degrees=[degree], state=Evaluation.State.PUBLISHED, _participant_count=2, _voter_count=2, ) questionnaire_1 = baker.make(Questionnaire, order=1, type=Questionnaire.Type.TOP) questionnaire_2 = baker.make(Questionnaire, order=4, type=Questionnaire.Type.TOP) questionnaire_3 = baker.make(Questionnaire, order=1, type=Questionnaire.Type.BOTTOM) questionnaire_4 = baker.make(Questionnaire, order=4, type=Questionnaire.Type.BOTTOM) question_1 = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire_1) question_2 = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire_2) question_3 = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire_3) question_4 = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire_4) evaluation.general_contribution.questionnaires.set( [questionnaire_1, questionnaire_2, questionnaire_3, questionnaire_4] ) make_rating_answer_counters(question_1, evaluation.general_contribution) make_rating_answer_counters(question_2, evaluation.general_contribution) make_rating_answer_counters(question_3, evaluation.general_contribution) make_rating_answer_counters(question_4, evaluation.general_contribution) cache_results(evaluation) binary_content = BytesIO() ResultsExporter().export( binary_content, [evaluation.course.semester], [([course_degree.id for course_degree in evaluation.course.degrees.all()], [evaluation.course.type.id])], True, True, ) binary_content.seek(0) workbook = xlrd.open_workbook(file_contents=binary_content.read()) self.assertEqual(workbook.sheets()[0].row_values(4)[0], questionnaire_1.name) self.assertEqual(workbook.sheets()[0].row_values(5)[0], question_1.text) self.assertEqual(workbook.sheets()[0].row_values(7)[0], questionnaire_2.name) self.assertEqual(workbook.sheets()[0].row_values(8)[0], question_2.text) self.assertEqual(workbook.sheets()[0].row_values(10)[0], questionnaire_3.name) self.assertEqual(workbook.sheets()[0].row_values(11)[0], question_3.text) self.assertEqual(workbook.sheets()[0].row_values(13)[0], questionnaire_4.name) self.assertEqual(workbook.sheets()[0].row_values(14)[0], question_4.text)
def test_preview_with_rating_answers(self): evaluation = baker.make(Evaluation, state='evaluated', course=baker.make(Course, semester=self.semester)) questionnaire = baker.make(Questionnaire, type=Questionnaire.Type.TOP) likert_question = baker.make(Question, type=Question.LIKERT, questionnaire=questionnaire, order=1) evaluation.general_contribution.questionnaires.set([questionnaire]) participants = baker.make(UserProfile, _quantity=20) evaluation.participants.set(participants) evaluation.voters.set(participants) baker.make(RatingAnswerCounter, question=likert_question, contribution=evaluation.general_contribution, answer=1, count=20) cache_results(evaluation) url = f'/results/semester/{self.semester.id}/evaluation/{evaluation.id}' self.app.get(url, user=self.manager)
def test_include_not_enough_voters(self): semester = baker.make(Semester) degree = baker.make(Degree) enough_voters_evaluation = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, course__semester=semester, course__degrees=[degree], _voter_count=1000, _participant_count=1000, ) not_enough_voters_evaluation = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, course__semester=semester, course__degrees=[degree], _voter_count=1, _participant_count=1000, ) cache_results(enough_voters_evaluation) cache_results(not_enough_voters_evaluation) course_types = [ enough_voters_evaluation.course.type.id, not_enough_voters_evaluation.course.type.id ] # First, make sure that the one with only a single voter does not appear sheet = self.get_export_sheet(semester, degree, course_types, include_not_enough_voters=False) self.assertEqual(len(sheet.row_values(0)), 2) self.assertEqual( sheet.row_values(0)[1][:-1], enough_voters_evaluation.full_name) # Now, check with the option enabled sheet = self.get_export_sheet(semester, degree, course_types, include_not_enough_voters=True) self.assertEqual(len(sheet.row_values(0)), 3) self.assertEqual( { enough_voters_evaluation.full_name, not_enough_voters_evaluation.full_name }, {sheet.row_values(0)[1][:-1], sheet.row_values(0)[2][:-1]}, )
def test_private_evaluation(self): semester = baker.make(Semester) manager = make_manager() student = baker.make(UserProfile, email="*****@*****.**") student_external = baker.make(UserProfile, email="*****@*****.**") contributor = baker.make(UserProfile, email="*****@*****.**") responsible = baker.make(UserProfile, email="*****@*****.**") editor = baker.make(UserProfile, email="*****@*****.**") voter1 = baker.make(UserProfile, email="*****@*****.**") voter2 = baker.make(UserProfile, email="*****@*****.**") non_participant = baker.make(UserProfile, email="*****@*****.**") degree = baker.make(Degree) course = baker.make(Course, semester=semester, degrees=[degree], is_private=True, responsibles=[responsible, editor]) private_evaluation = baker.make( Evaluation, course=course, state='published', participants=[student, student_external, voter1, voter2], voters=[voter1, voter2] ) private_evaluation.general_contribution.questionnaires.set([baker.make(Questionnaire)]) baker.make( Contribution, evaluation=private_evaluation, contributor=editor, role=Contribution.Role.EDITOR, textanswer_visibility=Contribution.TextAnswerVisibility.GENERAL_TEXTANSWERS, ) baker.make(Contribution, evaluation=private_evaluation, contributor=contributor, role=Contribution.Role.EDITOR) cache_results(private_evaluation) url = '/results/' self.assertNotIn(private_evaluation.full_name, self.app.get(url, user=non_participant)) self.assertIn(private_evaluation.full_name, self.app.get(url, user=student)) self.assertIn(private_evaluation.full_name, self.app.get(url, user=responsible)) self.assertIn(private_evaluation.full_name, self.app.get(url, user=editor)) self.assertIn(private_evaluation.full_name, self.app.get(url, user=contributor)) with run_in_staff_mode(self): self.assertIn(private_evaluation.full_name, self.app.get(url, user=manager)) self.app.get(url, user=student_external, status=403) # external users can't see results semester view url = '/results/semester/%s/evaluation/%s' % (semester.id, private_evaluation.id) self.app.get(url, user=non_participant, status=403) self.app.get(url, user=student, status=200) self.app.get(url, user=responsible, status=200) self.app.get(url, user=editor, status=200) self.app.get(url, user=contributor, status=200) with run_in_staff_mode(self): self.app.get(url, user=manager, status=200) self.app.get(url, user=student_external, status=200) # this external user participates in the evaluation and can see the results
def test_calculate_average_course_distribution(self): baker.make(RatingAnswerCounter, question=self.question_grade, contribution=self.contribution1, answer=1, count=2) course = self.evaluation.course single_result = baker.make( Evaluation, name_de="Single Result", name_en="Single Result", course=course, weight=3, is_single_result=True, vote_start_datetime=datetime.now(), vote_end_date=datetime.now().date(), state="published", ) single_result_questionnaire = Questionnaire.single_result_questionnaire( ) single_result_question = single_result_questionnaire.questions.first() contribution = baker.make(Contribution, evaluation=single_result, contributor=None, questionnaires=[single_result_questionnaire]) baker.make(RatingAnswerCounter, question=single_result_question, contribution=contribution, answer=2, count=1) baker.make(RatingAnswerCounter, question=single_result_question, contribution=contribution, answer=3, count=1) cache_results(single_result) cache_results(self.evaluation) distribution = calculate_average_course_distribution(course) self.assertEqual(distribution[0], 0.25) self.assertEqual(distribution[1], 0.375) self.assertEqual(distribution[2], 0.375) self.assertEqual(distribution[3], 0) self.assertEqual(distribution[4], 0)
def test_multiple_evaluations(self): semester = baker.make(Semester) degree = baker.make(Degree) evaluation1 = baker.make( Evaluation, course__semester=semester, course__degrees=[degree], state=Evaluation.State.PUBLISHED ) evaluation2 = baker.make( Evaluation, course__semester=semester, course__degrees=[degree], state=Evaluation.State.PUBLISHED ) cache_results(evaluation1) cache_results(evaluation2) sheet = self.get_export_sheet(semester, degree, [evaluation1.course.type.id, evaluation2.course.type.id]) self.assertEqual( set(sheet.row_values(0)[1:]), set((evaluation1.full_name + "\n", evaluation2.full_name + "\n")) )
def test_include_unpublished(self): semester = baker.make(Semester) degree = baker.make(Degree) published_evaluation = baker.make( Evaluation, state=Evaluation.State.PUBLISHED, course__semester=semester, course__degrees=[degree], course__type__order=1, ) unpublished_evaluation = baker.make( Evaluation, state=Evaluation.State.REVIEWED, course__semester=semester, course__degrees=[degree], course__type__order=2, ) course_types = [ published_evaluation.course.type.id, unpublished_evaluation.course.type.id ] cache_results(published_evaluation) cache_results(unpublished_evaluation) # First, make sure that the unpublished does not appear sheet = self.get_export_sheet(include_unpublished=False, semester=semester, degree=degree, course_types=course_types) self.assertEqual(len(sheet.row_values(0)), 2) self.assertEqual( sheet.row_values(0)[1][:-1], published_evaluation.full_name) # Now, make sure that it appears when wanted sheet = self.get_export_sheet(include_unpublished=True, semester=semester, degree=degree, course_types=course_types) self.assertEqual(len(sheet.row_values(0)), 3) # These two should be ordered according to evaluation.course.type.order self.assertEqual( sheet.row_values(0)[1][:-1], published_evaluation.full_name) self.assertEqual( sheet.row_values(0)[2][:-1], unpublished_evaluation.full_name)
def test_get_single_result_rating_result(self): single_result_evaluation = baker.make(Evaluation, state='published', is_single_result=True) questionnaire = Questionnaire.objects.get(name_en=Questionnaire.SINGLE_RESULT_QUESTIONNAIRE_NAME) contribution = baker.make( Contribution, contributor=baker.make(UserProfile), evaluation=single_result_evaluation, questionnaires=[questionnaire], role=Contribution.Role.EDITOR, textanswer_visibility=Contribution.TextAnswerVisibility.GENERAL_TEXTANSWERS, ) baker.make(RatingAnswerCounter, question=questionnaire.questions.first(), contribution=contribution, answer=1, count=1) baker.make(RatingAnswerCounter, question=questionnaire.questions.first(), contribution=contribution, answer=4, count=1) cache_results(single_result_evaluation) distribution = calculate_average_distribution(single_result_evaluation) self.assertEqual(distribution, (0.5, 0, 0, 0.5, 0)) rating_result = get_single_result_rating_result(single_result_evaluation) self.assertEqual(rating_result.counts, (1, 0, 0, 1, 0))