示例#1
0
 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)
示例#2
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)
示例#3
0
 def testQuestionSelect(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     question_id = get_questions_no_credentials(
         connection, survey_id).first().question_id
     question = question_select(connection, question_id)
     self.assertEqual(question.question_id, question_id)
示例#4
0
 def testInsertFacility(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 == 'facility')
     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]
     answer_exec = connection.execute(answer_insert(
         answer={'id': 'revisit ID', 'lon': 90, 'lat': 0},
         answer_metadata={'facility_name': 'cool facility',
                          'facility_sector': 'health'},
         question_id=question_id,
         submission_id=submission_id,
         survey_id=survey_id,
         type_constraint_name=tcn,
         is_type_exception=False,
         sequence_number=seq,
         allow_multiple=mul))
     answer_id = answer_exec.inserted_primary_key[0]
     self.assertIsNotNone(answer_id)
     condition = answer_table.c.answer_id == answer_id
     answer = connection.execute(
         answer_table.select().where(condition)).first()
     location = get_geo_json(connection, answer)['coordinates']
     self.assertEqual(location, [90, 0])
     facility_id = answer.answer_text
     self.assertEqual(facility_id, 'revisit ID')
示例#5
0
    def testGetSubmissionsByEmail(self):
        survey_id = connection.execute(survey_table.select().where(
            survey_table.c.survey_title == 'test_title')).first().survey_id
        for i in range(2):
            connection.execute(submission_insert(
                submitter='test_submitter{}'.format(i),
                submitter_email='*****@*****.**', survey_id=survey_id))

        submissions = get_submissions_by_email(
            connection, 'test_email', survey_id=survey_id
        )
        self.assertEqual(submissions.rowcount, 2)

        submissions = get_submissions_by_email(
            connection,
            'test_email',
            survey_id=survey_id,
            submitters=['test_submitter1']
        )
        self.assertEqual(submissions.rowcount, 1)

        submissions = get_submissions_by_email(
            connection,
            'test_email',
            survey_id=survey_id,
            order_by='submitter', direction='desc'
        )
        self.assertEqual(
            submissions.first()['submitter'],
            'test_submitter1'
        )
示例#6
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)
示例#7
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)
示例#8
0
 def testGetAnswersForQuestion(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 == 'integer')
     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]
     connection.execute(answer_insert(answer=1, question_id=question_id,
                                      answer_metadata={},
                                      submission_id=submission_id,
                                      survey_id=survey_id,
                                      type_constraint_name=tcn,
                                      is_type_exception=False,
                                      sequence_number=seq,
                                      allow_multiple=mul))
     self.assertEqual(
         get_answers_for_question(connection, question_id).rowcount, 1)
示例#9
0
 def testAnswerInsertNoMetadata(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 == 'integer')
     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]
     answer_exec = connection.execute(answer_insert(
         answer=1, question_id=question_id,
         answer_metadata=None,
         submission_id=submission_id,
         survey_id=survey_id,
         type_constraint_name=tcn,
         is_type_exception=False,
         sequence_number=seq,
         allow_multiple=mul))
     answer_id = answer_exec.inserted_primary_key[0]
     self.assertIsNotNone(answer_id)
示例#10
0
 def testDisplay(self):
     survey = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first()
     self.assertEqual(survey.survey_title,
                      display(connection,
                              survey.survey_id).survey_title)
     self.assertRaises(SurveyDoesNotExistError, display, connection,
                       str(uuid.uuid4()))
示例#11
0
 def tearDown(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]
     connection.execute(question_branch_table.delete().where(
         question_branch_table.c.to_question_id ==
         to_question.question_id))
示例#12
0
 def testGetSurveyIdFromPrefix(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     self.assertEqual(
         get_survey_id_from_prefix(connection, survey_id[:10]),
         survey_id)
     self.assertRaises(SurveyPrefixDoesNotIdentifyASurveyError,
                       get_survey_id_from_prefix, connection,
                       str(uuid.uuid4()))
示例#13
0
 def testGetSurveysForUserByEmail(self):
     user = connection.execute(auth_user_table.select().where(
         auth_user_table.c.email == 'test_email')).first()
     condition = survey_table.c.auth_user_id == user.auth_user_id
     surveys = connection.execute(survey_table.select().where(
         condition)).fetchall()
     surveys_by_email = get_surveys_by_email(connection, user.email)
     self.assertEqual(len(surveys), len(surveys_by_email))
     self.assertEqual(surveys[0].survey_id,
                      surveys_by_email[0].survey_id)
示例#14
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)
示例#15
0
    def testInsertLocation(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 == 'location')
        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]
        answer_exec = connection.execute(answer_insert(
            answer={'lon': 90, 'lat': 0},
            answer_metadata={},
            question_id=question_id,
            submission_id=submission_id,
            survey_id=survey_id,
            type_constraint_name=tcn,
            is_type_exception=False,
            sequence_number=seq,
            allow_multiple=mul))
        answer_id = answer_exec.inserted_primary_key[0]
        self.assertIsNotNone(answer_id)
        condition = answer_table.c.answer_id == answer_id
        answer = connection.execute(
            answer_table.select().where(condition)).first()
        location = get_geo_json(connection, answer)['coordinates']
        self.assertEqual(location, [90, 0])

        submission_2_exec = connection.execute(
            submission_insert(submitter='test_submitter',
                              submitter_email='*****@*****.**',
                              survey_id=survey_id))
        submission_2_id = submission_2_exec.inserted_primary_key[0]
        answer_2_exec = connection.execute(answer_insert(
            answer=None, question_id=question_id,
            answer_metadata={},
            submission_id=submission_2_id,
            survey_id=survey_id,
            type_constraint_name=tcn,
            is_type_exception=False,
            sequence_number=seq,
            allow_multiple=mul))
        answer_2_id = answer_2_exec.inserted_primary_key[0]
        condition_2 = answer_table.c.answer_id == answer_2_id
        answer_2 = connection.execute(
            answer_table.select().where(condition_2)).first()
        location_2 = get_geo_json(connection, answer_2)
        self.assertEqual(location_2,
                         {'coordinates': [], 'type': 'MultiPoint'})
