def testRelativeReference(self): with io.open(os.path.join(filedir, 'test_relative_reference.json'), 'r', encoding='utf-8') as h: data = json.load(h) q = questionnaire.Questionnaire(data) self.assertIsNotNone(q, "Must instantiate Questionnaire") self.assertEqual('Questionnaire', q.resource_type) q._server = MockServer() group = q.item[0].item[0] self.assertEqual('Observation.subject', group.linkId) question = group.item[0] self.assertEqual('Observation.subject._type', question.linkId) self.assertIsNotNone(question.answerOption) with self.assertRaises(Exception): question.answerOption[0].valueReference.resolved() reference = question.answerOption[0].valueReference # resolve relative resource relative = reference.resolved(valueset.ValueSet) self.assertIsNotNone(relative, "Must resolve relative ValueSet") self.assertEqual('ValueSet', relative.resource_type) self.assertEqual('Type options for Observation.subject', relative.name) # 2nd resolve, should pull from cache relative = reference.resolved(medication.Medication) self.assertIsNone(relative, "Must not resolve on resource type mismatch") relative = reference.resolved(resource.Resource) self.assertIsNotNone( relative, "Must resolve relative ValueSet even if requesting `Resource`")
def instantiate_from(self, filename): datadir = os.environ.get('FHIR_UNITTEST_DATADIR') or \ os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'fhir-parser', 'downloads')) with io.open(os.path.join(datadir, filename), 'r', encoding='utf-8') as handle: js = json.load(handle) self.assertEqual("Questionnaire", js["resourceType"]) return questionnaire.Questionnaire(js)
def testQuestionnaire5(self): inst = self.instantiate_from("questionnaire-example-bluebook.json") self.assertIsNotNone( inst, "Must have instantiated a Questionnaire instance") self.implQuestionnaire5(inst) js = inst.as_json() self.assertEqual("Questionnaire", js["resourceType"]) inst2 = questionnaire.Questionnaire(js) self.implQuestionnaire5(inst2)
def testContainedResourceDetection(self): with io.open(os.path.join(filedir, 'test_contained_resource.json'), 'r', encoding='utf-8') as h: data = json.load(h) q = questionnaire.Questionnaire(data) self.assertIsNotNone(q, "Must instantiate Questionnaire") self.assertEqual('Questionnaire', q.resource_type) group = q.item[0].item[3] self.assertEqual('Observation.subject', group.linkId) question = group.item[0] self.assertEqual('Observation.subject._type', question.linkId) self.assertIsNotNone(question.answerOption) with self.assertRaises(Exception): question.answerOption[0].valueReference.resolved() reference = question.answerOption[0].valueReference # 1st resolve, extracting from contained resources contained = reference.resolved(medication.Medication) self.assertIsNone(contained, "Must not resolve on resource type mismatch") contained = reference.resolved(valueset.ValueSet) self.assertIsNotNone(contained, "Must resolve contained ValueSet") self.assertEqual('ValueSet', contained.resource_type) self.assertEqual('Type options for Observation.subject', contained.name) # 2nd resolve, should pull from cache contained = reference.resolved(medication.Medication) self.assertIsNone(contained, "Must not resolve on resource type mismatch") contained = reference.resolved(resource.Resource) self.assertIsNotNone( contained, "Must resolve contained ValueSet even if requesting `Resource`") contained = reference.resolved(valueset.ValueSet) self.assertIsNotNone(contained, "Must resolve contained ValueSet") self.assertEqual('ValueSet', contained.resource_type)