1# frozen_string_literal: true
2
3require 'fast_spec_helper'
4require_relative '../../../../rubocop/cop/scalability/cron_worker_context'
5
6RSpec.describe RuboCop::Cop::Scalability::CronWorkerContext do
7  subject(:cop) { described_class.new }
8
9  it 'adds an offense when including CronjobQueue' do
10    expect_offense(<<~CODE)
11      class SomeWorker
12        include CronjobQueue
13                ^^^^^^^^^^^^ Manually define an ApplicationContext for cronjob-workers.[...]
14      end
15    CODE
16  end
17
18  it 'does not add offenses for other workers' do
19    expect_no_offenses(<<~CODE)
20      class SomeWorker
21      end
22    CODE
23  end
24
25  it 'does not add an offense when the class defines a context' do
26    expect_no_offenses(<<~CODE)
27      class SomeWorker
28        include CronjobQueue
29
30        with_context user: 'bla'
31      end
32    CODE
33  end
34
35  it 'does not add an offense when the worker calls `with_context`' do
36    expect_no_offenses(<<~CODE)
37      class SomeWorker
38        include CronjobQueue
39
40        def perform
41          with_context(user: 'bla') do
42            # more work
43          end
44        end
45      end
46    CODE
47  end
48
49  it 'does not add an offense when the worker calls `bulk_perform_async_with_contexts`' do
50    expect_no_offenses(<<~CODE)
51      class SomeWorker
52        include CronjobQueue
53
54        def perform
55          SomeOtherWorker.bulk_perform_async_with_contexts(things,
56                                                           arguments_proc: -> (thing) { thing.id },
57                                                           context_proc: -> (thing) { { project: thing.project } })
58        end
59      end
60    CODE
61  end
62
63  it 'does not add an offense when the worker calls `bulk_perform_in_with_contexts`' do
64    expect_no_offenses(<<~CODE)
65      class SomeWorker
66        include CronjobQueue
67
68        def perform
69          SomeOtherWorker.bulk_perform_in_with_contexts(10.minutes, things,
70                                                        arguments_proc: -> (thing) { thing.id },
71                                                        context_proc: -> (thing) { { project: thing.project } })
72        end
73      end
74    CODE
75  end
76end
77