Example #1
0
 def testSurveyInsert(self):
     auth_user_id = connection.execute(auth_user_table.select().where(
         auth_user_table.c.email == 'test_email')).first().auth_user_id
     stmt = survey_insert(survey_title='test insert',
                          survey_metadata=None,
                          auth_user_id=auth_user_id)
     survey_id = connection.execute(stmt).inserted_primary_key[0]
     condition = survey_table.c.survey_title == 'test insert'
     get_stmt = connection.execute(
         survey_table.select().where(condition)).first()
     self.assertEqual(get_stmt.survey_id, survey_id)
Example #2
0
    def testGetFreeTitle(self):
        auth_user_id = connection.execute(auth_user_table.select().where(
            auth_user_table.c.email == 'test_email')).first().auth_user_id

        self.assertEqual(
            get_free_title(connection, 'test insert', auth_user_id),
            'test insert')

        stmt = survey_insert(survey_title='test insert',
                             survey_metadata={},
                             auth_user_id=auth_user_id)
        connection.execute(stmt)
        self.assertEqual(
            get_free_title(connection, 'test insert', auth_user_id),
            'test insert(1)')
        stmt2 = survey_insert(survey_title='test insert(1)',
                              survey_metadata={},
                              auth_user_id=auth_user_id)
        connection.execute(stmt2)
        self.assertEqual(
            get_free_title(connection, 'test insert', auth_user_id),
            'test insert(2)')
Example #3
0
def _create_survey(connection: Connection, data: dict) -> str:
    """
    Use the given connection to create a survey within a transaction. If
    this is an update to an existing survey, it will also copy over existing
    submissions.

    :param connection: the SQLAlchemy connection used for the transaction
    :param data: a JSON representation of the survey
    :return: the UUID of the survey in the database
    """
    is_update = 'survey_id' in data

    email = data['email']
    user_id = get_auth_user_by_email(connection, email).auth_user_id
    title = data['survey_title']
    data_q = data['questions']

    # First, create an entry in the survey table
    safe_title = get_free_title(connection, title, user_id)
    survey_values = {
        'auth_user_id': user_id,
        'survey_metadata': data['survey_metadata'],
        'survey_title': safe_title}
    executable = survey_insert(**survey_values)
    exc = [('survey_title_survey_owner_key',
            SurveyAlreadyExistsError(safe_title))]
    result = execute_with_exceptions(connection, executable, exc)
    survey_id = result.inserted_primary_key[0]

    # a map of old submission_id to new submission_id
    submission_map = None
    if is_update:
        submission_map = {
            entry[0]: entry[1] for entry in
            _copy_submission_entries(
                connection,
                data['survey_id'],
                survey_id,
                data['email'])
        }

    # Now insert questions.  Inserting branches has to come afterward so
    # that the question_id values actually exist in the tables.
    questions = list(_create_questions(connection, data_q, survey_id,
                                       submission_map=submission_map))
    if -1 not in set(q['question_to_sequence_number'] for q in questions):
        raise SurveyDoesNotEndError()
    _create_branches(connection, data_q, questions, survey_id)

    return survey_id
Example #4
0
 def testDeleteRecord(self):
     auth_user_id = connection.execute(auth_user_table.select().where(
         auth_user_table.c.email == 'test_email')).first().auth_user_id
     exec_stmt = connection.execute(survey_insert(
         survey_title='delete me',
         survey_metadata={},
         auth_user_id=auth_user_id))
     survey_id = exec_stmt.inserted_primary_key[0]
     connection.execute(delete_record(survey_table, 'survey_id', survey_id))
     condition = survey_table.c.survey_id == survey_id
     self.assertEqual(
         connection.execute(
             survey_table.select().where(condition)).rowcount,
         0)
Example #5
0
    def testUpdateRecord(self):
        auth_user_id = connection.execute(auth_user_table.select().where(
            auth_user_table.c.email == 'test_email')).first().auth_user_id
        exec_stmt = connection.execute(survey_insert(
            survey_title='update me',
            survey_metadata={},
            auth_user_id=auth_user_id))
        survey_id = exec_stmt.inserted_primary_key[0]
        connection.execute(update_record(survey_table, 'survey_id', survey_id,
                                         survey_title='updated'))
        condition = survey_table.c.survey_id == survey_id
        new_record = connection.execute(
            survey_table.select().where(condition)).first()
        self.assertEqual(new_record.survey_title, 'updated')
        self.assertNotEqual(new_record.survey_last_update_time,
                            new_record.created_on)

        connection.execute(update_record(
            survey_table, 'survey_id', survey_id,
            values_dict={'survey_title': 'update2'}))

        new_record = connection.execute(survey_table.select().where(
            condition)).first()
        self.assertEqual(new_record.survey_title, 'update2')

        self.assertRaises(TypeError, update_record, survey_table,
                          'survey_id',
                          survey_id,
                          values_dict={'survey_title': 'updated2'},
                          survey_title='updated3')
        self.assertRaises(TypeError, update_record, survey_table,
                          'survey_id',
                          survey_id)

        connection.execute(delete_record(survey_table, 'survey_id', survey_id))

        if __name__ == '__main__':
            unittest.main()