def testNoLogic(self): survey_id = connection.execute(survey_table.select().where( survey_table.c.survey_title == 'test_title')).first().survey_id sequence_number = get_free_sequence_number(connection, survey_id) with self.assertRaises(TypeError) as exc: question_insert(hint=None, allow_multiple=None, logic=None, sequence_number=sequence_number, question_to_sequence_number=1, question_title='test insert', type_constraint_name='text', survey_id=survey_id) self.assertEqual(str(exc.exception), 'logic must not be None')
def testQuestionChoiceInsert(self): survey_id = connection.execute( survey_table.select().where( survey_table.c.survey_title == 'test_title' )).first().survey_id seq_number = get_free_sequence_number(connection, survey_id) stmt = question_insert(hint=None, allow_multiple=None, logic={ 'required': False, 'allow_other': False, 'allow_dont_know': False }, sequence_number=seq_number, question_title='test choice', type_constraint_name='multiple_choice', question_to_sequence_number=-1, survey_id=survey_id) question_id = connection.execute(stmt).inserted_primary_key[0] c_stmt = question_choice_insert(question_id=question_id, choice='test choice', choice_number=1, type_constraint_name='multiple_choice', question_sequence_number=seq_number, allow_multiple=False, survey_id=survey_id) choice_id = connection.execute(c_stmt).inserted_primary_key[0] cond = question_choice_table.c.question_id == question_id self.assertEqual(connection.execute( question_choice_table.select().where(cond) ).first().question_choice_id, choice_id)
def _create_questions(connection: Connection, questions: list, survey_id: str, submission_map: dict=None) -> Iterator: """ Create the questions of a survey. If this is an update to an existing survey, it will also copy over answers to the questions. :param connection: the SQLAlchemy Connection object for the transaction :param questions: a list of dictionaries, each containing the values associated with a question :param survey_id: the UUID of the survey :param submission_map: a dictionary mapping old submission_id to new :return: an iterable of the resultant question fields """ for number, question in enumerate(questions, start=1): values = question.copy() values['sequence_number'] = number values['survey_id'] = survey_id existing_q_id = values.pop('question_id', None) executable = question_insert(**values) tcn = values['type_constraint_name'] exceptions = [('question_type_constraint_name_fkey', TypeConstraintDoesNotExistError(tcn)), ('minimal_logic', MissingMinimalLogicError(values['logic']))] result = execute_with_exceptions(connection, executable, exceptions) result_ipk = result.inserted_primary_key q_id = result_ipk[0] choices = list(_create_choices(connection, values, q_id, submission_map=submission_map, existing_question_id=existing_q_id)) if existing_q_id is not None: question_fields = {'question_id': q_id, 'sequence_number': result_ipk[1], 'allow_multiple': result_ipk[2], 'type_constraint_name': result_ipk[3], 'survey_id': survey_id} for answer in get_answers_for_question(connection, existing_q_id): new_tcn = result_ipk[3] old_tcn = question_select(connection, existing_q_id).type_constraint_name if new_tcn != old_tcn: continue answer_values = question_fields.copy() answer_values['answer_metadata'] = answer.answer_metadata new_submission_id = submission_map[answer.submission_id] is_type_exception = _get_is_type_exception(answer) answer_values['is_type_exception'] = is_type_exception if is_type_exception: answer_values['answer'] = answer.answer_text else: answer_values['answer'] = answer['answer_' + new_tcn] allow_other = values['logic']['allow_other'] allow_dont_know = values['logic']['allow_dont_know'] with_type_exception = allow_other or allow_dont_know if new_tcn == 'multiple_choice' and not with_type_exception: continue answer_values['submission_id'] = new_submission_id connection.execute(answer_insert(**answer_values)) q_to_seq_number = values['question_to_sequence_number'] yield {'question_id': q_id, 'type_constraint_name': tcn, 'sequence_number': values['sequence_number'], 'allow_multiple': values['allow_multiple'], 'choice_ids': choices, 'question_to_sequence_number': q_to_seq_number}