def _get_validation_result(email, codes_to_answers):
    result = _ValidationResult()

    summaries = ParticipantSummaryDao().get_by_email(email)
    if not summaries:
        result.add_error('No ParticipantSummary found for %r.' % email)
        return result
    if len(summaries) > 1:
        result.add_error('%d ParticipantSummary values found for %r.' %
                         (len(summaries), email))
        return result
    participant_id = summaries[0].participantId

    code_dao = CodeDao()
    qra_dao = QuestionnaireResponseAnswerDao()
    with qra_dao.session() as session:
        for code_string, answer_string in codes_to_answers.iteritems():
            result.tests_count += 1

            question_code = code_dao.get_code(PPI_SYSTEM, code_string)
            if not question_code:
                result.add_error(
                    'Could not find question code %r, skipping answer %r.' %
                    (code_string, answer_string))
                continue
            if question_code.codeType != CodeType.QUESTION:
                result.add_error(
                    'Code %r type is %s, not QUESTION; skipping.' %
                    (code_string, question_code.codeType))
                continue

            qras = qra_dao.get_current_answers_for_concepts(
                session, participant_id, [question_code.codeId])
            qra_values = set()
            for qra in qras:
                try:
                    qra_values.add(
                        _get_value_for_qra(qra, question_code, code_dao,
                                           session))
                except ValueError as e:
                    result.add_error(e.message)
                    continue

            if answer_string:
                expected_values = set(
                    _boolean_to_lower(v.strip())
                    for v in answer_string.split('|'))
            else:
                expected_values = set()
            if expected_values != qra_values:
                result.add_error(
                    '%s: Expected %s, found %s.' %
                    (question_code.value, _format_values(expected_values),
                     _format_values(qra_values)))
    return result
Ejemplo n.º 2
0
    def test_insert(self):
        participant_id = self.create_participant()
        questionnaire_id = self.create_questionnaire('questionnaire1.json')
        with open(data_path('questionnaire_response3.json')) as fd:
            resource = json.load(fd)

        # Sending response with the dummy participant id in the file is an error
        self.send_post(_questionnaire_response_url('{participant_id}'),
                       resource,
                       expected_status=httplib.NOT_FOUND)

        # Fixing participant id but not the questionnaire id is also an error
        resource['subject']['reference'] = \
            resource['subject']['reference'].format(participant_id=participant_id)
        self.send_post(_questionnaire_response_url(participant_id),
                       resource,
                       expected_status=httplib.BAD_REQUEST)

        # Fix the reference
        resource['questionnaire']['reference'] = \
            resource['questionnaire']['reference'].format(questionnaire_id=questionnaire_id)

        # Sending the response before the consent is an error.
        self.send_post(_questionnaire_response_url(participant_id),
                       resource,
                       expected_status=httplib.BAD_REQUEST)

        # After consent, the post succeeds
        self.send_consent(participant_id)
        response = self.send_post(_questionnaire_response_url(participant_id),
                                  resource)
        resource['id'] = response['id']
        # The resource gets rewritten to include the version
        resource['questionnaire'][
            'reference'] = 'Questionnaire/%s/_history/1' % questionnaire_id
        self.assertJsonResponseMatches(resource, response)

        # Do a get to fetch the questionnaire
        get_response = self.send_get(
            _questionnaire_response_url(participant_id) + "/" + response['id'])
        self.assertJsonResponseMatches(resource, get_response)

        code_dao = CodeDao()

        # Ensure we didn't create codes in the extra system
        self.assertIsNone(code_dao.get_code(PPI_EXTRA_SYSTEM, 'IgnoreThis'))

        name_of_child = code_dao.get_code("sys", "nameOfChild")
        birth_weight = code_dao.get_code("sys", "birthWeight")
        birth_length = code_dao.get_code("sys", "birthLength")
        vitamin_k_dose_1 = code_dao.get_code("sys", "vitaminKDose1")
        vitamin_k_dose_2 = code_dao.get_code("sys", "vitaminKDose2")
        hep_b_given = code_dao.get_code("sys", "hepBgiven")
        abnormalities_at_birth = code_dao.get_code("sys",
                                                   "abnormalitiesAtBirth")
        answer_dao = QuestionnaireResponseAnswerDao()
        with answer_dao.session() as session:
            code_ids = [
                code.codeId for code in [
                    name_of_child, birth_weight, birth_length,
                    vitamin_k_dose_1, vitamin_k_dose_2, hep_b_given,
                    abnormalities_at_birth
                ]
            ]
            current_answers = answer_dao.get_current_answers_for_concepts(session,\
                from_client_participant_id(participant_id), code_ids)
        self.assertEquals(7, len(current_answers))
        questionnaire = QuestionnaireDao().get_with_children(questionnaire_id)
        question_id_to_answer = {
            answer.questionId: answer
            for answer in current_answers
        }
        code_id_to_answer = {
            question.codeId:
            question_id_to_answer.get(question.questionnaireQuestionId)
            for question in questionnaire.questions
        }
        self.assertEquals("Cathy Jones",
                          code_id_to_answer[name_of_child.codeId].valueString)
        self.assertEquals(3.25,
                          code_id_to_answer[birth_weight.codeId].valueDecimal)
        self.assertEquals(44.3,
                          code_id_to_answer[birth_length.codeId].valueDecimal)
        self.assertEquals(44,
                          code_id_to_answer[birth_length.codeId].valueInteger)
        self.assertEquals(True,
                          code_id_to_answer[hep_b_given.codeId].valueBoolean)
        self.assertEquals(
            0, code_id_to_answer[abnormalities_at_birth.codeId].valueInteger)
        self.assertEquals(datetime.date(1972, 11, 30),
                          code_id_to_answer[vitamin_k_dose_1.codeId].valueDate)
        self.assertEquals(
            datetime.datetime(1972, 11, 30, 12, 34, 42),
            code_id_to_answer[vitamin_k_dose_2.codeId].valueDateTime)