Added tests for CanCleanupParam concern.
This commit is contained in:
parent
90f4b03e07
commit
eb43369754
2 changed files with 97 additions and 4 deletions
|
@ -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)
|
||||
|
||||
if import mode is enabled, just do not used :action and :controller
|
||||
|
||||
result = Model.filter_unused_params(params)
|
||||
|
||||
returns
|
||||
|
@ -78,12 +80,13 @@ returns
|
|||
=end
|
||||
|
||||
def filter_unused_params(data)
|
||||
|
||||
# we do want to set this via database
|
||||
%i[action controller updated_at created_at updated_by_id created_by_id updated_by created_by].each do |key|
|
||||
params = %i[action controller updated_at created_at updated_by_id created_by_id updated_by created_by]
|
||||
if Setting.get('import_mode') == true
|
||||
params = %i[action controller]
|
||||
end
|
||||
params.each do |key|
|
||||
data.delete(key)
|
||||
end
|
||||
|
||||
data
|
||||
end
|
||||
end
|
||||
|
|
|
@ -247,4 +247,94 @@ class ModelTest < ActiveSupport::TestCase
|
|||
assert_equal(4, searchable.count)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue