summaryrefslogtreecommitdiff
path: root/spec/models/concerns/case_sensitivity_spec.rb
blob: d6d41a25eac9358636b2f19ae82b4e23e9682b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

require 'spec_helper'

describe CaseSensitivity do
  describe '.iwhere' do
    let(:connection) { ActiveRecord::Base.connection }
    let(:model) do
      Class.new(ActiveRecord::Base) do
        include CaseSensitivity
        self.table_name = 'namespaces'
      end
    end

    let!(:model_1) { model.create(path: 'mOdEl-1', name: 'mOdEl 1') }
    let!(:model_2) { model.create(path: 'mOdEl-2', name: 'mOdEl 2') }

    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

    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

    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

    # Using `mysql` & `postgresql` metadata-tags here because both adapters build
    # the query slightly differently
    context 'for MySQL', :mysql do
      it 'builds a simple query' do
        query = model.iwhere(path: %w(MODEL-1 model-2), name: 'model 1').to_sql
        expected_query = <<~QRY.strip
        SELECT `namespaces`.* FROM `namespaces` WHERE (`namespaces`.`path` IN ('MODEL-1', 'model-2')) AND (`namespaces`.`name` = 'model 1')
        QRY

        expect(query).to eq(expected_query)
      end
    end

    context 'for PostgreSQL', :postgresql do
      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

        expect(query).to eq(expected_query)
      end
    end
  end
end