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_survey_survey_set_criterion_invalid() -> None: """A test for set_criterion() in class Survey.""" q1 = CheckboxQuestion(1, 'ABC', ['a', '1', ',']) q2 = YesNoQuestion(2, 'BBC') s = Survey([q1]) c = HomogeneousCriterion() assert s.set_criterion(c, q2) is False
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_survey_survey_get_questions() -> None: """A test for get_questions() in class Survey.""" q1 = CheckboxQuestion(1, 'ABC', ['a', '1', ',']) q2 = YesNoQuestion(2, 'BBC') s = Survey([q1, q2]) lst = s.get_questions() assert q1 in lst and q2 in lst
def test_criterion() -> None: ho_crit = HomogeneousCriterion() he_crit = HeterogeneousCriterion() lo_crit = LonelyMemberCriterion() ynq = YesNoQuestion(1, "Is earth round") ynq_1_answers = [Answer(True), Answer(True), Answer(True), Answer(True)] ynq_2_answers = [Answer(True), Answer(False), Answer(True), Answer(False)] ynq_3_answers = [Answer(True)] ynq_4_answers = [Answer(True), Answer(False)] assert ho_crit.score_answers(ynq, ynq_1_answers) == 1 assert ho_crit.score_answers(ynq, ynq_2_answers) == 1 / 3 assert ho_crit.score_answers(ynq, ynq_3_answers) == 1 assert ho_crit.score_answers(ynq, ynq_4_answers) == 0 assert he_crit.score_answers(ynq, ynq_1_answers) == 0.0 assert he_crit.score_answers(ynq, ynq_2_answers) == pytest.approx(2 / 3, rel=1e-3) assert he_crit.score_answers(ynq, ynq_3_answers) == 0.0 assert he_crit.score_answers(ynq, ynq_4_answers) == 1 assert lo_crit.score_answers(ynq, ynq_1_answers) == 1 assert lo_crit.score_answers(ynq, ynq_2_answers) == 0.0 assert lo_crit.score_answers(ynq, ynq_3_answers) == 1 assert lo_crit.score_answers(ynq, ynq_4_answers) == 0
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_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_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_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_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_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_questions() -> None: mcq_1 = MultipleChoiceQuestion(1, "Cities in Canada", ["Toronto", "Ottawa", "Vancouver"]) assert mcq_1.id == 1 assert mcq_1.text == "Cities in Canada" assert "Cities in Canada" in str(mcq_1) assert "Toronto" in str(mcq_1) assert "Ottawa" in str(mcq_1) assert "Montreal" not in str(mcq_1) mcq_1_ans = Answer("Toronto") mcq_1_c_ans = Answer("Toronto") mcq_2_ans = Answer("Montreal") mcq_3_ans = Answer("") assert mcq_1_ans.is_valid(mcq_1) assert not mcq_2_ans.is_valid(mcq_1) assert mcq_1.validate_answer(mcq_1_ans) assert not mcq_1.validate_answer(mcq_2_ans) assert not mcq_1.validate_answer(mcq_3_ans) assert mcq_1.get_similarity(mcq_1_ans, mcq_1_c_ans) == 1.0 assert mcq_1.get_similarity(mcq_1_ans, mcq_2_ans) == 0.0 nq_1 = NumericQuestion(1, "A n.o between 1 and 5", 1, 5) nq_1_ans = Answer(1) nq_2_ans = Answer(2) nq_3_ans = Answer(3) nq_4_ans = Answer(5) nq_5_ans = Answer(10) nq_6_ans = Answer(1) assert nq_1.validate_answer(nq_1_ans) assert nq_1.validate_answer(nq_2_ans) assert nq_1.validate_answer(nq_3_ans) assert nq_1.validate_answer(nq_4_ans) assert not nq_1.validate_answer(nq_5_ans) assert nq_1_ans.is_valid(nq_1) assert nq_2_ans.is_valid(nq_1) assert nq_3_ans.is_valid(nq_1) assert nq_4_ans.is_valid(nq_1) assert not nq_5_ans.is_valid(nq_1) assert nq_1.get_similarity(nq_1_ans, nq_6_ans) == 1.0 assert nq_1.get_similarity(nq_1_ans, nq_4_ans) == 0.0 assert nq_1.get_similarity(nq_2_ans, nq_3_ans) == 0.75 ynq = YesNoQuestion(1, "Is earth round") ynq_1 = Answer(True) ynq_2 = Answer(False) ynq_3 = Answer(False) assert ynq_1.is_valid(ynq) assert ynq_2.is_valid(ynq) assert not ynq.get_similarity(ynq_1, ynq_2) assert ynq.get_similarity(ynq_2, ynq_3) cbq = CheckboxQuestion(1, "Cities in Canada", ["Toronto", "Ottawa", "Vancouver"]) cbq_1 = Answer(["Toronto", "Ottawa"]) cbq_2 = Answer(["Seoul", "Busan"]) cbq_3 = Answer(["Toronto", "Ottawa"]) cbq_4 = Answer(["Toronto", "Vancouver"]) assert cbq.validate_answer(cbq_1) assert not cbq.validate_answer(cbq_2) assert cbq_1.is_valid(cbq) assert not cbq_2.is_valid(cbq) assert cbq.get_similarity(cbq_1, cbq_3) == 1 assert cbq.get_similarity(cbq_1, cbq_4) == 1 / 3 assert cbq.get_similarity(cbq_1, cbq_2) == 0.0
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_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_yesno_question_string() -> None: q = YesNoQuestion(1, 'choose!') assert str(q) == 'choose! (Yes or No)'
def test_survey_yesno_to_string() -> None: """A test for __str__() in class YesNoQuestion.""" q = YesNoQuestion(1, 'ABC') assert type(str(q)) is str
def test_survey_yesno_invalid_answer() -> None: """A test for validate_answer() in class YesNoQuestion.""" q = YesNoQuestion(1, 'ABC') a = Answer(2) assert q.validate_answer(a) is False
def test_survey_yesno_similarity_different() -> None: """A test for get_similarity() in class YesNoQuestion.""" q = YesNoQuestion(1, 'ABC') a1 = Answer(True) a2 = Answer(False) assert q.get_similarity(a1, a2) == 0.0
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_grouper() -> None: assert windows([3, 4, 6, 2, 3], 2) == [[3, 4], [4, 6], [6, 2], [2, 3]] assert windows(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [1, 6.0, False]] assert slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] assert slice_list(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [False]] g = Group([Student(1, "roney"), Student(2, "tim"), Student(3, "allen")]) g_1 = Group([Student(1, "roney"), Student(2, "tim"), Student(3, "allen")]) g_2 = Group([Student(5, "roney"), Student(6, "tim"), Student(7, "allen")]) assert len(g) == 3 assert Student(1, "roney") in g assert "roney" in str(g) gr = Grouping() assert gr.add_group(g) assert not gr.add_group(g_1) assert gr.add_group(g_2) assert len(gr) == 2 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = AlphaGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = AlphaGrouper(3) gr = ag.make_grouping(course_0, s) assert len(gr) == 1 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = RandomGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = RandomGrouper(3) gr = ag.make_grouping(course_0, s) assert len(gr) == 1 course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3]) s = Survey([q]) ag = GreedyGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3]) s = Survey([q]) ag = GreedyGrouper(3) gr = ag.make_grouping(course_0, s) assert len(gr) == 1 groups = gr.get_groups() assert groups[0]._member_id == [1, 3, 2] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3]) s = Survey([q]) s.set_criterion(LonelyMemberCriterion(), q) ag = GreedyGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 3] assert groups[1]._member_id == [2] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(True)) s3.set_answer(q, Answer(True)) s4.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(LonelyMemberCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 2] assert groups[1]._member_id == [3, 4] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) s4.set_answer(q, Answer(False)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(HeterogeneousCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 2] assert groups[1]._member_id == [3, 4] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) s4.set_answer(q, Answer(False)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(HomogeneousCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 2] assert groups[1]._member_id == [3, 4] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(False)) s4.set_answer(q, Answer(False)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(HomogeneousCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [2, 3] assert groups[1]._member_id == [1, 4]
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_survey_set_weight_invalid() -> None: """A test for set_weight() in class Survey.""" q1 = CheckboxQuestion(1, 'ABC', ['a', '1', ',']) q2 = YesNoQuestion(2, 'BBC') s = Survey([q1]) assert s.set_weight(2.0, q2) is False
def test_survey_survey_contain() -> None: """A test for __contain__() in class Survey.""" q1 = CheckboxQuestion(1, 'ABC', ['a', '1', ',']) q2 = YesNoQuestion(2, 'BBC') s = Survey([q1, q2]) assert q1 in s
def test_survey_survey_to_string() -> None: """A test for __str__() in class Survey.""" q1 = CheckboxQuestion(1, 'ABC', ['a', '1', ',']) q2 = YesNoQuestion(2, 'BBC') s = Survey([q1, q2]) assert type(str(s)) is str