Skip to content

Commit

Permalink
Add test coverage for new minter #read/#write methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo committed Oct 20, 2016
1 parent 265afe6 commit bca7a9d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
55 changes: 40 additions & 15 deletions spec/unit/db_minter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

describe ActiveFedora::Noid::Minter::Db do
before(:each) { reset_minter_state_table }
after( :all ) { reset_minter_state_table }
after(:all) { reset_minter_state_table }

before :each do
# default novel mintings
allow(ActiveFedora::Base).to receive(:exists?).and_return(false)
allow(ActiveFedora::Base).to receive(:gone?).and_return(false)
end

let(:minter) { described_class.new }
let(:other) { described_class.new('.reedddk') }

describe '#initialize' do
Expand All @@ -19,10 +18,10 @@
expect{ described_class.new('') }.to raise_error(Noid::TemplateError)
end
it 'returns object w/ default template' do
expect(minter).to be_instance_of described_class
expect(minter).to be_a Noid::Minter
expect(minter.template).to be_instance_of Noid::Template
expect(minter.template.to_s).to eq ActiveFedora::Noid.config.template
expect(subject).to be_instance_of described_class
expect(subject).to be_a Noid::Minter
expect(subject.template).to be_instance_of Noid::Template
expect(subject.template.to_s).to eq ActiveFedora::Noid.config.template
end
it 'accepts valid template arg' do
expect(other).to be_instance_of described_class
Expand All @@ -33,33 +32,59 @@
end

describe '#mint' do
subject { minter.mint }
it { is_expected.not_to be_empty }
it 'mints a non-empty identifer' do
expect(subject.mint).not_to be_empty
end
it 'does not mint the same ID twice in a row' do
expect(subject).not_to eq described_class.new.mint
expect(subject.mint).not_to eq subject.mint
end
it 'is valid' do
expect(minter.valid?(subject)).to be true
expect(described_class.new.valid?(subject)).to be true
expect(subject).to be_valid(subject.mint)
end
it 'is invalid under a different template' do
expect(described_class.new('.reedddk').valid?(subject)).to be false
expect(other).not_to be_valid(subject.mint)
end
end

describe '#read' do
it 'returns a MinterState instance' do
expect(subject.read).to be_a(MinterState)
end
it 'has the expected namespace' do
expect(subject.read.namespace).to eq ActiveFedora::Noid.config.namespace
end
it 'has the expected template' do
expect(subject.read.template).to eq ActiveFedora::Noid.config.template
end
end

describe '#write' do
let(:starting_state) { subject.read }
let(:minter) { Noid::Minter.new(starting_state.noid_options) }
before { minter.mint }
it 'changes the state of the minter' do
expect { subject.write(minter) }.to change { subject.read.seq }
.from(starting_state.seq).to(minter.seq)
.and change { subject.read.counters }
.from(starting_state.counters).to(JSON.generate(minter.counters))
.and change { subject.read.random }
.from(starting_state.random).to(Marshal.dump(minter.instance_variable_get(:@rand)))
end
end

context 'conflicts' do
let(:existing_pid) { 'ef12ef12f' }
let(:unique_pid) { 'bb22bb22b' }
before :each do
expect(minter).to receive(:next_id).and_return(existing_pid, unique_pid)
expect(subject).to receive(:next_id).and_return(existing_pid, unique_pid)
end

context 'when the pid already exists in Fedora' do
before do
expect(ActiveFedora::Base).to receive(:exists?).with(existing_pid).and_return(true)
end
it 'skips the existing pid' do
expect(minter.mint).to eq unique_pid
expect(subject.mint).to eq unique_pid
end
end

Expand All @@ -69,7 +94,7 @@
expect(ActiveFedora::Base).to receive(:gone?).with(gone_pid).and_return(true)
end
it 'skips the deleted pid' do
expect(minter.mint).to eq unique_pid
expect(subject.mint).to eq unique_pid
end
end
end
Expand Down
42 changes: 32 additions & 10 deletions spec/unit/file_minter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
allow(ActiveFedora::Base).to receive(:gone?).and_return(false)
end

let(:minter) { described_class.new }

it { is_expected.to respond_to(:mint) }
it 'has a default statefile' do
expect(subject.statefile).to eq ActiveFedora::Noid.config.statefile
Expand All @@ -30,30 +28,54 @@
end

describe '#mint' do
subject { minter.mint }
it { is_expected.not_to be_empty }
it 'mints a non-empty identifer' do
expect(subject.mint).not_to be_empty
end
it 'does not mint the same ID twice in a row' do
expect(subject).not_to eq described_class.new.mint
expect(subject.mint).not_to eq subject.mint
end
it 'is valid' do
expect(minter.valid?(subject)).to be true
expect(described_class.new.valid?(subject)).to be true
expect(subject).to be_valid(subject.mint)
end
end

describe '#read' do
it 'returns a hash' do
expect(subject.read).to be_a(Hash)
end
it 'has the expected template' do
expect(subject.read[:template]).to eq ActiveFedora::Noid.config.template
end
end

describe '#write' do
let(:starting_state) { subject.read }
let(:minter) { Noid::Minter.new(starting_state) }
before { minter.mint }
it 'changes the state of the minter' do
expect { subject.write(minter) }.to change { subject.read[:seq] }
.from(starting_state[:seq]).to(minter.seq)
.and change { subject.read[:rand] }
.from(starting_state[:rand]).to(Marshal.dump(minter.instance_variable_get(:@rand)))
.and change { subject.read[:counters] }
.to(minter.counters)

end
end

context 'conflicts' do
let(:unique_pid) { 'bb22bb22b' }
let(:existing_pid) { 'ef12ef12f' }
before :each do
expect(minter).to receive(:next_id).and_return(existing_pid, unique_pid)
expect(subject).to receive(:next_id).and_return(existing_pid, unique_pid)
end

context 'when the pid already exists in Fedora' do
before do
expect(ActiveFedora::Base).to receive(:exists?).with(existing_pid).and_return(true)
end
it 'skips the existing pid' do
expect(minter.mint).to eq unique_pid
expect(subject.mint).to eq unique_pid
end
end

Expand All @@ -63,7 +85,7 @@
expect(ActiveFedora::Base).to receive(:gone?).with(gone_pid).and_return(true)
end
it 'skips the deleted pid' do
expect(minter.mint).to eq unique_pid
expect(subject.mint).to eq unique_pid
end
end
end
Expand Down

0 comments on commit bca7a9d

Please sign in to comment.