rspec_sequel_matchers 0.2.0
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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +47 -0
- data/Rakefile +46 -0
- data/lib/rspec_sequel/association.rb +30 -0
- data/lib/rspec_sequel/base.rb +33 -0
- data/lib/rspec_sequel/matchers/have_column.rb +35 -0
- data/lib/rspec_sequel/matchers/have_many_to_many.rb +22 -0
- data/lib/rspec_sequel/matchers/have_many_to_one.rb +22 -0
- data/lib/rspec_sequel/matchers/have_one_to_many.rb +22 -0
- data/lib/rspec_sequel/matchers/validate_exact_length.rb +25 -0
- data/lib/rspec_sequel/matchers/validate_format.rb +25 -0
- data/lib/rspec_sequel/matchers/validate_includes.rb +25 -0
- data/lib/rspec_sequel/matchers/validate_integer.rb +21 -0
- data/lib/rspec_sequel/matchers/validate_length_range.rb +25 -0
- data/lib/rspec_sequel/matchers/validate_max_length.rb +25 -0
- data/lib/rspec_sequel/matchers/validate_min_length.rb +25 -0
- data/lib/rspec_sequel/matchers/validate_not_string.rb +21 -0
- data/lib/rspec_sequel/matchers/validate_numeric.rb +21 -0
- data/lib/rspec_sequel/matchers/validate_presence.rb +21 -0
- data/lib/rspec_sequel/matchers/validate_unique.rb +33 -0
- data/lib/rspec_sequel/validation.rb +77 -0
- data/lib/rspec_sequel_matchers.rb +9 -0
- data/rspec_sequel_matchers.gemspec +107 -0
- data/spec/have_column_matcher_spec.rb +89 -0
- data/spec/have_many_to_many_matcher_spec.rb +56 -0
- data/spec/have_many_to_one_matcher_spec.rb +55 -0
- data/spec/have_one_to_many_matcher_spec.rb +55 -0
- data/spec/migrations/001_create_items.rb +15 -0
- data/spec/migrations/002_create_comments.rb +17 -0
- data/spec/migrations/003_create_comments_items.rb +15 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/validate_exact_length_matcher_spec.rb +88 -0
- data/spec/validate_format_matcher_spec.rb +88 -0
- data/spec/validate_includes_matcher_spec.rb +88 -0
- data/spec/validate_integer_matcher_spec.rb +77 -0
- data/spec/validate_length_range_matcher_spec.rb +88 -0
- data/spec/validate_max_length_matcher_spec.rb +88 -0
- data/spec/validate_min_length_matcher_spec.rb +88 -0
- data/spec/validate_not_string_matcher_spec.rb +77 -0
- data/spec/validate_numeric_matcher_spec.rb +77 -0
- data/spec/validate_presence_matcher_spec.rb +77 -0
- data/spec/validate_unique_matcher_spec.rb +79 -0
- metadata +153 -0
@@ -0,0 +1,107 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rspec_sequel_matchers}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jonathan Tron", "Joseph Halter"]
|
12
|
+
s.date = %q{2010-03-02}
|
13
|
+
s.email = %q{team@openhood.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"lib/rspec_sequel/association.rb",
|
25
|
+
"lib/rspec_sequel/base.rb",
|
26
|
+
"lib/rspec_sequel/matchers/have_column.rb",
|
27
|
+
"lib/rspec_sequel/matchers/have_many_to_many.rb",
|
28
|
+
"lib/rspec_sequel/matchers/have_many_to_one.rb",
|
29
|
+
"lib/rspec_sequel/matchers/have_one_to_many.rb",
|
30
|
+
"lib/rspec_sequel/matchers/validate_exact_length.rb",
|
31
|
+
"lib/rspec_sequel/matchers/validate_format.rb",
|
32
|
+
"lib/rspec_sequel/matchers/validate_includes.rb",
|
33
|
+
"lib/rspec_sequel/matchers/validate_integer.rb",
|
34
|
+
"lib/rspec_sequel/matchers/validate_length_range.rb",
|
35
|
+
"lib/rspec_sequel/matchers/validate_max_length.rb",
|
36
|
+
"lib/rspec_sequel/matchers/validate_min_length.rb",
|
37
|
+
"lib/rspec_sequel/matchers/validate_not_string.rb",
|
38
|
+
"lib/rspec_sequel/matchers/validate_numeric.rb",
|
39
|
+
"lib/rspec_sequel/matchers/validate_presence.rb",
|
40
|
+
"lib/rspec_sequel/matchers/validate_unique.rb",
|
41
|
+
"lib/rspec_sequel/validation.rb",
|
42
|
+
"lib/rspec_sequel_matchers.rb",
|
43
|
+
"rspec_sequel_matchers.gemspec",
|
44
|
+
"spec/have_column_matcher_spec.rb",
|
45
|
+
"spec/have_many_to_many_matcher_spec.rb",
|
46
|
+
"spec/have_many_to_one_matcher_spec.rb",
|
47
|
+
"spec/have_one_to_many_matcher_spec.rb",
|
48
|
+
"spec/migrations/001_create_items.rb",
|
49
|
+
"spec/migrations/002_create_comments.rb",
|
50
|
+
"spec/migrations/003_create_comments_items.rb",
|
51
|
+
"spec/spec_helper.rb",
|
52
|
+
"spec/validate_exact_length_matcher_spec.rb",
|
53
|
+
"spec/validate_format_matcher_spec.rb",
|
54
|
+
"spec/validate_includes_matcher_spec.rb",
|
55
|
+
"spec/validate_integer_matcher_spec.rb",
|
56
|
+
"spec/validate_length_range_matcher_spec.rb",
|
57
|
+
"spec/validate_max_length_matcher_spec.rb",
|
58
|
+
"spec/validate_min_length_matcher_spec.rb",
|
59
|
+
"spec/validate_not_string_matcher_spec.rb",
|
60
|
+
"spec/validate_numeric_matcher_spec.rb",
|
61
|
+
"spec/validate_presence_matcher_spec.rb",
|
62
|
+
"spec/validate_unique_matcher_spec.rb"
|
63
|
+
]
|
64
|
+
s.homepage = %q{http://github.com/openhood/rspec_sequel_matchers}
|
65
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
66
|
+
s.require_paths = ["lib"]
|
67
|
+
s.rubygems_version = %q{1.3.6}
|
68
|
+
s.summary = %q{RSpec Matchers for Sequel}
|
69
|
+
s.test_files = [
|
70
|
+
"spec/have_column_matcher_spec.rb",
|
71
|
+
"spec/have_many_to_many_matcher_spec.rb",
|
72
|
+
"spec/have_many_to_one_matcher_spec.rb",
|
73
|
+
"spec/have_one_to_many_matcher_spec.rb",
|
74
|
+
"spec/migrations/001_create_items.rb",
|
75
|
+
"spec/migrations/002_create_comments.rb",
|
76
|
+
"spec/migrations/003_create_comments_items.rb",
|
77
|
+
"spec/spec_helper.rb",
|
78
|
+
"spec/validate_exact_length_matcher_spec.rb",
|
79
|
+
"spec/validate_format_matcher_spec.rb",
|
80
|
+
"spec/validate_includes_matcher_spec.rb",
|
81
|
+
"spec/validate_integer_matcher_spec.rb",
|
82
|
+
"spec/validate_length_range_matcher_spec.rb",
|
83
|
+
"spec/validate_max_length_matcher_spec.rb",
|
84
|
+
"spec/validate_min_length_matcher_spec.rb",
|
85
|
+
"spec/validate_not_string_matcher_spec.rb",
|
86
|
+
"spec/validate_numeric_matcher_spec.rb",
|
87
|
+
"spec/validate_presence_matcher_spec.rb",
|
88
|
+
"spec/validate_unique_matcher_spec.rb"
|
89
|
+
]
|
90
|
+
|
91
|
+
if s.respond_to? :specification_version then
|
92
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
93
|
+
s.specification_version = 3
|
94
|
+
|
95
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
96
|
+
s.add_runtime_dependency(%q<rspec>, [">= 2.0.0.a"])
|
97
|
+
s.add_runtime_dependency(%q<sequel>, [">= 3.8.0"])
|
98
|
+
else
|
99
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.a"])
|
100
|
+
s.add_dependency(%q<sequel>, [">= 3.8.0"])
|
101
|
+
end
|
102
|
+
else
|
103
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.a"])
|
104
|
+
s.add_dependency(%q<sequel>, [">= 3.8.0"])
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
|
3
|
+
describe "have_column_matcher" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
define_model :item
|
7
|
+
end
|
8
|
+
|
9
|
+
subject{ Item }
|
10
|
+
|
11
|
+
describe "messages" do
|
12
|
+
describe "without type" do
|
13
|
+
it "should contain a description" do
|
14
|
+
@matcher = have_column :name
|
15
|
+
@matcher.description.should == "have a column :name"
|
16
|
+
end
|
17
|
+
it "should set failure messages" do
|
18
|
+
@matcher = have_column :name
|
19
|
+
@matcher.matches? subject
|
20
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
21
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
22
|
+
end
|
23
|
+
end
|
24
|
+
describe "with type as symbol" do
|
25
|
+
it "should contain a description" do
|
26
|
+
@matcher = have_column :name, :type => :string
|
27
|
+
@matcher.description.should == "have a column :name with type string"
|
28
|
+
end
|
29
|
+
it "should set failure messages" do
|
30
|
+
@matcher = have_column :password, :type => :string
|
31
|
+
@matcher.matches? subject
|
32
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
33
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
34
|
+
end
|
35
|
+
end
|
36
|
+
describe "with type as object" do
|
37
|
+
it "should contain a description" do
|
38
|
+
@matcher = have_column :name, :type => String
|
39
|
+
@matcher.description.should == "have a column :name with type String"
|
40
|
+
end
|
41
|
+
it "should set failure messages" do
|
42
|
+
@matcher = have_column :password, :type => String
|
43
|
+
@matcher.matches? subject
|
44
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
45
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
46
|
+
end
|
47
|
+
it "should explicit found type if different than expected" do
|
48
|
+
@matcher = have_column :name, :type => Integer
|
49
|
+
@matcher.matches? subject
|
50
|
+
explanation = " (type found: string, varchar(255))"
|
51
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
|
52
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
|
53
|
+
end
|
54
|
+
end
|
55
|
+
describe "on anonymous Sequel::Model class" do
|
56
|
+
it "should set failure messages" do
|
57
|
+
@matcher = have_column :password
|
58
|
+
@matcher.matches? Sequel::Model(:comments)
|
59
|
+
@matcher.failure_message.should == "expected Comment to " + @matcher.description
|
60
|
+
@matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
|
61
|
+
end
|
62
|
+
end
|
63
|
+
describe "on Sequel::Model class" do
|
64
|
+
it "should set failure messages" do
|
65
|
+
@matcher = have_column :password
|
66
|
+
@matcher.matches? Item
|
67
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
68
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
69
|
+
end
|
70
|
+
end
|
71
|
+
describe "on Sequel::Model instance" do
|
72
|
+
it "should set failure messages" do
|
73
|
+
@matcher = have_column :password
|
74
|
+
@matcher.matches? Item.new
|
75
|
+
@matcher.failure_message.should == "expected #<Item @values={}> to " + @matcher.description
|
76
|
+
@matcher.negative_failure_message.should == "expected #<Item @values={}> to not " + @matcher.description
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "matchers" do
|
82
|
+
it{ should have_column(:name) }
|
83
|
+
it{ should have_column(:name, :type => :string) }
|
84
|
+
it{ should have_column(:name, :type => String) }
|
85
|
+
it{ should_not have_column(:password) }
|
86
|
+
it{ should_not have_column(:name, :type => :integer) }
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
|
3
|
+
describe "have_many_to_many_matcher" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
define_model :item
|
7
|
+
define_model :comment do
|
8
|
+
many_to_many :items
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
subject{ Comment }
|
13
|
+
|
14
|
+
describe "messages" do
|
15
|
+
describe "without option" do
|
16
|
+
it "should contain a description" do
|
17
|
+
@matcher = have_many_to_many :items
|
18
|
+
@matcher.description.should == "have a many_to_many association :items"
|
19
|
+
end
|
20
|
+
it "should set failure messages" do
|
21
|
+
@matcher = have_many_to_many :items
|
22
|
+
@matcher.matches? subject
|
23
|
+
@matcher.failure_message.should == "expected Comment to " + @matcher.description
|
24
|
+
@matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
|
25
|
+
end
|
26
|
+
end
|
27
|
+
describe "with options" do
|
28
|
+
it "should contain a description" do
|
29
|
+
@matcher = have_many_to_many :items, :class_name => "Item"
|
30
|
+
@matcher.description.should == 'have a many_to_many association :items with option(s) :class_name => "Item"'
|
31
|
+
end
|
32
|
+
it "should set failure messages" do
|
33
|
+
@matcher = have_many_to_many :items, :class_name => "Item"
|
34
|
+
@matcher.matches? subject
|
35
|
+
@matcher.failure_message.should == "expected Comment to " + @matcher.description
|
36
|
+
@matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
|
37
|
+
end
|
38
|
+
it "should explicit used options if different than expected" do
|
39
|
+
@matcher = have_many_to_many :items, :class_name => "Price"
|
40
|
+
@matcher.matches? subject
|
41
|
+
explanation = ' expected :class_name == "Price" but found "Item" instead'
|
42
|
+
@matcher.failure_message.should == "expected Comment to " + @matcher.description + explanation
|
43
|
+
@matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description + explanation
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "matchers" do
|
49
|
+
it{ should have_many_to_many(:items) }
|
50
|
+
it{ should have_many_to_many(:items, :class_name => "Item", :join_table => :comments_items) }
|
51
|
+
it{ should_not have_many_to_many(:prices) }
|
52
|
+
it{ should_not have_many_to_many(:items, :class_name => "Price") }
|
53
|
+
it{ should_not have_many_to_many(:items, :join_table => :items_comments) }
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
|
3
|
+
describe "have_many_to_one_matcher" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
define_model :item
|
7
|
+
define_model :comment do
|
8
|
+
many_to_one :item
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
subject{ Comment }
|
13
|
+
|
14
|
+
describe "messages" do
|
15
|
+
describe "without option" do
|
16
|
+
it "should contain a description" do
|
17
|
+
@matcher = have_many_to_one :item
|
18
|
+
@matcher.description.should == "have a many_to_one association :item"
|
19
|
+
end
|
20
|
+
it "should set failure messages" do
|
21
|
+
@matcher = have_many_to_one :item
|
22
|
+
@matcher.matches? subject
|
23
|
+
@matcher.failure_message.should == "expected Comment to " + @matcher.description
|
24
|
+
@matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
|
25
|
+
end
|
26
|
+
end
|
27
|
+
describe "with options" do
|
28
|
+
it "should contain a description" do
|
29
|
+
@matcher = have_many_to_one :item, :class_name => "Item"
|
30
|
+
@matcher.description.should == 'have a many_to_one association :item with option(s) :class_name => "Item"'
|
31
|
+
end
|
32
|
+
it "should set failure messages" do
|
33
|
+
@matcher = have_many_to_one :item, :class_name => "Item"
|
34
|
+
@matcher.matches? subject
|
35
|
+
@matcher.failure_message.should == "expected Comment to " + @matcher.description
|
36
|
+
@matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description
|
37
|
+
end
|
38
|
+
it "should explicit used options if different than expected" do
|
39
|
+
@matcher = have_many_to_one :item, :class_name => "Price"
|
40
|
+
@matcher.matches? subject
|
41
|
+
explanation = ' expected :class_name == "Price" but found "Item" instead'
|
42
|
+
@matcher.failure_message.should == "expected Comment to " + @matcher.description + explanation
|
43
|
+
@matcher.negative_failure_message.should == "expected Comment to not " + @matcher.description + explanation
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "matchers" do
|
49
|
+
it{ should have_many_to_one(:item) }
|
50
|
+
it{ should have_many_to_one(:item, :class_name => "Item") }
|
51
|
+
it{ should_not have_many_to_one(:price) }
|
52
|
+
it{ should_not have_many_to_one(:item, :class_name => "Price") }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
|
3
|
+
describe "have_one_to_many_matcher" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
define_model :item do
|
7
|
+
one_to_many :comments
|
8
|
+
end
|
9
|
+
define_model :comment
|
10
|
+
end
|
11
|
+
|
12
|
+
subject{ Item }
|
13
|
+
|
14
|
+
describe "messages" do
|
15
|
+
describe "without option" do
|
16
|
+
it "should contain a description" do
|
17
|
+
@matcher = have_one_to_many :comments
|
18
|
+
@matcher.description.should == "have a one_to_many association :comments"
|
19
|
+
end
|
20
|
+
it "should set failure messages" do
|
21
|
+
@matcher = have_one_to_many :comments
|
22
|
+
@matcher.matches? subject
|
23
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
24
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
25
|
+
end
|
26
|
+
end
|
27
|
+
describe "with options" do
|
28
|
+
it "should contain a description" do
|
29
|
+
@matcher = have_one_to_many :comments, :class_name => "Comment"
|
30
|
+
@matcher.description.should == 'have a one_to_many association :comments with option(s) :class_name => "Comment"'
|
31
|
+
end
|
32
|
+
it "should set failure messages" do
|
33
|
+
@matcher = have_one_to_many :comments, :class_name => "Comment"
|
34
|
+
@matcher.matches? subject
|
35
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
36
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
37
|
+
end
|
38
|
+
it "should explicit used options if different than expected" do
|
39
|
+
@matcher = have_one_to_many :comments, :class_name => "Price"
|
40
|
+
@matcher.matches? subject
|
41
|
+
explanation = ' expected :class_name == "Price" but found "Comment" instead'
|
42
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
|
43
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "matchers" do
|
49
|
+
it{ should have_one_to_many(:comments) }
|
50
|
+
it{ should have_one_to_many(:comments, :class_name => "Comment") }
|
51
|
+
it{ should_not have_one_to_many(:prices) }
|
52
|
+
it{ should_not have_one_to_many(:comments, :class_name => "Price") }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateComments < Sequel::Migration
|
2
|
+
|
3
|
+
def up
|
4
|
+
create_table :comments do
|
5
|
+
primary_key :id
|
6
|
+
foreign_key :item_id, :items, :type => Fixnum
|
7
|
+
String :content
|
8
|
+
DateTime :created_at
|
9
|
+
index :item_id
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def down
|
14
|
+
drop_table :comments
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateCommentsItems < Sequel::Migration
|
2
|
+
|
3
|
+
def up
|
4
|
+
create_table :comments_items do
|
5
|
+
foreign_key :comment_id, :comments, :type => Fixnum
|
6
|
+
foreign_key :item_id, :items, :type => Fixnum
|
7
|
+
index [:comment_id, :item_id], :unique => true
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def down
|
12
|
+
drop_table :comments_items
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "rspec"
|
2
|
+
require "rubygems"
|
3
|
+
require "sequel"
|
4
|
+
require "sequel/extensions/inflector"
|
5
|
+
require "sequel/extensions/migration"
|
6
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "rspec_sequel_matchers")
|
7
|
+
|
8
|
+
# connect to an in-memory database
|
9
|
+
begin
|
10
|
+
Sequel.sqlite
|
11
|
+
rescue Sequel::AdapterNotFound
|
12
|
+
puts "sqlite not available. Install it with: sudo gem install sqlite3-ruby"
|
13
|
+
exit 1 # failure
|
14
|
+
end
|
15
|
+
|
16
|
+
# drop all tables and migrate on start
|
17
|
+
db = Sequel::Model.db
|
18
|
+
db.tables.each do |table_name|
|
19
|
+
db.drop_table table_name
|
20
|
+
end
|
21
|
+
Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), "migrations"))
|
22
|
+
|
23
|
+
module RspecHelpers
|
24
|
+
def define_model(model, &block)
|
25
|
+
model_name = model.to_s.camelize.to_sym
|
26
|
+
table_name = model.to_s.tableize.to_sym
|
27
|
+
@defined_models ||= []
|
28
|
+
@defined_models << model_name
|
29
|
+
klass = Object.const_set model_name, Sequel::Model(table_name)
|
30
|
+
klass.class_eval &block if block_given?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
Rspec.configure do |c|
|
35
|
+
c.mock_framework = :rspec
|
36
|
+
c.color_enabled = true
|
37
|
+
c.include(RspecHelpers)
|
38
|
+
c.include(RspecSequel::Matchers)
|
39
|
+
# undefine models defined via define_model (if any)
|
40
|
+
# truncate all tables between each spec
|
41
|
+
c.after(:each) do
|
42
|
+
db = Sequel::Model.db
|
43
|
+
db.tables.each do |table_name|
|
44
|
+
db["TRUNCATE #{table_name}"]
|
45
|
+
end
|
46
|
+
(@defined_models||[]).each do |model_name|
|
47
|
+
Object.send(:remove_const, model_name)
|
48
|
+
end
|
49
|
+
@defined_models = nil
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
|
3
|
+
describe "validate_exact_length_matcher" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
define_model :item do
|
7
|
+
plugin :validation_helpers
|
8
|
+
def validate
|
9
|
+
validates_exact_length 4, :name, :allow_nil => true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject{ Item }
|
15
|
+
|
16
|
+
describe "arguments" do
|
17
|
+
it "should require attribute" do
|
18
|
+
lambda{
|
19
|
+
@matcher = validate_exact_length
|
20
|
+
}.should raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
it "should require additionnal parameters" do
|
23
|
+
lambda{
|
24
|
+
@matcher = validate_exact_length :name
|
25
|
+
}.should raise_error(ArgumentError)
|
26
|
+
end
|
27
|
+
it "should refuse invalid additionnal parameters" do
|
28
|
+
lambda{
|
29
|
+
@matcher = validate_exact_length :id, :name
|
30
|
+
}.should raise_error(ArgumentError)
|
31
|
+
end
|
32
|
+
it "should accept valid additionnal parameters" do
|
33
|
+
lambda{
|
34
|
+
@matcher = validate_exact_length 4, :name
|
35
|
+
}.should_not raise_error(ArgumentError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "messages" do
|
40
|
+
describe "without option" do
|
41
|
+
it "should contain a description" do
|
42
|
+
@matcher = validate_exact_length 4, :name
|
43
|
+
@matcher.description.should == "validate length of :name is exactly 4"
|
44
|
+
end
|
45
|
+
it "should set failure messages" do
|
46
|
+
@matcher = validate_exact_length 4, :name
|
47
|
+
@matcher.matches? subject
|
48
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
49
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
50
|
+
end
|
51
|
+
end
|
52
|
+
describe "with options" do
|
53
|
+
it "should contain a description" do
|
54
|
+
@matcher = validate_exact_length 4, :name, :allow_nil => true
|
55
|
+
@matcher.description.should == "validate length of :name is exactly 4 with option(s) :allow_nil => true"
|
56
|
+
end
|
57
|
+
it "should set failure messages" do
|
58
|
+
@matcher = validate_exact_length 4, :price, :allow_nil => true
|
59
|
+
@matcher.matches? subject
|
60
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
61
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
62
|
+
end
|
63
|
+
it "should explicit used options if different than expected" do
|
64
|
+
@matcher = validate_exact_length 4, :name, :allow_blank => true
|
65
|
+
@matcher.matches? subject
|
66
|
+
explanation = " but called with option(s) :allow_nil => true instead"
|
67
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
|
68
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
|
69
|
+
end
|
70
|
+
it "should warn if invalid options are used" do
|
71
|
+
@matcher = validate_exact_length 4, :name, :allow_anything => true
|
72
|
+
@matcher.matches? subject
|
73
|
+
explanation = " but option :allow_anything is not valid"
|
74
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
|
75
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "matchers" do
|
81
|
+
it{ should validate_exact_length(4, :name) }
|
82
|
+
it{ should validate_exact_length(4, :name, :allow_nil => true) }
|
83
|
+
it{ should_not validate_exact_length(4, :price) }
|
84
|
+
it{ should_not validate_exact_length(3, :name) }
|
85
|
+
it{ should_not validate_exact_length(4, :name, :allow_blank => true) }
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
|
3
|
+
describe "validate_format_matcher" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
define_model :item do
|
7
|
+
plugin :validation_helpers
|
8
|
+
def validate
|
9
|
+
validates_format /[abc]+/, :name, :allow_nil => true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject{ Item }
|
15
|
+
|
16
|
+
describe "arguments" do
|
17
|
+
it "should require attribute" do
|
18
|
+
lambda{
|
19
|
+
@matcher = validate_format
|
20
|
+
}.should raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
it "should require additionnal parameters" do
|
23
|
+
lambda{
|
24
|
+
@matcher = validate_format :name
|
25
|
+
}.should raise_error(ArgumentError)
|
26
|
+
end
|
27
|
+
it "should refuse invalid additionnal parameters" do
|
28
|
+
lambda{
|
29
|
+
@matcher = validate_format :id, :name
|
30
|
+
}.should raise_error(ArgumentError)
|
31
|
+
end
|
32
|
+
it "should accept valid additionnal parameters" do
|
33
|
+
lambda{
|
34
|
+
@matcher = validate_format /[abc]+/, :name
|
35
|
+
}.should_not raise_error(ArgumentError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "messages" do
|
40
|
+
describe "without option" do
|
41
|
+
it "should contain a description" do
|
42
|
+
@matcher = validate_format /[abc]+/, :name
|
43
|
+
@matcher.description.should == "validate format of :name against /[abc]+/"
|
44
|
+
end
|
45
|
+
it "should set failure messages" do
|
46
|
+
@matcher = validate_format /[abc]+/, :name
|
47
|
+
@matcher.matches? subject
|
48
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
49
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
50
|
+
end
|
51
|
+
end
|
52
|
+
describe "with options" do
|
53
|
+
it "should contain a description" do
|
54
|
+
@matcher = validate_format /[abc]+/, :name, :allow_nil => true
|
55
|
+
@matcher.description.should == "validate format of :name against /[abc]+/ with option(s) :allow_nil => true"
|
56
|
+
end
|
57
|
+
it "should set failure messages" do
|
58
|
+
@matcher = validate_format /[abc]+/, :price, :allow_nil => true
|
59
|
+
@matcher.matches? subject
|
60
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description
|
61
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
|
62
|
+
end
|
63
|
+
it "should explicit used options if different than expected" do
|
64
|
+
@matcher = validate_format /[abc]+/, :name, :allow_blank => true
|
65
|
+
@matcher.matches? subject
|
66
|
+
explanation = " but called with option(s) :allow_nil => true instead"
|
67
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
|
68
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
|
69
|
+
end
|
70
|
+
it "should warn if invalid options are used" do
|
71
|
+
@matcher = validate_format /[abc]+/, :name, :allow_anything => true
|
72
|
+
@matcher.matches? subject
|
73
|
+
explanation = " but option :allow_anything is not valid"
|
74
|
+
@matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
|
75
|
+
@matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "matchers" do
|
81
|
+
it{ should validate_format(/[abc]+/, :name) }
|
82
|
+
it{ should validate_format(/[abc]+/, :name, :allow_nil => true) }
|
83
|
+
it{ should_not validate_format(/[abc]+/, :price) }
|
84
|
+
it{ should_not validate_format(/[abc]/, :name) }
|
85
|
+
it{ should_not validate_format(/[abc]+/, :name, :allow_blank => true) }
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|