示例#16
0
 def testSubmissionInsert(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     submission_exec = connection.execute(submission_insert(
         submitter='test_submitter', submitter_email='*****@*****.**',
         survey_id=survey_id))
     submission_id = submission_exec.inserted_primary_key[0]
     sub_exec = connection.execute(submission_table.select().where(
         submission_table.c.submission_id ==
         submission_id))
     submission = sub_exec.first()
     self.assertEqual(submission_id, submission.submission_id)
示例#17
0
 def testGetNumberOfSubmissions(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     submission_exec = connection.execute(
         submission_insert(submitter='test_submitter',
                           submitter_email='*****@*****.**',
                           survey_id=survey_id))
     submission_id = submission_exec.inserted_primary_key[0]
     connection.execute(submission_table.select().where(
         submission_table.c.submission_id ==
         submission_id))
     self.assertEqual(get_number_of_submissions(connection, survey_id), 1)
示例#18
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()
示例#19
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)
示例#20
0
 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')
示例#21
0
    def testGetQuestions(self):
        survey_id = connection.execute(survey_table.select().where(
            survey_table.c.survey_title == 'test_title')).first().survey_id
        questions = get_questions(connection, survey_id,
                                  email='test_email')
        self.assertGreater(questions.rowcount, 0)

        auth_user_id = get_auth_user_by_email(connection,
                                              'test_email').auth_user_id
        questions = get_questions(connection, survey_id,
                                  auth_user_id=auth_user_id)
        self.assertGreater(questions.rowcount, 0)

        self.assertRaises(TypeError, get_questions, connection, survey_id)
        self.assertRaises(TypeError, get_questions, connection, survey_id,
                          auth_user_id='',
                          email='')
示例#22
0
 def testGetSubmissionsWithFilter(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 == 'integer')
     question = connection.execute(q_where).first()
     question_id = question.question_id
     tcn = question.type_constraint_name
     seq = question.sequence_number
     mul = question.allow_multiple
     for i in range(2):
         submission_exec = connection.execute(submission_insert(
             submitter='test_submitter',
             submitter_email='*****@*****.**',
             survey_id=survey_id))
         submission_id = submission_exec.inserted_primary_key[0]
         connection.execute(answer_insert(
             answer=i,
             question_id=question_id,
             submission_id=submission_id,
             answer_metadata={},
             survey_id=survey_id,
             type_constraint_name=tcn,
             is_type_exception=False,
             sequence_number=seq,
             allow_multiple=mul))
     self.assertEqual(
         len(get_submissions_by_email(
             connection,
             'test_email',
             survey_id=survey_id
         ).fetchall()),
         2)
     f_result = get_submissions_by_email(
         connection,
         'test_email',
         survey_id=survey_id,
         filters=[
             {
                 'question_id': question_id,
                 'answer_integer': 1
             }
         ]
     ).fetchall()
     self.assertEqual(len(f_result), 1)
示例#23
0
 def testSurveySelect(self):
     user = connection.execute(auth_user_table.select().where(
         auth_user_table.c.email == 'test_email')).first()
     survey = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first()
     self.assertEqual(survey.survey_title,
                      survey_select(connection, survey.survey_id,
                                    auth_user_id=user.auth_user_id)
                      .survey_title)
     self.assertEqual(survey.survey_title,
                      survey_select(connection, survey.survey_id,
                                    email=user.email).survey_title)
     self.assertRaises(TypeError, survey_select, connection,
                       survey.survey_id,
                       auth_user_id=user.auth_user_id,
                       email=user.email)
     self.assertRaises(TypeError, survey_select, connection,
                       survey.survey_id)
示例#24
0
 def testSubmissionSelect(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     submission_exec = connection.execute(
         submission_insert(submitter='test_submitter',
                           submitter_email='*****@*****.**',
                           survey_id=survey_id))
     submission_id = submission_exec.inserted_primary_key[0]
     submission = submission_select(connection, submission_id,
                                    email='test_email')
     self.assertEqual(submission_id, submission.submission_id)
     user_id = connection.execute(auth_user_table.select().where(
         auth_user_table.c.email == 'test_email')).first().auth_user_id
     submission2 = submission_select(connection, submission_id,
                                     auth_user_id=user_id)
     self.assertEqual(submission_id,
                      submission2.submission_id)
     self.assertRaises(TypeError, submission_select, connection,
                       submission_id,
                       auth_user_id='', email='')
     self.assertRaises(TypeError, submission_select, connection,
                       submission_id)
示例#25
0
 def testGetEmailAddress(self):
     survey = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first()
     self.assertEqual(
         get_email_address(connection, survey.survey_id),
         'test_email')
示例#26
0
 def testGetQuestionsNoCredentials(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     questions = get_questions_no_credentials(connection, survey_id)
     self.assertGreater(questions.rowcount, 0)
示例#27
0
 def testGetFreeSequenceNumber(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'test_title')).first().survey_id
     self.assertEqual(
         get_free_sequence_number(connection, survey_id),
         11)
示例#28
0
 def testGetRequired(self):
     survey_id = connection.execute(survey_table.select().where(
         survey_table.c.survey_title == 'what is life')).first().survey_id
     reqs = get_required(connection, survey_id)
     self.assertEqual(reqs.rowcount, 2)