Example #1
0
    def testGetResource_withAddedPackages_retrievesResource(self):
        vs_1 = value_set_pb2.ValueSet()
        vs_1.url.value = 'vs1'

        vs_2 = value_set_pb2.ValueSet()
        vs_2.url.value = 'vs2'

        package_1 = fhir_package.FhirPackage(
            package_info=mock.MagicMock(),
            structure_definitions=mock_resource_collection_containing([]),
            search_parameters=mock_resource_collection_containing([]),
            code_systems=mock_resource_collection_containing([]),
            value_sets=mock_resource_collection_containing([vs_1]),
        )
        package_2 = fhir_package.FhirPackage(
            package_info=mock.MagicMock(),
            structure_definitions=mock_resource_collection_containing([]),
            search_parameters=mock_resource_collection_containing([]),
            code_systems=mock_resource_collection_containing([]),
            value_sets=mock_resource_collection_containing([vs_2]),
        )

        manager = fhir_package.FhirPackageManager()
        manager.add_package(package_1)
        manager.add_package(package_2)

        self.assertEqual(manager.get_resource('vs1'), vs_1)
        self.assertEqual(manager.get_resource('vs2'), vs_2)
Example #2
0
  def testExpandValueSet_withIntensionalValueSet_makesExpectedCalls(
      self, mock_expand_extensional_value_set):
    mock_expand_extensional_value_set.return_value = None

    expanded_value_set = value_set_pb2.ValueSet()
    codes = [
        value_set_pb2.ValueSet.Expansion.Contains(
            system=datatypes_pb2.Uri(value='include-system-1'),
            version=datatypes_pb2.String(value='include-version-1'),
            code=datatypes_pb2.Code(value='code-1-2'),
        ),
        value_set_pb2.ValueSet.Expansion.Contains(
            system=datatypes_pb2.Uri(value='include-system-2'),
            version=datatypes_pb2.String(value='include-version-2'),
            code=datatypes_pb2.Code(value='code-2-1'),
        ),
    ]
    expanded_value_set.expansion.contains.extend(codes)

    mock_client = unittest.mock.MagicMock(
        spec=terminology_service_client.TerminologyServiceClient)
    mock_client.expand_value_set.return_value = expanded_value_set

    value_set = value_set_pb2.ValueSet()
    result = value_sets.expand_value_set(value_set, mock_client)
    self.assertCountEqual(result.expansion.contains, codes)
    def testValueSetExpansionFromTerminologyService_withMissingId_raisesError(
            self):
        value_set = value_set_pb2.ValueSet()

        with self.assertRaises(ValueError):
            client = terminology_service_client.TerminologyServiceClient({})
            client.expand_value_set(value_set)
Example #4
0
  def testValueSetToInsertStatement_withValueSet_buildsValidQuery(self):
    value_set = value_set_pb2.ValueSet()
    value_set.url.value = 'vs-url'
    value_set.version.value = 'vs-version'

    for code, system in (('c1', 's1'), ('c2', 's2')):
      coding = value_set.expansion.contains.add()
      coding.code.value = code
      coding.system.value = system

    table = build_valueset_codes_table()

    query = value_sets.valueset_codes_insert_statement_for(value_set, table)
    query_string = str(query.compile(compile_kwargs={'literal_binds': True}))
    self.assertEqual(query_string, (
        'INSERT INTO valueset_codes (valueseturi, valuesetversion, system, code) '
        'SELECT codes.valueseturi, codes.valuesetversion, codes.system, codes.code '
        '\nFROM ('
        "SELECT 'vs-url' AS valueseturi, 'vs-version' AS valuesetversion, 's1' AS system, 'c1' AS code "
        'UNION ALL '
        "SELECT 'vs-url' AS valueseturi, 'vs-version' AS valuesetversion, 's2' AS system, 'c2' AS code"
        ') AS codes '
        'LEFT OUTER JOIN valueset_codes '
        'ON codes.valueseturi = valueset_codes.valueseturi '
        'AND codes.valuesetversion = valueset_codes.valuesetversion '
        'AND codes.system = valueset_codes.system '
        'AND codes.code = valueset_codes.code '
        '\nWHERE valueset_codes.valueseturi IS NULL '
        'AND valueset_codes.valuesetversion IS NULL '
        'AND valueset_codes.system IS NULL '
        'AND valueset_codes.code IS NULL'))
    def testValueSetExpansionFromTerminologyService_withBadUrl_raisesError(
            self):
        value_set = value_set_pb2.ValueSet()
        value_set.id.value = 'an-id'
        value_set.url.value = 'mystery-url'

        with self.assertRaises(ValueError):
            client = terminology_service_client.TerminologyServiceClient({})
            client.expand_value_set(value_set)
