Skip to content

Commit

Permalink
Automatically seed the database. Fixes #59
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 9, 2017
1 parent 303c14a commit b6255ef
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -7,3 +7,6 @@ AllCops:

Rails:
Enabled: true

RSpec/NestedGroups:
MaxNesting: 4
9 changes: 9 additions & 0 deletions app/models/minter_state.rb
Expand Up @@ -3,4 +3,13 @@ class MinterState < ActiveRecord::Base
validates :namespace, presence: true, uniqueness: true
validates :template, presence: true
validates :template, format: { with: Object.const_get('Noid::Template::VALID_PATTERN'), message: 'value fails regex' }

# Creates an initial row for the namespace.
# @return [MinterState] the initial minter state
def self.seed!(namespace:, template:)
create!(
namespace: namespace,
template: template
)
end
end
7 changes: 6 additions & 1 deletion lib/active_fedora/noid/minter/db.rb
Expand Up @@ -44,7 +44,12 @@ def next_id
end

def instance
MinterState.lock.find_by(
MinterState.lock.find_by!(
namespace: ActiveFedora::Noid.config.namespace,
template: ActiveFedora::Noid.config.template
)
rescue ActiveRecord::RecordNotFound
MinterState.seed!(
namespace: ActiveFedora::Noid.config.namespace,
template: ActiveFedora::Noid.config.template
)
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/active_fedora/noid/seed_generator.rb
Expand Up @@ -20,7 +20,7 @@ def checks
end

def seed_row
MinterState.create!(
MinterState.seed!(
namespace: namespace,
template: template
)
Expand Down
10 changes: 6 additions & 4 deletions spec/support/shared_examples/minter.rb
@@ -1,8 +1,7 @@
# frozen_string_literal: true
shared_examples 'a minter' do
describe '#mint' do
subject { minter.mint }
let(:other) { described_class.new('.reedddk') }
subject { minter.mint }

it { is_expected.not_to be_empty }

Expand All @@ -19,8 +18,11 @@
expect(described_class.new('.reedddk').valid?(subject)).to be false
end

it 'is invalid under a different template' do
expect(other).not_to be_valid(minter.mint)
context 'with a different template' do
let(:other) { described_class.new('.reedddk') }
it 'is invalid under a different template' do
expect(other).not_to be_valid(minter.mint)
end
end
end

Expand Down
58 changes: 35 additions & 23 deletions spec/unit/db_minter_spec.rb
Expand Up @@ -4,17 +4,16 @@
describe ActiveFedora::Noid::Minter::Db do
before { reset_minter_state_table }
after(:all) { reset_minter_state_table }
subject(:db_minter) { described_class.new }

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

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

it_behaves_like 'a minter' do
let(:minter) { described_class.new }
let(:minter) { db_minter }
end

describe '#initialize' do
Expand All @@ -23,41 +22,54 @@
expect { described_class.new('') }.to raise_error(Noid::TemplateError)
end
it 'returns object w/ default template' do
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
expect(db_minter).to be_instance_of described_class
expect(db_minter).to be_a Noid::Minter
expect(db_minter.template).to be_instance_of Noid::Template
expect(db_minter.template.to_s).to eq ActiveFedora::Noid.config.template
end
it 'accepts valid template arg' do
expect(other).to be_instance_of described_class
expect(other).to be_a Noid::Minter
expect(other.template).to be_instance_of Noid::Template
expect(other.template.to_s).to eq '.reedddk'
context 'with a user provided template' do
let(:db_minter) { described_class.new('.reedddk') }

it 'accepts valid template arg' do
expect(db_minter).to be_instance_of described_class
expect(db_minter).to be_a Noid::Minter
expect(db_minter.template).to be_instance_of Noid::Template
expect(db_minter.template.to_s).to eq '.reedddk'
end
end
end

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

context 'when the database has been initialized' do
it 'has the expected namespace and template' do
expect(subject).to include(namespace: ActiveFedora::Noid.config.namespace,
template: ActiveFedora::Noid.config.template)
end
end
it 'has the expected template' do
expect(subject.read[:template]).to eq ActiveFedora::Noid.config.template

context 'when the database has not been initialized' do
before do
MinterState.destroy_all
end
it 'has the expected namespace and template' do
expect(subject).to include(namespace: ActiveFedora::Noid.config.namespace,
template: ActiveFedora::Noid.config.template)
end
end
end

describe '#write!' do
let(:starting_state) { subject.read }
let(:starting_state) { db_minter.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] }
expect { db_minter.write!(minter) }.to change { db_minter.read[:seq] }
.from(starting_state[:seq]).to(minter.seq)
.and change { subject.read[:counters] }
.and change { db_minter.read[:counters] }
.from(starting_state[:counters]).to(minter.counters)
.and change { subject.read[:rand] }
.and change { db_minter.read[:rand] }
.from(starting_state[:rand]).to(Marshal.dump(minter.instance_variable_get(:@rand)))
end
end
Expand Down

0 comments on commit b6255ef

Please sign in to comment.