Improved RSpec system_init_done helper for DB migrations
Since DB migrations usually always check for system_init_done this is the new default behavior for all examples in a `type: :db_migration` spec. The default state is that Zammad is already initialized. This can be changed by passing `false` as a value to the meta tag `system_init_done`. See `spec/support/db_migration.rb` for more information.
This commit is contained in:
parent
2eaae8e5c4
commit
1e04b35d5a
3 changed files with 28 additions and 19 deletions
|
@ -2,9 +2,7 @@ require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
|
|
||||||
it 'performs no action for new systems' do
|
it 'performs no action for new systems', system_init_done: false do
|
||||||
system_init_done(false)
|
|
||||||
|
|
||||||
migrate do |instance|
|
migrate do |instance|
|
||||||
expect(instance).not_to receive(:attributes)
|
expect(instance).not_to receive(:attributes)
|
||||||
end
|
end
|
||||||
|
@ -13,8 +11,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
context 'valid [:data_option]' do
|
context 'valid [:data_option]' do
|
||||||
|
|
||||||
it 'does not change converted text attribute' do
|
it 'does not change converted text attribute' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
attribute = create(:object_manager_attribute_text)
|
attribute = create(:object_manager_attribute_text)
|
||||||
|
|
||||||
expect do
|
expect do
|
||||||
|
@ -25,8 +21,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not change select attribute' do
|
it 'does not change select attribute' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
attribute = create(:object_manager_attribute_select)
|
attribute = create(:object_manager_attribute_select)
|
||||||
|
|
||||||
expect do
|
expect do
|
||||||
|
@ -37,8 +31,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not change tree_select attribute' do
|
it 'does not change tree_select attribute' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
attribute = create(:object_manager_attribute_tree_select)
|
attribute = create(:object_manager_attribute_tree_select)
|
||||||
|
|
||||||
expect do
|
expect do
|
||||||
|
@ -52,8 +44,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
context '[:data_option]' do
|
context '[:data_option]' do
|
||||||
|
|
||||||
it 'ensures an empty Hash' do
|
it 'ensures an empty Hash' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
attribute = create(:object_manager_attribute_text, data_option: nil)
|
attribute = create(:object_manager_attribute_text, data_option: nil)
|
||||||
migrate
|
migrate
|
||||||
attribute.reload
|
attribute.reload
|
||||||
|
@ -65,8 +55,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
context '[:data_option][:options]' do
|
context '[:data_option][:options]' do
|
||||||
|
|
||||||
it 'ensures an empty Hash' do
|
it 'ensures an empty Hash' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
attribute = create(:object_manager_attribute_text, data_option: {})
|
attribute = create(:object_manager_attribute_text, data_option: {})
|
||||||
migrate
|
migrate
|
||||||
attribute.reload
|
attribute.reload
|
||||||
|
@ -75,8 +63,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'converts String to Hash' do
|
it 'converts String to Hash' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
wrong = {
|
wrong = {
|
||||||
default: '',
|
default: '',
|
||||||
options: '',
|
options: '',
|
||||||
|
@ -96,8 +82,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
context '[:data_option][:relation]' do
|
context '[:data_option][:relation]' do
|
||||||
|
|
||||||
it 'ensures an empty String' do
|
it 'ensures an empty String' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
wrong = {
|
wrong = {
|
||||||
default: '',
|
default: '',
|
||||||
options: {},
|
options: {},
|
||||||
|
@ -112,8 +96,6 @@ RSpec.describe CheckForObjectAttributes, type: :db_migration do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'converts Hash to String' do
|
it 'converts Hash to String' do
|
||||||
system_init_done
|
|
||||||
|
|
||||||
wrong = {
|
wrong = {
|
||||||
default: '',
|
default: '',
|
||||||
options: {},
|
options: {},
|
||||||
|
|
|
@ -27,6 +27,27 @@ module DbMigrationHelper
|
||||||
instance.migrate(direction)
|
instance.migrate(direction)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
|
||||||
|
# Execute in RSpec class context
|
||||||
|
base.class_exec do
|
||||||
|
|
||||||
|
# This method simulates a system that is is already initialized
|
||||||
|
# aka `Setting.exists?(name: 'system_init_done')`
|
||||||
|
# It's possible to simulate a not yet initialized system by adding the
|
||||||
|
# meta tag `system_init_done` to `false` to the needing example:
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# it 'does stuff in an unitialized system', system_init_done: false do
|
||||||
|
#
|
||||||
|
before(:each) do |example|
|
||||||
|
initialized = example.metadata.fetch(:system_init_done, true)
|
||||||
|
system_init_done(initialized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
module SystemInitDoneHelper
|
module SystemInitDoneHelper
|
||||||
def system_init_done(state = true)
|
def system_init_done(state = true)
|
||||||
|
# generally allow all calls to Setting.find_by to avoid
|
||||||
|
# RSpec errors where a different Setting is accessed
|
||||||
|
allow(Setting).to receive(:find_by).and_call_original
|
||||||
|
|
||||||
|
# just mock the Setting check for `system_init_done`
|
||||||
|
# and return the given parameter value
|
||||||
expect(Setting).to receive(:find_by).with(name: 'system_init_done').and_return(state)
|
expect(Setting).to receive(:find_by).with(name: 'system_init_done').and_return(state)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue