2019-03-30 03:23:56 -04:00
# frozen_string_literal: true
2015-10-07 08:03:18 -04:00
require 'spec_helper'
2020-06-24 14:09:03 -04:00
RSpec . describe CaseSensitivity do
2015-10-07 11:37:39 -04:00
describe '.iwhere' do
2021-07-23 08:09:05 -04:00
let_it_be ( :connection ) { Namespace . connection }
2020-12-15 10:09:59 -05:00
let_it_be ( :model ) do
2018-08-20 10:48:08 -04:00
Class . new ( ActiveRecord :: Base ) do
include CaseSensitivity
self . table_name = 'namespaces'
2015-10-07 08:03:18 -04:00
end
end
2020-12-15 10:09:59 -05:00
let_it_be ( :model_1 ) { model . create! ( path : 'mOdEl-1' , name : 'mOdEl 1' ) }
let_it_be ( :model_2 ) { model . create! ( path : 'mOdEl-2' , name : 'mOdEl 2' ) }
2015-10-07 08:03:18 -04:00
2018-08-20 10:48:08 -04:00
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-07 08:03:18 -04:00
2018-08-20 10:48:08 -04:00
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-07 08:03:18 -04:00
2018-08-20 10:48:08 -04:00
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-07 08:03:18 -04:00
2020-12-15 10:09:59 -05:00
it 'finds instances by custom Arel attributes' do
expect ( model . iwhere ( model . arel_table [ :path ] = > 'MODEL-1' ) ) . to contain_exactly ( model_1 )
end
2019-06-13 09:12:28 -04:00
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-07 08:03:18 -04:00
2019-06-13 09:12:28 -04:00
expect ( query ) . to eq ( expected_query )
2015-10-07 08:03:18 -04:00
end
end
end