Refactoring: Migrate db_auto_increment to RSpec
This commit is contained in:
parent
dde7208ebc
commit
7700817143
3 changed files with 255 additions and 52 deletions
|
@ -0,0 +1,253 @@
|
|||
RSpec.shared_examples 'ApplicationModel::CanCreatesAndUpdates' do
|
||||
describe '.create_if_not_exists' do
|
||||
let!(:record) { create(described_class.name.underscore) }
|
||||
|
||||
context 'when given a valid #id' do
|
||||
let(:id) { record.id }
|
||||
|
||||
it 'returns that record' do
|
||||
expect(described_class.create_if_not_exists(id: id)).to eq(record)
|
||||
end
|
||||
|
||||
it 'does not create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(id: id)
|
||||
expect(described_class).not_to have_received(:create).with(id: id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #id' do
|
||||
let(:id) { described_class.pluck(:id).max + 1 }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(id: id)
|
||||
expect(described_class).to have_received(:create).with(id: id)
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #name attribute' do
|
||||
context 'when given a valid #name' do
|
||||
let(:name) { record.name }
|
||||
|
||||
it 'returns that record' do
|
||||
expect(described_class.create_if_not_exists(name: name)).to eq(record)
|
||||
end
|
||||
|
||||
it 'does not create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(name: name)
|
||||
expect(described_class).not_to have_received(:create).with(name: name)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #name' do
|
||||
let(:name) { described_class.pluck(:name).max + 'foo' }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(name: name)
|
||||
expect(described_class).to have_received(:create).with(name: name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #login attribute' do
|
||||
context 'when given a valid #login' do
|
||||
let(:login) { record.login }
|
||||
|
||||
it 'returns that record' do
|
||||
expect(described_class.create_if_not_exists(login: login)).to eq(record)
|
||||
end
|
||||
|
||||
it 'does not create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(login: login)
|
||||
expect(described_class).not_to have_received(:create).with(login: login)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #login' do
|
||||
let(:login) { described_class.pluck(:login).max + 'foo' }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(login: login)
|
||||
expect(described_class).to have_received(:create).with(login: login)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #email attribute' do
|
||||
context 'when given a valid #email' do
|
||||
let(:email) { record.email }
|
||||
|
||||
it 'returns that record' do
|
||||
expect(described_class.create_if_not_exists(email: email)).to eq(record)
|
||||
end
|
||||
|
||||
it 'does not create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(email: email)
|
||||
expect(described_class).not_to have_received(:create).with(email: email)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #email' do
|
||||
let(:email) { described_class.pluck(:email).max + 'foo' }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(email: email)
|
||||
expect(described_class).to have_received(:create).with(email: email)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #source and #locale attributes' do
|
||||
context 'when given a valid #source and #locale' do
|
||||
let(:source) { record.source }
|
||||
let(:locale) { record.locale }
|
||||
|
||||
it 'returns that record' do
|
||||
expect(described_class.create_if_not_exists(source: source, locale: locale)).to eq(record)
|
||||
end
|
||||
|
||||
it 'does not create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(source: source, locale: locale)
|
||||
expect(described_class).not_to have_received(:create).with(source: source, locale: locale)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #source or #locale' do
|
||||
let(:source) { described_class.pluck(:source).max + 'foo' }
|
||||
let(:locale) { record.locale }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_if_not_exists(source: source, locale: locale)
|
||||
expect(described_class).to have_received(:create).with(source: source, locale: locale)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
|
||||
include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
|
||||
include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
|
||||
include_examples 'for #source and #locale attributes' if (described_class.attribute_names & %w[source locale]).many?
|
||||
end
|
||||
|
||||
describe '.create_or_update' do
|
||||
let!(:record) { create(described_class.name.underscore) }
|
||||
let(:yesterday) { 1.day.ago }
|
||||
|
||||
context 'when given a valid #id' do
|
||||
let(:id) { record.id }
|
||||
|
||||
it 'updates other attributes on (and returns) that record' do
|
||||
expect { described_class.create_or_update(id: id, updated_at: yesterday) }
|
||||
.to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #id' do
|
||||
let(:id) { described_class.pluck(:id).max + 1 }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_or_update(id: id)
|
||||
expect(described_class).to have_received(:create).with(id: id)
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #name attribute' do
|
||||
context 'when given a valid #name' do
|
||||
let(:name) { record.name }
|
||||
|
||||
it 'updates other attributes on (and returns) that record' do
|
||||
expect { described_class.create_or_update(name: name, updated_at: yesterday) }
|
||||
.to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #name' do
|
||||
let(:name) { described_class.pluck(:name).max + 'foo' }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_or_update(name: name)
|
||||
expect(described_class).to have_received(:create).with(name: name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #login attribute' do
|
||||
context 'when given a valid #login' do
|
||||
let(:login) { record.login }
|
||||
|
||||
it 'updates other attributes on (and returns) that record' do
|
||||
expect { described_class.create_or_update(login: login, updated_at: yesterday) }
|
||||
.to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #login' do
|
||||
let(:login) { described_class.pluck(:login).max + 'foo' }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_or_update(login: login)
|
||||
expect(described_class).to have_received(:create).with(login: login)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #email attribute' do
|
||||
context 'when given a valid #email' do
|
||||
let(:email) { record.email }
|
||||
|
||||
it 'updates other attributes on (and returns) that record' do
|
||||
expect { described_class.create_or_update(email: email, updated_at: yesterday) }
|
||||
.to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #email' do
|
||||
let(:email) { described_class.pluck(:email).max + 'foo' }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_or_update(email: email)
|
||||
expect(described_class).to have_received(:create).with(email: email)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'for #locale attribute' do
|
||||
context 'when given a valid #locale' do
|
||||
let(:locale) { record.locale }
|
||||
|
||||
it 'updates other attributes on (and returns) that record' do
|
||||
expect { described_class.create_or_update(locale: locale, updated_at: yesterday) }
|
||||
.to change { record.reload.updated_at.to_i }.to(yesterday.to_i)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given an invalid #locale' do
|
||||
let(:locale) { record.locale }
|
||||
|
||||
it 'attempts to create a new record' do
|
||||
allow(described_class).to receive(:create)
|
||||
described_class.create_or_update(locale: locale)
|
||||
expect(described_class).to have_received(:create).with(locale: locale)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
include_examples 'for #name attribute' if described_class.attribute_names.include?('name')
|
||||
include_examples 'for #login attribute' if described_class.attribute_names.include?('login')
|
||||
include_examples 'for #email attribute' if described_class.attribute_names.include?('email')
|
||||
include_examples 'for #locale attribute' if described_class.attribute_names.include?('locale')
|
||||
end
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
require 'models/application_model/can_assets_examples'
|
||||
require 'models/application_model/can_associations_examples'
|
||||
require 'models/application_model/can_creates_and_updates_examples'
|
||||
require 'models/application_model/can_latest_change_examples'
|
||||
require 'models/application_model/can_lookup_examples'
|
||||
require 'models/application_model/checks_import_examples'
|
||||
|
@ -7,6 +8,7 @@ require 'models/application_model/checks_import_examples'
|
|||
RSpec.shared_examples 'ApplicationModel' do |options = {}|
|
||||
include_examples 'ApplicationModel::CanAssets', options[:can_assets]
|
||||
include_examples 'ApplicationModel::CanAssociations'
|
||||
include_examples 'ApplicationModel::CanCreatesAndUpdates'
|
||||
include_examples 'ApplicationModel::CanLatestChange'
|
||||
include_examples 'ApplicationModel::CanLookup'
|
||||
include_examples 'ApplicationModel::ChecksImport'
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
require 'test_helper'
|
||||
|
||||
class DbAutoIncrementTest < ActiveSupport::TestCase
|
||||
|
||||
test 'id overwrite' do
|
||||
|
||||
setting_backup = Setting.get('system_init_done')
|
||||
|
||||
Setting.set('system_init_done', false)
|
||||
|
||||
Ticket::StateType.create_if_not_exists( id: 200, name: 'unit test 1', updated_by_id: 1, created_by_id: 1 )
|
||||
state_type = Ticket::StateType.where( name: 'unit test 1' ).first
|
||||
assert_equal( Ticket::StateType.to_s, state_type.class.to_s )
|
||||
assert_equal( 'unit test 1', state_type.name )
|
||||
|
||||
Ticket::StateType.create_if_not_exists( id: 200, name: 'unit test 1 _ should not be created', updated_by_id: 1, created_by_id: 1 )
|
||||
state_type = Ticket::StateType.where( id: 200 ).first
|
||||
assert_equal( Ticket::StateType.to_s, state_type.class.to_s )
|
||||
assert_equal( 'unit test 1', state_type.name )
|
||||
|
||||
Ticket::StateType.create_or_update( id: 200, name: 'unit test 1 _ should be updated', updated_by_id: 1, created_by_id: 1 )
|
||||
state_type = Ticket::StateType.where( name: 'unit test 1 _ should be updated' ).first
|
||||
assert_equal( Ticket::StateType.to_s, state_type.class.to_s )
|
||||
assert_equal( 'unit test 1 _ should be updated', state_type.name )
|
||||
|
||||
state_type = Ticket::StateType.where( id: 200 ).first
|
||||
assert_equal( Ticket::StateType.to_s, state_type.class.to_s )
|
||||
assert_equal( 'unit test 1 _ should be updated', state_type.name )
|
||||
|
||||
Ticket::State.create_if_not_exists( id: 210, name: 'unit test 1', state_type_id: Ticket::StateType.where(name: 'unit test 1 _ should be updated').first.id, updated_by_id: 1, created_by_id: 1 )
|
||||
state = Ticket::State.where( name: 'unit test 1' ).first
|
||||
assert_equal( Ticket::State.to_s, state.class.to_s )
|
||||
assert_equal( 'unit test 1', state.name )
|
||||
|
||||
Ticket::State.create_if_not_exists( id: 210, name: 'unit test 1 _ should not be created', state_type_id: Ticket::StateType.where(name: 'unit test 1 _ should be updated').first.id, updated_by_id: 1, created_by_id: 1 )
|
||||
state = Ticket::State.where( id: 210 ).first
|
||||
assert_equal( Ticket::State.to_s, state.class.to_s )
|
||||
assert_equal( 'unit test 1', state.name )
|
||||
|
||||
Ticket::State.create_or_update( id: 210, name: 'unit test 1 _ should be updated', state_type_id: Ticket::StateType.where(name: 'unit test 1 _ should be updated').first.id, updated_by_id: 1, created_by_id: 1 )
|
||||
state = Ticket::State.where( name: 'unit test 1 _ should be updated' ).first
|
||||
assert_equal( Ticket::State.to_s, state.class.to_s )
|
||||
assert_equal( 'unit test 1 _ should be updated', state.name )
|
||||
|
||||
state = Ticket::State.where( id: 210 ).first
|
||||
assert_equal( Ticket::State.to_s, state.class.to_s )
|
||||
assert_equal( 'unit test 1 _ should be updated', state.name )
|
||||
|
||||
Setting.set('system_init_done', setting_backup)
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue