Module: RSpec::SidekiqPro::Matchers::JobMatcher
- Includes:
- Matchers::Composable
- Included in:
- EnqueueSidekiqJobs, HaveEnqueuedSidekiqJobs
- Defined in:
- lib/rspec/sidekiq_pro/matchers/job_matcher.rb
Instance Attribute Summary collapse
-
#actual_jobs ⇒ Object
readonly
Returns the value of attribute actual_jobs.
-
#expected_arguments ⇒ Object
readonly
Returns the value of attribute expected_arguments.
-
#expected_batch ⇒ Object
readonly
Returns the value of attribute expected_batch.
-
#expected_count ⇒ Object
readonly
Returns the value of attribute expected_count.
-
#expected_interval ⇒ Object
readonly
Returns the value of attribute expected_interval.
-
#expected_schedule ⇒ Object
readonly
Returns the value of attribute expected_schedule.
-
#expected_timestamp ⇒ Object
readonly
Returns the value of attribute expected_timestamp.
-
#expected_without_batch ⇒ Object
readonly
Returns the value of attribute expected_without_batch.
-
#worker_class ⇒ Object
readonly
Returns the value of attribute worker_class.
Instance Method Summary collapse
- #actual_jobs_details_in_failure_message ⇒ Object
- #actual_jobs_size_in_failure_message ⇒ Object
- #at(timestamp) ⇒ Object
- #batch_match?(expected_batch, bid) ⇒ Boolean
- #does_not_match?(jobs) ⇒ Boolean
- #exactly(times) ⇒ Object
-
#expectations_in_failure_message ⇒ Object
rubocop:disable Layout/ExtraSpacing It becomes unreadable when not allowing alignement.
-
#expected_interval_output ⇒ Object
rubocop:enable Layout/ExtraSpacing.
- #expected_job_description ⇒ Object
- #failure_message_diff ⇒ Object
- #filter_jobs(jobs) ⇒ Object
- #in(interval) ⇒ Object
- #job_details_in_failure_message(job) ⇒ Object
- #matches?(jobs) ⇒ Boolean
- #normalize_arguments(arguments) ⇒ Object
- #once ⇒ Object
- #output_arguments(arguments) ⇒ Object
- #output_batch(value) ⇒ Object
- #output_schedule(timestamp) ⇒ Object
- #times ⇒ Object (also: #time)
- #twice ⇒ Object
- #with(*expected_arguments, &block) ⇒ Object
- #within_batch(batch_expectation = :__undef__, &block) ⇒ Object
- #without_batch ⇒ Object
Instance Attribute Details
#actual_jobs ⇒ Object (readonly)
Returns the value of attribute actual_jobs.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def actual_jobs @actual_jobs end |
#expected_arguments ⇒ Object (readonly)
Returns the value of attribute expected_arguments.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def expected_arguments @expected_arguments end |
#expected_batch ⇒ Object (readonly)
Returns the value of attribute expected_batch.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def expected_batch @expected_batch end |
#expected_count ⇒ Object (readonly)
Returns the value of attribute expected_count.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def expected_count @expected_count end |
#expected_interval ⇒ Object (readonly)
Returns the value of attribute expected_interval.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def expected_interval @expected_interval end |
#expected_schedule ⇒ Object (readonly)
Returns the value of attribute expected_schedule.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def expected_schedule @expected_schedule end |
#expected_timestamp ⇒ Object (readonly)
Returns the value of attribute expected_timestamp.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def @expected_timestamp end |
#expected_without_batch ⇒ Object (readonly)
Returns the value of attribute expected_without_batch.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def expected_without_batch @expected_without_batch end |
#worker_class ⇒ Object (readonly)
Returns the value of attribute worker_class.
9 10 11 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 9 def worker_class @worker_class end |
Instance Method Details
#actual_jobs_details_in_failure_message ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 181 def actual_jobs.flat_map do |job| (job).map.with_index do |line, index| if actual_jobs.size > 1 indent = " " indent = " - " if index.zero? line = "#{indent}#{line[2..]}" end line end end end |
#actual_jobs_size_in_failure_message ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 150 def if actual_jobs.empty? "no #{worker_class} found" else "found #{actual_jobs.size} #{worker_class}" end end |
#at(timestamp) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 39 def at() raise "setting expecations with both `at` and `in` is not supported" if @expected_interval @expected_timestamp = @expected_schedule = .to_i self end |
#batch_match?(expected_batch, bid) ⇒ Boolean
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 233 def batch_match?(expected_batch, bid) case expected_batch when :__undef__ !bid.nil? when String expected_batch == bid when ::Sidekiq::Batch expected_batch.bid == bid else return unless bid batch = ::Sidekiq::Batch.new(bid) values_match?(expected_batch, batch) end end |
#does_not_match?(jobs) ⇒ Boolean
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 97 def does_not_match?(jobs) @actual_jobs = jobs filtered_jobs = filter_jobs(actual_jobs) if expected_count filtered_jobs.count != expected_count else filtered_jobs.empty? end end |
#exactly(times) ⇒ Object
55 56 57 58 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 55 def exactly(times) @expected_count = times self end |
#expectations_in_failure_message ⇒ Object
rubocop:disable Layout/ExtraSpacing It becomes unreadable when not allowing alignement
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 160 def = [] << " exactly: #{expected_count} time(s)" if expected_count << " arguments: #{expected_arguments}" if expected_arguments << " in: #{expected_interval_output}" if expected_interval << " at: #{}" if << " batch: #{output_batch(expected_batch)}" if expected_batch << " batch: no batch" if expected_without_batch end |
#expected_interval_output ⇒ Object
rubocop:enable Layout/ExtraSpacing
197 198 199 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 197 def expected_interval_output "#{expected_interval.inspect} (#{output_schedule(expected_schedule)})" end |
#expected_job_description ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 116 def expected_job_description description = "#{worker_class} job" if expected_count == 1 description += " once" elsif expected_count == 2 description += " twice" elsif expected_count description += " #{expected_count} times" end if expected_arguments.is_a?(Proc) description += " with some arguments" elsif expected_arguments description += " with arguments #{expected_arguments}" end description end |
#failure_message_diff ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 136 def = [] += << "" if .any? << if expected_arguments || expected_schedule || expected_without_batch || expected_batch [-1] = "#{[-1]}:" += end .join("\n") end |
#filter_jobs(jobs) ⇒ Object
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 222 def filter_jobs(jobs) jobs.select do |job| next if expected_arguments && !values_match?(expected_arguments, job["args"]) next if expected_schedule && !values_match?(expected_schedule.to_i, job["at"].to_i) next if expected_without_batch && job["bid"] next if expected_batch && !batch_match?(expected_batch, job["bid"]) true end end |
#in(interval) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 31 def in(interval) raise "setting expecations with both `at` and `in` is not supported" if @expected_timestamp @expected_interval = interval @expected_schedule = interval.from_now.to_i self end |
#job_details_in_failure_message(job) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 171 def (job) = [] << " arguments: #{job["args"]}" if expected_arguments << " at: #{output_schedule(job["at"])}" if expected_schedule && job["at"] << " at: no schedule" if expected_schedule && !job["at"] << " batch: #{output_batch(job["bid"])}" if (expected_without_batch || expected_batch) && job["bid"] << " batch: no batch" if (expected_without_batch || expected_batch) && !job["bid"] end |
#matches?(jobs) ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 86 def matches?(jobs) @actual_jobs = jobs filtered_jobs = filter_jobs(actual_jobs) if expected_count filtered_jobs.count == expected_count else filtered_jobs.any? end end |
#normalize_arguments(arguments) ⇒ Object
108 109 110 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 108 def normalize_arguments(arguments) JSON.parse(JSON.dump(arguments)) end |
#once ⇒ Object
47 48 49 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 47 def once exactly(1) end |
#output_arguments(arguments) ⇒ Object
112 113 114 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 112 def output_arguments(arguments) arguments.map(&:inspect).join(", ") end |
#output_batch(value) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 205 def output_batch(value) case value when :__undef__ "to be present" when String "<Sidekiq::Batch bid: #{value.inspect}>" when Sidekiq::Batch "<Sidekiq::Batch bid: #{value.bid.inspect}>" else if value.respond_to?(:description) value.description else value end end end |
#output_schedule(timestamp) ⇒ Object
201 202 203 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 201 def output_schedule() Time.at() if end |
#times ⇒ Object Also known as: time
60 61 62 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 60 def times self end |
#twice ⇒ Object
51 52 53 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 51 def twice exactly(2) end |
#with(*expected_arguments, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 19 def with(*expected_arguments, &block) if block raise ArgumentError, "setting block to `with` is not supported for this matcher" if supports_value_expectations? raise ArgumentError, "setting arguments and block together in `with` is not supported" if expected_arguments.any? @expected_arguments = block else @expected_arguments = normalize_arguments(expected_arguments) end self end |
#within_batch(batch_expectation = :__undef__, &block) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 72 def within_batch(batch_expectation = :__undef__, &block) raise "setting expecations with both `without_batch` and `within_batch` is not supported" if @expected_without_batch if block raise ArgumentError, "setting arguments and block together in `with_batch` is not supported" if batch_expectation != :__undef__ @expected_batch = block else @expected_batch = batch_expectation end self end |
#without_batch ⇒ Object
65 66 67 68 69 70 |
# File 'lib/rspec/sidekiq_pro/matchers/job_matcher.rb', line 65 def without_batch raise "setting expecations with both `without_batch` and `within_batch` is not supported" if @expected_batch @expected_without_batch = true self end |