示例#1
0
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
示例#2
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
示例#3
0
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