rspec-redis_helper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +6 -0
- data/lib/rspec-redis_helper/version.rb +5 -0
- data/lib/rspec-redis_helper.rb +39 -0
- data/rspec-redis_helper.gemspec +22 -0
- data/spec/helper.rb +15 -0
- data/spec/redis_helper_spec.rb +42 -0
- data/spec/redis_spec.rb +30 -0
- metadata +94 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mark Lanett
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# RSpec::RedisHelper
|
2
|
+
|
3
|
+
This module will help you write specs which use Redis.
|
4
|
+
It sets up two Redis connections.
|
5
|
+
It also clears out Redis between examples.
|
6
|
+
It uses db 1 on localhost by default.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'rspec-redis_helper'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install rspec-redis_helper
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Configure RSpec in your support or helper file like so:
|
25
|
+
|
26
|
+
RSpec.configure do |spec|
|
27
|
+
spec.include RSpec::RedisHelper, redis: true
|
28
|
+
|
29
|
+
# clean the Redis database around each run
|
30
|
+
# @see https://www.relishapp.com/rspec/rspec-core/docs/hooks/around-hooks
|
31
|
+
spec.around( :each, redis: true ) do |example|
|
32
|
+
with_clean_redis do
|
33
|
+
example.run
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
You will also need to configure your app to use one of these redis connections,
|
39
|
+
or to be initialized from RSpec::RedisHelper::TEST.
|
40
|
+
|
41
|
+
Then mark your Redis-using specs with redis: true, like so:
|
42
|
+
|
43
|
+
describe MyApp, redis: true do
|
44
|
+
it "has access to redis" do
|
45
|
+
redis.should_not be_nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "rspec-redis_helper/version"
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module RedisHelper
|
5
|
+
|
6
|
+
TEST = { url: "redis://127.0.0.1:6379/1" }
|
7
|
+
|
8
|
+
def redis
|
9
|
+
@redis ||= ::Redis.connect(TEST)
|
10
|
+
end
|
11
|
+
|
12
|
+
def redis2
|
13
|
+
@redis2 ||= ::Redis.connect(TEST)
|
14
|
+
end
|
15
|
+
|
16
|
+
def with_watch( redis, *args )
|
17
|
+
redis.watch( *args )
|
18
|
+
begin
|
19
|
+
yield
|
20
|
+
ensure
|
21
|
+
redis.unwatch
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def with_clean_redis(&block)
|
26
|
+
redis.client.disconnect # auto connect after fork
|
27
|
+
redis2.client.disconnect # auto connect after fork
|
28
|
+
redis.flushall # clean before run
|
29
|
+
begin
|
30
|
+
yield
|
31
|
+
ensure
|
32
|
+
redis.flushall # clean up after run
|
33
|
+
redis.quit # quit (close) connection
|
34
|
+
redis2.quit # quit (close) connection
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rspec-redis_helper/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Mark Lanett"]
|
6
|
+
gem.email = ["mark.lanett@gmail.com"]
|
7
|
+
gem.description = %q{Helper for RSpec tests which use Redis}
|
8
|
+
gem.summary = %q{Helper for RSpec tests which use Redis}
|
9
|
+
gem.homepage = "http://github.com/mlanett/rspec-redis_helper"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rspec-redis_helper"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RSpec::RedisHelper::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "redis"
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
gem.add_development_dependency "rspec"
|
22
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require "bundler/setup" # set up gem paths
|
3
|
+
require "redis"
|
4
|
+
require "rspec-redis_helper" # load this gem
|
5
|
+
|
6
|
+
RSpec.configure do |spec|
|
7
|
+
spec.include RSpec::RedisHelper, redis: true
|
8
|
+
|
9
|
+
# clean the Redis database around each example
|
10
|
+
spec.around( :each, redis: true ) do |example|
|
11
|
+
with_clean_redis do
|
12
|
+
example.run
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require "helper"
|
3
|
+
|
4
|
+
describe RSpec::RedisHelper do
|
5
|
+
|
6
|
+
include RSpec::RedisHelper
|
7
|
+
|
8
|
+
it "should be redis" do
|
9
|
+
redis.should be_kind_of(Redis)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should clean redis before and after" do
|
13
|
+
redis.set "it", "bar"
|
14
|
+
with_clean_redis do
|
15
|
+
redis.get("it").should eq(nil)
|
16
|
+
redis.set "it", "bar"
|
17
|
+
end
|
18
|
+
redis.get("it").should eq(nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has a watch helper" do
|
22
|
+
with_clean_redis do
|
23
|
+
with_watch( redis, "it" ) do
|
24
|
+
redis.multi do |r|
|
25
|
+
r.set "it", "my value"
|
26
|
+
end
|
27
|
+
end.should eq(["OK"])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "can use the redis2 connection to intentionally fail transactions" do
|
32
|
+
with_clean_redis do
|
33
|
+
with_watch( redis, "it" ) do
|
34
|
+
redis2.set "it", "interference"
|
35
|
+
redis.multi do |r|
|
36
|
+
r.set "it", "my value"
|
37
|
+
end
|
38
|
+
end.should be_nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/spec/redis_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require "helper"
|
3
|
+
|
4
|
+
describe Redis, redis: true, redis_configuration: true do
|
5
|
+
|
6
|
+
it "should be redis" do
|
7
|
+
redis.should be_kind_of(Redis)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has sorted sets" do
|
11
|
+
redis.zcard("it").should eq(0)
|
12
|
+
redis.zadd("it", 2, "bar")
|
13
|
+
redis.zadd("it", 3, "goo")
|
14
|
+
redis.zcard("it").should eq(2)
|
15
|
+
redis.zrange("it", 0, 0).should eq(["bar"]) # 0th item = smallest item
|
16
|
+
redis.zrangebyscore( "it", 0, 1, limit: [0,1] ).should eq([])
|
17
|
+
redis.zrangebyscore( "it", 0, 2, limit: [0,1] ).should eq(["bar"])
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can select by score" do
|
21
|
+
redis.zadd "it", 1, "a"
|
22
|
+
redis.zadd "it", 2, "b"
|
23
|
+
redis.zadd "it", 3, "c"
|
24
|
+
redis.zadd "it", 4, "d"
|
25
|
+
# inclusive by default http://redis.io/commands/zrangebyscore
|
26
|
+
redis.zrangebyscore( "it", "-inf", 2 ).should eq(["a","b"])
|
27
|
+
redis.zrangebyscore( "it", "-inf", 2, limit: [0,1] ).should eq(["a"])
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-redis_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease: !!null
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Lanett
|
9
|
+
autorequire: !!null
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-24 00:00:00.000000000 -07:00
|
13
|
+
default_executable: !!null
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: redis
|
17
|
+
requirement: &70259979329660 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70259979329660
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &70259979329200 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70259979329200
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
requirement: &70259979328740 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *70259979328740
|
48
|
+
description: Helper for RSpec tests which use Redis
|
49
|
+
email:
|
50
|
+
- mark.lanett@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/rspec-redis_helper.rb
|
61
|
+
- lib/rspec-redis_helper/version.rb
|
62
|
+
- rspec-redis_helper.gemspec
|
63
|
+
- spec/helper.rb
|
64
|
+
- spec/redis_helper_spec.rb
|
65
|
+
- spec/redis_spec.rb
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/mlanett/rspec-redis_helper
|
68
|
+
licenses: []
|
69
|
+
post_install_message: !!null
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project: !!null
|
87
|
+
rubygems_version: 1.5.0
|
88
|
+
signing_key: !!null
|
89
|
+
specification_version: 3
|
90
|
+
summary: Helper for RSpec tests which use Redis
|
91
|
+
test_files:
|
92
|
+
- spec/helper.rb
|
93
|
+
- spec/redis_helper_spec.rb
|
94
|
+
- spec/redis_spec.rb
|