mongomatic 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,340 @@
1
+ require 'helper'
2
+ require 'minitest/autorun'
3
+
4
+ class TestValidations < MiniTest::Unit::TestCase
5
+ def test_array_style_errors
6
+ f = Foobar.new
7
+ assert !f.valid?
8
+ assert_equal ["color must not be blank", "missing style"], f.errors.full_messages
9
+ f["color"] = "pink"; f.valid?
10
+ assert_equal ["missing style"], f.errors.full_messages
11
+ f["style"] = "awesome"; f.valid?
12
+ assert_equal [], f.errors.full_messages
13
+ end
14
+
15
+ def test_be_expect
16
+ p = Person.new
17
+ class << p
18
+ def validate
19
+ expectations do
20
+ be_expected self['alive'], "Alive must be true"
21
+ not_be_expected self['dead'], "Dead must be false"
22
+ end
23
+ end
24
+ end
25
+
26
+ assert !p.valid?
27
+ assert_equal ['Alive must be true'], p.errors.full_messages
28
+
29
+ p['alive'] = true
30
+ assert p.valid?
31
+
32
+ p['dead'] = true
33
+ assert !p.valid?
34
+ assert_equal ['Dead must be false'], p.errors.full_messages
35
+ end
36
+
37
+ def test_be_expected_with_block
38
+ p = Person.new
39
+ class << p
40
+ def validate
41
+ expectations do
42
+ be_expected lambda { self['name'].is_a? String }, "Name must be a string"
43
+ not_be_expected lambda { self['alive'] && self['dead'] }, "Cannot be alive and dead"
44
+ end
45
+ end
46
+ end
47
+
48
+ p['name'] = 1
49
+ assert !p.valid?
50
+ assert_equal p.errors.full_messages, ["Name must be a string"]
51
+
52
+ p['alive'] = true
53
+ p['dead'] = true
54
+ p['name'] = "Jordan"
55
+
56
+ assert !p.valid?
57
+ assert_equal p.errors.full_messages, ["Cannot be alive and dead"]
58
+
59
+ p['dead'] = false
60
+ assert p.valid?
61
+ end
62
+
63
+ def test_be_expected_with_method_call
64
+ p = Person.new
65
+ class << p
66
+ def validate
67
+ expectations do
68
+ be_expected :method_1, "Method 1 must return true"
69
+ not_be_expected :method_2, "Method 2 must return false"
70
+ end
71
+ end
72
+
73
+ def method_1
74
+ (self['name'] == 'Jordan') ? true : false
75
+ end
76
+
77
+ def method_2
78
+ (self['age'] == 21) ? false : true
79
+ end
80
+ end
81
+
82
+ assert !p.valid?
83
+ assert_equal ["Method 1 must return true", "Method 2 must return false"], p.errors.full_messages
84
+
85
+ p['name'] = 'Jordan'
86
+ p['age'] = 21
87
+
88
+ assert p.valid?
89
+ end
90
+
91
+ def test_be_present
92
+ p = Person.new
93
+ class << p
94
+ def validate
95
+ expectations do
96
+ be_present self['name'], 'name cannot be blank'
97
+ not_be_present self['age'], 'age must be blank'
98
+ end
99
+ end
100
+ end
101
+
102
+ assert !p.valid?
103
+ assert_equal ['name cannot be blank'], p.errors.full_messages
104
+
105
+ p['name'] = "Jordan"
106
+ p['age'] = 21
107
+
108
+
109
+ assert !p.valid?
110
+ assert_equal ['age must be blank'], p.errors.full_messages
111
+
112
+ p['age'] = nil
113
+
114
+ assert p.valid?
115
+
116
+ end
117
+
118
+ def test_be_a_number
119
+ p = Person.new
120
+ class << p
121
+ def validate
122
+ expectations do
123
+ be_a_number self['age'], 'Age is not a number'
124
+ not_be_a_number self['name'], 'Name cannot be a number'
125
+ be_a_number self['birth_year'], 'Birth year is not a number', :allow_nil => true
126
+ end
127
+ end
128
+ end
129
+
130
+ assert !p.valid?
131
+ assert_equal ["Age is not a number"], p.errors.full_messages
132
+
133
+ p['age'] = 21
134
+ p['name'] = 65
135
+
136
+ assert !p.valid?
137
+ assert_equal ["Name cannot be a number"], p.errors.full_messages
138
+
139
+ p['name'] = 'Jordan'
140
+
141
+ assert p.valid?
142
+ end
143
+
144
+ def test_be_match
145
+ p = Person.new
146
+ class << p
147
+ def validate
148
+ expectations do
149
+ be_match self['name'], "Name must start with uppercase letter", :with => /[A-Z][a-z]*/
150
+ not_be_match self['nickname'], "Nickname cannot start with uppercase letter", :with => /[A-Z][a-z]*/
151
+ be_match self['age'], "Age must only contain digits", :with => /\d+/, :allow_nil => true
152
+ end
153
+ end
154
+ end
155
+
156
+ assert !p.valid?
157
+ assert_equal ["Name must start with uppercase letter"], p.errors.full_messages
158
+
159
+ p['name'] = 'Jordan'
160
+ p['nickname'] = 'Jordan'
161
+
162
+ assert !p.valid?
163
+ assert_equal ["Nickname cannot start with uppercase letter"], p.errors.full_messages
164
+
165
+ p['nickname'] = 'jordan'
166
+
167
+ assert p.valid?
168
+
169
+ p['age'] = 'asd'
170
+
171
+ assert !p.valid?
172
+ assert_equal ["Age must only contain digits"], p.errors.full_messages
173
+
174
+ p['age'] = '21'
175
+
176
+ assert p.valid?
177
+
178
+ end
179
+
180
+ def test_be_of_length
181
+ p = Person.new
182
+ class << p
183
+ def validate
184
+ expectations do
185
+ be_of_length self['name'], "Name must be 3 characters long", :minimum => 3
186
+ be_of_length self['nickname'], "Nickname must not be longer than 5 characters", :maximum => 5
187
+ be_of_length self['computers'], "Can only specify between 1 and 3 computers", :range => 1..3
188
+ be_of_length self['status'], "Status must be a minimum of 1 character", :minumum => 1, :allow_nil => true
189
+ end
190
+ end
191
+ end
192
+
193
+ assert !p.valid?
194
+ assert_equal ["Name must be 3 characters long",
195
+ "Can only specify between 1 and 3 computers"], p.errors.full_messages
196
+
197
+ p['name'] = 'Jordan'
198
+ p['nickname'] = 'Jordan'
199
+
200
+ assert !p.valid?
201
+ assert_equal ["Nickname must not be longer than 5 characters",
202
+ "Can only specify between 1 and 3 computers"], p.errors.full_messages
203
+
204
+ p['nickname'] = 'abc'
205
+ p['computers'] = ['comp_a']
206
+
207
+ assert p.valid?
208
+ end
209
+
210
+ def test_be_reference
211
+ id = Person.new('name' => 'jordan').insert
212
+ p = Person.new
213
+ class << p
214
+ def validate
215
+ expectations do
216
+ be_reference self['friend'], 'friend must be an ObjectId'
217
+ end
218
+ end
219
+ end
220
+
221
+ assert !p.valid?
222
+ assert_equal ["friend must be an ObjectId"], p.errors.full_messages
223
+
224
+ p['friend'] = id
225
+
226
+ assert p.valid?
227
+ end
228
+
229
+ def test_expectations_must_be_in_helper_block
230
+ p = Person.new
231
+ class << p
232
+ def validate
233
+ be_present self['name'], ''
234
+ end
235
+ end
236
+
237
+ assert_raises NoMethodError do
238
+ p.valid?
239
+ end
240
+
241
+ class << p
242
+ def validate
243
+ expectations { }
244
+ be_present
245
+ end
246
+ end
247
+
248
+ assert_raises NameError do
249
+ p.valid?
250
+ end
251
+ end
252
+
253
+ def test_errors_on_two_part_error_messages
254
+ p = Person.new
255
+ class << p
256
+ def validate
257
+ expectations do
258
+ be_expected self['name'], ['name', 'cannot be empty']
259
+ be_of_length self['name'], ['name', 'must be at least 3 characters long'], :minimum => 3
260
+ be_a_number self['age'], ['age', 'must be a number']
261
+ end
262
+ end
263
+ end
264
+
265
+ p.valid?
266
+ assert_equal ['cannot be empty', 'must be at least 3 characters long'], p.errors.on(:name)
267
+ assert_equal ['must be a number'], p.errors.on('age')
268
+
269
+ p['name'] = 'Jo'
270
+ p['age'] = 21
271
+
272
+ p.valid?
273
+ assert_equal ['must be at least 3 characters long'], p.errors.on('name')
274
+ assert_equal [], p.errors.on(:age)
275
+
276
+ p['name'] = 'Jordan'
277
+
278
+ p.valid?
279
+ assert_equal [], p.errors.on(:name)
280
+ end
281
+
282
+ def test_errors_on_one_part_error_message
283
+ p = Person.new
284
+ class << p
285
+ def validate
286
+ expectations do
287
+ be_expected self['name'], ['name', 'cannot be empty' ]
288
+ be_of_length self['name'], ['name', 'must be at least 3 characters long'], :minimum => 3
289
+ be_a_number self['age'], ['age','must be a number']
290
+ end
291
+ end
292
+ end
293
+
294
+ p.valid?
295
+ assert_equal ['cannot be empty', 'must be at least 3 characters long'], p.errors.on(:name)
296
+ assert_equal ['must be a number'], p.errors.on('age')
297
+
298
+ p['name'] = 'Jo'
299
+ p['age'] = 21
300
+
301
+ p.valid?
302
+ assert_equal ['must be at least 3 characters long'], p.errors.on('name')
303
+ assert_equal [], p.errors.on(:age)
304
+
305
+ p['name'] = 'Jordan'
306
+
307
+ p.valid?
308
+ assert_equal [], p.errors.on(:name)
309
+ end
310
+
311
+ def test_errors_on_case_insensitive
312
+ p = Person.new
313
+ class << p
314
+ def validate
315
+ expectations do
316
+ be_expected self['name'], ['name', 'cannot be empty']
317
+ be_expected self['age'], ['age','cannot be empty']
318
+ end
319
+ end
320
+ end
321
+
322
+ p.valid?
323
+ assert_equal ['cannot be empty'], p.errors.on('name')
324
+ assert_equal ['cannot be empty'], p.errors.on(:age)
325
+ end
326
+
327
+ def test_errors_on_multi_word_fields
328
+ p = Person.new
329
+ class << p
330
+ def validate
331
+ expectations do
332
+ be_expected self['hair_color'], ['hair_color', 'must exist']
333
+ end
334
+ end
335
+ end
336
+
337
+ p.valid?
338
+ assert_equal ['must exist'], p.errors.on(:hair_color)
339
+ end
340
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
- - 2
9
- version: 0.6.2
8
+ - 3
9
+ version: 0.6.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -14,22 +14,21 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-11 00:00:00 -06:00
17
+ date: 2010-11-12 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: shoulda
21
+ name: minitest
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 2
30
- - 11
31
- - 1
32
- version: 2.11.1
30
+ - 0
31
+ version: "2.0"
33
32
  type: :development
34
33
  version_requirements: *id001
35
34
  - !ruby/object:Gem::Dependency
@@ -105,8 +104,11 @@ files:
105
104
  - README.rdoc
106
105
  - test/helper.rb
107
106
  - test/test_exceptions.rb
107
+ - test/test_find.rb
108
+ - test/test_misc.rb
108
109
  - test/test_modifiers.rb
109
- - test/test_mongomatic.rb
110
+ - test/test_persistence.rb
111
+ - test/test_validations.rb
110
112
  has_rdoc: true
111
113
  homepage: http://mongomatic.com/
112
114
  licenses: []
@@ -142,5 +144,8 @@ summary: Mongomatic is a simple Ruby object mapper for Mongo
142
144
  test_files:
143
145
  - test/helper.rb
144
146
  - test/test_exceptions.rb
147
+ - test/test_find.rb
148
+ - test/test_misc.rb
145
149
  - test/test_modifiers.rb
146
- - test/test_mongomatic.rb
150
+ - test/test_persistence.rb
151
+ - test/test_validations.rb