示例#1
0
 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)
示例#2
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)
示例#3
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)
示例#4
0
    def testQuestionChoiceSelect(self):
        q_where = question_table.select().where(
            question_table.c.type_constraint_name == 'multiple_choice')
        question_id = connection.execute(q_where).first().question_id
        choice_id = get_choices(connection,
                                question_id).first().question_choice_id
        choice = question_choice_select(connection, choice_id)
        self.assertIsNotNone(choice)

        self.assertRaises(QuestionChoiceDoesNotExistError,
                          question_choice_select, connection,
                          str(uuid.uuid4()))
示例#5
0
def _determine_choices(connection: Connection,
                       existing_question_id: str,
                       choices: list) -> tuple:
    """
    Pre-process the choices coming from the survey JSON to determine which
    choices to insert and which are updates.

    :param connection: a SQLAlchemy Connection
    :param existing_question_id: the UUID of the existing question (if this is
                                 an update) or None otherwise
    :param choices: the list of choices from the JSON submission
    :return: a tuple of (list of new choices, dictionary of new choice : id of
             old choice)
    :raise RepeatedChoiceError: if a choice is supplied more than once
    :raise QuestionChoiceDoesNotExistError: if an old_choice supplied does not
                                            exist
    """
    # the choices associated with the existing question
    old_choices = []
    if existing_question_id is not None:
        old_choices = get_choices(connection, existing_question_id)
    # a dictionary of choice text : choice id
    old_choice_dict = {ch.choice: ch.question_choice_id for ch in old_choices}
    # the choices to be inserted
    new_choices = []
    # a dictionary of new_choice : choice id
    updates = {}
    old_choice_repeats = set()
    if choices is None:
        return new_choices, updates
    for entry in choices:
        try:
            # choice update
            old_choice = entry['old_choice']
            if old_choice not in old_choice_dict:
                raise QuestionChoiceDoesNotExistError(old_choice)
            if old_choice in old_choice_repeats:
                raise RepeatedChoiceError(entry)
            old_choice_repeats.add(old_choice)
            new_choice = entry['new_choice']
            new_choices.append(new_choice)
            updates[new_choice] = old_choice_dict[old_choice]
        except TypeError:
            # new choice entry
            new_choices.append(entry)
            if entry in old_choice_dict:
                updates[entry] = old_choice_dict[entry]
    new_choice_set = set(new_choices)
    if len(new_choice_set) != len(new_choices):
        raise RepeatedChoiceError(new_choices)
    return new_choices, updates
示例#6
0
def _get_fields(connection: Connection, question: RowProxy) -> dict:
    """
    Extract the relevant fields from a record in the question table.

    :param connection: a SQLAlchemy Connection
    :param question: A RowProxy for a record in the question table.
    :return: A dictionary of the fields.
    """
    result = {'question_id': question.question_id,
              'question_title': question.question_title,
              'hint': question.hint,
              'sequence_number': question.sequence_number,
              'question_to_sequence_number':
                  question.question_to_sequence_number,
              'allow_multiple': question.allow_multiple,
              'type_constraint_name': question.type_constraint_name,
              'logic': question.logic}
    if question.type_constraint_name == 'multiple_choice':
        choices = get_choices(connection, question.question_id)
        result['choices'] = [_get_choice_fields(choice) for choice in choices]
        branches = get_branches(connection, question.question_id)
        if branches.rowcount > 0:
            result['branches'] = [_get_branch_fields(brn) for brn in branches]
    return result
示例#7
0
 def testGetChoices(self):
     q_where = question_table.select().where(
         question_table.c.type_constraint_name == 'multiple_choice')
     question_id = connection.execute(q_where).first().question_id
     choices = get_choices(connection, question_id)
     self.assertGreater(choices.rowcount, 0)