require_relative 'naming' require_relative 'special_cases' module IpaTestKit class Generator class ValidationTestGenerator class << self def generate(ig_metadata) ig_metadata.groups .reject { |group| SpecialCases.exclude_resource? group.resource } .each do |group| new(group).generate next unless group.resource == 'MedicationRequest' # The Medication validation test lives in the MedicationRequest # group, so we need to pass in that group's metadata medication_group_metadata = ig_metadata.groups.find { |group| group.resource == 'Medication' } new(medication_group_metadata, group).generate end end end attr_accessor :group_metadata, :medication_request_metadata def initialize(group_metadata, medication_request_metadata = nil) self.group_metadata = group_metadata self.medication_request_metadata = medication_request_metadata end def template @template ||= File.read(File.join(__dir__, 'templates', 'validation.rb.erb')) end def output @output ||= ERB.new(template).result(binding) end def base_output_file_name "#{class_name.underscore}.rb" end def output_file_directory File.join(__dir__, '..', 'generated', directory_name) end def output_file_name File.join(output_file_directory, base_output_file_name) end def directory_name Naming.snake_case_for_profile(medication_request_metadata || group_metadata) end def profile_identifier Naming.snake_case_for_profile(group_metadata) end def profile_url group_metadata.profile_url end def profile_name group_metadata.profile_name end def test_id "ipa_010_#{profile_identifier}_validation_test" end def class_name "#{Naming.upper_camel_case_for_profile(group_metadata)}ValidationTest" end def resource_type group_metadata.resource end def conformance_expectation read_interaction[:expectation] end def generate FileUtils.mkdir_p(output_file_directory) File.open(output_file_name, 'w') { |f| f.write(output) } test_metadata = { id: test_id, file_name: base_output_file_name } if resource_type == 'Medication' medication_request_metadata.add_test(test_metadata) else group_metadata.add_test(test_metadata) end end def description <<~DESCRIPTION #{description_intro} It verifies the presence of mandatory elements and that elements with required bindings contain appropriate values. CodeableConcept element bindings will fail if none of their codings have a code/system belonging to the bound ValueSet. Quantity, Coding, and code element bindings will fail if their code/system are not found in the valueset. DESCRIPTION end def description_intro if resource_type == 'Medication' <<~MEDICATION_INTRO This test verifies resources returned from previous tests conform to the [#{profile_name}](#{profile_url}). MEDICATION_INTRO else <<~GENERIC_INTRO This test verifies resources returned from the first search conform to the [#{profile_name}](#{profile_url}). GENERIC_INTRO end end end end end