Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ar_exists_and_present_blank_spec.rb « performance « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3321d400ae15a7d2294818aa1c21bee99888b70f (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../../rubocop/cop/performance/ar_exists_and_present_blank.rb'

RSpec.describe RuboCop::Cop::Performance::ARExistsAndPresentBlank do
  include CopHelper

  subject(:cop) { described_class.new }

  context 'when it is not haml file' do
    it 'does not flag it as an offense' do
      expect(subject).to receive(:in_haml_file?).with(anything).at_least(:once).and_return(false)

      expect_no_offenses <<~SOURCE
        return unless @users.exists?
        show @users if @users.present?
      SOURCE
    end
  end

  context 'when it is haml file' do
    before do
      expect(subject).to receive(:in_haml_file?).with(anything).at_least(:once).and_return(true)
    end

    context 'the same object uses exists? and present?' do
      it 'flags it as an offense' do
        expect_offense <<~SOURCE
        return unless @users.exists?
        show @users if @users.present?
                       ^^^^^^^^^^^^^^^ Avoid `@users.present?`, because it will generate database query 'Select TABLE.*' which is expensive. Suggest to use `@users.any?` to replace `@users.present?`
        SOURCE

        expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARExistsAndPresentBlank')
      end
    end

    context 'the same object uses exists? and blank?' do
      it 'flags it as an offense' do
        expect_offense <<~SOURCE
        return unless @users.exists?
        show @users if @users.blank?
                       ^^^^^^^^^^^^^ Avoid `@users.blank?`, because it will generate database query 'Select TABLE.*' which is expensive. Suggest to use `@users.empty?` to replace `@users.blank?`
        SOURCE

        expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARExistsAndPresentBlank')
      end
    end

    context 'the same object uses exists?, blank? and present?' do
      it 'flags it as an offense' do
        expect_offense <<~SOURCE
        return unless @users.exists?
        show @users if @users.blank?
                       ^^^^^^^^^^^^^ Avoid `@users.blank?`, because it will generate database query 'Select TABLE.*' which is expensive. Suggest to use `@users.empty?` to replace `@users.blank?`
        show @users if @users.present?
                       ^^^^^^^^^^^^^^^ Avoid `@users.present?`, because it will generate database query 'Select TABLE.*' which is expensive. Suggest to use `@users.any?` to replace `@users.present?`
        SOURCE

        expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARExistsAndPresentBlank', 'Performance/ARExistsAndPresentBlank')
      end
    end

    RSpec.shared_examples 'different object uses exists? and present?/blank?' do |another_method|
      it 'does not flag it as an offense' do
        expect_no_offenses <<~SOURCE
        return unless @users.exists?
        present @emails if @emails.#{another_method}
        SOURCE
      end
    end

    it_behaves_like 'different object uses exists? and present?/blank?', 'present?'
    it_behaves_like 'different object uses exists? and present?/blank?', 'blank?'

    RSpec.shared_examples 'Only using one present?/blank? without exists?' do |non_exists_method|
      it 'does not flag it as an offense' do
        expect_no_offenses "@users.#{non_exists_method}"
      end
    end

    it_behaves_like 'Only using one present?/blank? without exists?', 'present?'
    it_behaves_like 'Only using one present?/blank? without exists?', 'blank?'

    context 'when using many present?/empty? without exists?' do
      it 'does not flag it as an offense' do
        expect_no_offenses <<~SOURCE
        @user.present?
        @user.blank?
        @user.present?
        @user.blank?
        SOURCE
      end
    end

    context 'when just using exists? without present?/blank?' do
      it 'does not flag it as an offense' do
        expect_no_offenses '@users.exists?'

        expect_no_offenses <<~SOURCE
        @users.exists?
        @users.some_other_method?
        @users.exists?
        SOURCE
      end
    end
  end
end