Skip to content

Commit

Permalink
fixed all unit test bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunwei Wang committed Aug 28, 2024
1 parent 8960202 commit b4b7555
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
32 changes: 27 additions & 5 deletions lib/fhir_models/bootstrap/common_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,30 @@ module ClassMethods
def load_igs(ig_file_name)
ig_loader = FHIR::Generator::IGLoader.new(ig_file_name)
@ig_resources = ig_loader.load
@cache = {}
end

def find_structure_definition(structure_defs, target_name)
return nil if target_name.nil?
return @cache[target_name] if @cache[target_name]

structure_defs.find do |sd|
definition = structure_defs.find do |sd|
sd['id'] == target_name || sd['name'] == target_name || sd['url'] == target_name
end

@cache[target_name] = create_structure_definition(definition) if definition
@cache[target_name]
end

# ----------------------------------------------------------------
# Types
# ----------------------------------------------------------------
def create_structure_definition(definition)
return nil if definition.nil?

module_version::StructureDefinition.new(definition)
end

# # ----------------------------------------------------------------
# # Types
# # ----------------------------------------------------------------

def primitive_types
@ig_resources.primitive_types
Expand Down Expand Up @@ -61,11 +72,22 @@ def extension_definition(extension_name)
end
deprecate :get_extension_definition, :extension_definition

# Get the basetype (String) for a given profile or extension.
def basetype(uri)
return nil if uri.nil?

defn = @ig_resources.profiles.detect { |x| x['url'] == uri } || @ig_resources.extension_definitions.detect { |x| x['url'] == uri }
return nil if defn.nil?

defn['type']
end
deprecate :get_basetype, :basetype

# Get the StructureDefinition for a given profile.
def profile(profile_url)
find_structure_definition(@ig_resources.profiles, profile_url)
end
deprecate :get_profile, :profi
deprecate :get_profile, :profile

def profiles_for_resource(resource_name)
return nil if resource_name.nil?
Expand Down
4 changes: 4 additions & 0 deletions lib/fhir_models/bootstrap/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'mime/types'
require 'yaml'
require 'bcp47'
require_relative '../r4/bootstrap/definitions'

module FHIR
class Model
Expand Down Expand Up @@ -254,9 +255,12 @@ def validate_reference_type(ref, meta, contained_here, errors)
meta['type_profiles'].each do |p|
basetype = p.split('/').last
matches_one_profile = true if ref.reference.include?(basetype)
break if matches_one_profile

# check profiled resources
profile_basetype = FHIR::Definitions.basetype(p)
matches_one_profile = true if profile_basetype && ref.reference.include?(profile_basetype)
break if matches_one_profile
end
matches_one_profile = true if meta['type_profiles'].include?('http://hl7.org/fhir/StructureDefinition/Resource')
if !matches_one_profile && ref.reference.start_with?('#')
Expand Down
20 changes: 10 additions & 10 deletions spec/lib/fhir_models/r4/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
name = 'string'

result = FHIR::Definitions.type_definition(name)
expect(result['id']).to eq(name)
expect(result['version']).to eq(fhir_version)
expect(result.id).to eq(name)
expect(result.version).to eq(fhir_version)
end

it 'returns R4 complex type definition' do
name = 'Address'

result = FHIR::Definitions.type_definition(name)
expect(result['id']).to eq(name)
expect(result['version']).to eq(fhir_version)
expect(result.id).to eq(name)
expect(result.version).to eq(fhir_version)
end
end

Expand All @@ -26,8 +26,8 @@
name = 'Patient'

result = FHIR::Definitions.resource_definition(name)
expect(result['id']).to eq(name)
expect(result['version']).to eq(fhir_version)
expect(result.id).to eq(name)
expect(result.version).to eq(fhir_version)
end
end

Expand All @@ -36,8 +36,8 @@
name = 'valueset-expression'

result = FHIR::Definitions.extension_definition(name)
expect(result['id']).to eq(name)
expect(result['version']).to eq(fhir_version)
expect(result.id).to eq(name)
expect(result.version).to eq(fhir_version)
end
end

Expand All @@ -46,8 +46,8 @@
url = 'http://hl7.org/fhir/StructureDefinition/vitalsigns'

result = FHIR::Definitions.profile(url)
expect(result['url']).to eq(url)
expect(result['version']).to eq(fhir_version)
expect(result.url).to eq(url)
expect(result.version).to eq(fhir_version)
end

it 'returns R4 profiles by resource type' do
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/fhir_models/r4b/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
describe '#resource_definition' do
it 'returns R4B resource definition' do
result = FHIR::R4B::Definitions.resource_definition('Patient')
expect(result['id']).to eq('Patient')
expect(result['version']).to eq('4.3.0')
expect(result.id).to eq('Patient')
expect(result.version).to eq('4.3.0')
end
end
end

0 comments on commit b4b7555

Please sign in to comment.