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

avoid_using_pluck_without_limit_spec.rb « database « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 801cf449726e08e04dafa4f3401649d77082f33c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true

require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/database/avoid_using_pluck_without_limit'

RSpec.describe RuboCop::Cop::Database::AvoidUsingPluckWithoutLimit, feature_category: :database do
  context 'when using pluck without a limit' do
    it 'flags the use of pluck as a model scope' do
      expect_offense(<<~RUBY)
        class MyModel < ApplicationRecord
          scope :all_users, -> { where(user_id: User.pluck(:id)) }
                                                     ^^^^^ #{described_class::MSG}
        end
      RUBY
    end

    it 'flags the use of pluck as a regular method' do
      expect_offense(<<~RUBY)
        class MyModel < ApplicationRecord
          def all
           self.pluck(:id)
                ^^^^^ #{described_class::MSG}
          end
        end
      RUBY
    end

    it 'flags the use of pluck inside where' do
      expect_offense(<<~RUBY)
        class MyModel < ApplicationRecord
          def all_projects
           Project.where(id: self.pluck(:id))
                                  ^^^^^ #{described_class::MSG}
          end
        end
      RUBY
    end

    it 'flags the use of pluck inside a model class method' do
      allow(cop).to receive(:in_model?).and_return(true)

      expect_offense(<<~RUBY)
        class MyClass < Model
          def all_users
           User.where(id: self.pluck(:id))
                               ^^^^^ #{described_class::MSG}
          end
        end
      RUBY
    end

    it 'flags the use of pluck inside a finder' do
      allow(cop).to receive(:in_finder?).and_return(true)

      expect_offense(<<~RUBY)
        class MyFinder
          def find(path)
           Project.where(path: path).pluck(:id)
                                     ^^^^^ #{described_class::MSG}
          end
        end
      RUBY
    end

    it 'flags the use of pluck inside a service' do
      allow(cop).to receive(:in_service_class?).and_return(true)

      expect_offense(<<~RUBY)
        class MyService
          def delete_all(project)
           delete(project.for_scan_result_policy_read(scan_result_policy_reads.pluck(:id)))
                                                                               ^^^^^ #{described_class::MSG}
          end
        end
      RUBY
    end
  end

  context 'when using pluck with a limit' do
    it 'does not flags the use of pluck as a model scope' do
      expect_no_offenses(<<~RUBY)
        class MyModel < ApplicationRecord
          scope :all_users, ->(limit) { where(user_id: User.limit(limit).pluck(:id)) }
        end
      RUBY
    end

    it 'does not flags the use of pluck as a regular method' do
      expect_no_offenses(<<~RUBY)
        class MyModel < ApplicationRecord
          def all(limit)
            self.limit(limit).pluck(:id)
          end
        end
      RUBY
    end

    it 'does not flags the use of pluck inside where' do
      expect_no_offenses(<<~RUBY)
        class MyModel < ApplicationRecord
          def all_projects(limit)
            Project.where(id: self.limit(limit).pluck(:id))
          end
        end
      RUBY
    end

    it 'does not flags the use of pluck inside a model class method' do
      allow(cop).to receive(:in_model?).and_return(true)

      expect_no_offenses(<<~RUBY)
        class MyClass < Model
          def all_users
           User.where(id: self.limit(100).pluck(:id))
          end
        end
      RUBY
    end

    it 'does not flags the use of pluck inside a finder' do
      allow(cop).to receive(:in_finder?).and_return(true)

      expect_no_offenses(<<~RUBY)
        class MyFinder
          def find(path)
           Project.where(path: path).limit(100).pluck(:id)
          end
        end
      RUBY
    end

    it 'flags the use of pluck inside a service' do
      allow(cop).to receive(:in_service_class?).and_return(true)

      expect_no_offenses(<<~RUBY)
        class MyService
          def delete_all(project)
           delete(project.for_scan_result_policy_read(scan_result_policy_reads.limit(100).pluck(:id)))
          end
        end
      RUBY
    end
  end
end