Example #6
0
  def testValueSetExpansionForExtensionalSets_withIntensionalSet_raisesError(
      self):
    value_set = value_set_pb2.ValueSet()

    include = value_set.compose.include.add()
    filter_ = include.filter.add()
    filter_.op.value = 1
    filter_.value.value = 'medicine'
    self.assertIsNone(value_sets._expand_extensional_value_set(value_set))
Example #7
0
  def testValueSetExpansionForExtensionalSets_withExtensionalSet_expandsCodes(
      self):
    value_set = value_set_pb2.ValueSet()

    # Add an include set with three codes.
    include_1 = value_set.compose.include.add()
    include_1.version.value = 'include-version-1'
    include_1.system.value = 'include-system-1'

    code_1_1 = include_1.concept.add()
    code_1_1.code.value = 'code-1-1'

    code_1_2 = include_1.concept.add()
    code_1_2.code.value = 'code-1-2'

    code_1_3 = include_1.concept.add()
    code_1_3.code.value = 'code-1-3'

    # Add an include set with one code.
    include_2 = value_set.compose.include.add()
    include_2.version.value = 'include-version-2'
    include_2.system.value = 'include-system-2'

    code_2_1 = include_2.concept.add()
    code_2_1.code.value = 'code-2-1'

    # Add a copy of code_1_3 to the exclude set.
    exclude = value_set.compose.exclude.add()
    exclude.version.value = 'include-version-1'
    exclude.system.value = 'include-system-1'
    exclude_code = exclude.concept.add()
    exclude_code.code.value = 'code-1-3'

    result = value_sets._expand_extensional_value_set(value_set)
    expected = [
        value_set_pb2.ValueSet.Expansion.Contains(
            system=datatypes_pb2.Uri(value='include-system-1'),
            version=datatypes_pb2.String(value='include-version-1'),
            code=datatypes_pb2.Code(value='code-1-1'),
        ),
        value_set_pb2.ValueSet.Expansion.Contains(
            system=datatypes_pb2.Uri(value='include-system-1'),
            version=datatypes_pb2.String(value='include-version-1'),
            code=datatypes_pb2.Code(value='code-1-2'),
        ),
        value_set_pb2.ValueSet.Expansion.Contains(
            system=datatypes_pb2.Uri(value='include-system-2'),
            version=datatypes_pb2.String(value='include-version-2'),
            code=datatypes_pb2.Code(value='code-2-1'),
        ),
    ]
    self.assertCountEqual(result.expansion.contains, expected)
