예제 #1
0
  def testInsert_rightParticipantId(self):
    self._make_summary()
    summary = ParticipantSummaryDao().get(self.participant.participantId)
    self.assertIsNone(summary.physicalMeasurementsStatus)
    with FakeClock(TIME_2):
      measurements = self.dao.insert(self._make_physical_measurements())

    expected_measurements = PhysicalMeasurements(
        physicalMeasurementsId=1,
        participantId=self.participant.participantId,
        resource=self._with_id(self.measurement_json, '1'),
        created=TIME_2,
        finalized=TIME_1,
        final=True,
        logPositionId=1,
        createdSiteId=1,
        finalizedSiteId=2)
    self.assertEquals(expected_measurements.asdict(), measurements.asdict())
    measurements = self.dao.get(measurements.physicalMeasurementsId)
    self.assertEquals(expected_measurements.asdict(), measurements.asdict())
    # Completing physical measurements changes the participant summary status
    summary = ParticipantSummaryDao().get(self.participant.participantId)
    self.assertEquals(PhysicalMeasurementsStatus.COMPLETED, summary.physicalMeasurementsStatus)
    self.assertEquals(TIME_2, summary.physicalMeasurementsTime)
    self.assertEquals(TIME_2, summary.lastModified)
예제 #2
0
 def _make_physical_measurements(self, **kwargs):
     """Makes a new PhysicalMeasurements (same values every time) with valid/complete defaults.
 Kwargs pass through to PM constructor, overriding defaults.
 """
     for k, default_value in (('physicalMeasurementsId',
                               1), ('participantId',
                                    self.participant.participantId),
                              ('resource', self.pm_json),
                              ('createdSiteId', self.site.siteId),
                              ('finalizedSiteId', self.site.siteId)):
         if k not in kwargs:
             kwargs[k] = default_value
     return PhysicalMeasurements(**kwargs)
