resque_spec 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -46,6 +46,19 @@ And I write this spec using the `resque_spec` matcher
46
46
 
47
47
  (And I take note of the `before` block that is calling `reset!` for every spec)
48
48
 
49
+ And I might use the `in` statement to specify the queue:
50
+
51
+ describe "#recalculate" do
52
+ before do
53
+ ResqueSpec.reset!
54
+ end
55
+
56
+ it "adds person.calculate to the Person queue" do
57
+ person.recalculate
58
+ Person.should have_queued(person.id, :calculate).in(:people)
59
+ end
60
+ end
61
+
49
62
  And I might write this as a Cucumber step
50
63
 
51
64
  Then /the (\w?) has (\w?) queued/ do |thing, method|
data/lib/resque_spec.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'resque_spec/resque_spec'
2
2
  require 'resque_spec/resque_scheduler_spec'
3
3
  require 'resque_spec/helpers'
4
+ require 'resque_spec/matchers'
4
5
 
5
6
  config = RSpec.configuration
6
7
  config.include ResqueSpec::Helpers
@@ -20,7 +20,7 @@ module ResqueSpec
20
20
  def disable_perform
21
21
  ::Resque.module_eval do
22
22
  def self.enqueue(klass, *args)
23
- ResqueSpec::Resque.enqueue(klass, *args)
23
+ ::Resque::Job.create(ResqueSpec.queue_name(klass), klass, *args)
24
24
  end
25
25
  end
26
26
  end
@@ -0,0 +1,81 @@
1
+ require 'rspec'
2
+
3
+ RSpec::Matchers.define :have_queued do |*expected_args|
4
+ match do |actual|
5
+ ResqueSpec.in_queue?(actual, *expected_args, :queue_name => @queue_name)
6
+ end
7
+
8
+ chain :in do |queue_name|
9
+ @queue_name = queue_name
10
+ end
11
+
12
+ failure_message_for_should do |actual|
13
+ "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
14
+ end
15
+
16
+ failure_message_for_should_not do |actual|
17
+ "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
18
+ end
19
+
20
+ description do
21
+ "have queued arguments of [#{expected_args.join(', ')}]"
22
+ end
23
+ end
24
+
25
+ RSpec::Matchers.define :have_queue_size_of do |size|
26
+ match do |actual|
27
+ (@queue ||= ResqueSpec.queue_for(actual)).size == size
28
+ end
29
+
30
+ chain :in do |queue_name|
31
+ @queue = ResqueSpec.queues[queue_name]
32
+ end
33
+
34
+ failure_message_for_should do |actual|
35
+ "expected that #{actual} would have #{size} entries queued, but got #{@queue.size} instead"
36
+ end
37
+
38
+ failure_message_for_should_not do |actual|
39
+ "expected that #{actual} would not have #{size} entries queued, but got #{@queue..size} instead"
40
+ end
41
+
42
+ description do
43
+ "have a queue size of #{size}"
44
+ end
45
+ end
46
+
47
+ RSpec::Matchers.define :have_scheduled do |*expected_args|
48
+ match do |actual|
49
+ ResqueSpec.scheduled_anytime?(actual, *expected_args)
50
+ end
51
+
52
+ failure_message_for_should do |actual|
53
+ "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
54
+ end
55
+
56
+ failure_message_for_should_not do |actual|
57
+ "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
58
+ end
59
+
60
+ description do
61
+ "have scheduled arguments"
62
+ end
63
+ end
64
+
65
+ RSpec::Matchers.define :have_scheduled_at do |*expected_args|
66
+ match do |actual|
67
+ ResqueSpec.scheduled?(actual, *expected_args)
68
+ end
69
+
70
+ failure_message_for_should do |actual|
71
+ "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
72
+ end
73
+
74
+ failure_message_for_should_not do |actual|
75
+ "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
76
+ end
77
+
78
+ description do
79
+ "have scheduled at the given time the arguments"
80
+ end
81
+ end
@@ -3,11 +3,11 @@ require 'resque_spec'
3
3
  module ResqueSpec
