def test_exam_init(question1, question2): """GIVEN Exam initialized with one/two questions THEN questions attribute have the given questions """ ex1 = exam2pdf.Exam(question1) ex2 = exam2pdf.Exam(question1, question2) assert ex1.questions == (question1,) assert ex2.questions == (question1, question2)
def test_exam_print(): data = ( dict( [ ("field A", "A1"), ("field B", "A2"), ("field C", "T"), ("field D", "A3"), ("field E", "A4"), ("field F", "S"), ("field G", 2), ("void", ""), ] ), ) text, q_image, level, a_image = ( f"text: A1", f"image: .", f"level: 2", f"image: S", ) ex = exam2pdf.Exam() ex.load_sequence = ("field A", "void", "void", "field G", "void", "field F") ex.load(data) assert text in ex.__str__() assert q_image in ex.__str__() assert level in ex.__str__() assert a_image in ex.__str__()
def test_exam(): """GIVEN an empty Exam THEN questions attribute is an empty tuple """ ex = exam2pdf.Exam() assert ex.questions == tuple()
def test_exam_laad_sequence1(): """GIVEN an Exam THEN Exam.laad_sequence is found set to default. """ ex = exam2pdf.Exam() assert ex.load_sequence == ()
def test_exam_load0(): """test empty iterable """ ex = exam2pdf.Exam() ex.load(iter(())) assert ex.questions == tuple()
def test_from_csv_no_question(no_question_file): """GIVEN a csv file without question WHEN it tries to read THEN exception is raised """ ex = exam2pdf.Exam() with pytest.raises(exam2pdf.Exam2pdfException): ex.from_csv(no_question_file)
def test_from_csv_different_encodings(files_with_different_encoding): """GIVEN csv files with different encodings WHEN they are read THEN it does not fail """ ex = exam2pdf.Exam() for file_path in files_with_different_encoding: ex.from_csv(file_path)
def test_from_csv_empty_file(empty_file): """GIVEN an empty csv file WHEN it tries to read THEN exception is raised """ ex = exam2pdf.Exam() with pytest.raises(exam2pdf.Exam2pdfException): ex.from_csv(empty_file)
def test_exam_show_up1(): """GIVEN an Exam THEN Exam.show_up is found set to default. """ ex = exam2pdf.Exam() expected = exam2pdf.exam.ATTRIBUTE_SHOWED_UP assert ex.show_up == expected
def test_from_csv_one_truefalse_question(truefalse_question_file): """GIVEN a csv file with one truefalse question WHEN it is read THEN it recognized as truefalse because False option is found""" ex = exam2pdf.Exam() ex.load_sequence = ("question", "void", "void", "void", "A", "void", "B") ex.from_csv(truefalse_question_file) assert ex.questions[0].correct_option == "Falso"
def test_exam_show_up2(): """GIVEN an Exam AND Exam.show_up is set THEN Exam.show_up matches. """ text = "one two three" ex = exam2pdf.Exam() ex.show_up = text.split() assert ex.show_up == tuple(text.split())
def test_exam_laad_sequence2(): """GIVEN an Exam AND Exam.load_sequence is set THEN Exam.load_sequence is found set """ ex = exam2pdf.Exam() expected = ("hello", "2", "times") ex.load_sequence = (expected[0], int(expected[1]), expected[2]) assert ex.load_sequence == expected
def test_print_exam_without_permission(tmp_path, no_write_permission_dir): """GIVEN an Exam WHEN user has no permission to write in the directory AND print_exam is called THEN Exception is risen """ file_path = no_write_permission_dir / "exam.pdf" ex = exam2pdf.Exam() with pytest.raises(Exam2pdfException): ex.print_exam(file_path)
def test_exam_questions_add(question1, question2): """GIVEN an Exam WHEN add_question two questions THEN added questions are in questions attribute """ ex = exam2pdf.Exam() ex.add_question(question1) ex.add_question(question2) assert question1 in ex.questions assert question2 in ex.questions
def test_exam_load4(): """test setting _attribute_selector """ data = ( dict([("text", "T"), ("subject", "S"), ("XXX level", 2), ("void", "")]), ) ex = exam2pdf.Exam() ex.load_sequence = ("text", "subject", "void", "level") with pytest.raises(exam2pdf.Exam2pdfException): ex.load(data)
def test_exam_load2(): """test without setting _attribute_selector and missing row """ ex = exam2pdf.Exam() reader = (dict([]), dict([("A", "What?"), ("B", "topic")])) ex.load(reader) print(ex) assert ex.questions[0].text == "What?" assert ex.questions[0].subject == "topic"
def test_exam_questions_add_and_set(question1, question2): """GIVEN an Exam WHEN a question is added AND a tuple with one question is set THEN attribute assignment override the added questions """ ex = exam2pdf.Exam() ex.add_question(question1) ex.questions = (question2,) assert question1 not in ex.questions assert question2 in ex.questions
def test_from_csv_one_question(tmp_path, question_data_file): """GIVEN a csv file with one multi choice question and three answers with images WHEN it is read THEN a sample of correct information are found""" ex = exam2pdf.Exam() ex.from_csv(question_data_file) assert len(ex.questions) == 1 assert ex.questions[0].text == "Q" assert len(ex.questions[0].answers) == 3 assert ex.questions[0].answers[2].image == tmp_path / "ci"
def test_print_exam(tmp_path): """GIVEN an empty Exam instance WHEN print_exam is called THEN an empty pdf file is saved""" pdf_magic_no = b"PDF" file_path = tmp_path / "Exam" ex = exam2pdf.Exam() ex.print_exam(file_path) try: data = file_path.read_bytes() except FileNotFoundError: assert False, "File not found" assert data.find(pdf_magic_no) == 1
def test_exam_load1(): """test without setting _attribute_selector 2 rows -> 2 questions with 2 answers each but second answer image is not provided """ data = ( dict( [ ("text", "ab"), ("subject", "ac"), ("image", "ad"), ("level", "1"), ("a0 text", "ae"), ("a0 image", "af"), ("a1 text", "ag"), ] ), dict( [ ("text", "ba"), ("subject", "bc"), ("image", "bd"), ("level", "2"), ("a0 text", "be"), ("a0 image", "bf"), ("a1 text", "bg"), ] ), ) ex = exam2pdf.Exam() ex.load(data) for i in (0, 1): assert ex.questions[i].text == data[i]["text"] assert ex.questions[i].subject == data[i]["subject"] assert ex.questions[i].image == Path(data[i]["image"]) assert ex.questions[i].level == int(data[i]["level"]) assert ex.questions[i].answers[0].text == data[i]["a0 text"] assert ex.questions[i].answers[0].image == Path(data[i]["a0 image"]) assert ex.questions[i].answers[1].text == data[i]["a1 text"] assert ex.questions[i].answers[1].image == Path() # default value # third answer of second question is not provided with pytest.raises(IndexError): _ = ex.questions[1].answers[2] # third question is not provided with pytest.raises(IndexError): _ = ex.questions[2]
def test_shuffle(): data = ( dict( [ ("question", " Q1"), ("A", "A1"), ("B", "B1"), ("C", "C1"), ("D", "D1"), ("E", "E1"), ("void", ""), ] ), dict( [ ("question", "Q2"), ("A", "A2"), ("B", "B2"), ("C", "C2"), ("D", "D2"), ("E", "E2"), ("void", ""), ] ), ) correct_values = ("D", "A") ex = exam2pdf.Exam() ex.load_sequence = ( "question", "void", "void", "void", "A", "void", "B", "void", "C", "void", "D", "void", "E", ) ex.load(data) random.seed(1) ex.answers_shuffle() for question, value in zip(ex.questions, correct_values): assert question.correct_option == value
def test_exam_load3(): """test setting _attribute_selector """ data = ( dict( [ ("A text", "A"), ("B text", "B"), ("text", "T"), ("C text", "A3"), ("D text", "A4"), ("subject", "S"), ("level", 2), ("void", ""), ] ), ) ex = exam2pdf.Exam() ex.load_sequence = ( "text", "subject", "void", "level", "A text", "void", "B text", "void", "C text", ) ex.load(data) assert ex.questions[0].text == data[0]["text"] assert ex.questions[0].subject == data[0]["subject"] assert ex.questions[0].image == Path() assert ex.questions[0].level == data[0]["level"] assert ex.questions[0].answers[0].text == data[0]["A text"] assert ex.questions[0].answers[0].image == Path() assert ex.questions[0].answers[1].text == data[0]["B text"] assert ex.questions[0].answers[1].image == Path() assert ex.questions[0].answers[2].text == data[0]["C text"] assert ex.questions[0].answers[2].image == Path() # no further elements loaded with pytest.raises(IndexError): _ = ex.questions[0].answers[3] with pytest.raises(IndexError): _ = ex.questions[1].answers[2]
def test_exam_truefalse_question(): question1 = exam2pdf.TrueFalseQuest("mc quest1 text", "subject") question1.answers = ( exam2pdf.TrueFalseAnswer(True), exam2pdf.TrueFalseAnswer(False), ) question2 = exam2pdf.Question("mc quest2 text", "subject") question2.answers = ( exam2pdf.TrueFalseAnswer(False), exam2pdf.TrueFalseAnswer(True), ) ex = exam2pdf.Exam(question1, question2) assert ex.questions[0].answers[1].image == Path() assert ex.questions[0].correct_answer.boolean is True assert ex.questions[1].text == "mc quest2 text" assert ex.questions[1].correct_answer.text == "Falso"
def test_exam_add_path_parent2(tmp_path): image = Path("images/image.png") folder_path = tmp_path q1 = exam2pdf.Question("q1 text", "") q1.answers = ( exam2pdf.Answer("a1 text", image), exam2pdf.Answer("a2 text", image), ) q2 = exam2pdf.Question("q2 text", "", image) q2.add_answer(exam2pdf.Answer("a3 text")) ex = exam2pdf.Exam(q1, q2) ex.add_path_parent(folder_path) assert ex.questions[0].image == Path() assert ex.questions[0].answers[0].image == folder_path / image assert ex.questions[0].answers[1].image == folder_path / image assert ex.questions[1].image == folder_path / image assert ex.questions[1].answers[0].image == Path()
def mix_dummy_exam(): q1 = exam2pdf.Question( "question 1: correct is n. two", "subject 1", Path("a.png") ) a1 = exam2pdf.Answer("answer 1", Path("b.png")) a2 = exam2pdf.Answer("answer 2", Path("c.png")) a3 = exam2pdf.Answer("answer 3", Path("a.png")) q1.answers = (a1, a2, a3) q1.correct_option = "B" q2 = exam2pdf.Question( "question 2: correct is n. one", "subject 1", Path("a.png") ) a1 = exam2pdf.Answer("answer 1") a2 = exam2pdf.Answer("answer 2") a3 = exam2pdf.Answer("answer 3") q2.answers = (a1, a2, a3) q3 = exam2pdf.TrueFalseQuest("question 3: correct is True (first)") a1 = exam2pdf.TrueFalseAnswer(True) a2 = exam2pdf.TrueFalseAnswer(False) q3.answers = (a1, a2) q4 = exam2pdf.Question("question 4: no answer", "subject 2", Path("b.png")) q5 = exam2pdf.TrueFalseQuest("question 5: correct is False (first))") a1 = exam2pdf.TrueFalseAnswer(False) a2 = exam2pdf.TrueFalseAnswer(True) q5.answers = (a1, a2) q6 = exam2pdf.Question( "question 6: correct is n. three", "subject 4", Path("c.png") ) a1 = exam2pdf.Answer("answer 1") a2 = exam2pdf.Answer("answer 2") a3 = exam2pdf.Answer("answer 3") a4 = exam2pdf.Answer("answer 4") q6.add_answer(a1) q6.add_answer(a2) q6.add_answer(a3, is_correct=True) q6.add_answer(a4) dummy_ex = exam2pdf.Exam(q1, q2, q3, q4, q5, q6) return dummy_ex
def test_exam_question(): question1 = exam2pdf.Question("mc quest1 text", "subject") question1.answers = ( exam2pdf.Answer("Q1 A1"), exam2pdf.Answer("Q1 A2"), exam2pdf.Answer("Q1 A3"), ) question2 = exam2pdf.Question("mc quest2 text", "subject") question2.answers = ( exam2pdf.Answer("Q2 A1"), exam2pdf.Answer("Q2 A2"), exam2pdf.Answer("Q2 A3"), ) ex = exam2pdf.Exam(question1, question2) assert ex.questions[0].answers[1].image == Path() assert ex.questions[0].correct_answer.text == "Q1 A1" assert ex.questions[1].text == "mc quest2 text"
def test_exam_mix_question(): question = exam2pdf.Question("mc quest1 text", "subject") question.answers = ( exam2pdf.Answer("Q1 A1"), exam2pdf.Answer("Q1 A2"), exam2pdf.Answer("Q1 A3"), ) truefalse_quest = exam2pdf.TrueFalseQuest("mc quest2 text", "subject") truefalse_quest.answers = ( exam2pdf.TrueFalseAnswer(False), exam2pdf.TrueFalseAnswer(True), ) ex = exam2pdf.Exam(question, truefalse_quest) assert ex.questions[0].answers[1].image == Path() assert ex.questions[0].correct_option == "A" assert ex.questions[1].text == "mc quest2 text" assert ex.questions[1].correct_answer.text == "Falso"
def dummy_exam(): q1, q2, q3, q4, q5 = ( exam2pdf.Question("q1 text", "q1 subject", Path("a.png"), 1), exam2pdf.Question("q2 text", "q2 subject", Path("b.png"), 2), exam2pdf.Question("q3 text", "q3 subject", Path("c.png"), 3), exam2pdf.Question("q4 text", "q4 subject", Path("a.png"), 4), exam2pdf.Question("q5 text", "q5 subject", Path("b.png"), 5), ) q1.answers = ( exam2pdf.Answer("q1 a1"), exam2pdf.Answer("q1 a2", Path("t1.jpg")), ) q1.correct_index = 1 q2.answers = ( exam2pdf.Answer("q2 a1", Path("t2.png")), exam2pdf.Answer("q2 a2"), ) return exam2pdf.Exam(q1, q2, q3, q4, q5)
def test_exam_add_path_parent1(tmp_path): """test with a file path """ image = Path("images/image.png") file_path = tmp_path / "A.txt" file_path.touch() q1 = exam2pdf.Question("q1 text", "") q1.answers = ( exam2pdf.Answer("a1 text", image), exam2pdf.Answer("a2 text", image), ) q2 = exam2pdf.Question("q2 text", "", image) q2.add_answer(exam2pdf.Answer("a3 text")) ex = exam2pdf.Exam(q1, q2) ex.add_path_parent(file_path) assert ex.questions[0].image == Path() assert ex.questions[0].answers[0].image == file_path.parent / image assert ex.questions[0].answers[1].image == file_path.parent / image assert ex.questions[1].image == file_path.parent / image assert ex.questions[1].answers[0].image == Path()
def test_from_csv_kwargs(weired_csv_file): """GIVEN a csv file WHEN a legitimate keyword argument for DictReader is used as from_csv THEN keyword argument is correctly applied""" fieldnames = ( "question", "subject", "image", "level", "A", "Ai", "B", "Bi", "C", "Ci", ) ex = exam2pdf.Exam() ex.from_csv(weired_csv_file, fieldnames=fieldnames, delimiter=";") assert ex.questions[0].text == "Q" assert ex.questions[0].level == 1