Example #8
0
  def testValueSetsFromFhirPackage_withValueSets_succeeds(self):
    definition = structure_definition_pb2.StructureDefinition()
    element = definition.differential.element.add()
    element.binding.value_set.value = 'http://hl7.org/fhir/ValueSet/financial-taskcode'

    another_definition = structure_definition_pb2.StructureDefinition()
    another_element = definition.differential.element.add()
    another_element.binding.value_set.value = 'http://hl7.org/fhir/ValueSet/action-participant-role'

    value_set = value_set_pb2.ValueSet()
    value_set.url.value = 'a-url'

    another_value_set = value_set_pb2.ValueSet()
    another_value_set.url.value = 'another-url'

    duplicate_value_set = value_set_pb2.ValueSet()
    duplicate_value_set.url.value = 'http://hl7.org/fhir/ValueSet/action-participant-role'

    package = fhir_package.FhirPackage(
        package_info=unittest.mock.MagicMock(),
        structure_definitions=[definition, another_definition],
        search_parameters=[],
        code_systems=[],
        value_sets=[value_set, another_value_set, duplicate_value_set],
    )

    result = get_resolver().value_sets_from_fhir_package(package)

    self.assertCountEqual(
        [vs.url.value for vs in result],
        [
            element.binding.value_set.value,
            another_element.binding.value_set.value,
            value_set.url.value,
            another_value_set.url.value,
        ],
    )
    def testValueSetExpansionFromTerminologyService_withNoPagination_retrievesCodes(
            self, mock_session):
        mock_session().headers = {}
        mock_session().__enter__().get('url').json.return_value = {
            'resourceType': 'ValueSet',
            'id': 'vs-id',
            'url': 'vs-url',
            'status': 'draft',
            'expansion': {
                'timestamp': '2022-02-14T15:51:40-05:00',
                'contains': [{
                    'code': 'code-1'
                }]
            }
        }
        # Reset to hide the get('url') call we made above.
        mock_session().__enter__().get.reset_mock()

        value_set = value_set_pb2.ValueSet()
        value_set.url.value = 'http://hl7.org/fhir/ValueSet/financial-taskcode'

        client = terminology_service_client.TerminologyServiceClient({})
        result = client.expand_value_set(value_set)

        # Ensure we called requests correctly.
        mock_session().__enter__().get.assert_called_once_with(
            'https://tx.fhir.org/r4/ValueSet/$expand',
            params={
                'offset': 0,
                'url': 'http://hl7.org/fhir/ValueSet/financial-taskcode'
            })
        self.assertEqual(mock_session().headers['Accept'], 'application/json')

        # Ensure we got the expected protos back.
        expected = [
            value_set_pb2.ValueSet.Expansion.Contains(),
        ]
        expected[0].code.value = 'code-1'
        self.assertCountEqual(result.expansion.contains, expected)
    def testValueSetExpansion_withValueSet_retrievesCodes(self, mock_session):
        mock_session().headers = {}
        mock_session().__enter__().get('url').json.side_effect = [
            {
                'resourceType': 'ValueSet',
                'id': 'vs-id',
                'url': 'vs-url',
                'status': 'draft',
                'expansion': {
                    'timestamp': '2022-02-14T15:51:40-05:00',
                    'offset': 0,
                    'total': 2,
                    'contains': [{
                        'code': 'code-1'
                    }]
                }
            },
            {
                'resourceType': 'ValueSet',
                'id': 'vs-id',
                'url': 'vs-url',
                'status': 'draft',
                'expansion': {
                    'timestamp': '2022-02-14T15:51:40-05:00',
                    'offset': 1,
                    'total': 2,
                    'contains': [{
                        'code': 'code-2'
                    }]
                }
            },
        ]
        # Reset to hide the get('url') call we made above.
        mock_session().__enter__().get.reset_mock()

        value_set = value_set_pb2.ValueSet()
        value_set.url.value = 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16'
        value_set.version.value = '1.0'

        client = terminology_service_client.TerminologyServiceClient(
            {'https://cts.nlm.nih.gov/fhir/': 'the-api-key'})
        result = client.expand_value_set(value_set)

        # Ensure we called requests correctly.
        self.assertEqual(mock_session().__enter__().get.call_args_list, [
            unittest.mock.call(
                'https://cts.nlm.nih.gov/fhir/r4/ValueSet/$expand',
                params={
                    'url': 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16',
                    'offset': 0,
                    'valueSetVerson': '1.0'
                }),
            unittest.mock.call(
                'https://cts.nlm.nih.gov/fhir/r4/ValueSet/$expand',
                params={
                    'url': 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16',
                    'offset': 1,
                    'valueSetVerson': '1.0'
                }),
        ])
        self.assertEqual(mock_session().auth, ('apikey', 'the-api-key'))
        self.assertEqual(mock_session().headers['Accept'], 'application/json')

        # Ensure we got the expected protos back.
        expected = [
            value_set_pb2.ValueSet.Expansion.Contains(),
            value_set_pb2.ValueSet.Expansion.Contains(),
        ]
        expected[0].code.value = 'code-1'
        expected[1].code.value = 'code-2'
        self.assertCountEqual(result.expansion.contains, expected)
Example #11
0
  def testValueSetToInsertStatement_withEmptyCodes_raisesError(self):
    value_set = value_set_pb2.ValueSet()
    table = build_valueset_codes_table()

    with self.assertRaises(ValueError):
      value_sets.valueset_codes_insert_statement_for(value_set, table)