# doesn't feel right to mess with $LOAD_PATH, but cannot require mabmapper without it $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__)) # silence that minitest related error about minitest/autorun (since minitest version >= 5) gem 'minitest' require 'mabmapper' require 'minitest/autorun' require 'nokogiri' require 'pry' # Extending Ruby's Module class simplifys tests massively class Module def define_field_test(doc_id, assertion_options) field_name = self.to_s.gsub(/\ATest/, '').underscore.to_sym expected_value = assertion_options.values.first define_method "test_#{field_name}_for_#{doc_id}" do assert_doc_field_value_equal(doc_id, field_name, expected_value) end end end def load_engine! begin engine_file = "mabmapper/aleph_mab_xml_engine" # TODO: Make me configurable require engine_file "#{engine_file}".classify.constantize.new rescue LoadError exit 1 end end def assert_doc_field_value_equal(doc_id, field_name, expected_value) doc = Nokogiri::XML(@engine.process('', load_mab("test_#{field_name}", doc_id)).to_xml) field_values = doc.css(field_name.to_s).map(&:text).map { |field_value| (field_value.first == '{' && field_value.last == '}') ? JSON.parse(field_value) : field_value } if field_values.length == 0 assert_equal expected_value, nil elsif field_values.length == 1 assert_equal expected_value, field_values.first.presence else assert_equal expected_value, field_values end end def load_mab(directory_name, record_id) mab_file_name = ["../mab_files/#{directory_name}/PAD01.#{record_id}.PRIMO.xml", "../mab_files/#{directory_name}/#{record_id}.xml"] .map { |path| File.expand_path path, __FILE__ } .find { |path| FileTest.exist? path } File.read(mab_file_name) end