2015-10-07 08:03:18 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe CaseSensitivity do
|
2015-10-07 11:37:39 -04:00
|
|
|
describe '.iwhere' do
|
2015-10-07 08:03:18 -04:00
|
|
|
let(:connection) { ActiveRecord::Base.connection }
|
|
|
|
let(:model) { Class.new { include CaseSensitivity } }
|
|
|
|
|
|
|
|
describe 'using PostgreSQL' do
|
2015-10-07 11:37:39 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::Database).to receive(:postgresql?).and_return(true)
|
|
|
|
allow(Gitlab::Database).to receive(:mysql?).and_return(false)
|
|
|
|
end
|
|
|
|
|
2015-10-07 08:03:18 -04:00
|
|
|
describe 'with a single column/value pair' do
|
|
|
|
it 'returns the criteria for a column and a value' do
|
|
|
|
criteria = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:foo)
|
|
|
|
.and_return('"foo"')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{LOWER("foo") = LOWER(:value)}, value: 'bar')
|
|
|
|
.and_return(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2015-10-07 11:37:39 -04:00
|
|
|
expect(model.iwhere(foo: 'bar')).to eq(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the criteria for a column with a table, and a value' do
|
|
|
|
criteria = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:'foo.bar')
|
|
|
|
.and_return('"foo"."bar"')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{LOWER("foo"."bar") = LOWER(:value)}, value: 'bar')
|
|
|
|
.and_return(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2016-01-28 05:41:58 -05:00
|
|
|
expect(model.iwhere('foo.bar'.to_sym => 'bar')).to eq(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with multiple column/value pairs' do
|
|
|
|
it 'returns the criteria for a column and a value' do
|
|
|
|
initial = double(:criteria)
|
|
|
|
final = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:foo)
|
|
|
|
.and_return('"foo"')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:bar)
|
|
|
|
.and_return('"bar"')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{LOWER("foo") = LOWER(:value)}, value: 'bar')
|
|
|
|
.and_return(initial)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(initial).to receive(:where)
|
|
|
|
.with(%q{LOWER("bar") = LOWER(:value)}, value: 'baz')
|
|
|
|
.and_return(final)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2015-10-07 11:37:39 -04:00
|
|
|
got = model.iwhere(foo: 'bar', bar: 'baz')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
|
|
|
expect(got).to eq(final)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the criteria for a column with a table, and a value' do
|
|
|
|
initial = double(:criteria)
|
|
|
|
final = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:'foo.bar')
|
|
|
|
.and_return('"foo"."bar"')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:'foo.baz')
|
|
|
|
.and_return('"foo"."baz"')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{LOWER("foo"."bar") = LOWER(:value)}, value: 'bar')
|
|
|
|
.and_return(initial)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(initial).to receive(:where)
|
|
|
|
.with(%q{LOWER("foo"."baz") = LOWER(:value)}, value: 'baz')
|
|
|
|
.and_return(final)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2016-01-28 05:41:58 -05:00
|
|
|
got = model.iwhere('foo.bar'.to_sym => 'bar',
|
|
|
|
'foo.baz'.to_sym => 'baz')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
|
|
|
expect(got).to eq(final)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'using MySQL' do
|
2015-10-07 11:37:39 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::Database).to receive(:postgresql?).and_return(false)
|
|
|
|
allow(Gitlab::Database).to receive(:mysql?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2015-10-07 08:03:18 -04:00
|
|
|
describe 'with a single column/value pair' do
|
|
|
|
it 'returns the criteria for a column and a value' do
|
|
|
|
criteria = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:foo)
|
|
|
|
.and_return('`foo`')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{`foo` = :value}, value: 'bar')
|
|
|
|
.and_return(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2015-10-07 11:37:39 -04:00
|
|
|
expect(model.iwhere(foo: 'bar')).to eq(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the criteria for a column with a table, and a value' do
|
|
|
|
criteria = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:'foo.bar')
|
|
|
|
.and_return('`foo`.`bar`')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{`foo`.`bar` = :value}, value: 'bar')
|
|
|
|
.and_return(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model.iwhere('foo.bar'.to_sym => 'bar'))
|
|
|
|
.to eq(criteria)
|
2015-10-07 08:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with multiple column/value pairs' do
|
|
|
|
it 'returns the criteria for a column and a value' do
|
|
|
|
initial = double(:criteria)
|
|
|
|
final = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:foo)
|
|
|
|
.and_return('`foo`')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:bar)
|
|
|
|
.and_return('`bar`')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{`foo` = :value}, value: 'bar')
|
|
|
|
.and_return(initial)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(initial).to receive(:where)
|
|
|
|
.with(%q{`bar` = :value}, value: 'baz')
|
|
|
|
.and_return(final)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2015-10-07 11:37:39 -04:00
|
|
|
got = model.iwhere(foo: 'bar', bar: 'baz')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
|
|
|
expect(got).to eq(final)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the criteria for a column with a table, and a value' do
|
|
|
|
initial = double(:criteria)
|
|
|
|
final = double(:criteria)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:'foo.bar')
|
|
|
|
.and_return('`foo`.`bar`')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(connection).to receive(:quote_table_name)
|
|
|
|
.with(:'foo.baz')
|
|
|
|
.and_return('`foo`.`baz`')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(model).to receive(:where)
|
|
|
|
.with(%q{`foo`.`bar` = :value}, value: 'bar')
|
|
|
|
.and_return(initial)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(initial).to receive(:where)
|
|
|
|
.with(%q{`foo`.`baz` = :value}, value: 'baz')
|
|
|
|
.and_return(final)
|
2015-10-07 08:03:18 -04:00
|
|
|
|
2016-01-28 05:41:58 -05:00
|
|
|
got = model.iwhere('foo.bar'.to_sym => 'bar',
|
|
|
|
'foo.baz'.to_sym => 'baz')
|
2015-10-07 08:03:18 -04:00
|
|
|
|
|
|
|
expect(got).to eq(final)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|