2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-10-24 18:46:33 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe CaseSensitivity do
2015-10-24 18:46:33 +05:30
describe '.iwhere' do
let ( :connection ) { ActiveRecord :: Base . connection }
2018-11-20 20:47:30 +05:30
let ( :model ) do
Class . new ( ActiveRecord :: Base ) do
include CaseSensitivity
self . table_name = 'namespaces'
2015-10-24 18:46:33 +05:30
end
end
2018-11-20 20:47:30 +05:30
let! ( :model_1 ) { model . create ( path : 'mOdEl-1' , name : 'mOdEl 1' ) }
let! ( :model_2 ) { model . create ( path : 'mOdEl-2' , name : 'mOdEl 2' ) }
2015-10-24 18:46:33 +05:30
2018-11-20 20:47:30 +05:30
it 'finds a single instance by a single attribute regardless of case' do
expect ( model . iwhere ( path : 'MODEL-1' ) ) . to contain_exactly ( model_1 )
end
2015-10-24 18:46:33 +05:30
2018-11-20 20:47:30 +05:30
it 'finds multiple instances by a single attribute regardless of case' do
expect ( model . iwhere ( path : %w( MODEL-1 model-2 ) ) ) . to contain_exactly ( model_1 , model_2 )
end
2015-10-24 18:46:33 +05:30
2018-11-20 20:47:30 +05:30
it 'finds instances by multiple attributes' do
expect ( model . iwhere ( path : %w( MODEL-1 model-2 ) , name : 'model 1' ) )
. to contain_exactly ( model_1 )
end
2015-10-24 18:46:33 +05:30
2019-10-12 21:52:04 +05:30
it 'builds a query using LOWER' do
query = model . iwhere ( path : %w( MODEL-1 model-2 ) , name : 'model 1' ) . to_sql
expected_query = << ~ QRY . strip
SELECT \ " namespaces \" .* FROM \" namespaces \" WHERE (LOWER( \" namespaces \" . \" path \" ) IN (LOWER('MODEL-1'), LOWER('model-2'))) AND (LOWER( \" namespaces \" . \" name \" ) = LOWER('model 1'))
QRY
2015-10-24 18:46:33 +05:30
2019-10-12 21:52:04 +05:30
expect ( query ) . to eq ( expected_query )
2015-10-24 18:46:33 +05:30
end
end
end