summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/loose_index_scan_distinct_count_spec.rb
blob: e0eac26e4d940001e21d495ee8449d11c00bb068 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::LooseIndexScanDistinctCount do
  context 'counting distinct users' do
    let_it_be(:user) { create(:user) }
    let_it_be(:other_user) { create(:user) }

    let(:column) { :creator_id }

    before_all do
      create_list(:project, 3, creator: user)
      create_list(:project, 1, creator: other_user)
    end

    subject(:count) { described_class.new(Project, :creator_id).count(from: Project.minimum(:creator_id), to: Project.maximum(:creator_id) + 1) }

    it { is_expected.to eq(2) }

    context 'when STI model is queried' do
      it 'does not raise error' do
        expect { described_class.new(Group, :owner_id).count(from: 0, to: 1) }.not_to raise_error
      end
    end

    context 'when model with default_scope is queried' do
      it 'does not raise error' do
        expect { described_class.new(GroupMember, :id).count(from: 0, to: 1) }.not_to raise_error
      end
    end

    context 'when the fully qualified column is given' do
      let(:column) { 'projects.creator_id' }

      it { is_expected.to eq(2) }
    end

    context 'when AR attribute is given' do
      let(:column) { Project.arel_table[:creator_id] }

      it { is_expected.to eq(2) }
    end

    context 'when invalid value is given for the column' do
      let(:column) { Class.new }

      it { expect { described_class.new(Group, column) }.to raise_error(Gitlab::Database::LooseIndexScanDistinctCount::ColumnConfigurationError) }
    end

    context 'when null values are present' do
      before do
        create_list(:project, 2).each { |p| p.update_column(:creator_id, nil) }
      end

      it { is_expected.to eq(2) }
    end
  end

  context 'counting STI models' do
    let!(:groups) { create_list(:group, 3) }
    let!(:namespaces) { create_list(:namespace, 2) }

    let(:max_id) { Namespace.maximum(:id) + 1 }

    it 'counts groups' do
      count = described_class.new(Group, :id).count(from: 0, to: max_id)
      expect(count).to eq(3)
    end
  end
end