From a77361869a37f97f0e0aa5056de752e64fee2afe Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 11 Sep 2015 19:19:13 +0200 Subject: [PATCH] Improved unit tests. --- app/models/application_model.rb | 24 ++++++++++++++++++++++-- test/unit/email_address_test.rb | 11 ++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/models/application_model.rb b/app/models/application_model.rb index e3c32b104..14c418d82 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -331,7 +331,7 @@ returns =begin -create model if not exists (check exists based on id, name, login or locale) +create model if not exists (check exists based on id, name, login, email or locale) result = Model.create_if_not_exists( attributes ) @@ -359,6 +359,13 @@ returns records.each {|loop_record| return loop_record if loop_record.login == data[:login] } + elsif data[:email] + + # do lookup with == to handle case insensitive databases + records = where( email: data[:email] ) + records.each {|loop_record| + return loop_record if loop_record.email == data[:email] + } elsif data[:locale] && data[:source] # do lookup with == to handle case insensitive databases @@ -372,7 +379,7 @@ returns =begin -create or update model (check exists based on id, name, login or locale) +create or update model (check exists based on id, name, login, email or locale) result = Model.create_or_update( attributes ) @@ -418,6 +425,19 @@ returns record = new( data ) record.save return record + elsif data[:email] + + # do lookup with == to handle case insensitive databases + records = where( email: data[:email] ) + records.each {|loop_record| + if loop_record.email.downcase == data[:email].downcase + loop_record.update_attributes( data ) + return loop_record + end + } + record = new( data ) + record.save + return record elsif data[:locale] # do lookup with == to handle case insensitive databases diff --git a/test/unit/email_address_test.rb b/test/unit/email_address_test.rb index ac8af3f93..9741b00d2 100644 --- a/test/unit/email_address_test.rb +++ b/test/unit/email_address_test.rb @@ -4,8 +4,7 @@ require 'test_helper' class EmailAddressTest < ActiveSupport::TestCase test 'basic tests' do - EmailAddress.delete_all - email_address1 = EmailAddress.create( + email_address1 = EmailAddress.create_or_update( realname: 'address #1', email: 'address1@example.com', active: true, @@ -22,8 +21,7 @@ class EmailAddressTest < ActiveSupport::TestCase test 'group tests' do - EmailAddress.delete_all - email_address1 = EmailAddress.create( + email_address1 = EmailAddress.create_or_update( realname: 'address #1', email: 'address1@example.com', active: true, @@ -55,8 +53,7 @@ class EmailAddressTest < ActiveSupport::TestCase created_by_id: 1, ) - EmailAddress.delete_all - email_address1 = EmailAddress.create( + email_address1 = EmailAddress.create_or_update( realname: 'address #1', email: 'address1@example.com', active: true, @@ -64,7 +61,7 @@ class EmailAddressTest < ActiveSupport::TestCase updated_by_id: 1, created_by_id: 1, ) - email_address2 = EmailAddress.create( + email_address2 = EmailAddress.create_or_update( realname: 'address #2', email: 'address2@example.com', active: true,