ryansch-resque_spec 0.2.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Les Hill
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,119 @@
1
+ ResqueSpec
2
+ ==========
3
+
4
+ A simple RSpec and Cucumber matcher for Resque.enqueue and Resque.enqueue_at (from `ResqueScheduler`), loosely based on
5
+ [http://github.com/justinweiss/resque_unit](http://github.com/justinweiss/resque_unit).
6
+
7
+ This should work with Resque v1.6.0 and up and RSpec v1.3.0 and up.
8
+
9
+ Install
10
+ -------
11
+
12
+ Install the gem
13
+
14
+ % gem install resque_spec
15
+
16
+ And update your Gemfile (Not using bundler? Do the necessary thing for
17
+ your app's gem management)
18
+
19
+ group :test do
20
+ gem 'resque_spec'
21
+ end
22
+
23
+ Resque with Specs
24
+ -----------------
25
+
26
+ Given this scenario
27
+
28
+ Given a person
29
+ When I recalculate
30
+ Then the person has calculate queued
31
+
32
+ And I write this spec using the `resque_spec` matcher
33
+
34
+ describe "#recalculate" do
35
+ before do
36
+ ResqueSpec.reset!
37
+ end
38
+
39
+ it "adds person.calculate to the Person queue" do
40
+ person.recalculate
41
+ Person.should have_queued(person.id, :calculate)
42
+ end
43
+ end
44
+
45
+ (And I take note of the `before` block that is calling `reset!` for every spec)
46
+
47
+ And I might write this as a Cucumber step
48
+
49
+ Then /the (\w?) has (\w?) queued/ do |thing, method|
50
+ thing_obj = instance_variable_get("@#{thing}")
51
+ thing_obj.class.should have_queued(thing_obj.id, method.to_sym)
52
+ end
53
+
54
+ Then I write some code to make it pass:
55
+
56
+ class Person
57
+ @queue = :people
58
+
59
+ def recalculate
60
+ Resque.enqueue(Person, id, :calculate)
61
+ end
62
+ end
63
+
64
+ ResqueScheduler with Specs
65
+ --------------------------
66
+
67
+ Given this scenario
68
+
69
+ Given a person
70
+ When I schedule a recalculate
71
+ Then the person has calculate scheduled
72
+
73
+ And I write this spec using the `resque_spec` matcher
74
+
75
+ describe "#recalculate" do
76
+ before do
77
+ ResqueSpec.reset!
78
+ end
79
+
80
+ it "adds person.calculate to the Person queue" do
81
+ person.recalculate
82
+ Person.should have_scheduled(person.id, :calculate)
83
+ end
84
+ end
85
+
86
+ (And I take note of the `before` block that is calling `reset!` for every spec)
87
+
88
+ And I might write this as a Cucumber step
89
+
90
+ Then /the (\w?) has (\w?) scheduled/ do |thing, method|
91
+ thing_obj = instance_variable_get("@#{thing}")
92
+ thing_obj.class.should have_scheduled(thing_obj.id, method.to_sym)
93
+ end
94
+
95
+ Then I write some code to make it pass:
96
+
97
+ class Person
98
+ @queue = :people
99
+
100
+ def recalculate
101
+ Resque.enqueue_at(Time.now + 3600, Person, id, :calculate)
102
+ end
103
+ end
104
+
105
+ Note on Patches/Pull Requests
106
+ =============================
107
+
108
+ * Fork the project.
109
+ * Make your feature addition or bug fix.
110
+ * Add tests for it. This is important so I don't break it in a
111
+ future version unintentionally.
112
+ * Commit, do not mess with rakefile, version, or history.
113
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
114
+ * Send me a pull request. Bonus points for topic branches.
115
+
116
+ Copyright
117
+ =========
118
+
119
+ Copyright (c) 2010 Les Hill. See LICENSE for details.
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'spec/rake/spectask'
5
+ Spec::Rake::SpecTask.new(:spec) do |spec|
6
+ spec.libs << 'lib' << 'spec'
7
+ spec.spec_files = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
11
+ spec.libs << 'lib' << 'spec'
12
+ spec.pattern = 'spec/**/*_spec.rb'
13
+ spec.rcov = true
14
+ end
15
+
16
+ task :default => :spec
17
+
18
+ require 'rake/rdoctask'
19
+ Rake::RDocTask.new do |rdoc|
20
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
21
+
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = "resque_spec #{version}"
24
+ rdoc.rdoc_files.include('README*')
25
+ rdoc.rdoc_files.include('lib/**/*.rb')
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'resque_spec/resque_spec'
2
+ require 'resque_spec/resque_scheduler_spec'
@@ -0,0 +1,62 @@
1
+ require 'resque_spec'
2
+
3
+ module ResqueSpec
4
+
5
+ def scheduled?(klass, time, *args)
6
+ schedule_for(klass).any? {|entry| entry[:klass] == klass && entry[:time] == time && entry[:args] == args}
7
+ end
8
+
9
+ def scheduled_anytime?(klass, *args)
10
+ schedule_for(klass).any? {|entry| entry[:klass] == klass && entry[:args] == args}
11
+ end
12
+
13
+ def schedule_for(klass)
14
+ name = queue_name(klass).to_s << "_scheduled"
15
+ queues[name]
16
+ end
17
+
18
+ module ResqueScheduler
19
+ def enqueue_at(time, klass, *args)
20
+ ResqueSpec.schedule_for(klass) << {:klass => klass, :time => time, :args => args}
21
+ end
22
+ end
23
+ end
24
+
25
+ Resque.extend(ResqueSpec::ResqueScheduler)
26
+
27
+ Spec::Matchers.define :have_scheduled do |*expected_args|
28
+ match do |actual|
29
+ ResqueSpec.scheduled_anytime?(actual, *expected_args)
30
+ end
31
+
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
+ end
44
+
45
+ Spec::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
62
+
@@ -0,0 +1,70 @@
1
+ require 'resque'
2
+ require 'spec'
3
+
4
+ module ResqueSpec
5
+ extend self
6
+
7
+ def in_queue?(klass, *args)
8
+ queue_for(klass).any? {|entry| entry[:klass] == klass && entry[:args] == args}
9
+ end
10
+
11
+ def queue_for(klass)
12
+ queues[queue_name(klass)]
13
+ end
14
+
15
+ def queue_name(klass)
16
+ name_from_instance_var(klass) or
17
+ name_from_queue_accessor(klass) or
18
+ raise ::Resque::NoQueueError.new("Jobs must be placed onto a queue.")
19
+ end
20
+
21
+ def queues
22
+ @queues ||= Hash.new {|h,k| h[k] = []}
23
+ end
24
+
25
+ def reset!
26
+ queues.clear
27
+ end
28
+
29
+ module Resque
30
+ def enqueue(klass, *args)
31
+ ResqueSpec.queue_for(klass) << {:klass => klass, :args => args}
32
+ end
33
+ def dequeue(klass, *args)
34
+ ResqueSpec.queue_for(klass).delete_if do |job|
35
+ job[:class] == klass.to_s && args.empty? || job[:args] == args
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def name_from_instance_var(klass)
43
+ klass.instance_variable_get(:@queue)
44
+ end
45
+
46
+ def name_from_queue_accessor(klass)
47
+ klass.respond_to?(:queue) and klass.queue
48
+ end
49
+ end
50
+
51
+ Resque.extend(ResqueSpec::Resque)
52
+
53
+ Spec::Matchers.define :have_queued do |*expected_args|
54
+ match do |actual|
55
+ ResqueSpec.in_queue?(actual, *expected_args)
56
+ end
57
+
58
+ failure_message_for_should do |actual|
59
+ "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
60
+ end
61
+
62
+ failure_message_for_should_not do |actual|
63
+ "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
64
+ end
65
+
66
+ description do
67
+ "have queued arguments"
68
+ end
69
+ end
70
+
@@ -0,0 +1,3 @@
1
+ module ResqueSpec
2
+ VERSION = '0.2.3.1'
3
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ryansch-resque_spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Les Hill
9
+ - Nick Morgan
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-10-18 00:00:00.000000000 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: resque
18
+ requirement: &70217040727940 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: 1.6.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *70217040727940
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: &70217040727440 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.3.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *70217040727440
38
+ description: RSpec matchers for Resque
39
+ email: ryan@instanceinc.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - lib/resque_spec/resque_scheduler_spec.rb
45
+ - lib/resque_spec/resque_spec.rb
46
+ - lib/resque_spec/version.rb
47
+ - lib/resque_spec.rb
48
+ - LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ has_rdoc: true
52
+ homepage: http://github.com/ryansch/resque_spec
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.6
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.6.2
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: RSpec matchers for Resque
76
+ test_files: []