remarkable_mongo_ign 0.1.2

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.
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe 'have_key' do
4
+ subject do
5
+ Article.new
6
+ end
7
+
8
+ describe 'messages' do
9
+
10
+ it 'should contain a description' do
11
+ matcher = have_key(:title, String)
12
+ matcher.description.should == 'have key(s) title with type String'
13
+ end
14
+
15
+ it 'should set has_key? message' do
16
+ matcher = have_key(:owner, String)
17
+ matcher.matches?(subject)
18
+ matcher.failure_message.should == 'Expected Article to have key named owner with type String'
19
+ end
20
+
21
+ end
22
+
23
+ describe 'matchers' do
24
+ it { should have_key(:title, String) }
25
+ it { should have_keys(:title, :body, String) }
26
+ end
27
+
28
+ describe 'macros' do
29
+ should_have_key :title, String
30
+ should_have_keys :title, :body, String
31
+ end
32
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe 'validate_confirmation_of' do
4
+ include ModelBuilder
5
+
6
+ # Defines a model, create a validation and returns a raw matcher
7
+ def define_and_validate(options={})
8
+ @model = define_model :person do
9
+ include MongoMapper::Document
10
+
11
+ key :name, String
12
+ key :email, String
13
+ key :age, String
14
+
15
+ validates_confirmation_of :name, :email, options
16
+ end
17
+
18
+ validate_confirmation_of(:name, :email)
19
+ end
20
+
21
+ describe 'messages' do
22
+ before(:each){ @matcher = define_and_validate }
23
+
24
+ it 'should contain a description' do
25
+ @matcher.description.should == 'require name and email to be confirmed'
26
+ end
27
+
28
+ it 'should set responds_to_confirmation? message' do
29
+ @matcher = validate_confirmation_of(:age)
30
+ @matcher.matches?(@model)
31
+ @matcher.failure_message.should == 'Expected Person instance responds to age_confirmation'
32
+ end
33
+
34
+ it 'should set confirms? message' do
35
+ @model.instance_eval{ def age_confirmation=(*args); end }
36
+ @matcher = validate_confirmation_of(:age)
37
+ @matcher.matches?(@model)
38
+ @matcher.failure_message.should == 'Expected Person to be valid only when age is confirmed'
39
+ end
40
+
41
+ end
42
+
43
+ describe 'matchers' do
44
+
45
+ describe 'without options' do
46
+ before(:each){ define_and_validate }
47
+
48
+ it { should validate_confirmation_of(:name) }
49
+ it { should validate_confirmation_of(:name, :email) }
50
+ it { should_not validate_confirmation_of(:name, :age) }
51
+ end
52
+
53
+ create_message_specs(self)
54
+ end
55
+
56
+ describe 'macros' do
57
+ before(:each){ define_and_validate }
58
+
59
+ should_validate_confirmation_of :name
60
+ should_validate_confirmation_of :name, :email
61
+ should_not_validate_confirmation_of :name, :age
62
+ end
63
+
64
+ end
@@ -0,0 +1,147 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe 'validate_length_of' do
4
+ include ModelBuilder
5
+
6
+ # Defines a model, create a validation and returns a raw matcher
7
+ def define_and_validate(options={})
8
+ options = options.merge(:within => 3..5) if options.slice(:within, :maximum, :minimum, :is).empty?
9
+
10
+ @model = define_model :product do
11
+ include MongoMapper::Document
12
+
13
+ key :size, String
14
+ key :category, String
15
+
16
+ validates_length_of :size, options
17
+ end
18
+
19
+ validate_length_of(:size)
20
+ end
21
+
22
+ describe 'messages' do
23
+ before(:each){ @matcher = define_and_validate }
24
+
25
+ it 'should contain a description' do
26
+ @matcher.within(3..5)
27
+ @matcher.description.should == 'ensure length of size is within 3..5 characters'
28
+
29
+ @matcher.within(nil).is(3)
30
+ @matcher.description.should == 'ensure length of size is equal to 3 characters'
31
+
32
+ @matcher.is(nil).maximum(5)
33
+ @matcher.description.should == 'ensure length of size is maximum 5 characters'
34
+
35
+ @matcher.maximum(nil).minimum(3)
36
+ @matcher.description.should == 'ensure length of size is minimum 3 characters'
37
+
38
+ @matcher.allow_nil(false)
39
+ @matcher.description.should == 'ensure length of size is minimum 3 characters and not allowing nil values'
40
+
41
+ @matcher.allow_blank
42
+ @matcher.description.should == 'ensure length of size is minimum 3 characters, not allowing nil values, and allowing blank values'
43
+ end
44
+
45
+ it 'should set less_than_min_length? message' do
46
+ @matcher.within(4..5).matches?(@model)
47
+ @matcher.failure_message.should == 'Expected Product to be invalid when size length is less than 4 characters'
48
+ end
49
+
50
+ it 'should set exactly_min_length? message' do
51
+ @matcher.should_receive(:less_than_min_length?).and_return(true)
52
+ @matcher.within(2..5).matches?(@model)
53
+ @matcher.failure_message.should == 'Expected Product to be valid when size length is 2 characters'
54
+ end
55
+
56
+ it 'should set more_than_max_length? message' do
57
+ @matcher.within(3..4).matches?(@model)
58
+ @matcher.failure_message.should == 'Expected Product to be invalid when size length is more than 4 characters'
59
+ end
60
+
61
+ it 'should set exactly_max_length? message' do
62
+ @matcher.should_receive(:more_than_max_length?).and_return(true)
63
+ @matcher.within(3..6).matches?(@model)
64
+ @matcher.failure_message.should == 'Expected Product to be valid when size length is 6 characters'
65
+ end
66
+
67
+ it 'should set allow_blank? message' do
68
+ @matcher.within(3..5).allow_blank.matches?(@model)
69
+ @matcher.failure_message.should == 'Expected Product to allow blank values for size'
70
+ end
71
+
72
+ it 'should set allow_nil? message' do
73
+ @matcher.within(3..5).allow_nil.matches?(@model)
74
+ @matcher.failure_message.should == 'Expected Product to allow nil values for size'
75
+ end
76
+ end
77
+
78
+ describe 'matcher' do
79
+ # Wrap specs without options. Usually a couple specs.
80
+ describe 'without options' do
81
+ before(:each){ define_and_validate }
82
+
83
+ it { should validate_length_of(:size, :within => 3..5) }
84
+ it { should_not validate_length_of(:category, :within => 3..5) }
85
+ end
86
+
87
+ describe "with message option" do
88
+
89
+ # if RAILS_VERSION =~ /^2.3/
90
+ # it { should define_and_validate(:message => 'not valid').within(3..5).message('not valid') }
91
+ # it { should_not define_and_validate(:message => 'not valid').within(3..5).message('valid') }
92
+ # else
93
+ # it { should define_and_validate(:too_short => 'not valid', :too_long => 'not valid').within(3..5).message('not valid') }
94
+ # it { should_not define_and_validate(:too_short => 'not valid', :too_long => 'not valid').within(3..5).message('valid') }
95
+ # end
96
+
97
+ it { should define_and_validate(:is => 4, :message => 'not valid').is(4).message('not valid') }
98
+ it { should_not define_and_validate(:is => 4, :message => 'not valid').is(4).message('valid') }
99
+ end
100
+
101
+ describe "with within option" do
102
+ it { should define_and_validate(:within => 3..5).within(3..5) }
103
+ it { should_not define_and_validate(:within => 3..5).within(2..5) }
104
+ it { should_not define_and_validate(:within => 3..5).within(4..5) }
105
+ it { should_not define_and_validate(:within => 3..5).within(3..4) }
106
+ it { should_not define_and_validate(:within => 3..5).within(3..6) }
107
+ end
108
+
109
+ describe "with minimum option" do
110
+ it { should define_and_validate(:minimum => 3).minimum(3) }
111
+ it { should_not define_and_validate(:minimum => 3).minimum(2) }
112
+ it { should_not define_and_validate(:minimum => 3).minimum(4) }
113
+ end
114
+
115
+ describe "with maximum option" do
116
+ it { should define_and_validate(:maximum => 3).maximum(3) }
117
+ it { should_not define_and_validate(:maximum => 3).maximum(2) }
118
+ it { should_not define_and_validate(:maximum => 3).maximum(4) }
119
+ end
120
+
121
+ describe "with is option" do
122
+ it { should define_and_validate(:is => 3).is(3) }
123
+ it { should_not define_and_validate(:is => 3).is(2) }
124
+ it { should_not define_and_validate(:is => 3).is(4) }
125
+ end
126
+
127
+ # Those are macros to test optionals which accept only boolean values
128
+ create_optional_boolean_specs(:allow_nil, self)
129
+ create_optional_boolean_specs(:allow_blank, self)
130
+ end
131
+
132
+ # In macros we include just a few tests to assure that everything works properly
133
+ describe 'macros' do
134
+ before(:each) { define_and_validate }
135
+
136
+ should_validate_length_of :size, :within => 3..5
137
+
138
+ should_not_validate_length_of :size, :within => 2..5
139
+ should_not_validate_length_of :size, :within => 4..5
140
+ should_not_validate_length_of :size, :within => 3..4
141
+ should_not_validate_length_of :size, :within => 3..6
142
+
143
+ should_validate_length_of :size do |m|
144
+ m.within = 3..5
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe 'validate_presence_of' do
4
+ subject do
5
+ Article.new
6
+ end
7
+
8
+ describe 'messages' do
9
+
10
+ it 'should contain a description' do
11
+ matcher = validate_presence_of(:title, :body)
12
+ matcher.description.should == 'require title and body to be set'
13
+ end
14
+
15
+ it 'should set allow_nil? message' do
16
+ matcher = validate_presence_of(:body)
17
+ matcher.matches?(subject)
18
+ matcher.failure_message.should == 'Expected Article to require body to be set'
19
+ matcher.negative_failure_message.should == 'Did not expect Article to require body to be set'
20
+ end
21
+
22
+ end
23
+
24
+ describe 'matchers' do
25
+ it { should validate_presence_of(:title) }
26
+ it { should_not validate_presence_of(:body) }
27
+ end
28
+
29
+ describe 'macros' do
30
+ should_validate_presence_of(:title)
31
+ should_not_validate_presence_of(:body)
32
+ end
33
+ end
@@ -0,0 +1,64 @@
1
+ # This is based on Shoulda model builder for Test::Unit.
2
+ #
3
+ module ModelBuilder
4
+ def self.included(base)
5
+ return unless base.name =~ /^Spec/
6
+
7
+ base.class_eval do
8
+ after(:each) do
9
+ if @defined_constants
10
+ @defined_constants.each do |class_name|
11
+ Object.send(:remove_const, class_name)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ base.extend ClassMethods
18
+ end
19
+
20
+ def define_constant(class_name, &block)
21
+ class_name = class_name.to_s.camelize
22
+
23
+ klass = Class.new
24
+ Object.const_set(class_name, klass)
25
+
26
+ klass.class_eval(&block) if block_given?
27
+
28
+ @defined_constants ||= []
29
+ @defined_constants << class_name
30
+
31
+ klass
32
+ end
33
+
34
+ def define_model(name, columns = {}, &block)
35
+ instance = define_constant(name.to_s.classify, &block).new
36
+
37
+ self.class.subject { instance } if self.class.respond_to?(:subject)
38
+ instance
39
+ end
40
+
41
+ module ClassMethods
42
+ # This is a macro to run validations of boolean optionals such as :allow_nil
43
+ # and :allow_blank. This macro tests all scenarios. The specs must have a
44
+ # define_and_validate method defined.
45
+ #
46
+ def create_optional_boolean_specs(optional, base, options={})
47
+ base.describe "with #{optional} option" do
48
+ it { should define_and_validate(options.merge(optional => true)).send(optional) }
49
+ it { should define_and_validate(options.merge(optional => false)).send(optional, false) }
50
+ it { should_not define_and_validate(options.merge(optional => true)).send(optional, false) }
51
+ it { should_not define_and_validate(options.merge(optional => false)).send(optional) }
52
+ end
53
+ end
54
+
55
+ def create_message_specs(base)
56
+ base.describe "with message option" do
57
+ it { should define_and_validate(:message => 'valid_message').message('valid_message') }
58
+ it { should_not define_and_validate(:message => 'not_valid').message('valid_message') }
59
+ end
60
+ end
61
+ end
62
+
63
+ end
64
+
data/spec/models.rb ADDED
@@ -0,0 +1,42 @@
1
+ class Article
2
+ include MongoMapper::Document
3
+
4
+ key :title, String, :required => true
5
+ key :body, String
6
+
7
+ many :comments
8
+ many :unknowns
9
+ many :ratings, :class_name => "Rate"
10
+ many :assets
11
+ belongs_to :user
12
+ belongs_to :unknown
13
+ belongs_to :site, :class_name => 'Site'
14
+ end
15
+
16
+ class Comment
17
+ include MongoMapper::EmbeddedDocument
18
+
19
+ key :body, String
20
+ end
21
+
22
+ class User
23
+ include MongoMapper::Document
24
+
25
+ key :login, String
26
+ end
27
+
28
+ class Rate
29
+ include MongoMapper::EmbeddedDocument
30
+ end
31
+
32
+ class Rating
33
+ include MongoMapper::EmbeddedDocument
34
+ end
35
+
36
+ class Site
37
+ include MongoMapper::Document
38
+ end
39
+
40
+ class Webiste
41
+ include MongoMapper::EmbeddedDocument
42
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,17 @@
1
+ require "rubygems"
2
+ require "mongo_mapper"
3
+
4
+ MongoMapper.database = "remarkable_mongomapper"
5
+
6
+ def reset_test_db!
7
+ MongoMapper.connection.drop_database("remarkable_mongomapper")
8
+ end
9
+
10
+ Spec::Runner.configure do |config|
11
+ config.before(:all) { reset_test_db! }
12
+ config.after(:all) { reset_test_db! }
13
+ end
14
+
15
+ require File.join(File.dirname(__FILE__), "..", "lib", "remarkable/mongo_mapper")
16
+ require File.join(File.dirname(__FILE__), "models")
17
+ require File.join(File.dirname(__FILE__), "model_builder")
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remarkable_mongo_ign
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - "Nicolas M\xC3\xA9rouze"
13
+ - Chandra Patni
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-21 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: remarkable
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 1
31
+ - 13
32
+ version: 3.1.13
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongo_mapper_ign
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 7
45
+ - 6
46
+ version: 0.7.6
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description:
50
+ email: nicolas.merouze@gmail.com, rubyorchard@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.md
58
+ files:
59
+ - .gitignore
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - VERSION
64
+ - lib/remarkable/mongo_mapper.rb
65
+ - lib/remarkable/mongo_mapper/base.rb
66
+ - lib/remarkable/mongo_mapper/describe.rb
67
+ - lib/remarkable/mongo_mapper/human_names.rb
68
+ - lib/remarkable/mongo_mapper/matchers/allow_values_for_matcher.rb
69
+ - lib/remarkable/mongo_mapper/matchers/association_matcher.rb
70
+ - lib/remarkable/mongo_mapper/matchers/have_key_matcher.rb
71
+ - lib/remarkable/mongo_mapper/matchers/validate_confirmation_of_matcher.rb
72
+ - lib/remarkable/mongo_mapper/matchers/validate_length_of_matcher.rb
73
+ - lib/remarkable/mongo_mapper/matchers/validate_presence_of_matcher.rb
74
+ - locales/en.yml
75
+ - remarkable_mongo.gemspec
76
+ - remarkable_mongo_ign.gemspec
77
+ - spec/matchers/allow_values_for_matcher_spec.rb
78
+ - spec/matchers/association_matcher_spec.rb
79
+ - spec/matchers/have_key_matcher_spec.rb
80
+ - spec/matchers/validate_confirmation_of_matcher_spec.rb
81
+ - spec/matchers/validate_length_of_matcher_spec.rb
82
+ - spec/matchers/validate_presence_of_matcher_spec.rb
83
+ - spec/model_builder.rb
84
+ - spec/models.rb
85
+ - spec/spec.opts
86
+ - spec/spec_helper.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/rubyorchard/remarkable_mongo
89
+ licenses: []
90
+
91
+ post_install_message:
92
+ rdoc_options:
93
+ - --charset=UTF-8
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.3.6
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Remarkable Matchers for MongoDB ORMs
117
+ test_files:
118
+ - spec/matchers/allow_values_for_matcher_spec.rb
119
+ - spec/matchers/association_matcher_spec.rb
120
+ - spec/matchers/have_key_matcher_spec.rb
121
+ - spec/matchers/validate_confirmation_of_matcher_spec.rb
122
+ - spec/matchers/validate_length_of_matcher_spec.rb
123
+ - spec/matchers/validate_presence_of_matcher_spec.rb
124
+ - spec/model_builder.rb
125
+ - spec/models.rb
126
+ - spec/spec_helper.rb