Exemplo n.º 1
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'})
Exemplo n.º 2
0
 def _get_map_data(self, raw_answers: ResultProxy):
     for raw_answer in raw_answers:
         coord = get_geo_json(self.db, raw_answer)['coordinates']
         sub_id = raw_answer.submission_id
         subs = submission_api.get_one(self.db, sub_id,
                                       email=self.current_user)
         yield [coord[0], coord[1], json_encode(subs['result'])]
Exemplo n.º 3
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')
Exemplo n.º 4
0
def _jsonify(connection: Connection,
             answer: RowProxy,
             type_constraint_name: str) -> object:
    """
    This function returns a "nice" representation of an answer which can be
    serialized as JSON.

    :param connection: a SQLAlchemy Connection
    :param answer: a record from the answer table
    :param type_constraint_name: the type constraint name
    :return: the nice representation
    """
    if type_constraint_name in {'location', 'facility'}:
        return get_geo_json(connection, answer)['coordinates']
    elif type_constraint_name in {'date', 'time'}:
        return answer['answer_' + type_constraint_name].isoformat()
    elif type_constraint_name == 'decimal':
        return float(answer['answer_' + type_constraint_name])
    else:
        return answer['answer_' + type_constraint_name]