4
4
 
5
5
  def scheduled?(klass, time, *args)
6
- schedule_for(klass).any? {|entry| entry[:klass] == klass && entry[:time] == time && entry[:args] == args}
6
+ schedule_for(klass).any? {|entry| entry[:klass].to_s == klass.to_s && entry[:time] == time && entry[:args] == args}
7
7
  end
8
8
 
9
9
  def scheduled_anytime?(klass, *args)
10
- schedule_for(klass).any? {|entry| entry[:klass] == klass && entry[:args] == args}
10
+ schedule_for(klass).any? {|entry| entry[:klass].to_s == klass.to_s && entry[:args] == args}
11
11
  end
12
12
 
13
13
  def schedule_for(klass)
@@ -17,46 +17,11 @@ module ResqueSpec
17
17
 
18
18
  module ResqueScheduler
19
19
  def enqueue_at(time, klass, *args)
20
- ResqueSpec.schedule_for(klass) << {:klass => klass, :time => time, :args => args}
20
+ ResqueSpec.schedule_for(klass) << {:klass => klass.to_s, :time => time, :args => args}
21
21
  end
22
22
  end
23
- end
24
-
25
- Resque.extend(ResqueSpec::ResqueScheduler)
26
-
27
- RSpec::Matchers.define :have_scheduled do |*expected_args|
28
- match do |actual|
29
- ResqueSpec.scheduled_anytime?(actual, *expected_args)
30
- end
31
23
 
32
- failure_message_for_should do |actual|
33
- "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
34
- end
35
-
36
- failure_message_for_should_not do |actual|
37
- "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
38
- end
39
-
40
- description do
41
- "have scheduled arguments"
42
- end
43
24
  end
44
25
 
45
- RSpec::Matchers.define :have_scheduled_at do |*expected_args|
46
- match do |actual|
47
- ResqueSpec.scheduled?(actual, *expected_args)
48
- end
49
-
50
- failure_message_for_should do |actual|
51
- "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
52
- end
53
-
54
- failure_message_for_should_not do |actual|
55
- "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
56
- end
57
-
58
- description do
59
- "have scheduled at the given time the arguments"
60
- end
61
- end
26
+ Resque.extend(ResqueSpec::ResqueScheduler)
62
27
 
@@ -1,11 +1,12 @@
1
- require 'rspec'
2
1
  require 'resque'
3
2
 
4
3
  module ResqueSpec
5
4
  extend self
6
5
 
7
6
  def in_queue?(klass, *args)
8
- queue_for(klass).any? {|entry| entry[:klass] == klass && entry[:args] == args}
7
+ options = args.last.is_a?(Hash) ? args.pop : {}
8
+ queue = options[:queue_name] ? queues[options[:queue_name]] : queue_for(klass)
9
+ queue.any? {|entry| entry[:klass].to_s == klass.to_s && entry[:args] == args}
9
10
  end
10
11
 
11
12
  def queue_for(klass)
@@ -13,9 +14,13 @@ module ResqueSpec
13
14
  end
14
15
 
15
16
  def queue_name(klass)
17
+ if klass.is_a?(String)
18
+ klass = Kernel.const_get(klass) rescue nil
19
+ end
20
+
16
21
  name_from_instance_var(klass) or
17
22
  name_from_queue_accessor(klass) or
18
- raise ::Resque::NoQueueError.new("Jobs must be placed onto a queue.")
23
+ raise ::Resque::NoQueueError.new("Jobs must be placed onto a queue.")
19
24
  end
20
25
 
21
26
  def queue_size(klass)
@@ -30,58 +35,56 @@ module ResqueSpec
30
35
  queues.clear
31
36
  end
32
37
 
33
- module Resque
34
- extend self
35
- def enqueue(klass, *args)
36
- ResqueSpec.queue_for(klass) << {:klass => klass, :args => args}
38
+ private
39
+
40
+ def name_from_instance_var(klass)
41
+ klass.instance_variable_get(:@queue)
37
42
  end
