Added tests for CanCleanupParam concern.

This commit is contained in:
Martin Edenhofer 2018-01-30 01:19:23 +01:00
parent 90f4b03e07
commit eb43369754
2 changed files with 97 additions and 4 deletions

View file

@ -69,6 +69,8 @@ returns
remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id) remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
if import mode is enabled, just do not used :action and :controller
result = Model.filter_unused_params(params) result = Model.filter_unused_params(params)
returns returns
@ -78,12 +80,13 @@ returns
=end =end
def filter_unused_params(data) def filter_unused_params(data)
params = %i[action controller updated_at created_at updated_by_id created_by_id updated_by created_by]
# we do want to set this via database if Setting.get('import_mode') == true
%i[action controller updated_at created_at updated_by_id created_by_id updated_by created_by].each do |key| params = %i[action controller]
end
params.each do |key|
data.delete(key) data.delete(key)
end end
data data
end end
end end

View file

@ -247,4 +247,94 @@ class ModelTest < ActiveSupport::TestCase
assert_equal(4, searchable.count) assert_equal(4, searchable.count)
end end
test 'param_cleanup test' do
params = {
id: 123,
abc: true,
firstname: '123',
created_by_id: 1,
created_at: Time.zone.now,
updated_by_id: 1,
updated_at: Time.zone.now,
action: 'some action',
controller: 'some controller',
}
result = User.param_cleanup(params, true)
assert_not(result.key?(:id))
assert_not(result.key?(:abc))
assert_equal('123', result[:firstname])
assert_not(result.key?(:created_by_id))
assert_not(result.key?(:created_at))
assert_not(result.key?(:updated_by_id))
assert_not(result.key?(:updated_at))
assert_not(result.key?(:action))
assert_not(result.key?(:controller))
params = {
id: 123,
abc: true,
firstname: '123',
created_by_id: 1,
created_at: Time.zone.now,
updated_by_id: 1,
updated_at: Time.zone.now,
action: 'some action',
controller: 'some controller',
}
result = User.param_cleanup(params)
assert_equal(123, result[:id])
assert_not(result.key?(:abc))
assert_equal('123', result[:firstname])
assert_not(result.key?(:created_by_id))
assert_not(result.key?(:created_at))
assert_not(result.key?(:updated_by_id))
assert_not(result.key?(:updated_at))
assert_not(result.key?(:action))
assert_not(result.key?(:controller))
Setting.set('import_mode', true)
params = {
id: 123,
abc: true,
firstname: '123',
created_by_id: 1,
created_at: Time.zone.now,
updated_by_id: 1,
updated_at: Time.zone.now,
action: 'some action',
controller: 'some controller',
}
result = User.param_cleanup(params, true)
assert_not(result.key?(:abc))
assert_equal('123', result[:firstname])
assert_equal(1, result[:created_by_id])
assert(result[:created_at])
assert_equal(1, result[:updated_by_id])
assert(result[:updated_at])
assert_not(result.key?(:action))
assert_not(result.key?(:controller))
params = {
id: 123,
abc: true,
firstname: '123',
created_by_id: 1,
created_at: Time.zone.now,
updated_by_id: 1,
updated_at: Time.zone.now,
action: 'some action',
controller: 'some controller',
}
result = User.param_cleanup(params)
assert_equal(123, result[:id])
assert_equal('123', result[:firstname])
assert_equal(1, result[:created_by_id])
assert(result[:created_at])
assert_equal(1, result[:updated_by_id])
assert(result[:updated_at])
assert_not(result.key?(:action))
assert_not(result.key?(:controller))
end
end end