def test_survey_multi_similarity_same() -> None: """A test for get_similarity() in class MultipleChoiceQuestion.""" q = MultipleChoiceQuestion(1, 'ABC', ['A', 'B']) a = Answer('A') b = Answer('A') assert type(q.get_similarity(a, b)) is float assert q.get_similarity(a, b) == 1.0
def test_multiple_choice_question_get_similarity() -> None: q = MultipleChoiceQuestion(1, "how old are you", [10, 11]) a1 = Answer("hello") a2 = Answer("hel") a3 = Answer("hello") assert q.get_similarity(a1, a2) == 0.0 assert q.get_similarity(a1, a3) == 1.0
def test_multiple_choice_question_validate_answer() -> None: q = MultipleChoiceQuestion(1, "how old are you", [10, 11]) a = Answer(15) a2 = Answer("hello") a3 = Answer(10) assert q.validate_answer(a) is False assert q.validate_answer(a2) is False assert q.validate_answer(a3) is True
def test_criterion_lonely_score_answer_valid_different() -> None: """A test for score_answer() in class LonelyMemberCriterion.""" q1 = YesNoQuestion(1, 'ABC') a1 = Answer(True) a2 = Answer(False) a3 = Answer(False) c = LonelyMemberCriterion() assert c.score_answers(q1, [a1, a2, a3]) == 0.0
def test_heterogeneous_criterion() -> None: q = CheckboxQuestion(1, "choose", [1, 2, 3, 4]) a1 = Answer([1, 2, 3, 4]) a2 = Answer([1]) a3 = Answer(["hello"]) c = HeterogeneousCriterion() assert c.score_answers(q, [a1, a2]) == 0.75 # assert c.score_answers(q, [a3]) is InvalidAnswerError assert c.score_answers(q, [a1]) == 0.0
def test_checkbox_question_validate_answer() -> None: q = CheckboxQuestion(1, "choose", ['a', 'b', 'c']) a1 = Answer([]) a2 = Answer(['a', 'c']) a3 = Answer(['a', 'a']) a4 = Answer(True) assert q.validate_answer(a1) is False assert q.validate_answer(a2) is True assert q.validate_answer(a3) is False assert q.validate_answer(a4) is False
def test_student_get_answer() -> None: student = Student(1, "Jane") q1 = Question(1, "How are you") a1 = Answer("good") q2 = Question(2, "hello") a2 = Answer("hi") assert student.get_answer(q1) is None student.set_answer(q1, a1) student.set_answer(q2, a2) assert student.get_answer(q1) == a1 assert student.get_answer(q2) == a2
def test_student_set_answer(): student = Student(1, "Jane") q1 = Question(1, "How are you") a1 = Answer("good") q2 = Question(2, "hello") a2 = Answer("hi") a3 = Answer("test") student.set_answer(q1, a1) student.set_answer(q2, a2) student.set_answer(q2, a3) assert student.get_answer(q1) == a1 assert student.get_answer(q2) == a3
def test_numeric_question_validate_answer() -> None: q = NumericQuestion(1, 'choose!', 1, 1000) a = Answer(15) a2 = Answer("a") a3 = Answer(1000) a4 = Answer(1001) a5 = Answer(True) assert q.validate_answer(a) is True assert q.validate_answer(a2) is False assert q.validate_answer(a3) is True assert q.validate_answer(a4) is False assert q.validate_answer(a5) is False
def test_survey_survey_score_grouping() -> None: """A test for score_grouping() in class Survey.""" q1 = YesNoQuestion(1, 'BBC') q2 = MultipleChoiceQuestion(2, 'ABC', ['A', 'B', 'C']) a1 = Answer(True) a2 = Answer('A') a3 = Answer(True) a4 = Answer('C') a5 = Answer(True) a6 = Answer('B') a7 = Answer(False) a8 = Answer('C') stu1 = Student(100, 'Jack') stu2 = Student(200, 'Mike') stu3 = Student(300, 'Diana') stu4 = Student(400, 'Tom') stu1.set_answer(q1, a1) stu1.set_answer(q2, a2) stu2.set_answer(q1, a3) stu2.set_answer(q2, a4) stu3.set_answer(q1, a5) stu3.set_answer(q2, a6) stu4.set_answer(q1, a7) stu4.set_answer(q2, a8) s = Survey([q1, q2]) c = HomogeneousCriterion() s.set_weight(2.0, q1) s.set_criterion(c, q1) s.set_criterion(c, q2) g1 = Group([stu1, stu2]) g2 = Group([stu3, stu4]) grouping = Grouping() grouping.add_group(g1) grouping.add_group(g2) assert s.score_grouping(grouping) == 0.5
def test_course_all_answered() -> None: """A test for all_answered() in class Course.""" s1 = Student(1, 'A') s2 = Student(2, 'B') c = Course('CS') c.enroll_students([s1, s2]) q1 = YesNoQuestion(2, 'F') q2 = YesNoQuestion(3, 'K') a1 = Answer(True) a2 = Answer(False) survey = Survey([q1, q2]) s1.set_answer(q1, a1) s1.set_answer(q2, a2) s2.set_answer(q1, a1) s2.set_answer(q2, a2) assert c.all_answered(survey) is True
def test_student_get_answer() -> None: """A test for get_answer() in class Student.""" s = Student(123, 'Mike') ans = Answer(False) que = Question(2, 'F') s.set_answer(que, ans) test_ans = s.get_answer(que) assert ans == test_ans
def test_course_all_answered() -> None: student_1 = Student(2, "Jane") student_2 = Student(1, "John") course = Course("math") course_2 = Course("Eng") lst = [student_1, student_2] lst_2 = [student_1] course.enroll_students(lst) course_2.enroll_students(lst_2) q = CheckboxQuestion(1, "choose", [1, 2, 3]) a1 = Answer([1]) a2 = Answer([5]) student_1.set_answer(q, a1) student_2.set_answer(q, a2) s = Survey([q]) assert course.all_answered(s) is False assert course_2.all_answered(s) is True
def test_answer_is_valid() -> None: a_multiple = Answer("a") a_checkbox = Answer(["a", "b"]) a_yesno = Answer(True) a_yesno_2 = Answer(1) a_numeric = Answer(15) assert a_multiple.is_valid(MultipleChoiceQuestion(1, "choose", ["a", "b"]))\ is True assert a_multiple.is_valid(MultipleChoiceQuestion(1, "choose", ["c", "b"])) \ is False assert a_checkbox.is_valid(CheckboxQuestion(2, "choose", ["a", "b"])) is \ True assert a_checkbox.is_valid(CheckboxQuestion(2, "choose", ["a", "a"])) is \ False assert a_yesno.is_valid(YesNoQuestion(1, "choose")) is True assert a_yesno_2.is_valid(YesNoQuestion(1, "choose")) is False assert a_numeric.is_valid(NumericQuestion(1, "5+10", 1, 20)) is True assert a_numeric.is_valid(NumericQuestion(1, "5+10", 18, 20)) is False
def test_survey_survey_score_student() -> None: """A test for score_student() in class Survey.""" q1 = YesNoQuestion(1, 'BBC') q2 = MultipleChoiceQuestion(2, 'ABC', ['A', 'B', 'C']) a1 = Answer(True) a2 = Answer('A') a3 = Answer(True) a4 = Answer('C') stu1 = Student(100, 'Jack') stu2 = Student(200, 'Mike') stu1.set_answer(q1, a1) stu1.set_answer(q2, a2) stu2.set_answer(q1, a3) stu2.set_answer(q2, a4) s = Survey([q1, q2]) c = HomogeneousCriterion() s.set_weight(2.0, q1) s.set_criterion(c, q1) s.set_criterion(c, q2) assert s.score_students([stu1, stu2]) == 1.0
def test_make_window_grouping() -> None: grouper_ = grouper.GreedyGrouper(2) q = CheckboxQuestion(1, "choose", [1, 2, 3, 4]) s1 = Student(1, "John") s2 = Student(2, "Carl") s3 = Student(3, "Anne") a1 = Answer([1, 2, 3, 4]) a2 = Answer([1]) a3 = Answer([1]) s1.set_answer(q, a1) s2.set_answer(q, a2) s3.set_answer(q, a3) c = LonelyMemberCriterion() sur = Survey([q]) sur.set_weight(1, q) sur.set_criterion(c, q) q_2 = CheckboxQuestion(2, "choose", [1, 2, 3, 4]) a4 = Answer([1, 2]) a5 = Answer([1, 2]) a6 = Answer([1, 2]) s1.set_answer(q_2, a4) s2.set_answer(q_2, a5) s3.set_answer(q_2, a6) sur2 = Survey([q, q_2]) sur2.set_weight(1, q_2) sur2.set_criterion(c, q_2) cr = Course('dd') cr.enroll_students([s1, s2, s3]) assert grouper_.make_grouping( cr, sur).get_groups()[0].get_members()[0].name == "John" assert grouper_.make_grouping( cr, sur).get_groups()[0].get_members()[1].name == "Carl" assert grouper_.make_grouping( cr, sur).get_groups()[1].get_members()[0].name == "Anne"
def test_score_students() -> None: q = CheckboxQuestion(1, "choose", [1, 2, 3, 4]) s1 = Student(1, "John") s2 = Student(2, "Carl") s3 = Student(3, "Anne") a1 = Answer([1, 2, 3, 4]) a2 = Answer([1]) a3 = Answer([1]) s1.set_answer(q, a1) s2.set_answer(q, a2) s3.set_answer(q, a3) c = LonelyMemberCriterion() sur = Survey([q]) sur.set_weight(1, q) sur.set_criterion(c, q) assert sur.score_students([s1, s2, s3]) == 0.0 q_2 = CheckboxQuestion(2, "choose", [1, 2, 3, 4]) a4 = Answer([1, 2]) a5 = Answer([1, 2]) a6 = Answer([1, 2]) s1.set_answer(q_2, a4) s2.set_answer(q_2, a5) s3.set_answer(q_2, a6) sur1 = Survey([q_2]) sur2 = Survey([q, q_2]) sur2.set_weight(1, q_2) sur2.set_criterion(c, q_2) assert sur1.score_students([s1, s2, s3]) == 1 assert sur2.score_students([s1, s2, s3]) == 0.5
def return_sur() -> Survey: q = CheckboxQuestion(1, "choose", [1, 2, 3, 4]) s1 = Student(1, "John") s2 = Student(2, "Carl") s3 = Student(3, "Anne") a1 = Answer([1, 2, 3, 4]) a2 = Answer([1]) a3 = Answer([1]) s1.set_answer(q, a1) s2.set_answer(q, a2) s3.set_answer(q, a3) c = LonelyMemberCriterion() sur = Survey([q]) sur.set_weight(1, q) sur.set_criterion(c, q) q_2 = CheckboxQuestion(2, "choose", [1, 2, 3, 4]) a4 = Answer([1, 2]) a5 = Answer([1, 2]) a6 = Answer([1, 2]) s1.set_answer(q_2, a4) s2.set_answer(q_2, a5) s3.set_answer(q_2, a6) sur2 = Survey([q, q_2]) sur2.set_weight(1, q_2) sur2.set_criterion(c, q_2) return sur2
def test_lonely_member_criterion() -> None: q = CheckboxQuestion(1, "choose", [1, 2, 3, 4]) q2 = YesNoQuestion(2, "choose") a1 = Answer([1, 2, 3, 4]) a2 = Answer([1]) a3 = Answer([1]) a4 = Answer(True) a5 = Answer(False) a6 = Answer(True) a7 = Answer(False) c = LonelyMemberCriterion() assert c.score_answers(q, [a1, a2]) == 0.0 assert c.score_answers(q, [a2, a3]) == 1.0 assert c.score_answers(q2, [a4, a5, a6, a7]) == 1.0
def test_student() -> None: student_1 = Student(1, "roney") student_2 = Student(2, "tim") student_3 = Student(3, "allen") assert str(student_1) == "roney" assert str(student_2) == "tim" assert str(student_3) != "roney" assert student_1.id == 1 assert student_2.id != 4 assert student_3.id == 3 mcq_1 = MultipleChoiceQuestion(1, "Cities in Canada", ["Toronto", "Ottawa", "Vancouver"]) mcq_2 = MultipleChoiceQuestion(2, "C", ["A", "O", "V"]) mcq_1_ans = Answer("Toronto") student_1.set_answer(mcq_1, mcq_1_ans) assert student_1.has_answer(mcq_1) assert not student_1.has_answer(mcq_2) assert student_1.get_answer(mcq_1) == mcq_1_ans assert student_1.get_answer(mcq_2) == None assert student_2.get_answer(mcq_1) == None
def test_grouper_window_make_grouping() -> None: """A test for make_grouping() in class WindowGrouper.""" q1 = YesNoQuestion(1, 'BBC') q2 = MultipleChoiceQuestion(2, 'ABC', ['A', 'B', 'C']) a1 = Answer(True) a2 = Answer('A') a3 = Answer(True) a4 = Answer('B') a5 = Answer(True) a6 = Answer('B') a7 = Answer(False) a8 = Answer('C') stu1 = Student(100, 'Jack') stu2 = Student(200, 'Mike') stu3 = Student(300, 'Diana') stu4 = Student(400, 'Tom') stu1.set_answer(q1, a1) stu1.set_answer(q2, a2) stu2.set_answer(q1, a3) stu2.set_answer(q2, a4) stu3.set_answer(q1, a5) stu3.set_answer(q2, a6) stu4.set_answer(q1, a7) stu4.set_answer(q2, a8) window = WindowGrouper(2) survey = Survey([q1, q2]) course = Course('CS') cri = HomogeneousCriterion() survey.set_weight(2.0, q1) survey.set_criterion(cri, q1) survey.set_criterion(cri, q2) course.enroll_students([stu1, stu2, stu3, stu4]) result = window.make_grouping(course, survey).get_groups() assert stu1 in result[1] assert stu2 in result[0] assert stu3 in result[0] assert stu4 in result[1]
def test_criterion_hetero_score_answer_valid() -> None: """A test for score_answer() in class HeterogeneousCriterion.""" q1 = YesNoQuestion(1, 'ABC') a1 = Answer(True) a2 = Answer(True) a3 = Answer(True) a4 = Answer(True) a5 = Answer(False) a6 = Answer(False) c = HeterogeneousCriterion() # a1 a2 a3 a4 a5 a6 # a1 NA 1 1 1 0 0 # a2 1 NA 1 1 0 0 # a3 1 1 NA 1 0 0 # a4 1 1 1 NA 0 0 # a5 0 0 0 0 NA 1 # a6 0 0 0 0 1 NA # score = 0.533333... assert 0.53 < c.score_answers(q1, [a1, a2, a3, a4, a5, a6]) < 0.54
def test_yesno_question_validate_answer() -> None: q = YesNoQuestion(1, 'choose!') a1 = Answer(False) a2 = Answer(1) assert q.validate_answer(a1) is True assert q.validate_answer(a2) is False
def test_survey_checkbox_similarity() -> None: """A test for get_similarity() in class CheckboxQuestion.""" q = CheckboxQuestion(1, 'ABC', ['a', '1', ',']) a1 = Answer(['a', 'b']) a2 = Answer(['b', 'c', 'd']) assert q.get_similarity(a1, a2) == 0.25
def test_survey_answer_is_valid_yes() -> None: """A test for is_valid() in class Answer.""" q = YesNoQuestion(1, 'ABC') a = Answer(True) assert a.is_valid(q)
def test_survey_answer_is_valid_no() -> None: """A test for is_valid() in class Answer.""" q = CheckboxQuestion(1, 'ABC', ['a', '1', ',']) a = Answer(['b']) assert a.is_valid(q) is False
def test_criterion_hetero_score_answer_valid_one() -> None: """A test for score_answer() in class HeterogeneousCriterion.""" q1 = YesNoQuestion(1, 'ABC') a1 = Answer(True) c = HeterogeneousCriterion() assert c.score_answers(q1, [a1]) == 0.0
def test_numeric_question_get_similarity() -> None: q = NumericQuestion(1, 'choose!', 0, 10) a1 = Answer(5) a2 = Answer(6) assert q.get_similarity(a1, a2) == 0.9
def test_yesno_question_get_similarity() -> None: q = YesNoQuestion(1, 'choose') a1 = Answer(True) a2 = Answer(False) assert q.get_similarity(a1, a2) == 0.0
def test_checkbox_question_get_similarity() -> None: q = CheckboxQuestion(1, "choose", ['a', 'b', 'c']) a2 = Answer(['a', 'b', 'd']) a3 = Answer(['a', 'b', 'c']) assert q.get_similarity(a2, a3) == 0.5