Skip to content

Commit

Permalink
Merge 2790488 into 22698df
Browse files Browse the repository at this point in the history
  • Loading branch information
peetucket committed Jun 17, 2020
2 parents 22698df + 2790488 commit cc0cbcb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ RSpec/MultipleExpectations:
Max: 5
Exclude:
- 'spec/features/*'
- 'spec/services/discovery_report_spec.rb'

RSpec/NestedGroups:
Max: 4 # default: 3
Expand Down
12 changes: 11 additions & 1 deletion app/services/discovery_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ def each_row
summary[:total_size] += row.counts[:total_size]
summary[:objects_with_error] += 1 unless row.errors.empty?
row.counts[:mimetypes].each { |k, v| summary[:mimetypes][k] += v }
# log the output to a running progress file
File.open(batch.batch_context.progress_log_file, 'a') { |f| f.puts log_progress_info(dobj).to_yaml }
yield row
end
end

# @return [String] a different string each time
# return [Hash] progress info that will be logged as json in a running log file
def log_progress_info(dobj)
{
pid: dobj.pid,
timestamp: Time.now.strftime('%Y-%m-%d %H:%I:%S')
}
end

# @return [String] output_path to store report results, generate a different string each time
def output_path
Dir::Tmpname.create([self.class.name.underscore + '_', '.json'], batch.batch_context.output_dir) { |path| path }
end
Expand Down
26 changes: 22 additions & 4 deletions spec/services/discovery_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
RSpec.describe DiscoveryReport do
subject(:report) { described_class.new(batch) }

before do
Settings.job_output_parent_dir = 'tmp' # log to the tmp directory for tests
end

let(:batch) { batch_setup(:flat_dir_images) }

describe '#initialize' do
Expand All @@ -20,22 +24,36 @@
let(:validator1) { instance_double(ObjectFileValidator, counts: { total_size: 1, mimetypes: { a: 1, b: 2 } }, errors: {}) }
let(:validator2) { instance_double(ObjectFileValidator, counts: { total_size: 2, mimetypes: { b: 3, q: 4 } }, errors: {}) }
let(:validator3) { instance_double(ObjectFileValidator, counts: { total_size: 3, mimetypes: { q: 9 } }, errors: { foo: true }) }
let(:dig_obj1) { instance_double(PreAssembly::DigitalObject, pid: 'druid:1') }
let(:dig_obj2) { instance_double(PreAssembly::DigitalObject, pid: 'druid:2') }
let(:dig_obj3) { instance_double(PreAssembly::DigitalObject, pid: 'druid:3') }

it 'returns an Enumerable of Hashes' do
expect(report.each_row).to be_an(Enumerable)
end

it 'yields per objects_to_process, building an aggregate summary' do
expect(report).to receive(:process_dobj).with(1).and_return(validator1)
expect(report).to receive(:process_dobj).with(2).and_return(validator2)
expect(report).to receive(:process_dobj).with(3).and_return(validator3)
expect(batch).to receive(:objects_to_process).and_return([1, 2, 3])
# make sure that for the tests
# (a) the tmp job output dir exists for the progress log file to be written to
# (b) we get a new clean progress log file for the tests each time we run them
# In the actual application, `batch_context.output_dir_no_exists!` would get run and thus we would always have a unique folder created for each job
FileUtils.mkdir_p(batch.batch_context.output_dir)
FileUtils.rm(batch.batch_context.progress_log_file)
expect(report).to receive(:process_dobj).with(dig_obj1).and_return(validator1)
expect(report).to receive(:process_dobj).with(dig_obj2).and_return(validator2)
expect(report).to receive(:process_dobj).with(dig_obj3).and_return(validator3)
expect(batch).to receive(:objects_to_process).and_return([dig_obj1, dig_obj2, dig_obj3])
report.each_row { |_r| } # no-op
expect(report.summary).to match a_hash_including(
total_size: 6,
objects_with_error: 1,
mimetypes: { a: 1, b: 5, q: 13 }
)
expect(File).to exist(batch.batch_context.progress_log_file)
docs = YAML.load_stream(IO.read(batch.batch_context.progress_log_file))
expect(docs.size).to eq(3)
expect(docs[0][:pid]).to eq 'druid:1'
expect(docs[0][:timestamp].to_date).to eq Date.today
end
end

Expand Down

0 comments on commit cc0cbcb

Please sign in to comment.