begin require 'cover_me' rescue LoadError puts 'cover_me unavailable, running without code coverage measurement' end require 'bundler/setup' #require 'pry' PROJECT_ROOT = File.dirname(__FILE__) + '/../' require PROJECT_ROOT + 'lib/quality-measure-engine' Bundler.require(:test) ENV['DB_NAME'] = 'test' def reload_bundle(bundle_dir='.', measure_dir=ENV['MEASURE_DIR'] || 'measures') loader = QME::Database::Loader.new loader.drop_collection('bundles') loader.drop_collection('measures') loader.drop_collection('manual_exclusions') loader.save_bundle(bundle_dir, measure_dir) loader end def validate_measures(measure_dirs, loader) reload_bundle loader.get_db.collection('manual_exclusions').save({'measure_id'=>'test1', 'medical_record_id'=>'1234567890'}) measure_dirs.each do |dir| # check for sample data fixture_dir = File.join('fixtures', 'measures', File.basename(dir)) patient_files = Dir.glob(File.join(fixture_dir, 'patients', '*.json')) if patient_files.length==0 puts "Skipping #{dir}, no sample data in #{fixture_dir}" next end puts "Parsing #{dir}" loader.drop_collection('records') loader.drop_collection('query_cache') loader.drop_collection('patient_cache') # load measure from file system # this is innefficient, could just load it from DB as its already stored there measures = QME::Measure::Loader.load_measure(dir) # patients can include an optional test_id population identifier, extract this if present test_id = nil # load db with sample patient records patient_files.each do |patient_file| patient = JSON.parse(File.read(patient_file)) test_id ||= patient['test_id'] loader.save('records', patient) end # load expected results result_file = File.join('fixtures', 'measures', File.basename(dir), 'result.json') expected = JSON.parse(File.read(result_file)) # evaulate measure using Map/Reduce and validate results measures.each do |measure| measure_id = measure['id'] sub_id = measure['sub_id'] puts "Validating measure #{measure_id}#{sub_id}" executor = QME::MapReduce::Executor.new(measure_id, sub_id, 'effective_date'=>Time.gm(2010, 9, 19).to_i, 'test_id'=>test_id) executor.map_records_into_measure_groups result = executor.count_records_in_measure_groups if expected['initialPopulation'] == nil # multiple results for multi numerator/denominator measure # loop through list of results to find the matching one expected['results'].each do |expect| if expect['id'].eql?(measure_id) && (sub_id==nil || expect['sub_id'].eql?(sub_id)) result['population'].should match_population(expect['initialPopulation']) result['numerator'].should match_numerator(expect['numerator']) result['denominator'].should match_denominator(expect['denominator']) result['exclusions'].should match_exclusions(expect['exclusions']) (result['numerator']+result['antinumerator']).should eql(expect['denominator']) break end end else result['population'].should match_population(expected['initialPopulation']) result['numerator'].should match_numerator(expected['numerator']) result['denominator'].should match_denominator(expected['denominator']) result['exclusions'].should match_exclusions(expected['exclusions']) (result['numerator']+result['antinumerator']).should eql(expected['denominator']) end end puts ' - done' end end def validate_patient_mapping(loader) reload_bundle loader.drop_collection('records') loader.drop_collection('query_cache') loader.drop_collection('patient_cache') patient_file = File.join('fixtures', 'mapping', 'test1_numerator.json') patient = JSON.parse(File.read(patient_file)) loader.save('records', patient) executor = QME::MapReduce::Executor.new('test1', nil, 'effective_date'=>Time.gm(2010, 9, 19).to_i) result = executor.get_patient_result('patient1') result['population'].should be(true) result['numerator'].should be(true) result['denominator'].should be(true) result['exclusions'].should be(false) result['antinumerator'].should be(false) end