38
- end
39
43
 
40
- private
44
+ def name_from_queue_accessor(klass)
45
+ klass.respond_to?(:queue) and klass.queue
46
+ end
41
47
 
42
- def name_from_instance_var(klass)
43
- klass.instance_variable_get(:@queue)
44
- end
48
+ module Resque
49
+ extend self
45
50
 
46
- def name_from_queue_accessor(klass)
47
- klass.respond_to?(:queue) and klass.queue
48
- end
49
- end
51
+ def reset!
52
+ ResqueSpec.reset!
53
+ end
50
54
 
51
- Resque.extend(ResqueSpec::Resque)
55
+ module Job
56
+ extend self
52
57
 
53
- RSpec::Matchers.define :have_queued do |*expected_args|
54
- match do |actual|
55
- ResqueSpec.in_queue?(actual, *expected_args)
56
- end
58
+ def self.included(base)
59
+ base.instance_eval do
57
60
 
58
- failure_message_for_should do |actual|
59
- "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
60
- end
61
+ def create(queue, klass, *args)
62
+ raise ::Resque::NoQueueError.new("Jobs must be placed onto a queue.") if !queue
63
+ raise ::Resque::NoClassError.new("Jobs must be given a class.") if klass.to_s.empty?
64
+ ResqueSpec.queues[queue] << {:klass => klass.to_s, :args => args}
65
+ end
61
66
 
62
- failure_message_for_should_not do |actual|
63
- "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
64
- end
67
+ def destroy(queue, klass, *args)
68
+ raise ::Resque::NoQueueError.new("Jobs must have been placed onto a queue.") if !queue
69
+ raise ::Resque::NoClassError.new("Jobs must have been given a class.") if klass.to_s.empty?
65
70
 
66
- description do
67
- "have queued arguments of [#{expected_args.join(', ')}]"
68
- end
69
- end
71
+ old_count = ResqueSpec.queues[queue].size
70
72
 
71
- RSpec::Matchers.define :have_queue_size_of do |size|
72
- match do |actual|
73
- ResqueSpec.queue_size(actual) == size
74
- end
73
+ if args.empty?
74
+ ResqueSpec.queues[queue].delete_if{ |job| job[:klass] == klass.to_s }
75
+ else
76
+ ResqueSpec.queues[queue].delete_if{ |job| job[:klass] == klass.to_s and job[:args].to_a == args.to_a }
77
+ end
78
+ old_count - ResqueSpec.queues[queue].size
79
+ end
75
80
 
76
- failure_message_for_should do |actual|
77
- "expected that #{actual} would have #{size} entries queued"
78
- end
81
+ end
82
+ end
79
83
 
80
- failure_message_for_should_not do |actual|
81
- "expected that #{actual} would not have #{size} entries queued"
82
- end
84
+ end
83
85
 
84
- description do
85
- "have a queue size of #{size}"
86
86
  end
87
87
  end
88
+
89
+ Resque.extend ResqueSpec::Resque
90
+ Resque::Job.send :include, ResqueSpec::Resque::Job
@@ -1,3 +1,3 @@
1
1
  module ResqueSpec
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: resque_spec
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.3
5
+ version: 0.5.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Les Hill
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-02 00:00:00 -05:00
13
+ date: 2011-03-16 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,7 @@ extra_rdoc_files: []
45
45
 
46
46
  files:
47
47
  - lib/resque_spec/helpers.rb
48
+ - lib/resque_spec/matchers.rb
48
49
  - lib/resque_spec/resque_scheduler_spec.rb
49
50
  - lib/resque_spec/resque_spec.rb
50
51
  - lib/resque_spec/version.rb
@@ -76,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
77
  requirements: []
77
78
 
78
79
  rubyforge_project:
79
- rubygems_version: 1.5.0
80
+ rubygems_version: 1.6.0
80
81
  signing_key:
81
82
  specification_version: 3
82
83
  summary: RSpec matchers for Resque