Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Jun 18, 2019
1 parent f0691aa commit 2e903a1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
5 changes: 2 additions & 3 deletions app/modules/lib_ptg_box/unmarshaller/marc.rb
Expand Up @@ -23,9 +23,8 @@ def doi # rubocop:disable Metrics/MethodLength
end
end

def to_mrc
# @entry.to_mrc
''
def to_marc
@entry.to_marc
end

def to_xml
Expand Down
6 changes: 5 additions & 1 deletion app/modules/lib_ptg_box/work.rb
Expand Up @@ -15,7 +15,11 @@ def name
end

def new?
!!!@selection.collection.marc(@doi) && !marc? # rubocop:disable Style/DoubleNegation
if marc?
!!!@selection.collection.marc(@doi) # rubocop:disable Style/DoubleNegation
else
false
end
end

def marc?
Expand Down
6 changes: 3 additions & 3 deletions app/programs/assemble_marc_files/assemble_marc_files.rb
Expand Up @@ -43,8 +43,9 @@ def append_selection_month_marc_file(selection, month) # rubocop:disable Metrics
next unless work.new?

if work.marc?
mrc_file << work.marc.to_mrc
mrc_file << work.marc.to_marc
xml_file << work.marc.to_xml
xml_file << "\n"
log += "Catalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{selection.collection.name}' is new!\n"
else
log += "Catalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{selection.collection.name}' is missing!\n"
Expand All @@ -62,8 +63,7 @@ def recreate_selection_marc_files(selection) # rubocop:disable Metrics/MethodLen
xml_file = File.open(filename + '.xml', 'w')
selection.works.each do |work|
if work.marc?
mrc_file << work.marc.to_mrc
mrc_file << "\n"
mrc_file << work.marc.to_marc
xml_file << work.marc.to_xml
xml_file << "\n"
else
Expand Down
3 changes: 2 additions & 1 deletion spec/modules/lib_ptg_box/unmarshaller/kbart_spec.rb
Expand Up @@ -5,9 +5,10 @@
RSpec.describe LibPtgBox::Unmarshaller::Kbart do
subject(:kbart) { described_class.new(line) }

let(:line) { instance_double(String, 'line') }
let(:line) { instance_double(String, 'line', encoding: 'encoding', valid_encoding?: true) }

before do
allow(line).to receive(:force_encoding).with('Windows-1252').and_return(line)
allow(line).to receive(:force_encoding).with('UTF-16BE').and_return(line)
allow(line).to receive(:encode).with('UTF-8').and_return(line)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/modules/lib_ptg_box/unmarshaller/marc_file_spec.rb
Expand Up @@ -6,12 +6,14 @@
subject(:marc_file) { described_class.new(box_file) }

let(:box_file) { instance_double(Box::File, 'box_file', content: content) }
let(:content) { instance_double(String, 'content') }
let(:content) { instance_double(String, 'content', encoding: 'encoding', valid_encoding?: true) }
let(:string_io) { instance_double(StringIO, 'string_io') }
let(:reader) { instance_double(MARC::XMLReader, 'reader') }
let(:entries) { [] }

before do
allow(content).to receive(:force_encoding).with('UTF-8').and_return(content)
allow(content).to receive(:encode).with('UTF-8').and_return(content)
allow(StringIO).to receive(:new).with(content).and_return(string_io)
allow(MARC::XMLReader).to receive(:new).with(string_io).and_return(reader)
allow(reader).to receive(:entries).and_return(entries)
Expand Down
8 changes: 4 additions & 4 deletions spec/modules/lib_ptg_box/unmarshaller/marc_spec.rb
Expand Up @@ -22,12 +22,12 @@
end
end

xdescribe '#to_mrc' do
subject { marc.to_mrc }
xdescribe '#to_marc' do
subject { marc.to_marc }

before { allow(entry).to receive(:to_mrc).and_return('mrc') }
before { allow(entry).to receive(:to_marc).and_return('marc') }

it { is_expected.to eq('') }
it { is_expected.to eq('marc') }
end

describe '#to_xml' do
Expand Down
47 changes: 25 additions & 22 deletions spec/programs/assemble_marc_files/assemble_marc_files_spec.rb
Expand Up @@ -54,7 +54,7 @@ def mock_client
let(:work_name) { 'Star Wars' }
let(:work_new) { false }
let(:work_marc) { false }
let(:marc) { instance_double(LibPtgBox::Unmarshaller::Marc, 'marc', to_mrc: 'mrc', to_xml: 'xml') }
let(:marc) { instance_double(LibPtgBox::Unmarshaller::Marc, 'marc', to_marc: 'marc', to_xml: 'xml') }
let(:umpebc_metadata) { 'UMPEBC Metadata' }

# before(:all) do
Expand All @@ -78,35 +78,36 @@ def mock_client
subject { program.execute }

before do
allow(program).to receive(:reset).and_return("reset\n")
allow(program).to receive(:synchronize).with(lib_ptg_box).and_return("synchronize\n")
allow(program).to receive(:assemble_marc_files).with(collection).and_return("collection\n")
end

it { is_expected.to eq("synchronize\n") }
it { is_expected.to eq("execute\nreset\nsynchronize\n") }

context 'when UMPEBC Metadata folder' do
let(:collection_name) { umpebc_metadata }

it { is_expected.to eq("synchronize\ncollection\n") }
it { is_expected.to eq("execute\nreset\nsynchronize\ncollection\n") }
end
end

describe '#synchronize' do
subject { program.synchronize(lib_ptg_box) }

it { is_expected.to be_empty }
it { is_expected.to eq("synchronize\n") }

