def test_add_questions_answers(self): question1, question2 = Question(1, 'q1'), Question(2, 'q2') answer1, answer2 = Answer(1, 'a1'), Answer(2, 'a2') questions, answers = [question1, question2], [[answer1], [answer2]] self.__poll_submission_object.add_questions_answers(questions, answers) expected_dict = {question1: [answer1], question2: [answer2]} actual_dict = self.__poll_submission_object.questions_answers self.assertEqual(expected_dict, actual_dict) # tests if poll submission was added to each question and answer for question, answers in actual_dict.items(): self.assertIn(self.__poll_submission_object, question.poll_submissions) for answer in answers: self.assertIn(self.__poll_submission_object, answer.poll_submissions)
def get_answers(self, poll_name, submission_questions, answers_list): submission_answers = [] # list of lists correct_answer = { answer.text: (answer, question) for question in self.__answer_keys[poll_name] for answer in self.__answer_keys[poll_name][question] } for i in range(len(submission_questions)): question = submission_questions[i] answers = answers_list[i] current_answers = [] for answer_text in answers: if answer_text in correct_answer and question == correct_answer[ answer_text][1]: current_answers.append(correct_answer[answer_text][0]) else: if answer_text in self.__wrong_answers[poll_name]: current_answers.append( self.__wrong_answers[poll_name][answer_text]) else: wrong_answer = Answer(answer_text) current_answers.append(wrong_answer) self.__wrong_answers[poll_name][ answer_text] = wrong_answer submission_answers.append(current_answers) return submission_answers
def __parse_answer_key(self, file_path): """ Parses a given an answer key file. """ with open(file_path) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for idx, row in enumerate(csv_reader): if row[1] == '': title = row[0] if title in self.__answer_keys: self.__poll_analysis_system.logger.info( f'Answer Key: Your loaded answer key is already loaded. System did not load it again.' ) return else: self.__poll_analysis_system.logger.info( f'Answer Key: {title} was parsed successfully.') self.__answer_keys.setdefault(title, {}) else: answers_text_list = row[1].split( ';') # a semicolon separates the multiple answers # # is a multiple choice question if more than one answer is given is_multiple_choice = True if len( answers_text_list) > 1 else False question = Question(row[0], is_multiple_choice) self.__answer_keys[title].setdefault(question, []) for answer_text in answers_text_list: answer = Answer(answer_text, is_correct=True) self.__answer_keys[title][question].append(answer)
def __parse_answer_key(self, file_path): """ Parses a given an answer key file. """ with open(file_path) as txt_file: lines = txt_file.readlines() for line_idx in range(2, len(lines), 1): if lines[line_idx] == '\n': continue line = lines[line_idx].replace('\n', '') if line.startswith(' '): # current line is a poll name title = line.split('\t')[0].strip().replace(':', ' ') self.__poll_analysis_system.logger.info( f'Answer Key: {title} was parsed successfully.') self.__answer_keys.setdefault(title, {}) elif not line.startswith('Answer'): question = line.split(' ') question_number = question[0] question_number = question_number.replace('.', '') question_text = ' '.join(question[1:-3]).strip() question_object = Question(question_number, question_text) if question[-2] == 'Multiple': question_object.is_multiple_choice = True self.__answer_keys[title].setdefault(question_object, []) else: answer = line.split(' ') answer_number = answer[1][0] answer_text = ' '.join(answer[2:]).strip() answer_object = Answer(answer_number, answer_text, True) self.__answer_keys[title][question_object].append( answer_object)
class TestAnswerMethods(unittest.TestCase): def setUp(self): self.__answer_object = Answer('an answer', True) def test_text(self): # test getter self.assertEqual(self.__answer_object.text, 'an answer') # test setter self.__answer_object.text = 'new answer' self.assertEqual(self.__answer_object.text, 'new answer') def test_is_correct(self): # test getter self.assertEqual(self.__answer_object.is_correct, True) # test setter self.__answer_object.is_correct = False self.assertEqual(self.__answer_object.is_correct, False) def test_add_poll_submissions(self): placeholder = 'Poll Submission Placeholder' self.__answer_object.add_poll_submission(placeholder) self.assertIn(placeholder, self.__answer_object.poll_submissions)
def read_answer_keys(self, answer_key_files): """ Reads all the answer key files and populates the dictionary with the parsed answer keys. """ for file_name in answer_key_files: try: self.__parse_answer_key(file_name) except: answer_key = ntpath.basename(file_name).split(".")[0] self.__poll_analysis_system.logger.error( f'The provided Answer Key: {answer_key} is not valid.') if 'attendance poll' not in self.__answer_keys: question = Question('1', 'Are you attending this lecture?') answer = Answer('1', 'Yes', True) self.__answer_keys['attendance poll'] = {question: [answer]}
def setUp(self): self.__answer_object = Answer(1, 'an answer', True)