ippy04-remarkable_mongo 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/LICENSE +20 -0
- data/README.md +23 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/lib/remarkable/mongo_mapper.rb +30 -0
- data/lib/remarkable/mongo_mapper/base.rb +223 -0
- data/lib/remarkable/mongo_mapper/describe.rb +199 -0
- data/lib/remarkable/mongo_mapper/human_names.rb +37 -0
- data/lib/remarkable/mongo_mapper/matchers/allow_values_for_matcher.rb +86 -0
- data/lib/remarkable/mongo_mapper/matchers/association_matcher.rb +105 -0
- data/lib/remarkable/mongo_mapper/matchers/have_key_matcher.rb +38 -0
- data/lib/remarkable/mongo_mapper/matchers/validate_confirmation_of_matcher.rb +44 -0
- data/lib/remarkable/mongo_mapper/matchers/validate_length_of_matcher.rb +106 -0
- data/lib/remarkable/mongo_mapper/matchers/validate_presence_of_matcher.rb +37 -0
- data/locales/en.yml +135 -0
- data/remarkable_mongo.gemspec +79 -0
- data/spec/matchers/allow_values_for_matcher_spec.rb +71 -0
- data/spec/matchers/association_matcher_spec.rb +104 -0
- data/spec/matchers/have_key_matcher_spec.rb +32 -0
- data/spec/matchers/validate_confirmation_of_matcher_spec.rb +64 -0
- data/spec/matchers/validate_length_of_matcher_spec.rb +147 -0
- data/spec/matchers/validate_presence_of_matcher_spec.rb +33 -0
- data/spec/model_builder.rb +64 -0
- data/spec/models.rb +42 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +17 -0
- metadata +125 -0
@@ -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
data/spec/spec_helper.rb
ADDED
@@ -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,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ippy04-remarkable_mongo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Nicolas Merouze
|
13
|
+
- Justin Ip
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-02-27 00:00:00 -06: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
|
+
- 8
|
32
|
+
version: 3.1.8
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: mongo_mapper
|
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
|
+
- 0
|
46
|
+
version: 0.7.0
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description:
|
50
|
+
email: nicolas.merouze@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
|
+
- spec/matchers/allow_values_for_matcher_spec.rb
|
77
|
+
- spec/matchers/association_matcher_spec.rb
|
78
|
+
- spec/matchers/have_key_matcher_spec.rb
|
79
|
+
- spec/matchers/validate_confirmation_of_matcher_spec.rb
|
80
|
+
- spec/matchers/validate_length_of_matcher_spec.rb
|
81
|
+
- spec/matchers/validate_presence_of_matcher_spec.rb
|
82
|
+
- spec/model_builder.rb
|
83
|
+
- spec/models.rb
|
84
|
+
- spec/spec.opts
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
has_rdoc: true
|
87
|
+
homepage: http://github.com/ippy04/remarkable_mongo
|
88
|
+
licenses: []
|
89
|
+
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options:
|
92
|
+
- --charset=UTF-8
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
requirements: []
|
110
|
+
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.3.6
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Remarkable Matchers for MongoDB ORMs
|
116
|
+
test_files:
|
117
|
+
- spec/matchers/allow_values_for_matcher_spec.rb
|
118
|
+
- spec/matchers/association_matcher_spec.rb
|
119
|
+
- spec/matchers/have_key_matcher_spec.rb
|
120
|
+
- spec/matchers/validate_confirmation_of_matcher_spec.rb
|
121
|
+
- spec/matchers/validate_length_of_matcher_spec.rb
|
122
|
+
- spec/matchers/validate_presence_of_matcher_spec.rb
|
123
|
+
- spec/model_builder.rb
|
124
|
+
- spec/models.rb
|
125
|
+
- spec/spec_helper.rb
|