Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #on matcher #197

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ AwesomeJob.perform_in 5.minutes, 'Awesome', true
expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(5.minutes)
```

#### Testing queue set for job

Use the chainable `#on` matcher

```ruby
class AwesomeJob
include Sidekiq::Job

sidekiq_options queue: :low
end

AwesomeJob.perform_async("a little awesome")

# test with..
expect(AwesomeJob).to have_enqueued_sidekiq_job("a little awesome").on("low")

# Setting the queue when enqueuing
AwesomeJob.set(queue: "high").perform_async("Very Awesome!")

expect(AwesomeJob).to have_enqueued_sidekiq_job("Very Awesome!").on("high")
```


#### Testing ActiveMailer jobs

Expand Down
38 changes: 26 additions & 12 deletions lib/rspec/sidekiq/matchers/have_enqueued_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ def initialize(job)
@job = job
end

def matches?(option, value)
raise ArgumentError, "Option `#{option}` is not defined." unless %w[at].include?(option.to_s)
send("#{option}_evaluator", value)
def matches?(options)
with_context(**options)
end

private

def at_evaluator(value)
return false if job['at'].to_s.empty?
value == Time.at(job['at']).to_i
return false if job["at"].to_s.empty?
value == Time.at(job["at"]).to_i
end

def with_context(**expected_context)
expected_context.all? do |key, value|
if key == "at"
# send to custom evaluator
at_evaluator(value)
else
job.context.has_key?(key) && job.context[key] == value
end
end
end
end

Expand Down Expand Up @@ -92,7 +102,7 @@ def args
end

def context
@context||= job.except("args")
@context ||= job.except("args")
end
end

Expand Down Expand Up @@ -121,10 +131,9 @@ def arguments_matches?(job, arguments)
end

def options_matches?(job, options)
options.all? do |option, value|
parser = JobOptionParser.new(job)
parser.matches?(option, value)
end
parser = JobOptionParser.new(job)

parser.matches?(options)
end

def unwrap_jobs(jobs)
Expand All @@ -151,12 +160,17 @@ def matches?(klass)
end

def at(timestamp)
@expected_options['at'] = timestamp.to_time.to_i
@expected_options["at"] = timestamp.to_time.to_i
self
end

def in(interval)
@expected_options['at'] = (Time.now.to_f + interval.to_f).to_i
@expected_options["at"] = (Time.now.to_f + interval.to_f).to_i
self
end

def on(queue)
@expected_options["queue"] = queue
self
end

Expand Down
1 change: 1 addition & 0 deletions rspec-sidekiq.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Gem::Specification.new do |s|
s.license = "MIT"

s.add_dependency "rspec-core", "~> 3.0"
s.add_dependency "rspec-expectations", "~> 3.0"
s.add_dependency "sidekiq", ">= 5", "< 8"

s.add_development_dependency "pry"
Expand Down
14 changes: 14 additions & 0 deletions spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@
expect(worker).to have_enqueued_sidekiq_job(*worker_args_at).at(tomorrow)
end
end

context "#on queue" do
it "matches the queue in the context" do
worker.perform_async(*worker_args)
expect(worker).to have_enqueued_sidekiq_job(*worker_args).on("default")
end

context "when setting queue at runtime" do
it "matches the queue set" do
worker.set(queue: "highest").perform_async(*worker_args)
expect(worker).to have_enqueued_sidekiq_job(*worker_args).on("highest")
end
end
end
end

context 'ActiveJob' do
Expand Down