Added tests for create_if_not_exists and create_or_update.
This commit is contained in:
parent
ed59645cd8
commit
9ca8459d19
2 changed files with 78 additions and 5 deletions
|
@ -302,6 +302,7 @@ returns
|
|||
cache = cache_get( data[:name] )
|
||||
return cache if cache
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( name: data[:name] )
|
||||
records.each {|loop_record|
|
||||
if loop_record.name == data[:name]
|
||||
|
@ -314,6 +315,7 @@ returns
|
|||
cache = cache_get( data[:login] )
|
||||
return cache if cache
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( login: data[:login] )
|
||||
records.each {|loop_record|
|
||||
if loop_record.login == data[:login]
|
||||
|
@ -344,16 +346,22 @@ returns
|
|||
record = find_by( id: data[:id] )
|
||||
return record if record
|
||||
elsif data[:name]
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( name: data[:name] )
|
||||
records.each {|loop_record|
|
||||
return loop_record if loop_record.name == data[:name]
|
||||
}
|
||||
elsif data[:login]
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( login: data[:login] )
|
||||
records.each {|loop_record|
|
||||
return loop_record if loop_record.login == data[:login]
|
||||
}
|
||||
elsif data[:locale] && data[:source]
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( locale: data[:locale], source: data[:source] )
|
||||
records.each {|loop_record|
|
||||
return loop_record if loop_record.source == data[:source]
|
||||
|
@ -376,15 +384,17 @@ returns
|
|||
|
||||
def self.create_or_update(data)
|
||||
if data[:id]
|
||||
records = where( id: data[:id] )
|
||||
records.each {|loop_record|
|
||||
loop_record.update_attributes( data )
|
||||
return loop_record
|
||||
}
|
||||
record = find_by( id: data[:id] )
|
||||
if record
|
||||
record.update_attributes( data )
|
||||
return record
|
||||
end
|
||||
record = new( data )
|
||||
record.save
|
||||
return record
|
||||
elsif data[:name]
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( name: data[:name] )
|
||||
records.each {|loop_record|
|
||||
if loop_record.name == data[:name]
|
||||
|
@ -396,6 +406,8 @@ returns
|
|||
record.save
|
||||
return record
|
||||
elsif data[:login]
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( login: data[:login] )
|
||||
records.each {|loop_record|
|
||||
if loop_record.login.downcase == data[:login].downcase
|
||||
|
@ -407,6 +419,8 @@ returns
|
|||
record.save
|
||||
return record
|
||||
elsif data[:locale]
|
||||
|
||||
# do lookup with == to handle case insensitive databases
|
||||
records = where( locale: data[:locale] )
|
||||
records.each {|loop_record|
|
||||
if loop_record.locale.downcase == data[:locale].downcase
|
||||
|
|
|
@ -2,6 +2,65 @@
|
|||
require 'test_helper'
|
||||
|
||||
class ModelTest < ActiveSupport::TestCase
|
||||
|
||||
test 'create_if_not_exists test' do
|
||||
group1 = Group.create_if_not_exists(
|
||||
name: 'model1-create_if_not_exists',
|
||||
active: true,
|
||||
updated_at: '2015-02-05 16:37:00',
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
assert_raises( ActiveRecord::RecordNotUnique ) {
|
||||
Group.create_if_not_exists(
|
||||
name: 'model1-Create_If_Not_Exists',
|
||||
active: true,
|
||||
updated_at: '2015-02-05 16:37:00',
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
}
|
||||
|
||||
group2 = Group.create_if_not_exists(
|
||||
name: 'model1-create_if_not_exists',
|
||||
active: true,
|
||||
updated_at: '2015-02-05 16:39:00',
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
assert_equal(group1.id, group2.id)
|
||||
assert_equal(group2.updated_at.to_s, '2015-02-05 16:37:00 UTC')
|
||||
end
|
||||
|
||||
test 'create_or_update test' do
|
||||
group1 = Group.create_or_update(
|
||||
name: 'model1-create_or_update',
|
||||
active: true,
|
||||
updated_at: '2015-02-05 16:37:00',
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
assert_raises( ActiveRecord::RecordNotUnique ) {
|
||||
Group.create_or_update(
|
||||
name: 'model1-Create_Or_Update',
|
||||
active: true,
|
||||
updated_at: '2015-02-05 16:37:00',
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
}
|
||||
|
||||
group2 = Group.create_or_update(
|
||||
name: 'model1-create_or_update',
|
||||
active: true,
|
||||
updated_at: '2015-02-05 16:39:00',
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
assert_equal(group1.id, group2.id)
|
||||
assert_equal(group2.updated_at.to_s, '2015-02-05 16:39:00 UTC')
|
||||
end
|
||||
|
||||
test 'references test' do
|
||||
|
||||
# create base
|
||||
|
|
Loading…
Reference in a new issue