예제 #3
0
    def test_schema(self):
        session = self.database.make_session()

        hpo = HPO(hpoId=1,
                  name='UNSET',
                  displayName='No organization set',
                  organizationType=OrganizationType.UNSET)
        code_book = CodeBook(codeBookId=1,
                             created=datetime.datetime.now(),
                             latest=True,
                             name="pmi",
                             system="http://foo/bar",
                             version="v0.1.1")
        session.add(hpo)
        session.add(code_book)
        session.commit()

        organization = Organization(organizationId=1,
                                    externalId='org',
                                    displayName='Organization',
                                    hpoId=1)
        session.add(organization)
        session.commit()

        site = Site(siteId=1,
                    siteName='site',
                    googleGroup='*****@*****.**',
                    mayolinkClientNumber=12345,
                    organizationId=1)
        code1 = Code(codeId=1,
                     codeBookId=1,
                     system="a",
                     value="b",
                     shortValue="q",
                     display=u"c",
                     topic=u"d",
                     codeType=CodeType.MODULE,
                     mapped=True,
                     created=datetime.datetime.now())
        codeHistory1 = CodeHistory(codeId=1,
                                   codeBookId=1,
                                   system="a",
                                   value="b",
                                   shortValue="q",
                                   display=u"c",
                                   topic=u"d",
                                   codeType=CodeType.MODULE,
                                   mapped=True,
                                   created=datetime.datetime.now())
        session.add(site)
        session.add(code1)
        session.add(codeHistory1)
        session.commit()

        code2 = Code(codeId=2,
                     codeBookId=1,
                     parentId=1,
                     system="a",
                     value="c",
                     display=u"X",
                     topic=u"d",
                     codeType=CodeType.QUESTION,
                     mapped=True,
                     created=datetime.datetime.now())
        codeHistory2 = CodeHistory(codeId=2,
                                   codeBookId=1,
                                   parentId=1,
                                   system="a",
                                   value="c",
                                   display=u"X",
                                   topic=u"d",
                                   codeType=CodeType.QUESTION,
                                   mapped=True,
                                   created=datetime.datetime.now())
        session.add(code2)
        session.add(codeHistory2)
        session.commit()

        code3 = Code(codeId=3,
                     codeBookId=1,
                     parentId=2,
                     system="a",
                     value="d",
                     display=u"Y",
                     topic=u"d",
                     codeType=CodeType.ANSWER,
                     mapped=False,
                     created=datetime.datetime.now())
        codeHistory3 = CodeHistory(codeId=3,
                                   codeBookId=1,
                                   parentId=2,
                                   system="a",
                                   value="d",
                                   display=u"Y",
                                   topic=u"d",
                                   codeType=CodeType.ANSWER,
                                   mapped=False,
                                   created=datetime.datetime.now())
        session.add(code3)
        session.add(codeHistory3)
        session.commit()

        session.commit()

        p = self._participant_with_defaults(
            participantId=1,
            version=1,
            biobankId=2,
            clientId='*****@*****.**',
            hpoId=hpo.hpoId,
            signUpTime=datetime.datetime.now(),
            lastModified=datetime.datetime.now())
        ps = self._participant_summary_with_defaults(
            participantId=1,
            biobankId=2,
            lastModified=datetime.datetime.now(),
            hpoId=hpo.hpoId,
            firstName=self.fake.first_name(),
            middleName=self.fake.first_name(),
            lastName=self.fake.last_name(),
            email=self.fake.email(),
            zipCode='78751',
            dateOfBirth=datetime.date.today(),
            genderIdentityId=1,
            consentForStudyEnrollment=QuestionnaireStatus.SUBMITTED,
            consentForStudyEnrollmentTime=datetime.datetime.now(),
            numBaselineSamplesArrived=2)
        p.participantSummary = ps
        session.add(p)
        ph = self._participant_history_with_defaults(
            participantId=1,
            biobankId=2,
            clientId='*****@*****.**',
            hpoId=hpo.hpoId,
            signUpTime=datetime.datetime.now(),
            lastModified=datetime.datetime.now())
        session.add(ph)
        session.commit()

        session.add(
            BiobankStoredSample(biobankStoredSampleId='WEB1234542',
                                biobankId=p.biobankId,
                                biobankOrderIdentifier='KIT',
                                test='1UR10',
                                confirmed=datetime.datetime.utcnow()))
        session.add(
            BiobankStoredSample(
                biobankStoredSampleId='WEB99999',  # Sample ID must be unique.
                biobankId=p.
                biobankId,  # Participant ID and test may be duplicated.
                biobankOrderIdentifier='KIT',
                test='1UR10',
                confirmed=datetime.datetime.utcnow()))

        pm = PhysicalMeasurements(physicalMeasurementsId=1,
                                  participantId=1,
                                  created=datetime.datetime.now(),
                                  resource='blah',
                                  final=False,
                                  logPosition=LogPosition())
        pm2 = PhysicalMeasurements(physicalMeasurementsId=2,
                                   participantId=1,
                                   created=datetime.datetime.now(),
                                   resource='blah',
                                   final=True,
                                   amendedMeasurementsId=1,
                                   logPosition=LogPosition())
        session.add(pm)
        session.add(pm2)
        session.commit()

        q1 = Measurement(measurementId=3,
                         physicalMeasurementsId=pm.physicalMeasurementsId,
                         codeSystem='codeSystem',
                         codeValue='codeValue',
                         measurementTime=datetime.datetime.now(),
                         valueCodeSystem='valueCodeSystem',
                         valueCodeValue='value3')
        session.add(q1)
        session.commit()

        m1 = Measurement(measurementId=1,
                         physicalMeasurementsId=pm.physicalMeasurementsId,
                         codeSystem='codeSystem',
                         codeValue='codeValue',
                         measurementTime=datetime.datetime.now(),
                         bodySiteCodeSystem='bodySiteCodeSystem',
                         bodySiteCodeValue='bodySiteCodeValue',
                         valueString='a',
                         valueDecimal=1.2,
                         valueUnit='cm',
                         valueCodeSystem='valueCodeSystem',
                         valueCodeValue='value',
                         valueDateTime=datetime.datetime.now(),
                         qualifierId=q1.measurementId)
        session.add(m1)
        session.commit()

        m2 = Measurement(measurementId=2,
                         physicalMeasurementsId=pm.physicalMeasurementsId,
                         codeSystem='codeSystem',
                         codeValue='codeValue',
                         measurementTime=datetime.datetime.now(),
                         valueCodeSystem='valueCodeSystem',
                         valueCodeValue='value2',
                         parentId=m1.measurementId,
                         qualifierId=q1.measurementId)
        session.add(m2)
        session.commit()

        q = Questionnaire(questionnaireId=1,
                          version=1,
                          created=datetime.datetime.now(),
                          lastModified=datetime.datetime.now(),
                          resource='what?')
        qh = QuestionnaireHistory(questionnaireId=1,
                                  version=1,
                                  created=datetime.datetime.now(),
                                  lastModified=datetime.datetime.now(),
                                  resource='what?')
        qh.questions.append(
            QuestionnaireQuestion(questionnaireQuestionId=1,
                                  questionnaireId=1,
                                  questionnaireVersion=1,
                                  linkId="1.2.3",
                                  codeId=2,
                                  repeats=True))
        qh.concepts.append(
            QuestionnaireConcept(questionnaireConceptId=1,
                                 questionnaireId=1,
                                 questionnaireVersion=1,
                                 codeId=1))
        session.add(q)
        session.add(qh)
        session.commit()

        qr = QuestionnaireResponse(questionnaireResponseId=1,
                                   questionnaireId=1,
                                   questionnaireVersion=1,
                                   participantId=1,
                                   created=datetime.datetime.now(),
                                   resource='blah')
        qr.answers.append(
            QuestionnaireResponseAnswer(questionnaireResponseAnswerId=1,
                                        questionnaireResponseId=1,
                                        questionId=1,
                                        endTime=datetime.datetime.now(),
                                        valueSystem='a',
                                        valueCodeId=3,
                                        valueDecimal=123,
                                        valueString=self.fake.first_name(),
                                        valueDate=datetime.date.today()))

        session.add(qr)
        session.commit()

        mv = MetricsVersion(metricsVersionId=1,
                            inProgress=False,
                            complete=True,
                            date=datetime.datetime.utcnow(),
                            dataVersion=1)
        session.add(mv)
        session.commit()

        mb = MetricsBucket(metricsVersionId=1,
                           date=datetime.date.today(),
                           hpoId='PITT',
                           metrics='blah')
        session.add(mb)
        session.commit()
    def from_client_json(resource_json, participant_id=None, **unused_kwargs):
        #pylint: disable=unused-argument
        measurements = []
        observations = []
        qualifier_map = {}
        created_site_id = None
        created_username = None
        finalized_site_id = None
        finalized_username = None
        for entry in resource_json['entry']:
            resource = entry.get('resource')
            if resource:
                resource_type = resource.get('resourceType')
                if resource_type == _OBSERVATION_RESOURCE_TYPE:
                    observations.append(
                        (entry['fullUrl'],
                         fhirclient.models.observation.Observation(resource)))
                elif resource_type == _COMPOSITION_RESOURCE_TYPE:
                    extensions = resource.get('extension', [])
                    if not extensions:
                        logging.warning(
                            'No extensions in composition resource (expected site info).'
                        )
                    for extension in extensions:
                        value_reference = extension.get('valueReference')
                        if value_reference:
                            url = extension.get('url')
                            if url == _CREATED_LOC_EXTENSION:
                                created_site_id = PhysicalMeasurementsDao.get_location_site_id(
                                    value_reference)
                            elif url == _FINALIZED_LOC_EXTENSION:
                                finalized_site_id = PhysicalMeasurementsDao.get_location_site_id(
                                    value_reference)
                            elif url not in _ALL_EXTENSIONS:
                                logging.warning(
                                    'Unrecognized extension URL: %r (should be one of %s)',
                                    url, _ALL_EXTENSIONS)
                        else:
                            logging.warning(
                                'No valueReference in extension, skipping: %r',
                                extension)
                    authors = resource.get('author')
                    for author in authors:
                        author_extension = author.get('extension')
                        reference = author.get('reference')
                        if author_extension and reference:
                            authoring_step = PhysicalMeasurementsDao.get_authoring_step(
                                author_extension)
                            if authoring_step == _FINALIZED_STATUS:
                                finalized_username = PhysicalMeasurementsDao.get_author_username(
                                    reference)
                            elif authoring_step == _CREATED_STATUS:
                                created_username = PhysicalMeasurementsDao.get_author_username(
                                    reference)
                else:
                    logging.warning(
                        'Unrecognized resource type (expected %r or %r), skipping: %r',
                        _OBSERVATION_RESOURCE_TYPE, _COMPOSITION_RESOURCE_TYPE,
                        resource_type)

        # Take two passes over the observations; once to find all the qualifiers and observations
        # without related qualifiers, and a second time to find all observations with related
        # qualifiers.
        for first_pass in [True, False]:
            for fullUrl, observation in observations:
                measurement = PhysicalMeasurementsDao.from_observation(
                    observation, fullUrl, qualifier_map, first_pass)
                if measurement:
                    measurements.append(measurement)
        return PhysicalMeasurements(participantId=participant_id,
                                    resource=json.dumps(resource_json),
                                    measurements=measurements,
                                    createdSiteId=created_site_id,
                                    createdUsername=created_username,
                                    finalizedSiteId=finalized_site_id,
                                    finalizedUsername=finalized_username)