rspec_sequel_matchers 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/{README.rdoc → README.md} +35 -17
- data/lib/rspec_sequel/base.rb +11 -2
- data/lib/rspec_sequel/matchers/have_one_to_one.rb +19 -0
- data/lib/rspec_sequel/validation.rb +3 -3
- data/spec/have_column_matcher_spec.rb +23 -23
- data/spec/have_many_to_many_matcher_spec.rb +14 -14
- data/spec/have_many_to_one_matcher_spec.rb +12 -12
- data/spec/have_one_to_many_matcher_spec.rb +12 -12
- data/spec/have_one_to_one_matcher_spec.rb +48 -0
- data/spec/migrations/001_create_items.rb +1 -1
- data/spec/migrations/004_create_profile.rb +18 -0
- data/spec/spec_helper.rb +10 -3
- data/spec/validate_exact_length_matcher_spec.rb +23 -23
- data/spec/validate_format_matcher_spec.rb +23 -23
- data/spec/validate_includes_matcher_spec.rb +23 -23
- data/spec/validate_integer_matcher_spec.rb +18 -18
- data/spec/validate_length_range_matcher_spec.rb +23 -23
- data/spec/validate_max_length_matcher_spec.rb +23 -23
- data/spec/validate_min_length_matcher_spec.rb +23 -23
- data/spec/validate_not_string_matcher_spec.rb +18 -18
- data/spec/validate_numeric_matcher_spec.rb +18 -18
- data/spec/validate_presence_matcher_spec.rb +18 -18
- data/spec/validate_unique_matcher_spec.rb +20 -20
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fbe09f732990a7ef67a071dd5a098bd59dba9b3
|
4
|
+
data.tar.gz: e560e7eff477f0f0880a6863672f742ef7bc6d2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba3d2bf0ca0f1a37c730f6190b1bc79140f3e48374f1ccd8ea6b08b774ee3e20a13413644cefb4f5dacc27103a5eceaf3e7d6590d63aa47ca57da5e071bc50bd
|
7
|
+
data.tar.gz: 4f37d2a825f9ecf1d5589d893b0e2e1dc7c2f34d6b188ed8011bfc79e6d6c40731ada9f91e39c8bcaf46acecaf700a93e04e326da49e0b426960359dde106675
|
data/LICENSE
CHANGED
data/{README.rdoc → README.md}
RENAMED
@@ -1,4 +1,9 @@
|
|
1
|
-
|
1
|
+
[![Build Status](https://travis-ci.org/openhood/rspec_sequel_matchers.svg?branch=master)](https://travis-ci.org/openhood/rspec_sequel_matchers)
|
2
|
+
|
3
|
+
# rspec_sequel_matchers
|
4
|
+
|
5
|
+
**Starting with version 0.4.0 this gem is only compatible with `RSpec >= 3.x`, if
|
6
|
+
you want to use it with `RSpec < 3.x` use the 0.3.x versions.**
|
2
7
|
|
3
8
|
Some Sequel Matchers for RSpec, using no other gem than rspec and sequel themself. As a consequence, you can use these matchers with Rails, Sinatra or any other framework. It's pretty feature complete.
|
4
9
|
|
@@ -25,32 +30,45 @@ There're also matchers for associations class methods:
|
|
25
30
|
* :many_to_many
|
26
31
|
* :many_to_one
|
27
32
|
* :one_to_many
|
33
|
+
* :one_to_one
|
28
34
|
|
29
35
|
And there's an additionnal matcher have_column to check columns existance and their type, see example usage bellow.
|
30
36
|
|
31
37
|
RspecSequel::Matchers has an extensive test suite and will give you as much explanation as possible in failure messages such as expected column type versus column type found in database.
|
32
38
|
|
33
|
-
|
39
|
+
## Install
|
40
|
+
|
41
|
+
```sh
|
42
|
+
gem install rspec_sequel_matchers
|
43
|
+
```
|
44
|
+
|
45
|
+
or with `Bundler`, put in you `Gemfile`:
|
34
46
|
|
35
|
-
|
47
|
+
```ruby
|
48
|
+
gem "rspec_sequel_matchers", group: :test
|
49
|
+
```
|
36
50
|
|
37
|
-
|
51
|
+
## Config
|
38
52
|
|
39
|
-
|
53
|
+
In spec_helper.rb
|
40
54
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
55
|
+
```ruby
|
56
|
+
RSpec.configure do |config|
|
57
|
+
config.include RspecSequel::Matchers
|
58
|
+
# ... other config ...
|
59
|
+
end
|
60
|
+
```
|
45
61
|
|
46
|
-
|
62
|
+
## Example usage
|
47
63
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
64
|
+
```ruby
|
65
|
+
describe Item do
|
66
|
+
it{ is_expected.to have_column :name, :type => String }
|
67
|
+
it{ is_expected.not_to have_column :wrong_name }
|
68
|
+
it{ is_expected.to validate_presence :name, :allow_nil => true }
|
69
|
+
end
|
70
|
+
```
|
53
71
|
|
54
|
-
|
72
|
+
## Copyright
|
55
73
|
|
56
|
-
Copyright (c) 2009 Jonathan Tron - Joseph Halter. See LICENSE for details.
|
74
|
+
Copyright (c) 2009-2014 Jonathan Tron - Joseph Halter. See LICENSE for details.
|
data/lib/rspec_sequel/base.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
|
+
require "sequel"
|
1
2
|
require "sequel/extensions/inflector"
|
3
|
+
require "rspec/mocks"
|
4
|
+
require "rspec/mocks/example_methods"
|
2
5
|
|
3
6
|
module RspecSequel
|
4
7
|
|
5
8
|
class Base
|
9
|
+
include RSpec::Mocks::ExampleMethods
|
10
|
+
|
6
11
|
def initialize(attribute, options={})
|
7
12
|
raise ArgumentError, "not enough params for matcher, required attribute is missing" if attribute.nil?
|
8
13
|
@attribute = attribute
|
9
14
|
@options = options
|
10
15
|
@description = []
|
11
16
|
end
|
17
|
+
|
12
18
|
def matches?(target)
|
13
19
|
@suffix = []
|
14
20
|
if target.is_a?(Sequel::Model)
|
@@ -19,15 +25,18 @@ module RspecSequel
|
|
19
25
|
valid?(target.db, target.new, target, @attribute, @options)
|
20
26
|
end
|
21
27
|
end
|
28
|
+
|
22
29
|
def failure_message
|
23
30
|
[@prefix, description, @suffix].flatten.compact.join(" ")
|
24
31
|
end
|
25
|
-
|
32
|
+
|
33
|
+
def failure_message_when_negated
|
26
34
|
[@prefix, "not", description, @suffix].flatten.compact.join(" ")
|
27
35
|
end
|
36
|
+
|
28
37
|
def hash_to_nice_string(hash)
|
29
38
|
hash.sort{|a,b| a[0].to_s<=>b[0].to_s}.collect{|param| param.collect{|v| v.inspect}.join(" => ")}.join(", ")
|
30
39
|
end
|
31
40
|
end
|
32
41
|
|
33
|
-
end
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RspecSequel
|
2
|
+
module Matchers
|
3
|
+
class HaveOneToOneMatcher < RspecSequel::Association
|
4
|
+
def association_type
|
5
|
+
:one_to_one
|
6
|
+
end
|
7
|
+
|
8
|
+
def valid?(db, i, c, attribute, options)
|
9
|
+
matching = super
|
10
|
+
|
11
|
+
matching
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def have_one_to_one(*args)
|
16
|
+
HaveOneToOneMatcher.new(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -44,7 +44,7 @@ module RspecSequel
|
|
44
44
|
called_count = 0
|
45
45
|
i = i.dup
|
46
46
|
i.class.columns # ensure colums are read again after .dup
|
47
|
-
i.
|
47
|
+
allow(i).to receive(validation_type) do |*args|
|
48
48
|
called_options = args.last.is_a?(Hash) ? args.pop : {}
|
49
49
|
called_attributes = args_to_called_attributes(args)
|
50
50
|
called_additionnal = args.shift if additionnal_param_required?
|
@@ -59,7 +59,7 @@ module RspecSequel
|
|
59
59
|
called_count += 1
|
60
60
|
end
|
61
61
|
end
|
62
|
-
|
62
|
+
end
|
63
63
|
i.valid?
|
64
64
|
if called_count>1
|
65
65
|
@suffix << "but validation is called too many times"
|
@@ -67,7 +67,7 @@ module RspecSequel
|
|
67
67
|
end
|
68
68
|
called_count==1
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
def args_to_called_attributes(args)
|
72
72
|
[args.pop].flatten
|
73
73
|
end
|
@@ -12,78 +12,78 @@ describe "have_column_matcher" do
|
|
12
12
|
describe "without type" do
|
13
13
|
it "should contain a description" do
|
14
14
|
@matcher = have_column :name
|
15
|
-
@matcher.description.
|
15
|
+
expect( @matcher.description ).to eq "have a column :name"
|
16
16
|
end
|
17
17
|
it "should set failure messages" do
|
18
18
|
@matcher = have_column :name
|
19
19
|
@matcher.matches? subject
|
20
|
-
@matcher.failure_message.
|
21
|
-
@matcher.
|
20
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description
|
21
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description
|
22
22
|
end
|
23
23
|
end
|
24
24
|
describe "with type as symbol" do
|
25
25
|
it "should contain a description" do
|
26
26
|
@matcher = have_column :name, :type => :string
|
27
|
-
@matcher.description.
|
27
|
+
expect( @matcher.description ).to eq "have a column :name with type string"
|
28
28
|
end
|
29
29
|
it "should set failure messages" do
|
30
30
|
@matcher = have_column :password, :type => :string
|
31
31
|
@matcher.matches? subject
|
32
|
-
@matcher.failure_message.
|
33
|
-
@matcher.
|
32
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description
|
33
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description
|
34
34
|
end
|
35
35
|
end
|
36
36
|
describe "with type as object" do
|
37
37
|
it "should contain a description" do
|
38
38
|
@matcher = have_column :name, :type => String
|
39
|
-
@matcher.description.
|
39
|
+
expect( @matcher.description ).to eq "have a column :name with type String"
|
40
40
|
end
|
41
41
|
it "should set failure messages" do
|
42
42
|
@matcher = have_column :password, :type => String
|
43
43
|
@matcher.matches? subject
|
44
|
-
@matcher.failure_message.
|
45
|
-
@matcher.
|
44
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description
|
45
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description
|
46
46
|
end
|
47
47
|
it "should explicit found type if different than expected" do
|
48
48
|
@matcher = have_column :name, :type => Integer
|
49
49
|
@matcher.matches? subject
|
50
50
|
explanation = " (type found: string, varchar(255))"
|
51
|
-
@matcher.failure_message.
|
52
|
-
@matcher.
|
51
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description + explanation
|
52
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description + explanation
|
53
53
|
end
|
54
54
|
end
|
55
55
|
describe "on anonymous Sequel::Model class" do
|
56
56
|
it "should set failure messages" do
|
57
57
|
@matcher = have_column :password
|
58
58
|
@matcher.matches? Sequel::Model(:comments)
|
59
|
-
@matcher.failure_message.
|
60
|
-
@matcher.
|
59
|
+
expect( @matcher.failure_message ).to eq "expected Comment to " + @matcher.description
|
60
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Comment to not " + @matcher.description
|
61
61
|
end
|
62
62
|
end
|
63
63
|
describe "on Sequel::Model class" do
|
64
64
|
it "should set failure messages" do
|
65
65
|
@matcher = have_column :password
|
66
66
|
@matcher.matches? Item
|
67
|
-
@matcher.failure_message.
|
68
|
-
@matcher.
|
67
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description
|
68
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description
|
69
69
|
end
|
70
70
|
end
|
71
71
|
describe "on Sequel::Model instance" do
|
72
72
|
it "should set failure messages" do
|
73
73
|
@matcher = have_column :password
|
74
74
|
@matcher.matches? Item.new
|
75
|
-
@matcher.failure_message.
|
76
|
-
@matcher.
|
75
|
+
expect( @matcher.failure_message ).to eq "expected #<Item @values={}> to " + @matcher.description
|
76
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected #<Item @values={}> to not " + @matcher.description
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
81
|
describe "matchers" do
|
82
|
-
it{
|
83
|
-
it{
|
84
|
-
it{
|
85
|
-
it{
|
86
|
-
it{
|
82
|
+
it{ is_expected.to have_column(:name) }
|
83
|
+
it{ is_expected.to have_column(:name, :type => :string) }
|
84
|
+
it{ is_expected.to have_column(:name, :type => String) }
|
85
|
+
it{ is_expected.not_to have_column(:password) }
|
86
|
+
it{ is_expected.not_to have_column(:name, :type => :integer) }
|
87
87
|
end
|
88
88
|
|
89
|
-
end
|
89
|
+
end
|
@@ -15,42 +15,42 @@ describe "have_many_to_many_matcher" do
|
|
15
15
|
describe "without option" do
|
16
16
|
it "should contain a description" do
|
17
17
|
@matcher = have_many_to_many :items
|
18
|
-
@matcher.description.
|
18
|
+
expect( @matcher.description ).to eq "have a many_to_many association :items"
|
19
19
|
end
|
20
20
|
it "should set failure messages" do
|
21
21
|
@matcher = have_many_to_many :items
|
22
22
|
@matcher.matches? subject
|
23
|
-
@matcher.failure_message.
|
24
|
-
@matcher.
|
23
|
+
expect( @matcher.failure_message ).to eq "expected Comment to " + @matcher.description
|
24
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Comment to not " + @matcher.description
|
25
25
|
end
|
26
26
|
end
|
27
27
|
describe "with options" do
|
28
28
|
it "should contain a description" do
|
29
29
|
@matcher = have_many_to_many :items, :class_name => "Item"
|
30
|
-
@matcher.description.
|
30
|
+
expect( @matcher.description ).to eq 'have a many_to_many association :items with option(s) :class_name => "Item"'
|
31
31
|
end
|
32
32
|
it "should set failure messages" do
|
33
33
|
@matcher = have_many_to_many :items, :class_name => "Item"
|
34
34
|
@matcher.matches? subject
|
35
|
-
@matcher.failure_message.
|
36
|
-
@matcher.
|
35
|
+
expect( @matcher.failure_message ).to eq "expected Comment to " + @matcher.description
|
36
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Comment to not " + @matcher.description
|
37
37
|
end
|
38
38
|
it "should explicit used options if different than expected" do
|
39
39
|
@matcher = have_many_to_many :items, :class_name => "Price"
|
40
40
|
@matcher.matches? subject
|
41
41
|
explanation = ' expected :class_name == "Price" but found "Item" instead'
|
42
|
-
@matcher.failure_message.
|
43
|
-
@matcher.
|
42
|
+
expect( @matcher.failure_message ).to eq "expected Comment to " + @matcher.description + explanation
|
43
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Comment to not " + @matcher.description + explanation
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
describe "matchers" do
|
49
|
-
it{
|
50
|
-
it{
|
51
|
-
it{
|
52
|
-
it{
|
53
|
-
it{
|
49
|
+
it{ is_expected.to have_many_to_many(:items) }
|
50
|
+
it{ is_expected.to have_many_to_many(:items, :class_name => "Item", :join_table => :comments_items) }
|
51
|
+
it{ is_expected.not_to have_many_to_many(:prices) }
|
52
|
+
it{ is_expected.not_to have_many_to_many(:items, :class_name => "Price") }
|
53
|
+
it{ is_expected.not_to have_many_to_many(:items, :join_table => :items_comments) }
|
54
54
|
end
|
55
55
|
|
56
|
-
end
|
56
|
+
end
|
@@ -15,41 +15,41 @@ describe "have_many_to_one_matcher" do
|
|
15
15
|
describe "without option" do
|
16
16
|
it "should contain a description" do
|
17
17
|
@matcher = have_many_to_one :item
|
18
|
-
@matcher.description.
|
18
|
+
expect( @matcher.description ).to eq "have a many_to_one association :item"
|
19
19
|
end
|
20
20
|
it "should set failure messages" do
|
21
21
|
@matcher = have_many_to_one :item
|
22
22
|
@matcher.matches? subject
|
23
|
-
@matcher.failure_message.
|
24
|
-
@matcher.
|
23
|
+
expect( @matcher.failure_message ).to eq "expected Comment to " + @matcher.description
|
24
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Comment to not " + @matcher.description
|
25
25
|
end
|
26
26
|
end
|
27
27
|
describe "with options" do
|
28
28
|
it "should contain a description" do
|
29
29
|
@matcher = have_many_to_one :item, :class_name => "Item"
|
30
|
-
@matcher.description.
|
30
|
+
expect( @matcher.description ).to eq 'have a many_to_one association :item with option(s) :class_name => "Item"'
|
31
31
|
end
|
32
32
|
it "should set failure messages" do
|
33
33
|
@matcher = have_many_to_one :item, :class_name => "Item"
|
34
34
|
@matcher.matches? subject
|
35
|
-
@matcher.failure_message.
|
36
|
-
@matcher.
|
35
|
+
expect( @matcher.failure_message ).to eq "expected Comment to " + @matcher.description
|
36
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Comment to not " + @matcher.description
|
37
37
|
end
|
38
38
|
it "should explicit used options if different than expected" do
|
39
39
|
@matcher = have_many_to_one :item, :class_name => "Price"
|
40
40
|
@matcher.matches? subject
|
41
41
|
explanation = ' expected :class_name == "Price" but found "Item" instead'
|
42
|
-
@matcher.failure_message.
|
43
|
-
@matcher.
|
42
|
+
expect( @matcher.failure_message ).to eq "expected Comment to " + @matcher.description + explanation
|
43
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Comment to not " + @matcher.description + explanation
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
describe "matchers" do
|
49
|
-
it{
|
50
|
-
it{
|
51
|
-
it{
|
52
|
-
it{
|
49
|
+
it{ is_expected.to have_many_to_one(:item) }
|
50
|
+
it{ is_expected.to have_many_to_one(:item, :class_name => "Item") }
|
51
|
+
it{ is_expected.not_to have_many_to_one(:price) }
|
52
|
+
it{ is_expected.not_to have_many_to_one(:item, :class_name => "Price") }
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
@@ -15,41 +15,41 @@ describe "have_one_to_many_matcher" do
|
|
15
15
|
describe "without option" do
|
16
16
|
it "should contain a description" do
|
17
17
|
@matcher = have_one_to_many :comments
|
18
|
-
@matcher.description.
|
18
|
+
expect( @matcher.description ).to eq "have a one_to_many association :comments"
|
19
19
|
end
|
20
20
|
it "should set failure messages" do
|
21
21
|
@matcher = have_one_to_many :comments
|
22
22
|
@matcher.matches? subject
|
23
|
-
@matcher.failure_message.
|
24
|
-
@matcher.
|
23
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description
|
24
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description
|
25
25
|
end
|
26
26
|
end
|
27
27
|
describe "with options" do
|
28
28
|
it "should contain a description" do
|
29
29
|
@matcher = have_one_to_many :comments, :class_name => "Comment"
|
30
|
-
@matcher.description.
|
30
|
+
expect( @matcher.description ).to eq 'have a one_to_many association :comments with option(s) :class_name => "Comment"'
|
31
31
|
end
|
32
32
|
it "should set failure messages" do
|
33
33
|
@matcher = have_one_to_many :comments, :class_name => "Comment"
|
34
34
|
@matcher.matches? subject
|
35
|
-
@matcher.failure_message.
|
36
|
-
@matcher.
|
35
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description
|
36
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description
|
37
37
|
end
|
38
38
|
it "should explicit used options if different than expected" do
|
39
39
|
@matcher = have_one_to_many :comments, :class_name => "Price"
|
40
40
|
@matcher.matches? subject
|
41
41
|
explanation = ' expected :class_name == "Price" but found "Comment" instead'
|
42
|
-
@matcher.failure_message.
|
43
|
-
@matcher.
|
42
|
+
expect( @matcher.failure_message ).to eq "expected Item to " + @matcher.description + explanation
|
43
|
+
expect( @matcher.failure_message_when_negated ).to eq "expected Item to not " + @matcher.description + explanation
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
describe "matchers" do
|
49
|
-
it{
|
50
|
-
it{
|
51
|
-
it{
|
52
|
-
it{
|
49
|
+
it{ is_expected.to have_one_to_many(:comments) }
|
50
|
+
it{ is_expected.to have_one_to_many(:comments, :class_name => "Comment") }
|
51
|
+
it{ is_expected.not_to have_one_to_many(:prices) }
|
52
|
+
it{ is_expected.not_to have_one_to_many(:comments, :class_name => "Price") }
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|