File: bulk_database_actions_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (99 lines) | stat: -rw-r--r-- 3,022 bytes parent folder | download
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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'
require 'gitlab/dangerfiles/spec_helper'

require_relative '../../../tooling/danger/bulk_database_actions'
require_relative '../../../tooling/danger/project_helper'

RSpec.describe Tooling::Danger::BulkDatabaseActions, feature_category: :tooling do
  include_context 'with dangerfile'

  let(:fake_danger) { DangerSpecHelper.fake_danger }
  let(:fake_project_helper) { instance_double(Tooling::Danger::ProjectHelper) }
  let(:comment_text) { "\n#{described_class::SUGGESTION}" }
  let(:file_lines) { file_diff.map { |line| line.delete_prefix('+') } }

  before do
    allow(bulk_database_actions).to receive(:project_helper).and_return(fake_project_helper)
    allow(bulk_database_actions.project_helper).to receive(:file_lines).and_return(file_lines)
    allow(bulk_database_actions.helper).to receive(:added_files).and_return([filename])
    allow(bulk_database_actions.helper).to receive(:changed_lines).with(filename).and_return(file_diff)

    bulk_database_actions.define_singleton_method(:add_suggestions_for) do |filename|
      Tooling::Danger::BulkDatabaseActions.new(filename, context: self).suggest
    end
  end

  subject(:bulk_database_actions) { fake_danger.new(helper: fake_helper) }

  context 'for single line method call' do
    let(:file_diff) do
      <<~DIFF.split("\n")
        +    def execute
        +      #{code}
        +
        +      ServiceResponse.success
        +    end
      DIFF
    end

    context 'when file is a non-spec Ruby file' do
      let(:filename) { 'app/services/personal_access_tokens/revoke_token_family_service.rb' }

      using RSpec::Parameterized::TableSyntax

      context 'when comment is expected' do
        where(:code) do
          [
            'update_all(revoked: true)',
            'destroy_all',
            'delete_all',
            'update(revoked: true)',
            'delete',
            'upsert',
            'upsert_all',
            'User.upsert',
            'User.last.destroy',
            'destroy',
            ' .destroy',
            'bulk_update',
            'bulk_delete',
            'bulk_upsert!',
            'bulk_insert!'
          ]
        end

        with_them do
          specify do
            expect(bulk_database_actions).to receive(:markdown).with(comment_text.chomp, file: filename, line: 2)

            bulk_database_actions.add_suggestions_for(filename)
          end
        end
      end

      context 'when no comment is expected' do
        where(:code) do
          [
            'we update bob',
            'update_two_factor',
            'delete_keys(key)',
            'destroy_hook(hook)',
            'destroy_all_merged',
            'update_all_mirrors'
          ]
        end

        with_them do
          specify do
            expect(bulk_database_actions).not_to receive(:markdown)

            bulk_database_actions.add_suggestions_for(filename)
          end
        end
      end
    end
  end
end