From 9ca8459d199b16d701e3686659be7dadccfababd Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 28 Jul 2015 15:19:54 +0200 Subject: [PATCH] Added tests for create_if_not_exists and create_or_update. --- app/models/application_model.rb | 24 +++++++++++--- test/unit/model_test.rb | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/app/models/application_model.rb b/app/models/application_model.rb index f5e4fd9c6..e3c32b104 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -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 diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index 0bdb6ea50..5bb8e5bf9 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -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