ssherman-mongo_mapper 0.8.6
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/UPGRADES +7 -0
- data/bin/mmconsole +60 -0
- data/examples/attr_accessible.rb +22 -0
- data/examples/attr_protected.rb +22 -0
- data/examples/cache_key.rb +24 -0
- data/examples/custom_types.rb +24 -0
- data/examples/identity_map.rb +33 -0
- data/examples/identity_map/automatic.rb +8 -0
- data/examples/identity_map/middleware.rb +14 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/safe.rb +43 -0
- data/examples/scopes.rb +52 -0
- data/examples/validating/embedded_docs.rb +29 -0
- data/lib/mongo_mapper.rb +79 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +41 -0
- data/lib/mongo_mapper/embedded_document.rb +31 -0
- data/lib/mongo_mapper/exceptions.rb +27 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins.rb +15 -0
- data/lib/mongo_mapper/plugins.rb~ +15 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations.rb +97 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +109 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +139 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
- data/lib/mongo_mapper/plugins/clone.rb +22 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +124 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
- data/lib/mongo_mapper/plugins/equality.rb +17 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +313 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +59 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
- data/lib/mongo_mapper/plugins/pagination.rb +14 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +53 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +76 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
- data/lib/mongo_mapper/plugins/validations.rb +50 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
- data/lib/mongo_mapper/version.rb +4 -0
- data/rails/init.rb +19 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
- data/test/functional/associations/test_in_array_proxy.rb +319 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +615 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +81 -0
- data/test/functional/associations/test_one_proxy.rb +181 -0
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +151 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +253 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +210 -0
- data/test/functional/test_identity_map.rb +514 -0
- data/test/functional/test_indexes.rb +42 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +416 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +175 -0
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_userstamps.rb +27 -0
- data/test/functional/test_validations.rb +342 -0
- data/test/models.rb +233 -0
- data/test/test_active_model_lint.rb +13 -0
- data/test/test_helper.rb +100 -0
- data/test/unit/associations/test_base.rb +212 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +217 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +213 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +644 -0
- data/test/unit/test_extensions.rb +376 -0
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +89 -0
- data/test/unit/test_mongo_mapper.rb +110 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +181 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +564 -0
- metadata +340 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SerializationTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@document = EDoc do
|
6
|
+
key :name, String
|
7
|
+
key :age, Integer
|
8
|
+
key :awesome, Boolean
|
9
|
+
key :preferences, Hash
|
10
|
+
key :created_at, Time
|
11
|
+
end
|
12
|
+
|
13
|
+
@instance = @document.new(
|
14
|
+
:name => 'John Doe',
|
15
|
+
:age => 25,
|
16
|
+
:awesome => true,
|
17
|
+
:preferences => {:language => 'Ruby'},
|
18
|
+
:created_at => Time.now.change(:usec => 0)
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
[:json].each do |format|
|
23
|
+
context format do
|
24
|
+
should "be reversable" do
|
25
|
+
serialized = @instance.send("to_#{format}")
|
26
|
+
unserialized = @document.send("from_#{format}", serialized)
|
27
|
+
|
28
|
+
assert_equal @instance, unserialized
|
29
|
+
end
|
30
|
+
|
31
|
+
should "allow attribute only filtering" do
|
32
|
+
serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
|
33
|
+
unserialized = @document.send("from_#{format}", serialized)
|
34
|
+
|
35
|
+
assert_equal @instance.name, unserialized.name
|
36
|
+
assert_equal @instance.age, unserialized.age
|
37
|
+
assert ! unserialized.awesome
|
38
|
+
assert_nil unserialized.created_at
|
39
|
+
end
|
40
|
+
|
41
|
+
should "allow attribute except filtering" do
|
42
|
+
serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
|
43
|
+
unserialized = @document.send("from_#{format}", serialized)
|
44
|
+
|
45
|
+
assert_nil unserialized.name
|
46
|
+
assert_nil unserialized.age
|
47
|
+
assert_equal @instance.awesome, unserialized.awesome
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TimeZonesTest < Test::Unit::TestCase
|
4
|
+
context "An instance of an embedded document" do
|
5
|
+
setup do
|
6
|
+
@document = EDoc do
|
7
|
+
key :name, String
|
8
|
+
key :created_at, Time
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
should "work without Time.zone" do
|
13
|
+
Time.zone = nil
|
14
|
+
|
15
|
+
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
16
|
+
doc.created_at.should == Time.local(2009, 8, 15, 14, 0, 0).utc
|
17
|
+
end
|
18
|
+
|
19
|
+
should "work with Time.zone set to the (default) UTC" do
|
20
|
+
Time.zone = 'UTC'
|
21
|
+
|
22
|
+
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
23
|
+
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
24
|
+
doc.created_at.should == Time.utc(2009, 8, 15, 14)
|
25
|
+
|
26
|
+
Time.zone = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
should "work with timezones that are not UTC" do
|
30
|
+
Time.zone = 'Hawaii'
|
31
|
+
|
32
|
+
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
33
|
+
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
34
|
+
doc.created_at.should == Time.utc(2009, 8, 16)
|
35
|
+
|
36
|
+
Time.zone = nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,564 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationsTest < Test::Unit::TestCase
|
4
|
+
context "Validations" do
|
5
|
+
context "on a Document" do
|
6
|
+
setup do
|
7
|
+
@document = Doc()
|
8
|
+
end
|
9
|
+
|
10
|
+
context "Validating uniquness of" do
|
11
|
+
should "not validate nil when allow_nil false" do
|
12
|
+
@document.key :name, String
|
13
|
+
@document.validates_uniqueness_of :name, :allow_nil => false
|
14
|
+
doc = @document.new(:name => nil)
|
15
|
+
doc.should have_error_on(:name)
|
16
|
+
doc.name = "Ryan"
|
17
|
+
doc.should_not have_error_on(:name)
|
18
|
+
end
|
19
|
+
|
20
|
+
should "not validate blank when allow_blank false" do
|
21
|
+
@document.key :name, String
|
22
|
+
@document.validates_uniqueness_of :name, :allow_blank => false
|
23
|
+
doc = @document.new(:name => "")
|
24
|
+
doc.should have_error_on(:name)
|
25
|
+
doc.name = "Ryan"
|
26
|
+
doc.should_not have_error_on(:name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "Validating acceptance of" do
|
31
|
+
should "work with validates_acceptance_of macro" do
|
32
|
+
@document.key :terms, String
|
33
|
+
@document.validates_acceptance_of :terms
|
34
|
+
doc = @document.new(:terms => '')
|
35
|
+
doc.should have_error_on(:terms)
|
36
|
+
doc.terms = '1'
|
37
|
+
doc.should_not have_error_on(:terms)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "validating confirmation of" do
|
42
|
+
should "work with validates_confirmation_of macro" do
|
43
|
+
@document.key :password, String
|
44
|
+
@document.validates_confirmation_of :password
|
45
|
+
doc = @document.new
|
46
|
+
doc.password = 'foobar'
|
47
|
+
doc.should have_error_on(:password)
|
48
|
+
doc.password_confirmation = 'foobar'
|
49
|
+
doc.should_not have_error_on(:password)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "validating format of" do
|
54
|
+
should "work with validates_format_of macro" do
|
55
|
+
@document.key :name, String
|
56
|
+
@document.validates_format_of :name, :with => /.+/
|
57
|
+
doc = @document.new
|
58
|
+
doc.should have_error_on(:name)
|
59
|
+
doc.name = 'John'
|
60
|
+
doc.should_not have_error_on(:name)
|
61
|
+
end
|
62
|
+
|
63
|
+
should "work with :format shorcut key" do
|
64
|
+
@document.key :name, String, :format => /.+/
|
65
|
+
doc = @document.new
|
66
|
+
doc.should have_error_on(:name)
|
67
|
+
doc.name = 'John'
|
68
|
+
doc.should_not have_error_on(:name)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "validating length of" do
|
73
|
+
should "work with validates_length_of macro" do
|
74
|
+
@document.key :name, String
|
75
|
+
@document.validates_length_of :name, :minimum => 5
|
76
|
+
doc = @document.new
|
77
|
+
doc.should have_error_on(:name)
|
78
|
+
end
|
79
|
+
|
80
|
+
context "with :length => integer shortcut" do
|
81
|
+
should "set maximum of integer provided" do
|
82
|
+
@document.key :name, String, :length => 5
|
83
|
+
doc = @document.new
|
84
|
+
doc.name = '123456'
|
85
|
+
doc.should have_error_on(:name)
|
86
|
+
doc.name = '12345'
|
87
|
+
doc.should_not have_error_on(:name)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "with :length => range shortcut" do
|
92
|
+
setup do
|
93
|
+
@document.key :name, String, :length => 5..7
|
94
|
+
end
|
95
|
+
|
96
|
+
should "set minimum of range min" do
|
97
|
+
doc = @document.new
|
98
|
+
doc.should have_error_on(:name)
|
99
|
+
doc.name = '123456'
|
100
|
+
doc.should_not have_error_on(:name)
|
101
|
+
end
|
102
|
+
|
103
|
+
should "set maximum of range max" do
|
104
|
+
doc = @document.new
|
105
|
+
doc.should have_error_on(:name)
|
106
|
+
doc.name = '12345678'
|
107
|
+
doc.should have_error_on(:name)
|
108
|
+
doc.name = '123456'
|
109
|
+
doc.should_not have_error_on(:name)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "with :length => hash shortcut" do
|
114
|
+
should "pass options through" do
|
115
|
+
@document.key :name, String, :length => {:minimum => 2}
|
116
|
+
doc = @document.new
|
117
|
+
doc.should have_error_on(:name)
|
118
|
+
doc.name = '12'
|
119
|
+
doc.should_not have_error_on(:name)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end # validates_length_of
|
123
|
+
|
124
|
+
context "Validating numericality of" do
|
125
|
+
should "work with validates_numericality_of macro" do
|
126
|
+
@document.key :age, Integer
|
127
|
+
@document.validates_numericality_of :age
|
128
|
+
doc = @document.new
|
129
|
+
doc.age = 'String'
|
130
|
+
doc.should have_error_on(:age)
|
131
|
+
doc.age = 23
|
132
|
+
doc.should_not have_error_on(:age)
|
133
|
+
end
|
134
|
+
|
135
|
+
context "with :numeric shortcut" do
|
136
|
+
should "work with integer or float" do
|
137
|
+
@document.key :weight, Float, :numeric => true
|
138
|
+
doc = @document.new
|
139
|
+
doc.weight = 'String'
|
140
|
+
doc.should have_error_on(:weight)
|
141
|
+
doc.weight = 23.0
|
142
|
+
doc.should_not have_error_on(:weight)
|
143
|
+
doc.weight = 23
|
144
|
+
doc.should_not have_error_on(:weight)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "with :numeric shortcut on Integer key" do
|
149
|
+
should "only work with integers" do
|
150
|
+
@document.key :age, Integer, :numeric => true
|
151
|
+
doc = @document.new
|
152
|
+
doc.age = 'String'
|
153
|
+
doc.should have_error_on(:age)
|
154
|
+
doc.age = 23.1
|
155
|
+
doc.should have_error_on(:age)
|
156
|
+
doc.age = 23
|
157
|
+
doc.should_not have_error_on(:age)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end # numericality of
|
161
|
+
|
162
|
+
context "validating presence of" do
|
163
|
+
should "work with validates_presence_of macro" do
|
164
|
+
@document.key :name, String
|
165
|
+
@document.validates_presence_of :name
|
166
|
+
doc = @document.new
|
167
|
+
doc.should have_error_on(:name)
|
168
|
+
end
|
169
|
+
|
170
|
+
should "work with :required shortcut on key definition" do
|
171
|
+
@document.key :name, String, :required => true
|
172
|
+
doc = @document.new
|
173
|
+
doc.should have_error_on(:name)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "validating exclusion of" do
|
178
|
+
should "throw error if enumerator not provided" do
|
179
|
+
@document.key :action, String
|
180
|
+
lambda {
|
181
|
+
@document.validates_exclusion_of :action
|
182
|
+
}.should raise_error(ArgumentError)
|
183
|
+
end
|
184
|
+
|
185
|
+
should "work with validates_exclusion_of macro" do
|
186
|
+
@document.key :action, String
|
187
|
+
@document.validates_exclusion_of :action, :within => %w(kick run)
|
188
|
+
|
189
|
+
doc = @document.new
|
190
|
+
doc.should_not have_error_on(:action)
|
191
|
+
|
192
|
+
doc.action = 'fart'
|
193
|
+
doc.should_not have_error_on(:action)
|
194
|
+
|
195
|
+
doc.action = 'kick'
|
196
|
+
doc.should have_error_on(:action, 'is reserved')
|
197
|
+
end
|
198
|
+
|
199
|
+
should "work with :not_in shortcut on key definition" do
|
200
|
+
@document.key :action, String, :not_in => %w(kick run)
|
201
|
+
|
202
|
+
doc = @document.new
|
203
|
+
doc.should_not have_error_on(:action)
|
204
|
+
|
205
|
+
doc.action = 'fart'
|
206
|
+
doc.should_not have_error_on(:action)
|
207
|
+
|
208
|
+
doc.action = 'kick'
|
209
|
+
doc.should have_error_on(:action, 'is reserved')
|
210
|
+
end
|
211
|
+
|
212
|
+
should "not have error if allow nil is true and value is nil" do
|
213
|
+
@document.key :action, String
|
214
|
+
@document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
215
|
+
|
216
|
+
doc = @document.new
|
217
|
+
doc.should_not have_error_on(:action)
|
218
|
+
end
|
219
|
+
|
220
|
+
should "not have error if allow blank is true and value is blank" do
|
221
|
+
@document.key :action, String
|
222
|
+
@document.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
223
|
+
|
224
|
+
doc = @document.new(:action => '')
|
225
|
+
doc.should_not have_error_on(:action)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
context "validating inclusion of" do
|
230
|
+
should "throw error if enumerator not provided" do
|
231
|
+
@document.key :action, String
|
232
|
+
lambda {
|
233
|
+
@document.validates_inclusion_of :action
|
234
|
+
}.should raise_error(ArgumentError)
|
235
|
+
end
|
236
|
+
|
237
|
+
should "work with validates_inclusion_of macro" do
|
238
|
+
@document.key :action, String
|
239
|
+
@document.validates_inclusion_of :action, :within => %w(kick run)
|
240
|
+
|
241
|
+
doc = @document.new
|
242
|
+
doc.should have_error_on(:action, 'is not in the list')
|
243
|
+
|
244
|
+
doc.action = 'fart'
|
245
|
+
doc.should have_error_on(:action, 'is not in the list')
|
246
|
+
|
247
|
+
doc.action = 'kick'
|
248
|
+
doc.should_not have_error_on(:action)
|
249
|
+
end
|
250
|
+
|
251
|
+
should "work with :in shortcut on key definition" do
|
252
|
+
@document.key :action, String, :in => %w(kick run)
|
253
|
+
|
254
|
+
doc = @document.new
|
255
|
+
doc.should have_error_on(:action, 'is not in the list')
|
256
|
+
|
257
|
+
doc.action = 'fart'
|
258
|
+
doc.should have_error_on(:action, 'is not in the list')
|
259
|
+
|
260
|
+
doc.action = 'kick'
|
261
|
+
doc.should_not have_error_on(:action)
|
262
|
+
end
|
263
|
+
|
264
|
+
should "not have error if allow nil is true and value is nil" do
|
265
|
+
@document.key :action, String
|
266
|
+
@document.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
|
267
|
+
|
268
|
+
doc = @document.new
|
269
|
+
doc.should_not have_error_on(:action)
|
270
|
+
end
|
271
|
+
|
272
|
+
should "not have error if allow blank is true and value is blank" do
|
273
|
+
@document.key :action, String
|
274
|
+
@document.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
|
275
|
+
|
276
|
+
doc = @document.new(:action => '')
|
277
|
+
doc.should_not have_error_on(:action)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
end # End on a Document
|
282
|
+
|
283
|
+
context "On an EmbeddedDocument" do
|
284
|
+
setup do
|
285
|
+
@embedded_doc = EDoc()
|
286
|
+
end
|
287
|
+
|
288
|
+
context "Validating acceptance of" do
|
289
|
+
should "work with validates_acceptance_of macro" do
|
290
|
+
@embedded_doc.key :terms, String
|
291
|
+
@embedded_doc.validates_acceptance_of :terms
|
292
|
+
doc = @embedded_doc.new(:terms => '')
|
293
|
+
doc.should have_error_on(:terms)
|
294
|
+
doc.terms = '1'
|
295
|
+
doc.should_not have_error_on(:terms)
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
context "validating confirmation of" do
|
300
|
+
should "work with validates_confirmation_of macro" do
|
301
|
+
@embedded_doc.key :password, String
|
302
|
+
@embedded_doc.validates_confirmation_of :password
|
303
|
+
doc = @embedded_doc.new
|
304
|
+
doc.password = 'foobar'
|
305
|
+
doc.should have_error_on(:password)
|
306
|
+
doc.password_confirmation = 'foobar'
|
307
|
+
doc.should_not have_error_on(:password)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
context "validating format of" do
|
312
|
+
should "work with validates_format_of macro" do
|
313
|
+
@embedded_doc.key :name, String
|
314
|
+
@embedded_doc.validates_format_of :name, :with => /.+/
|
315
|
+
doc = @embedded_doc.new
|
316
|
+
doc.should have_error_on(:name)
|
317
|
+
doc.name = 'John'
|
318
|
+
doc.should_not have_error_on(:name)
|
319
|
+
end
|
320
|
+
|
321
|
+
should "work with :format shorcut key" do
|
322
|
+
@embedded_doc.key :name, String, :format => /.+/
|
323
|
+
doc = @embedded_doc.new
|
324
|
+
doc.should have_error_on(:name)
|
325
|
+
doc.name = 'John'
|
326
|
+
doc.should_not have_error_on(:name)
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
context "validating length of" do
|
331
|
+
should "work with validates_length_of macro" do
|
332
|
+
@embedded_doc.key :name, String
|
333
|
+
@embedded_doc.validates_length_of :name, :minimum => 5
|
334
|
+
doc = @embedded_doc.new
|
335
|
+
doc.should have_error_on(:name)
|
336
|
+
end
|
337
|
+
|
338
|
+
context "with :length => integer shortcut" do
|
339
|
+
should "set maximum of integer provided" do
|
340
|
+
@embedded_doc.key :name, String, :length => 5
|
341
|
+
doc = @embedded_doc.new
|
342
|
+
doc.name = '123456'
|
343
|
+
doc.should have_error_on(:name)
|
344
|
+
doc.name = '12345'
|
345
|
+
doc.should_not have_error_on(:name)
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
context "with :length => range shortcut" do
|
350
|
+
setup do
|
351
|
+
@embedded_doc.key :name, String, :length => 5..7
|
352
|
+
end
|
353
|
+
|
354
|
+
should "set minimum of range min" do
|
355
|
+
doc = @embedded_doc.new
|
356
|
+
doc.should have_error_on(:name)
|
357
|
+
doc.name = '123456'
|
358
|
+
doc.should_not have_error_on(:name)
|
359
|
+
end
|
360
|
+
|
361
|
+
should "set maximum of range max" do
|
362
|
+
doc = @embedded_doc.new
|
363
|
+
doc.should have_error_on(:name)
|
364
|
+
doc.name = '12345678'
|
365
|
+
doc.should have_error_on(:name)
|
366
|
+
doc.name = '123456'
|
367
|
+
doc.should_not have_error_on(:name)
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
context "with :length => hash shortcut" do
|
372
|
+
should "pass options through" do
|
373
|
+
@embedded_doc.key :name, String, :length => {:minimum => 2}
|
374
|
+
doc = @embedded_doc.new
|
375
|
+
doc.should have_error_on(:name)
|
376
|
+
doc.name = '12'
|
377
|
+
doc.should_not have_error_on(:name)
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end # validates_length_of
|
381
|
+
|
382
|
+
context "Validating numericality of" do
|
383
|
+
should "work with validates_numericality_of macro" do
|
384
|
+
@embedded_doc.key :age, Integer
|
385
|
+
@embedded_doc.validates_numericality_of :age
|
386
|
+
doc = @embedded_doc.new
|
387
|
+
doc.age = 'String'
|
388
|
+
doc.should have_error_on(:age)
|
389
|
+
doc.age = 23
|
390
|
+
doc.should_not have_error_on(:age)
|
391
|
+
end
|
392
|
+
|
393
|
+
context "with :numeric shortcut" do
|
394
|
+
should "work with integer or float" do
|
395
|
+
@embedded_doc.key :weight, Float, :numeric => true
|
396
|
+
doc = @embedded_doc.new
|
397
|
+
doc.weight = 'String'
|
398
|
+
doc.should have_error_on(:weight)
|
399
|
+
doc.weight = 23.0
|
400
|
+
doc.should_not have_error_on(:weight)
|
401
|
+
doc.weight = 23
|
402
|
+
doc.should_not have_error_on(:weight)
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
context "with :numeric shortcut on Integer key" do
|
407
|
+
should "only work with integers" do
|
408
|
+
@embedded_doc.key :age, Integer, :numeric => true
|
409
|
+
doc = @embedded_doc.new
|
410
|
+
doc.age = 'String'
|
411
|
+
doc.should have_error_on(:age)
|
412
|
+
doc.age = 23.1
|
413
|
+
doc.should have_error_on(:age)
|
414
|
+
doc.age = 23
|
415
|
+
doc.should_not have_error_on(:age)
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end # numericality of
|
419
|
+
|
420
|
+
context "validating presence of" do
|
421
|
+
should "work with validates_presence_of macro" do
|
422
|
+
@embedded_doc.key :name, String
|
423
|
+
@embedded_doc.validates_presence_of :name
|
424
|
+
doc = @embedded_doc.new
|
425
|
+
doc.should have_error_on(:name)
|
426
|
+
end
|
427
|
+
|
428
|
+
should "work with :required shortcut on key definition" do
|
429
|
+
@embedded_doc.key :name, String, :required => true
|
430
|
+
doc = @embedded_doc.new
|
431
|
+
doc.should have_error_on(:name)
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
context "validating exclusion of" do
|
436
|
+
should "throw error if enumerator not provided" do
|
437
|
+
@embedded_doc.key :action, String
|
438
|
+
lambda {
|
439
|
+
@embedded_doc.validates_exclusion_of :action
|
440
|
+
}.should raise_error(ArgumentError)
|
441
|
+
end
|
442
|
+
|
443
|
+
should "work with validates_exclusion_of macro" do
|
444
|
+
@embedded_doc.key :action, String
|
445
|
+
@embedded_doc.validates_exclusion_of :action, :within => %w(kick run)
|
446
|
+
|
447
|
+
doc = @embedded_doc.new
|
448
|
+
doc.should_not have_error_on(:action)
|
449
|
+
|
450
|
+
doc.action = 'fart'
|
451
|
+
doc.should_not have_error_on(:action)
|
452
|
+
|
453
|
+
doc.action = 'kick'
|
454
|
+
doc.should have_error_on(:action, 'is reserved')
|
455
|
+
end
|
456
|
+
|
457
|
+
should "work with :not_in shortcut on key definition" do
|
458
|
+
@embedded_doc.key :action, String, :not_in => %w(kick run)
|
459
|
+
|
460
|
+
doc = @embedded_doc.new
|
461
|
+
doc.should_not have_error_on(:action)
|
462
|
+
|
463
|
+
doc.action = 'fart'
|
464
|
+
doc.should_not have_error_on(:action)
|
465
|
+
|
466
|
+
doc.action = 'kick'
|
467
|
+
doc.should have_error_on(:action, 'is reserved')
|
468
|
+
end
|
469
|
+
|
470
|
+
should "not have error if allow nil is true and value is nil" do
|
471
|
+
@embedded_doc.key :action, String
|
472
|
+
@embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
473
|
+
|
474
|
+
doc = @embedded_doc.new
|
475
|
+
doc.should_not have_error_on(:action)
|
476
|
+
end
|
477
|
+
|
478
|
+
should "not have error if allow blank is true and value is blank" do
|
479
|
+
@embedded_doc.key :action, String
|
480
|
+
@embedded_doc.validates_exclusion_of :action, :within => %w(kick run), :allow_nil => true
|
481
|
+
|
482
|
+
doc = @embedded_doc.new(:action => '')
|
483
|
+
doc.should_not have_error_on(:action)
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
context "validating inclusion of" do
|
488
|
+
should "throw error if enumerator not provided" do
|
489
|
+
@embedded_doc.key :action, String
|
490
|
+
lambda {
|
491
|
+
@embedded_doc.validates_inclusion_of :action
|
492
|
+
}.should raise_error(ArgumentError)
|
493
|
+
end
|
494
|
+
|
495
|
+
should "work with validates_inclusion_of macro" do
|
496
|
+
@embedded_doc.key :action, String
|
497
|
+
@embedded_doc.validates_inclusion_of :action, :within => %w(kick run)
|
498
|
+
|
499
|
+
doc = @embedded_doc.new
|
500
|
+
doc.should have_error_on(:action, 'is not in the list')
|
501
|
+
|
502
|
+
doc.action = 'fart'
|
503
|
+
doc.should have_error_on(:action, 'is not in the list')
|
504
|
+
|
505
|
+
doc.action = 'kick'
|
506
|
+
doc.should_not have_error_on(:action)
|
507
|
+
end
|
508
|
+
|
509
|
+
should "work with :in shortcut on key definition" do
|
510
|
+
@embedded_doc.key :action, String, :in => %w(kick run)
|
511
|
+
|
512
|
+
doc = @embedded_doc.new
|
513
|
+
doc.should have_error_on(:action, 'is not in the list')
|
514
|
+
|
515
|
+
doc.action = 'fart'
|
516
|
+
doc.should have_error_on(:action, 'is not in the list')
|
517
|
+
|
518
|
+
doc.action = 'kick'
|
519
|
+
doc.should_not have_error_on(:action)
|
520
|
+
end
|
521
|
+
|
522
|
+
should "not have error if allow nil is true and value is nil" do
|
523
|
+
@embedded_doc.key :action, String
|
524
|
+
@embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_nil => true
|
525
|
+
|
526
|
+
doc = @embedded_doc.new
|
527
|
+
doc.should_not have_error_on(:action)
|
528
|
+
end
|
529
|
+
|
530
|
+
should "not have error if allow blank is true and value is blank" do
|
531
|
+
@embedded_doc.key :action, String
|
532
|
+
@embedded_doc.validates_inclusion_of :action, :within => %w(kick run), :allow_blank => true
|
533
|
+
|
534
|
+
doc = @embedded_doc.new(:action => '')
|
535
|
+
doc.should_not have_error_on(:action)
|
536
|
+
end
|
537
|
+
end
|
538
|
+
|
539
|
+
end # End on an EmbeddedDocument
|
540
|
+
|
541
|
+
end # Validations
|
542
|
+
|
543
|
+
context "Adding validation errors" do
|
544
|
+
setup do
|
545
|
+
@document = Doc do
|
546
|
+
key :action, String
|
547
|
+
def action_present
|
548
|
+
errors.add(:action, 'is invalid') if action.blank?
|
549
|
+
end
|
550
|
+
end
|
551
|
+
end
|
552
|
+
|
553
|
+
should "work with validate callback" do
|
554
|
+
@document.validate :action_present
|
555
|
+
|
556
|
+
doc = @document.new
|
557
|
+
doc.action = nil
|
558
|
+
doc.should have_error_on(:action)
|
559
|
+
|
560
|
+
doc.action = 'kick'
|
561
|
+
doc.should_not have_error_on(:action)
|
562
|
+
end
|
563
|
+
end
|
564
|
+
end
|