resque_unit 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ ResqueUnit
2
+ ==========
3
+
4
+ ResqueUnit provides some extra assertions and a mock Resque for
5
+ testing Rails code that depends on Resque. You can install it as
6
+ either a gem or a plugin:
7
+
8
+ gem install resque_unit
9
+
10
+ and in your test.rb:
11
+
12
+ config.gem 'resque_unit'
13
+
14
+ If you'd rather install it as a plugin, you should be able to run
15
+
16
+ script/plugin install git://github.com/justinweiss/resque_unit.git
17
+
18
+ inside your Rails projects.
19
+
20
+ Examples
21
+ ========
22
+
23
+ ResqueUnit provides some extra assertions for your unit tests. For
24
+ example, if you have code that queues a resque job:
25
+
26
+ class MyJob
27
+ @queue = :low
28
+
29
+ def self.perform(x)
30
+ // do stuff
31
+ end
32
+ end
33
+
34
+ def queue_job
35
+ Resque.enqueue(MyJob, 1)
36
+ end
37
+
38
+ You can write a unit test for code that queues this job:
39
+
40
+ def test_job_queued
41
+ queue_job
42
+ assert_queued(MyJob) # assert that MyJob was queued in the :low queue
43
+ end
44
+
45
+ You can also verify that a job was queued with arguments:
46
+
47
+ def test_job_queued_with_arguments
48
+ queue_job
49
+ assert_queued(MyJob, [1])
50
+ end
51
+
52
+ And you can run all the jobs in the queue, so you can verify that they
53
+ run correctly:
54
+
55
+ def test_job_runs
56
+ queue_job
57
+ Resque.run!
58
+ assert stuff_was_done, "Job didn't run"
59
+ end
60
+
61
+ You can also access the queues directly:
62
+
63
+ def test_jobs_in_queue
64
+ queue_job
65
+ assert_equal 1, Resque.queue(:low).length
66
+ end
67
+
68
+ Caveats
69
+ =======
70
+
71
+ * You should make sure that you call `Resque.reset!` in your test's
72
+ setup method to clear all of the test queues.
73
+
74
+ Copyright (c) 2010 Justin Weiss, released under the MIT license
@@ -0,0 +1,9 @@
1
+ module ResqueUnit
2
+ end
3
+
4
+ require 'resque_unit/resque'
5
+ require 'resque_unit/assertions'
6
+
7
+
8
+ Test::Unit::TestCase.send(:include, ResqueUnit::Assertions)
9
+
@@ -0,0 +1,35 @@
1
+ # These are a group of assertions you can use in your unit tests to
2
+ # verify that your code is using Resque correctly.
3
+ module ResqueUnit::Assertions
4
+
5
+ # Asserts that +klass+ has been queued into its appropriate queue at
6
+ # least once. If +args+ is nil, it only asserts that the klass has
7
+ # been queued. Otherwise, it asserts that the klass has been queued
8
+ # with the correct arguments. Pass an empty array for +args+ if you
9
+ # want to assert that klass has been queued without arguments.
10
+ def assert_queued(klass, args = nil, message = nil)
11
+ queue = Resque.queue_for(klass)
12
+ assert_block (message || "#{klass} should have been queued in #{queue}: #{Resque.queue(queue).inspect}.") do
13
+ in_queue?(queue, klass, args)
14
+ end
15
+ end
16
+
17
+ # The opposite of +assert_queued+.
18
+ def assert_not_queued(klass, args = nil, message = nil)
19
+ queue = Resque.queue_for(klass)
20
+ assert_block (message || "#{klass} should have been queued in #{queue}.") do
21
+ !in_queue?(queue, klass, args)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def in_queue?(queue, klass, args = nil)
28
+ if args # verify the klass and args match some element in the queue
29
+ !Resque.queue(queue).select {|e| e[:klass] == klass && e[:args] == args}.empty?
30
+ else # if no args were passed, just verify the job is in the queue
31
+ !Resque.queue(queue).select {|e| e[:klass] == klass}.empty?
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,40 @@
1
+ # The fake Resque class. This needs to be loaded after the real Resque
2
+ # for the assertions in +ResqueUnit::Assertions+ to work.
3
+ module Resque
4
+
5
+ # Resets all the queues to the empty state. This should be called in
6
+ # your test's +setup+ method until I can figure out a way for it to
7
+ # automatically be called.
8
+ def self.reset!
9
+ @queue = Hash.new { |h, k| h[k] = [] }
10
+ end
11
+
12
+ # Returns an array of all the jobs that have been queued. Each
13
+ # element is of the form +{:klass => klass, :args => args}+ where
14
+ # +klass+ is the job's class and +args+ is an array of the arguments
15
+ # passed to the job.
16
+ def self.queue(queue)
17
+ self.reset unless @queue
18
+ @queue[queue]
19
+ end
20
+
21
+ # Executes all jobs in all queues in an undefined order.
22
+ def self.run!
23
+ @queue.each do |k, v|
24
+ while job = v.shift
25
+ job[:klass].perform(*job[:args])
26
+ end
27
+ end
28
+ end
29
+
30
+ # :nodoc:
31
+ def self.enqueue(klass, *args)
32
+ queue(queue_for(klass)) << {:klass => klass, :args => args}
33
+ end
34
+
35
+ # :nodoc:
36
+ def self.queue_for(klass)
37
+ klass.instance_variable_get(:@queue)
38
+ end
39
+
40
+ end
@@ -0,0 +1,91 @@
1
+ require 'test_helper'
2
+
3
+ class ResqueUnitTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ # should probably happen automatically, but I haven't thought of a
7
+ # good way to hook setup() yet.
8
+ Resque.reset!
9
+ end
10
+
11
+ context "A task that schedules a resque job" do
12
+ setup do
13
+ Resque.enqueue(LowPriorityJob)
14
+ end
15
+
16
+ should "pass the assert_queued(job) assertion" do
17
+ assert_queued(LowPriorityJob)
18
+ end
19
+
20
+ should "fail the assert_not_queued(job) assertion" do
21
+ assert_raise Test::Unit::AssertionFailedError do
22
+ assert_not_queued(LowPriorityJob)
23
+ end
24
+ end
25
+
26
+ context ", when Resque.run! is called," do
27
+ setup do
28
+ assert !LowPriorityJob.run?, "The job should not have been run yet"
29
+ Resque.run!
30
+ end
31
+
32
+ teardown do
33
+ LowPriorityJob.run = false
34
+ end
35
+
36
+ should "run the job" do
37
+ assert LowPriorityJob.run?, "The job should have run"
38
+ end
39
+
40
+ should "clear the job from the queue" do
41
+ assert_not_queued(LowPriorityJob)
42
+ end
43
+ end
44
+
45
+ # assert number of jobs?
46
+ end
47
+
48
+ context "An empty queue" do
49
+ should "pass the assert_not_queued(job) assertion" do
50
+ assert_not_queued(LowPriorityJob)
51
+ end
52
+
53
+ should "fail the assert_queued(job) assertion" do
54
+ assert_raise Test::Unit::AssertionFailedError do
55
+ assert_queued(LowPriorityJob)
56
+ end
57
+ end
58
+ end
59
+
60
+ context "A task that schedules a resque job with arguments" do
61
+ setup do
62
+ Resque.enqueue(JobWithArguments, 1, "test")
63
+ end
64
+
65
+ should "pass the assert_queued(job, *args) assertion if the args match" do
66
+ assert_queued(JobWithArguments, [1, "test"])
67
+ end
68
+
69
+ should "pass the assert_queued(job) assertion with no args passed" do
70
+ assert_queued(JobWithArguments)
71
+ end
72
+
73
+ should "fail the assert_queued(job) assertion if the args don't match" do
74
+ assert_raise Test::Unit::AssertionFailedError do
75
+ assert_queued(JobWithArguments, [2, "test"])
76
+ end
77
+ end
78
+
79
+ should "pass the assert_not_queued(job) assertion if the args don't match" do
80
+ assert_not_queued(JobWithArguments, [2, "test"])
81
+ end
82
+
83
+ should "fail the assert_not_queued(job) assertion if the args match" do
84
+ assert_raise Test::Unit::AssertionFailedError do
85
+ assert_not_queued(JobWithArguments, [1, "test"])
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,24 @@
1
+ class LowPriorityJob
2
+ @queue = :low
3
+ @run = false
4
+ def self.perform
5
+ self.run = true
6
+ end
7
+
8
+ def self.run?
9
+ @run
10
+ end
11
+
12
+ def self.run=(value)
13
+ @run = value
14
+ end
15
+
16
+ end
17
+
18
+ class JobWithArguments
19
+ @queue = :medium
20
+
21
+ def self.perform(num, text)
22
+
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+ require 'shoulda'
5
+ require 'resque_unit'
6
+ require 'sample_jobs'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque_unit
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Justin Weiss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-05-04 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: justin@uberweiss.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.md
33
+ files:
34
+ - lib/resque_unit/assertions.rb
35
+ - lib/resque_unit/resque.rb
36
+ - lib/resque_unit.rb
37
+ - test/resque_unit_test.rb
38
+ - test/sample_jobs.rb
39
+ - test/test_helper.rb
40
+ - README.md
41
+ has_rdoc: true
42
+ homepage: http://github.com/justinweiss/resque_unit
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Test::Unit support for resque job queueing
69
+ test_files:
70
+ - test/resque_unit_test.rb