def testQuestionBranchInsert(self): survey_id = connection.execute(survey_table.select().where( survey_table.c.survey_title == 'test_title')).first().survey_id to_question = get_questions_no_credentials( connection, survey_id).fetchall()[-1] q_where = question_table.select().where( cast(cast(question_table.c.logic['allow_other'], Text), Boolean)) from_question = connection.execute(q_where).fetchall()[0] choice = get_choices( connection, from_question.question_id).fetchall()[0] from_tcn = from_question.type_constraint_name branch_dict = {'question_choice_id': choice.question_choice_id, 'from_question_id': from_question.question_id, 'from_type_constraint': from_tcn, 'from_sequence_number': from_question.sequence_number, 'from_allow_multiple': from_question.allow_multiple, 'from_survey_id': survey_id, 'to_question_id': to_question.question_id, 'to_type_constraint': to_question.type_constraint_name, 'to_sequence_number': to_question.sequence_number, 'to_allow_multiple': to_question.allow_multiple, 'to_survey_id': survey_id} branch_exec = connection.execute(question_branch_insert(**branch_dict)) inserted_id = branch_exec.inserted_primary_key[0] the_branch = connection.execute(question_branch_table.select().where( question_branch_table.c.question_branch_id == inserted_id)).first() self.assertEqual(the_branch.to_question_id, to_question.question_id)
def _create_branches(connection: Connection, questions_json: list, question_dicts: list, survey_id: str): """ Create the branches in a survey. :param connection: the SQLAlchemy Connection object for the transaction :param questions_json: a list of dictionaries coming from the JSON input :param question_dicts: a list of dictionaries resulting from inserting the questions :param survey_id: the UUID of the survey """ for index, question_dict in enumerate(questions_json): from_dict = question_dicts[index] from_q_id = from_dict['question_id'] branches = question_dict['branches'] if branches is None: continue for branch in branches: choice_index = branch['choice_number'] question_choice_id = from_dict['choice_ids'][choice_index] from_tcn = question_dict['type_constraint_name'] from_mul = from_dict['allow_multiple'] to_question_index = branch['to_question_number'] - 1 to_question_id = question_dicts[to_question_index]['question_id'] to_tcn = question_dicts[to_question_index]['type_constraint_name'] to_seq = question_dicts[to_question_index]['sequence_number'] to_mul = question_dicts[to_question_index]['allow_multiple'] branch_dict = {'question_choice_id': question_choice_id, 'from_question_id': from_q_id, 'from_type_constraint': from_tcn, 'from_sequence_number': index + 1, 'from_allow_multiple': from_mul, 'from_survey_id': survey_id, 'to_question_id': to_question_id, 'to_type_constraint': to_tcn, 'to_sequence_number': to_seq, 'to_allow_multiple': to_mul, 'to_survey_id': survey_id} executable = question_branch_insert(**branch_dict) exc = [('question_branch_from_question_id_question_choice_id_key', MultipleBranchError(question_choice_id))] execute_with_exceptions(connection, executable, exc)