BigW Consortium Gitlab

case_sensitivity_spec.rb 5.78 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe CaseSensitivity, models: true do
4
  describe '.iwhere' do
5 6 7 8
    let(:connection) { ActiveRecord::Base.connection }
    let(:model)      { Class.new { include CaseSensitivity } }

    describe 'using PostgreSQL' do
9 10 11 12 13
      before do
        allow(Gitlab::Database).to receive(:postgresql?).and_return(true)
        allow(Gitlab::Database).to receive(:mysql?).and_return(false)
      end

14 15 16 17 18 19 20 21 22 23 24 25
      describe 'with a single column/value pair' do
        it 'returns the criteria for a column and a value' do
          criteria = double(:criteria)

          expect(connection).to receive(:quote_table_name).
            with(:foo).
            and_return('"foo"')

          expect(model).to receive(:where).
            with(%q{LOWER("foo") = LOWER(:value)}, value: 'bar').
            and_return(criteria)

26
          expect(model.iwhere(foo: 'bar')).to eq(criteria)
27 28 29 30 31 32 33 34 35 36 37 38 39
        end

        it 'returns the criteria for a column with a table, and a value' do
          criteria = double(:criteria)

          expect(connection).to receive(:quote_table_name).
            with(:'foo.bar').
            and_return('"foo"."bar"')

          expect(model).to receive(:where).
            with(%q{LOWER("foo"."bar") = LOWER(:value)}, value: 'bar').
            and_return(criteria)

40
          expect(model.iwhere('foo.bar'.to_sym => 'bar')).to eq(criteria)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        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)

          expect(connection).to receive(:quote_table_name).
            with(:foo).
            and_return('"foo"')

          expect(connection).to receive(:quote_table_name).
            with(:bar).
            and_return('"bar"')

          expect(model).to receive(:where).
            with(%q{LOWER("foo") = LOWER(:value)}, value: 'bar').
            and_return(initial)

          expect(initial).to receive(:where).
            with(%q{LOWER("bar") = LOWER(:value)}, value: 'baz').
            and_return(final)

65
          got = model.iwhere(foo: 'bar', bar: 'baz')
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

          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)

          expect(connection).to receive(:quote_table_name).
            with(:'foo.bar').
            and_return('"foo"."bar"')

          expect(connection).to receive(:quote_table_name).
            with(:'foo.baz').
            and_return('"foo"."baz"')

          expect(model).to receive(:where).
            with(%q{LOWER("foo"."bar") = LOWER(:value)}, value: 'bar').
            and_return(initial)

          expect(initial).to receive(:where).
            with(%q{LOWER("foo"."baz") = LOWER(:value)}, value: 'baz').
            and_return(final)

90 91
          got = model.iwhere('foo.bar'.to_sym => 'bar',
                             'foo.baz'.to_sym => 'baz')
92 93 94 95 96 97 98

          expect(got).to eq(final)
        end
      end
    end

    describe 'using MySQL' do
99 100 101 102 103
      before do
        allow(Gitlab::Database).to receive(:postgresql?).and_return(false)
        allow(Gitlab::Database).to receive(:mysql?).and_return(true)
      end

104 105 106 107 108 109 110 111 112
      describe 'with a single column/value pair' do
        it 'returns the criteria for a column and a value' do
          criteria = double(:criteria)

          expect(connection).to receive(:quote_table_name).
            with(:foo).
            and_return('`foo`')

          expect(model).to receive(:where).
113
            with(%q{`foo` = :value}, value: 'bar').
114 115
            and_return(criteria)

116
          expect(model.iwhere(foo: 'bar')).to eq(criteria)
117 118 119 120 121 122 123 124 125 126
        end

        it 'returns the criteria for a column with a table, and a value' do
          criteria = double(:criteria)

          expect(connection).to receive(:quote_table_name).
            with(:'foo.bar').
            and_return('`foo`.`bar`')

          expect(model).to receive(:where).
127
            with(%q{`foo`.`bar` = :value}, value: 'bar').
128 129
            and_return(criteria)

130
          expect(model.iwhere('foo.bar'.to_sym => 'bar')).
131
            to eq(criteria)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        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)

          expect(connection).to receive(:quote_table_name).
            with(:foo).
            and_return('`foo`')

          expect(connection).to receive(:quote_table_name).
            with(:bar).
            and_return('`bar`')

          expect(model).to receive(:where).
149
            with(%q{`foo` = :value}, value: 'bar').
150 151 152
            and_return(initial)

          expect(initial).to receive(:where).
153
            with(%q{`bar` = :value}, value: 'baz').
154 155
            and_return(final)

156
          got = model.iwhere(foo: 'bar', bar: 'baz')
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

          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)

          expect(connection).to receive(:quote_table_name).
            with(:'foo.bar').
            and_return('`foo`.`bar`')

          expect(connection).to receive(:quote_table_name).
            with(:'foo.baz').
            and_return('`foo`.`baz`')

          expect(model).to receive(:where).
174
            with(%q{`foo`.`bar` = :value}, value: 'bar').
175 176 177
            and_return(initial)

          expect(initial).to receive(:where).
178
            with(%q{`foo`.`baz` = :value}, value: 'baz').
179 180
            and_return(final)

181 182
          got = model.iwhere('foo.bar'.to_sym => 'bar',
                             'foo.baz'.to_sym => 'baz')
183 184 185 186 187 188 189

          expect(got).to eq(final)
        end
      end
    end
  end
end