def test_sql_add_firsts(self): """Creates a database table, inserts a row into it and creates a database copy of the database table. Queries last row_id, adds 1 to it and creates a Results object to pass it to the sql_insert_new_results. Adds 1 to the firsts variable with the sql_add_firsts function. Then queries the row with that row_id and checks that firsts value is plus 1. """ test_obj = TestVariables() selected_database = test_obj.selected_database selected_database_copy = test_obj.selected_database_copy self.setUpDatabase() self.setUpInsertRow(selected_database) self.setUpDatabaseCopy() row_id = self.setUpRowID(selected_database_copy) - 1 sql_m = SqlManager() new_question_obj_list = sql_m.sql_get_database_content(selected_database_copy) new_question_obj = new_question_obj_list[-1] firsts_before_update = new_question_obj.firsts sql_m.sql_add_firsts(row_id, selected_database_copy) new_question_obj_list = sql_m.sql_get_database_content(selected_database_copy) new_question_obj = new_question_obj_list[-1] firsts_after_update = new_question_obj.firsts firsts_should_right = 3 self.assertEqual(firsts_should_right, firsts_after_update) self.assertNotEqual(firsts_before_update, firsts_after_update) print("test_sql_subtract_firsts: successful") self.tearDownTable(selected_database) self.tearDownTable(selected_database_copy)
def insert_new_questions_from_file(*args): """Imports data from txt file into the selected_database table. *args: only needed to delete the gui entry_field after passing of data """ with open("import_questions.txt", encoding="utf8") as f: r = f.read() test_questions = r.split("Exercise") test_questions = test_questions[1:] questions_list = [] for i in range(0, len(test_questions)): questions_list.append(test_questions[i].split("\n\n")) for ii in range(0, len(questions_list)): states = [] answer_states = questions_list[ii][1] for i in range(0, len(answer_states)): answer_state = answer_states[i] if answer_state == "*": state = 1 states.append(state) elif answer_state == "-": state = 0 states.append(state) question = questions_list[ii][0].replace("\n", ".") answer1 = questions_list[ii][2].replace("\n", "."), answer2 = questions_list[ii][3].replace("\n", "."), answer3 = questions_list[ii][4].replace("\n", "."), answer4 = questions_list[ii][5].replace("\n", "."), answer5 = questions_list[ii][6].replace("\n", "."), right_answers = [ states[0], states[1], states[2], states[3], states[4] ] sql_m = SqlManager() table_content = sql_m.sql_get_database_content( SettingVariables().selected_database) if len(table_content) > 0: row_id = table_content[-1].row_id + 1 else: row_id = 1 variables = Question(row_id, question, answer1, answer2, answer3, answer4, answer5, right_answers) variables = [variables] sql_m = SqlManager() sql_m.sql_insert_new_questions( variables, SettingVariables().selected_database)
def setUpInsertRow(self, table_name): """Inserts a row into the passed table(can be database or database copy table).""" row_id = self.setUpRowID(table_name) new_question = Question(row_id, "q", "a1", "a2", "a3", "a4", "a5", [0, 1, 0, 1, 0], 2) new_question = [new_question] sql_m = SqlManager() sql_m.sql_insert_new_questions(new_question, table_name) query_should = [row_id, "q", "a1", "a2", "a3", "a4", "a5", "[0, 1, 0, 1, 0]", 2] query_question_objects = sql_m.sql_get_database_content(table_name) q_obj = query_question_objects[-1] query_real = [q_obj.row_id, q_obj.question, q_obj.answer1, q_obj.answer2, q_obj.answer3, q_obj.answer4, q_obj.answer5, q_obj.right_answers, q_obj.firsts] return query_real, query_should
def start_testing(self, table_name=SettingVariables().selected_database_copy): """Queries content of the selected_database_copy table and selects randomly some row. Creates a random sequence of 5 digits in range 0 and 4 to sort newly the possible answers as well as right_answers sequence. Then pass all variables to the testing gui. """ sql_m = SqlManager() all_questions = sql_m.sql_get_database_content(table_name) random_row_id = random.randint(1, len(all_questions)) random_answer_seq = random.sample([0, 1, 2, 3, 4], 5) question_vars = all_questions[random_row_id - 1] random_question = [ question_vars.row_id, question_vars.question, question_vars.answer1, question_vars.answer2, question_vars.answer3, question_vars.answer4, question_vars.answer5, question_vars.right_answers, question_vars.firsts ] row_id = random_question[0] question_var = random_question[1] answer_vars = random_question[2:7] answer_vars = [answer_vars[i] for i in random_answer_seq] # transform right_answer variable from str to list with 5 integers right_answers = random_question[7] right_answers = [ int(right_answers[i]) for i in range(1, len(right_answers), 3) ] # save the right_answer variable in same order as the random_answer_seq right_answers_new = [right_answers[i] for i in random_answer_seq] firsts = random_question[8] question_obj = Question(row_id, question_var, answer_vars[0], answer_vars[1], answer_vars[2], answer_vars[3], answer_vars[4], right_answers_new, firsts) self.gui_testing_menu(question_obj) return random_answer_seq, random_row_id, question_obj
def show_remaining_questions(self): """Opens a window which shows contents of selected_database_copy table.""" sql_m = SqlManager() database_copy = sql_m.sql_get_database_content( SettingVariables().selected_database_copy) self.gui_show_remaining_questions(database_copy)