def test_GET_config_by_date(self): t1 = datetime.datetime(2018, 1, 1) t2 = t1 + datetime.timedelta(days=1) # Use a custom config so the output is easier to parse on failure name = 'xxx' path = 'Config/{}'.format(name) conf = {'test_key': ['original', 'values']} # Create the configuration; verify it is created with FakeClock(t1): response = self.send_post(path, request_data=conf) self.assertEquals(conf, response) self.assertEquals(conf, config.load(name).configuration) with FakeClock(t2): # Alter the configuration; verify it is altered conf_A = {'test_key': ['new', 'values']} self.send_put(path, request_data=conf_A) response_A = self.send_get(path) self.assertEquals(conf_A, response_A) self.assertEquals(conf_A, config.load(name).configuration) # Fetch the first configuration; verify it is preserved response_B = self.send_get('{}?date={}'.format( path, t1.isoformat())) self.assertEquals(response_B, conf) self.assertEquals(response_B, config.load(name, date=t1).configuration)
def test_duplicate_consent_submission(self): """ Submit duplicate study enrollment questionnaires, so we can make sure a duplicate submission doesn't error out and the authored timestamp is updated. """ participant_id = self.create_participant() authored = datetime.datetime(2019, 3, 16, 1, 39, 33, tzinfo=pytz.utc) created = datetime.datetime(2019, 3, 16, 1, 51, 22) with FakeClock(created): self.send_consent(participant_id, authored=authored) summary = self.send_get( 'Participant/{0}/Summary'.format(participant_id)) self.assertEqual(parse(summary['consentForStudyEnrollmentTime']), created.replace(tzinfo=None)) self.assertEqual(parse(summary['consentForStudyEnrollmentAuthored']), authored.replace(tzinfo=None)) # submit consent questionnaire again with new timestamps. authored = datetime.datetime(2019, 3, 17, 1, 24, 16, tzinfo=pytz.utc) with FakeClock(datetime.datetime(2019, 3, 17, 1, 25, 58)): self.send_consent(participant_id, authored=authored) summary = self.send_get( 'Participant/{0}/Summary'.format(participant_id)) # created should remain the same as the first submission. self.assertEqual(parse(summary['consentForStudyEnrollmentTime']), created.replace(tzinfo=None)) self.assertEqual(parse(summary['consentForStudyEnrollmentAuthored']), authored.replace(tzinfo=None))
def test_set_pipeline_in_progress_while_in_progress(self): with FakeClock(TIME): self.metrics_version_dao.set_pipeline_in_progress() with FakeClock(TIME_2): with self.assertRaises(PreconditionFailed): # Until a day passes, setting the pipeline in progress will raise an error. self.metrics_version_dao.set_pipeline_in_progress() # After a day passes, break the lock. with FakeClock(TIME_3): self.metrics_version_dao.set_pipeline_in_progress() expected_mv = MetricsVersion(metricsVersionId=1, inProgress=False, complete=False, date=TIME, dataVersion=SERVING_METRICS_DATA_VERSION) self.assertEquals(expected_mv.asdict(), self.metrics_version_dao.get(1).asdict()) expected_mv2 = MetricsVersion(metricsVersionId=2, inProgress=True, complete=False, date=TIME_3, dataVersion=SERVING_METRICS_DATA_VERSION) self.assertEquals(expected_mv2.asdict(), self.metrics_version_dao.get(2).asdict())
def testInsert_duplicate(self): self._make_summary() with FakeClock(TIME_2): measurements = self.dao.insert(self._make_physical_measurements()) with FakeClock(TIME_3): measurements_2 = self.dao.insert(self._make_physical_measurements()) self.assertEquals(measurements.asdict(), measurements_2.asdict())
def test_pairing_at_different_levels(self): p = Participant() time = datetime.datetime(2016, 1, 1) with random_ids([1, 2]): with FakeClock(time): self.dao.insert(p) p.version = 1 p.siteId = 1 time2 = datetime.datetime(2016, 1, 2) with FakeClock(time2): self.dao.update(p) p2 = self.dao.get(1) ep = self._participant_with_defaults(participantId=1, version=2, biobankId=2, lastModified=time2, signUpTime=time, hpoId=PITT_HPO_ID, siteId=1, organizationId=PITT_ORG_ID, providerLink=p2.providerLink) self.assertEquals(ep.siteId, p2.siteId) # ensure that p2 get paired with expected awardee and organization from update(). self.assertEquals(ep.hpoId, p2.hpoId) self.assertEquals(ep.organizationId, p2.organizationId)
def test_delete_old_metrics(self): with FakeClock(TIME): self.metrics_version_dao.set_pipeline_in_progress() metrics_bucket_1 = MetricsBucket(metricsVersionId=1, date=datetime.date.today(), hpoId='', metrics='foo') metrics_bucket_2 = MetricsBucket(metricsVersionId=1, date=datetime.date.today(), hpoId=PITT, metrics='bar') self.metrics_bucket_dao.insert(metrics_bucket_1) self.metrics_bucket_dao.insert(metrics_bucket_2) # For up to 3 days, the metrics stay around. with FakeClock(TIME_4): self.metrics_version_dao.delete_old_versions() expected_mv = MetricsVersion( metricsVersionId=1, inProgress=True, complete=False, date=TIME, dataVersion=SERVING_METRICS_DATA_VERSION) expected_mv.buckets.append(metrics_bucket_1) expected_mv.buckets.append(metrics_bucket_2) self.assertEquals( expected_mv.asdict(follow=['buckets']), self.metrics_version_dao.get_with_children(1).asdict( follow=['buckets'])) # After 3 days, the metrics are gone. with FakeClock(TIME_5): self.metrics_version_dao.delete_old_versions() self.assertIsNone(self.metrics_version_dao.get_with_children(1))
def test_update_with_patch_cancel(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()) cancel = get_restore_or_cancel_info() with FakeClock(TIME_3): with PhysicalMeasurementsDao().session() as session: update = self.dao.update_with_patch( measurements.physicalMeasurementsId, session, cancel) self.assertEqual(update.status, PhysicalMeasurementsStatus.CANCELLED) self.assertEqual(update.reason, cancel['reason']) self.assertEqual(update.cancelledSiteId, 1) self.assertEqual(update.cancelledTime, TIME_3) self.assertEqual(update.cancelledUsername, cancel['cancelledInfo']['author']['value']) summary = ParticipantSummaryDao().get(self.participant.participantId) self.assertEqual(summary.physicalMeasurementsStatus, PhysicalMeasurementsStatus.CANCELLED) self.assertEqual(summary.physicalMeasurementsTime, None) self.assertEqual(summary.physicalMeasurementsFinalizedTime, TIME_1) self.assertEqual(summary.physicalMeasurementsCreatedSiteId, 1) self.assertEqual(summary.physicalMeasurementsFinalizedSiteId, 2)
def test_insert_second_codebook_different_system(self): code_book_1 = CodeBook(name="pmi", version="v1", system="a") with FakeClock(TIME): self.code_book_dao.insert(code_book_1) code_book_2 = CodeBook(name="pmi", version="v2", system="b") with FakeClock(TIME_2): self.code_book_dao.insert(code_book_2) expected_code_book = CodeBook(codeBookId=1, latest=True, created=TIME, name="pmi", version="v1", system="a") self.assertEquals(expected_code_book.asdict(), self.code_book_dao.get(1).asdict()) expected_code_book_2 = CodeBook(codeBookId=2, latest=True, created=TIME_2, name="pmi", version="v2", system="b") self.assertEquals(expected_code_book_2.asdict(), self.code_book_dao.get(2).asdict())
def test_participant_pairing_updates_onchange(self): provider_link = '[{"organization": {"reference": "Organization/AZ_TUCSON"}, "primary": true}]' TIME = datetime.datetime(2018, 1, 1) TIME2 = datetime.datetime(2018, 1, 2) insert_org = self.organization_dao.insert( Organization(externalId='tardis', displayName='bluebox', hpoId=PITT_HPO_ID)) with FakeClock(TIME): self.participant_dao.insert(Participant(participantId=1, biobankId=2)) participant = self.participant_dao.get(1) participant.organizationId = insert_org.organizationId self.participant_dao.update(participant) self.assertEquals(participant.hpoId, insert_org.hpoId) participant = self.participant_dao.get(1) p_summary = self.ps_dao.insert(self.participant_summary(participant)) with FakeClock(TIME2): insert_org.hpoId = AZ_HPO_ID self.organization_dao.update(insert_org) new_org = self.organization_dao.get_by_external_id('tardis') ps = self.ps_dao.get(p_summary.participantId) ph = self.ps_history.get([participant.participantId, 2]) participant = self.participant_dao.get(1) self.assertEquals(ps.lastModified, TIME2) self.assertEquals(ps.hpoId, new_org.hpoId) self.assertEquals(ph.hpoId, insert_org.hpoId) self.assertEquals(ph.organizationId, insert_org.organizationId) self.assertEquals(new_org.hpoId, participant.hpoId) self.assertEquals(new_org.organizationId, participant.organizationId) self.assertIsNone(participant.siteId) self.assertEquals(participant.providerLink, provider_link)
def testInsert_amend(self): self._make_summary() with FakeClock(TIME_2): measurements = self.dao.insert( self._make_physical_measurements(physicalMeasurementsId=1)) amendment_json = load_measurement_json_amendment( self.participant.participantId, measurements.physicalMeasurementsId, TIME_2) with FakeClock(TIME_3): new_measurements = self.dao.insert( self._make_physical_measurements( physicalMeasurementsId=2, resource=json.dumps(amendment_json))) measurements = self.dao.get(measurements.physicalMeasurementsId) amended_json = json.loads(measurements.resource) self.assertEquals('amended', amended_json['entry'][0]['resource']['status']) self.assertEquals('1', amended_json['id']) amendment_json = json.loads(new_measurements.resource) self.assertEquals('2', amendment_json['id']) self.assertTrue(new_measurements.final) self.assertEquals(TIME_3, new_measurements.created)
def test_make_pm_after_cancel_latest_pm(self): _id = self.participant_id.strip('P') self.send_consent(self.participant_id) measurement = load_measurement_json(self.participant_id) measurement2 = load_measurement_json(self.participant_id) path = 'Participant/%s/PhysicalMeasurements' % self.participant_id with FakeClock(self.time1): self.send_post(path, measurement) with FakeClock(self.time2): response2 = self.send_post(path, measurement2) # cancel latest PM path = path + '/' + response2['id'] cancel_info = get_restore_or_cancel_info() self.send_patch(path, cancel_info) response = self.send_get(path) self.assertEqual(response['status'], 'CANCELLED') self.assertEqual(response['reason'], 'a mistake was made.') self.assertEqual(response['cancelledUsername'], '*****@*****.**') self.assertEqual(response['cancelledSiteId'], 1) ps = self.send_get('ParticipantSummary?participantId=%s' % _id) # should still get first PM in participant summary self.assertEqual(ps['entry'][0]['resource']['physicalMeasurementsStatus'], 'COMPLETED') self.assertEqual(ps['entry'][0]['resource']['physicalMeasurementsCreatedSite'], 'hpo-site-monroeville') self.assertEqual(ps['entry'][0]['resource']['physicalMeasurementsFinalizedSite'], 'hpo-site-bannerphoenix') self.assertIsNotNone(ps['entry'][0]['resource']['physicalMeasurementsTime']) self.assertEquals(ps['entry'][0]['resource']['physicalMeasurementsTime'], self.time1.isoformat())
def testInsert_amend(self): self._make_summary() with FakeClock(TIME_2): measurements = self.dao.insert( self._make_physical_measurements(physicalMeasurementsId=1)) amendment_json = load_measurement_json_amendment( self.participant.participantId, measurements.physicalMeasurementsId, TIME_2) with FakeClock(TIME_3): new_measurements = self.dao.insert( self._make_physical_measurements( physicalMeasurementsId=2, resource=json.dumps(amendment_json))) measurements = self.dao.get(measurements.physicalMeasurementsId) amended_json = json.loads(measurements.resource) self.assertEquals('amended', amended_json['entry'][0]['resource']['status']) self.assertEquals('1', amended_json['id']) amendment_json = json.loads(new_measurements.resource) self.assertEquals('2', amendment_json['id']) self.assertTrue(new_measurements.final) self.assertEquals(TIME_3, new_measurements.created) ps_dao = ParticipantSummaryDao().get(self.participant.participantId) # An amendment should not add a distinct visit count to summary self.assertEqual(ps_dao.numberDistinctVisits, 1)
def test_updates_participant_summaries(self, mock_summary_job, mock_organization_job): mock_summary_job.return_value.__iter__.return_value = [[ self.EhrUpdatePidRow(11), ]] mock_organization_job.return_value.__iter__.return_value = [] with FakeClock(datetime.datetime(2019, 1, 1)): offline.update_ehr_status.update_ehr_status() mock_summary_job.return_value.__iter__.return_value = [[ self.EhrUpdatePidRow(11), self.EhrUpdatePidRow(12), ]] mock_organization_job.return_value.__iter__.return_value = [] with FakeClock(datetime.datetime(2019, 1, 2)): offline.update_ehr_status.update_ehr_status() summary = self.summary_dao.get(11) self.assertEqual(summary.ehrStatus, EhrStatus.PRESENT) self.assertEqual(summary.ehrReceiptTime, datetime.datetime(2019, 1, 1)) self.assertEqual(summary.ehrUpdateTime, datetime.datetime(2019, 1, 2)) summary = self.summary_dao.get(12) self.assertEqual(summary.ehrStatus, EhrStatus.PRESENT) self.assertEqual(summary.ehrReceiptTime, datetime.datetime(2019, 1, 2)) self.assertEqual(summary.ehrUpdateTime, datetime.datetime(2019, 1, 2))
def test_update_no_expected_version_with_ps(self): p = Participant() time = datetime.datetime(2016, 1, 1) with random_ids([1, 2]): with FakeClock(time): self.dao.insert(p) p.providerLink = make_primary_provider_link_for_name('PITT') time2 = datetime.datetime(2016, 1, 2) with FakeClock(time2): self.dao.update(p) summary = self.participant_summary(p) self.participant_summary_dao.insert(summary) # lastModified, hpoId, version is updated on p after being passed in p2 = self.dao.get(1) expected_participant = self._participant_with_defaults( participantId=1, version=2, biobankId=2, lastModified=time2, signUpTime=time, hpoId=PITT_HPO_ID, providerLink=p2.providerLink) self.assertEquals(expected_participant.asdict(), p2.asdict()) self.assertEquals(p.asdict(), p2.asdict()) # Updating the participant provider link also updates the HPO ID on the participant summary. ps = self.participant_summary_dao.get(1) expected_ps = self._participant_summary_with_defaults( participantId=1, biobankId=2, signUpTime=time, hpoId=PITT_HPO_ID, firstName=summary.firstName, lastName=summary.lastName, email=summary.email) self.assertEquals(expected_ps.asdict(), ps.asdict()) expected_ph = self._participant_history_with_defaults( participantId=1, biobankId=2, lastModified=time, signUpTime=time) # And updating the participant adds a new ParticipantHistory row. ph = self.participant_history_dao.get([1, 1]) self.assertEquals(expected_ph.asdict(), ph.asdict()) ph2 = self.participant_history_dao.get([1, 2]) expected_ph2 = self._participant_history_with_defaults( participantId=1, version=2, biobankId=2, lastModified=time2, signUpTime=time, hpoId=PITT_HPO_ID, providerLink=p2.providerLink) self.assertEquals(expected_ph2.asdict(), ph2.asdict())
def test_participant_race_answers(self): with FakeClock(TIME_1): participant_id = self.create_participant() self.send_consent(participant_id) questionnaire_id = self.create_questionnaire('questionnaire_the_basics.json') with open(data_path('questionnaire_the_basics_resp_multiple_race.json')) as f: resource = json.load(f) resource['subject']['reference'] = \ resource['subject']['reference'].format(participant_id=participant_id) resource['questionnaire']['reference'] = \ resource['questionnaire']['reference'].format(questionnaire_id=questionnaire_id) with FakeClock(TIME_2): resource['authored'] = TIME_2.isoformat() self.send_post(_questionnaire_response_url(participant_id), resource) code_dao = CodeDao() code1 = code_dao.get_code('http://terminology.pmi-ops.org/CodeSystem/ppi', 'WhatRaceEthnicity_White') code2 = code_dao.get_code('http://terminology.pmi-ops.org/CodeSystem/ppi', 'WhatRaceEthnicity_Hispanic') participant_race_answers_dao = ParticipantRaceAnswersDao() answers = participant_race_answers_dao.get_all() self.assertEqual(len(answers), 2) for answer in answers: self.assertIn(answer.codeId, [code1.codeId, code2.codeId]) # resubmit the answers, old value should be removed with open(data_path('questionnaire_the_basics_resp_multiple_race_2.json')) as f: resource = json.load(f) resource['subject']['reference'] = \ resource['subject']['reference'].format(participant_id=participant_id) resource['questionnaire']['reference'] = \ resource['questionnaire']['reference'].format(questionnaire_id=questionnaire_id) with FakeClock(TIME_2): resource['authored'] = TIME_2.isoformat() self.send_post(_questionnaire_response_url(participant_id), resource) code_dao = CodeDao() code1 = code_dao.get_code('http://terminology.pmi-ops.org/CodeSystem/ppi', 'WhatRaceEthnicity_NHPI') code2 = code_dao.get_code('http://terminology.pmi-ops.org/CodeSystem/ppi', 'PMI_PreferNotToAnswer') answers = participant_race_answers_dao.get_all() self.assertEqual(len(answers), 2) for answer in answers: self.assertIn(answer.codeId, [code1.codeId, code2.codeId])
def test_update_wrong_expected_version(self): q = Questionnaire(resource=RESOURCE_1) with FakeClock(TIME): self.dao.insert(q) q = Questionnaire(questionnaireId=1, version=2, resource=RESOURCE_2) with FakeClock(TIME_2): try: self.dao.update(q) self.fail("PreconditionFailed expected") except PreconditionFailed: pass
def test_get_ttl(self): with FakeClock(TIME_1): self.assertEquals(1, singletons.get(123, SingletonsTest.foo, 86401)) with FakeClock(TIME_2): self.assertEquals(1, singletons.get(123, SingletonsTest.foo, 86401)) with FakeClock(TIME_3): self.assertEquals(2, singletons.get(123, SingletonsTest.foo, 86401))
def test_creates_receipts(self, mock_summary_job, mock_organization_job): mock_summary_job.return_value.__iter__.return_value = [] mock_organization_job.return_value.__iter__.return_value = [ [ self.TableCountsRow(org_id='FOO_A', person_upload_time=datetime.datetime( 2019, 1, 1).replace(tzinfo=pytz.UTC)), ], ] with FakeClock(datetime.datetime(2019, 1, 1)): offline.update_ehr_status.update_ehr_status() foo_a_receipts = self.ehr_receipt_dao.get_by_organization_id( self.org_foo_a.organizationId) self.assertEqual(len(foo_a_receipts), 1) self.assertEqual(foo_a_receipts[0].receiptTime, datetime.datetime(2019, 1, 1)) foo_b_receipts = self.ehr_receipt_dao.get_by_organization_id( self.org_foo_b.organizationId) self.assertEqual(len(foo_b_receipts), 0) mock_summary_job.return_value.__iter__.return_value = [] mock_organization_job.return_value.__iter__.return_value = [ [ self.TableCountsRow(org_id='FOO_A', person_upload_time=datetime.datetime( 2019, 1, 1).replace(tzinfo=pytz.UTC)), self.TableCountsRow(org_id='FOO_A', person_upload_time=datetime.datetime( 2019, 1, 2).replace(tzinfo=pytz.UTC)), self.TableCountsRow(org_id='FOO_B', person_upload_time=datetime.datetime( 2019, 1, 2).replace(tzinfo=pytz.UTC)), ], ] with FakeClock(datetime.datetime(2019, 1, 2)): offline.update_ehr_status.update_ehr_status() foo_a_receipts = self.ehr_receipt_dao.get_by_organization_id( self.org_foo_a.organizationId) self.assertEqual(len(foo_a_receipts), 2) self.assertEqual(foo_a_receipts[0].receiptTime, datetime.datetime(2019, 1, 1)) self.assertEqual(foo_a_receipts[1].receiptTime, datetime.datetime(2019, 1, 2)) foo_b_receipts = self.ehr_receipt_dao.get_by_organization_id( self.org_foo_b.organizationId) self.assertEqual(len(foo_b_receipts), 1) self.assertEqual(foo_b_receipts[0].receiptTime, datetime.datetime(2019, 1, 2))
def test_update_wrong_expected_version(self): p = Participant() time = datetime.datetime(2016, 1, 1) with random_ids([1, 2]): with FakeClock(time): self.dao.insert(p) p.version = 2 p.providerLink = make_primary_provider_link_for_name('PITT') time2 = datetime.datetime(2016, 1, 2) with FakeClock(time2): with self.assertRaises(PreconditionFailed): self.dao.update(p)
def test_set_pipeline_finished_in_progress_no_buckets(self): with FakeClock(TIME): self.metrics_version_dao.set_pipeline_in_progress() with FakeClock(TIME_2): self.metrics_version_dao.set_pipeline_finished(True) expected_mv = MetricsVersion(metricsVersionId=1, inProgress=False, complete=True, date=TIME, dataVersion=SERVING_METRICS_DATA_VERSION) self.assertEquals(expected_mv.asdict(), self.metrics_version_dao.get(1).asdict()) self.assertEquals([], self.metrics_bucket_dao.get_active_buckets())
def test_metrics_update(self): self._create_data() with FakeClock(TIME): PublicMetricsExport.export('123') aggs1 = [a.asdict() for a in AggregateMetricsDao().get_all()] with FakeClock(TIME2): PublicMetricsExport.export('123') aggs2 = [a.asdict() for a in AggregateMetricsDao().get_all()] self.assertEquals(TIME2, MetricSetDao().get('123').lastModified) self.assertEquals(aggs1, aggs2)
def test_no_participant_to_mark(self): # make sure a csv with bad PIDS doesn't blow up. self._setup_participants() self._setup_file(wrong_pid=True) with FakeClock(TIME_3): exclude_ghost_participants.mark_ghost_participants()
def submit_questionnaire_response(self, participant_id, questionnaire_id, race_code, gender_code, first_name, middle_name, last_name, zip_code, state_code, street_address, city, sex_code, sexual_orientation_code, phone_number, recontact_method_code, language_code, education_code, income_code, date_of_birth, cabor_signature_uri): code_answers = [] _add_code_answer(code_answers, "race", race_code) _add_code_answer(code_answers, "genderIdentity", gender_code) _add_code_answer(code_answers, "state", state_code) _add_code_answer(code_answers, "sex", sex_code) _add_code_answer(code_answers, "sexualOrientation", sexual_orientation_code) _add_code_answer(code_answers, "recontactMethod", recontact_method_code) _add_code_answer(code_answers, "language", language_code) _add_code_answer(code_answers, "education", education_code) _add_code_answer(code_answers, "income", income_code) qr = make_questionnaire_response_json(participant_id, questionnaire_id, code_answers = code_answers, string_answers = [("firstName", first_name), ("middleName", middle_name), ("lastName", last_name), ("streetAddress", street_address), ("city", city), ("phoneNumber", phone_number), ("zipCode", zip_code)], date_answers = [("dateOfBirth", date_of_birth)], uri_answers = [("CABoRSignature", cabor_signature_uri)]) with FakeClock(TIME_1): self.send_post('Participant/%s/QuestionnaireResponse' % participant_id, qr)
def test_insert_with_external_id(self): p = Participant(externalId=3) time = datetime.datetime(2016, 1, 1) with random_ids([1, 2]): with FakeClock(time): self.dao.insert(p) expected_participant = self._participant_with_defaults( participantId=1, externalId=3, version=1, biobankId=2, lastModified=time, signUpTime=time) self.assertEquals(expected_participant.asdict(), p.asdict()) p2 = self.dao.get(1) self.assertEquals(p.asdict(), p2.asdict()) # Creating a participant also creates a ParticipantHistory row, but not a ParticipantSummary row ps = self.participant_summary_dao.get(1) self.assertIsNone(ps) ph = self.participant_history_dao.get([1, 1]) expected_ph = self._participant_history_with_defaults( participantId=1, externalId=3, biobankId=2, lastModified=time, signUpTime=time) self.assertEquals(expected_ph.asdict(), ph.asdict())
def test_insert_without_codebook_or_parent(self): code = Code(system="a", value="b", display=u"c", topic=u"d", codeType=CodeType.MODULE, mapped=True) with FakeClock(TIME): self.code_dao.insert(code) expected_code = Code(codeId=1, system="a", value="b", display=u"c", topic=u"d", codeType=CodeType.MODULE, mapped=True, created=TIME) self.assertEquals(expected_code.asdict(), self.code_dao.get(1).asdict()) expected_code_history = CodeHistory(codeHistoryId=1, codeId=1, system="a", value="b", display=u"c", topic=u"d", codeType=CodeType.MODULE, mapped=True, created=TIME) self.assertEquals(expected_code_history.asdict(), self.code_history_dao.get(1).asdict())
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)
def test_insert_both_names_and_email(self): self.insert_codes() p = Participant(participantId=1, biobankId=2) self.participant_dao.insert(p) self._setup_questionnaire() qr = QuestionnaireResponse(questionnaireResponseId=1, questionnaireId=1, questionnaireVersion=1, participantId=1, resource=QUESTIONNAIRE_RESPONSE_RESOURCE) qr.answers.extend(self._names_and_email_answers()) time = datetime.datetime(2016, 1, 1) with FakeClock(time): self.questionnaire_response_dao.insert(qr) expected_qr = QuestionnaireResponse( questionnaireResponseId=1, questionnaireId=1, questionnaireVersion=1, participantId=1, resource=with_id(QUESTIONNAIRE_RESPONSE_RESOURCE, 1), created=time) expected_qr.answers.extend(self._names_and_email_answers()) qr2 = self.questionnaire_response_dao.get(1) self.assertEquals(expected_qr.asdict(), qr2.asdict()) self.check_response(expected_qr)
def test_insert_both_names_and_email_and_login_phone_number(self): self.insert_codes() p = Participant(participantId=1, biobankId=2) self.participant_dao.insert(p) self._setup_questionnaire() qr = QuestionnaireResponse(questionnaireResponseId=1, questionnaireId=1, questionnaireVersion=1, participantId=1, resource=QUESTIONNAIRE_RESPONSE_RESOURCE) qr.answers.append(self.FN_ANSWER) qr.answers.append(self.LN_ANSWER) qr.answers.append(self.EMAIL_ANSWER) qr.answers.append(self.LOGIN_PHONE_NUMBER_ANSWER) time = datetime.datetime(2016, 1, 1) with FakeClock(time): qr.authored = time self.questionnaire_response_dao.insert(qr) expected_qr = QuestionnaireResponse( questionnaireResponseId=1, questionnaireId=1, questionnaireVersion=1, participantId=1, resource=with_id(QUESTIONNAIRE_RESPONSE_RESOURCE, 1), created=time, authored=time) expected_qr.answers.append(self.FN_ANSWER) expected_qr.answers.append(self.LN_ANSWER) expected_qr.answers.append(self.EMAIL_ANSWER) expected_qr.answers.append(self.LOGIN_PHONE_NUMBER_ANSWER) qr2 = self.questionnaire_response_dao.get(1) self.assertEquals(expected_qr.asdict(), qr2.asdict()) self.check_response(expected_qr)
def setup_participant(when, providerLink=self.provider_link): # Set up participant, questionnaire, and consent with FakeClock(when): participant = self.send_post('Participant', {"providerLink": [providerLink]}) participant_id = participant['participantId'] self.send_consent(participant_id) # Populate some answers to the questionnaire answers = { 'race': RACE_WHITE_CODE, 'genderIdentity': PMI_SKIP_CODE, 'firstName': self.fake.first_name(), 'middleName': self.fake.first_name(), 'lastName': self.fake.last_name(), 'zipCode': '78751', 'state': PMI_SKIP_CODE, 'streetAddress': '1234 Main Street', 'city': 'Austin', 'sex': PMI_SKIP_CODE, 'sexualOrientation': PMI_SKIP_CODE, 'phoneNumber': '512-555-5555', 'recontactMethod': PMI_SKIP_CODE, 'language': PMI_SKIP_CODE, 'education': PMI_SKIP_CODE, 'income': PMI_SKIP_CODE, 'dateOfBirth': datetime.date(1978, 10, 9), 'CABoRSignature': 'signature.pdf', } self.post_demographics_questionnaire(participant_id, questionnaire_id, time=when, **answers) return participant
def post_demographics_questionnaire(self, participant_id, questionnaire_id, cabor_signature_string=False, time=TIME_1, **kwargs): """POSTs answers to the demographics questionnaire for the participant""" answers = { 'code_answers': [], 'string_answers': [], 'date_answers': [('dateOfBirth', kwargs.get('dateOfBirth'))] } if cabor_signature_string: answers['string_answers'].append( ('CABoRSignature', kwargs.get('CABoRSignature'))) else: answers['uri_answers'] = [('CABoRSignature', kwargs.get('CABoRSignature'))] for link_id in self.code_link_ids: if link_id in kwargs: concept = Concept(PPI_SYSTEM, kwargs[link_id]) answers['code_answers'].append((link_id, concept)) for link_id in self.string_link_ids: code = kwargs.get(link_id) answers['string_answers'].append((link_id, code)) response_data = make_questionnaire_response_json( participant_id, questionnaire_id, **answers) with FakeClock(time): url = 'Participant/%s/QuestionnaireResponse' % participant_id return self.send_post(url, request_data=response_data)