context 'when UMPEBC Metadata folder' do
let(:collection_name) { umpebc_metadata }
let(:record) { instance_double(UmpebcKbart, 'record') }

before { allow(UmpebcKbart).to receive(:find_or_create_by!).with(name: selection.name, year: selection.year).and_return(record) }

it { is_expected.to be_empty }
it { is_expected.to eq("synchronize\n") }
end
end

describe '#append_selection_month_marc_files' do
describe '#append_selection_month_marc_file' do
subject { program.append_selection_month_marc_file(selection, month) }

let(:month) { Date.today.month }
Expand All @@ -116,24 +117,25 @@ def mock_client

before do
allow(File).to receive(:open).with(filename + '.mrc', 'w').and_return(mrc_file)
allow(mrc_file).to receive(:<<).with(marc.to_mrc)
allow(mrc_file).to receive(:<<).with(marc.to_marc)
allow(mrc_file).to receive(:close)
allow(File).to receive(:open).with(filename + '.xml', 'w').and_return(xml_file)
allow(xml_file).to receive(:<<).with(marc.to_xml)
allow(xml_file).to receive(:<<).with("\n")
allow(xml_file).to receive(:close)
end

it { is_expected.to be_empty }
it { is_expected.to eq("append_selection_month_marc_file\n") }

context 'when new work' do
let(:work_new) { true }

it { is_expected.to eq("Catalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{collection.name}' is missing!\n") }
it { is_expected.to eq("append_selection_month_marc_file\nCatalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{collection.name}' is missing!\n") }

context 'when catalog marc record' do # rubocop:disable RSpec/NestedGroups
let(:work_marc) { true }

it { is_expected.to eq("Catalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{collection.name}' is new!\n") }
it { is_expected.to eq("append_selection_month_marc_file\nCatalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{collection.name}' is new!\n") }
end
end
end
Expand All @@ -147,19 +149,20 @@ def mock_client

before do
allow(File).to receive(:open).with(filename + '.mrc', 'w').and_return(mrc_file)
allow(mrc_file).to receive(:<<).with(marc.to_mrc)
allow(mrc_file).to receive(:<<).with(marc.to_marc)
allow(mrc_file).to receive(:close)
allow(File).to receive(:open).with(filename + '.xml', 'w').and_return(xml_file)
allow(xml_file).to receive(:<<).with(marc.to_xml)
allow(xml_file).to receive(:<<).with("\n")
allow(xml_file).to receive(:close)
end

it { is_expected.to eq("Catalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{collection.name}' is missing!\n") }
it { is_expected.to eq("recreate_selection_marc_files\nCatalog MARC record for work '#{work.name}' in selection '#{selection.name}' in collection '#{collection.name}' is missing!\n") }

context 'when catalog marc record' do
let(:work_marc) { true }

it { is_expected.to be_empty }
it { is_expected.to eq("recreate_selection_marc_files\n") }
end
end

Expand All @@ -181,7 +184,7 @@ def mock_client
allow(xml_file).to receive(:close)
end

it { is_expected.to be_empty }
it { is_expected.to eq("recreate_collection_marc_files\n") }

context 'when marc files' do
let(:month) { Date.today.month }
Expand Down Expand Up @@ -213,7 +216,7 @@ def mock_client
allow(selection_xml_file).to receive(:close)
end

it { is_expected.to be_empty }
it { is_expected.to eq("recreate_collection_marc_files\n") }
end
end

Expand All @@ -228,7 +231,7 @@ def mock_client
end

it do
is_expected.to be_empty # rubocop:disable RSpec/ImplicitSubject
is_expected.to eq("replace_collection_marc_files\n") # rubocop:disable RSpec/ImplicitSubject
expect(collection).not_to have_received(:upload_marc_file)
end

Expand All @@ -249,7 +252,7 @@ def mock_client
end

it do # rubocop:disable RSpec/ExampleLength
is_expected.to be_empty # rubocop:disable RSpec/ImplicitSubject
is_expected.to eq("replace_collection_marc_files\n") # rubocop:disable RSpec/ImplicitSubject
expect(collection).not_to have_received(:upload_marc_file).with('.')
expect(collection).not_to have_received(:upload_marc_file).with('..')
expect(collection).to have_received(:upload_marc_file).with(selection.name + '.mrc')
Expand All @@ -262,10 +265,10 @@ def mock_client
end
end

describe '#assemble_marc_files' do
xdescribe '#assemble_marc_files' do
subject { program.assemble_marc_files(collection) }

it { is_expected.to be_empty }
it { is_expected.to eq("assemble_marc_files\n") }

context 'when UMPEBC Metadata folder' do
let(:collection_name) { umpebc_metadata }
Expand All @@ -274,7 +277,7 @@ def mock_client

before { allow(UmpebcKbart).to receive(:find_by).with(name: selection.name, year: selection.year).and_return(record) }

it { is_expected.to be_empty }
it { is_expected.to eq("assemble_marc_files\n") }

context 'when selection updated' do # rubocop:disable RSpec/NestedGroups
let(:month) { Date.today.month }
Expand All @@ -290,12 +293,12 @@ def mock_client
allow(record).to receive(:save).with(no_args)
end

it { is_expected.to eq("select\ncollect\nreplace\n") }
it { is_expected.to eq("assemble_marc_files\nselect\ncollect\nreplace\n") }

context 'when selection year updated' do # rubocop:disable RSpec/NestedGroups
let(:selection_year) { Date.today.year }

it { is_expected.to eq("month\nselect\ncollect\nreplace\n") }
it { is_expected.to eq("assemble_marc_files\nmonth\nselect\ncollect\nreplace\n") }
end
end
end
Expand Down

0 comments on commit 2e903a1

Please sign in to comment.