Example #1
0
 def testGetAnswerChoicesForChoiceId(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     q_where = question_table.select().where(
         question_table.c.type_constraint_name == 'multiple_choice')
     question = connection.execute(q_where).first()
     question_id = question.question_id
     tcn = question.type_constraint_name
     seq = question.sequence_number
     mul = question.allow_multiple
     submission_exec = connection.execute(
         submission_insert(submitter='test_submitter',
                           submitter_email='*****@*****.**',
                           survey_id=survey_id))
     submission_id = submission_exec.inserted_primary_key[0]
     choices = get_choices(connection, question_id)
     the_choice = choices.first()
     connection.execute(answer_choice_insert(
         question_choice_id=the_choice.question_choice_id,
         answer_choice_metadata={},
         question_id=question_id,
         submission_id=submission_id,
         survey_id=survey_id, type_constraint_name=tcn, sequence_number=seq,
         allow_multiple=mul))
     gacfci = get_answer_choices_for_choice_id
     actual_choices = gacfci(connection, the_choice.question_choice_id)
     self.assertEqual(actual_choices.rowcount, 1)
Example #2
0
 def testAnswerChoiceInsertNoMetadata(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     q_where = question_table.select().where(
         question_table.c.type_constraint_name == 'multiple_choice')
     question = connection.execute(q_where).first()
     question_id = question.question_id
     tcn = question.type_constraint_name
     seq = question.sequence_number
     mul = question.allow_multiple
     submission_exec = connection.execute(submission_insert(
         submitter='test_submitter', submitter_email='*****@*****.**',
         survey_id=survey_id))
     submission_id = submission_exec.inserted_primary_key[0]
     choices = get_choices(connection, question_id)
     the_choice = choices.first()
     exec_stmt = connection.execute(answer_choice_insert(
         question_choice_id=the_choice.question_choice_id,
         answer_choice_metadata=None,
         question_id=question_id,
         submission_id=submission_id,
         survey_id=survey_id, type_constraint_name=tcn, sequence_number=seq,
         allow_multiple=mul))
     answer_id = exec_stmt.inserted_primary_key[0]
     self.assertIsNotNone(answer_id)
Example #3
0
def _create_choices(connection: Connection,
                    values: dict,
                    question_id: str,
                    submission_map: dict,
                    existing_question_id: str=None) -> Iterator:
    """
    Create the choices of a survey question. 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 values: the dictionary of values associated with the question
    :param question_id: the UUID of the question
    :param submission_map: a dictionary mapping old submission_id to new
    :param existing_question_id: the UUID of the existing question (if this is
                                 an update)
    :return: an iterable of the resultant choice fields
    """
    choices = values['choices']
    new_choices, updates = _determine_choices(connection, existing_question_id,
                                              choices)

    for number, choice in enumerate(new_choices):
        choice_dict = {
            'question_id': question_id,
            'survey_id': values['survey_id'],
            'choice': choice,
            'choice_number': number,
            'type_constraint_name': values['type_constraint_name'],
            'question_sequence_number': values['sequence_number'],
            'allow_multiple': values['allow_multiple']}
        executable = question_choice_insert(**choice_dict)
        exc = [('unique_choice_names', RepeatedChoiceError(choice))]
        result = execute_with_exceptions(connection, executable, exc)
        result_ipk = result.inserted_primary_key
        question_choice_id = result_ipk[0]

        if choice in updates:
            question_fields = {'question_id': question_id,
                               'type_constraint_name': result_ipk[2],
                               'sequence_number': result_ipk[3],
                               'allow_multiple': result_ipk[4],
                               'survey_id': values['survey_id']}
            for answer in get_answer_choices_for_choice_id(connection,
                                                           updates[choice]):
                answer_values = question_fields.copy()
                new_submission_id = submission_map[answer.submission_id]
                answer_values['question_choice_id'] = question_choice_id
                answer_values['submission_id'] = new_submission_id
                answer_metadata = answer.answer_choice_metadata
                answer_values['answer_choice_metadata'] = answer_metadata
                connection.execute(answer_choice_insert(**answer_values))

        yield question_choice_id