cassandra_object 0.6.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cassandra_object/associations/one_to_many.rb +136 -0
- data/lib/cassandra_object/associations/one_to_one.rb +77 -0
- data/lib/cassandra_object/associations.rb +35 -0
- data/lib/cassandra_object/attributes.rb +93 -0
- data/lib/cassandra_object/base.rb +104 -0
- data/lib/cassandra_object/callbacks.rb +10 -0
- data/lib/cassandra_object/collection.rb +8 -0
- data/lib/cassandra_object/cursor.rb +86 -0
- data/lib/cassandra_object/dirty.rb +27 -0
- data/lib/cassandra_object/identity/abstract_key_factory.rb +36 -0
- data/lib/cassandra_object/identity/key.rb +20 -0
- data/lib/cassandra_object/identity/natural_key_factory.rb +51 -0
- data/lib/cassandra_object/identity/uuid_key_factory.rb +37 -0
- data/lib/cassandra_object/identity.rb +61 -0
- data/lib/cassandra_object/indexes.rb +129 -0
- data/lib/cassandra_object/legacy_callbacks.rb +33 -0
- data/lib/cassandra_object/migrations.rb +72 -0
- data/lib/cassandra_object/mocking.rb +15 -0
- data/lib/cassandra_object/persistence.rb +193 -0
- data/lib/cassandra_object/serialization.rb +6 -0
- data/lib/cassandra_object/type_registration.rb +7 -0
- data/lib/cassandra_object/types.rb +128 -0
- data/lib/cassandra_object/validation.rb +58 -0
- data/lib/cassandra_object.rb +30 -0
- data/vendor/active_support_shims.rb +4 -0
- data/vendor/activemodel/CHANGELOG +13 -0
- data/vendor/activemodel/CHANGES +12 -0
- data/vendor/activemodel/MIT-LICENSE +21 -0
- data/vendor/activemodel/README +21 -0
- data/vendor/activemodel/Rakefile +52 -0
- data/vendor/activemodel/activemodel.gemspec +19 -0
- data/vendor/activemodel/examples/validations.rb +29 -0
- data/vendor/activemodel/lib/active_model/attribute_methods.rb +291 -0
- data/vendor/activemodel/lib/active_model/callbacks.rb +91 -0
- data/vendor/activemodel/lib/active_model/conversion.rb +8 -0
- data/vendor/activemodel/lib/active_model/deprecated_error_methods.rb +33 -0
- data/vendor/activemodel/lib/active_model/dirty.rb +126 -0
- data/vendor/activemodel/lib/active_model/errors.rb +162 -0
- data/vendor/activemodel/lib/active_model/lint.rb +91 -0
- data/vendor/activemodel/lib/active_model/locale/en.yml +27 -0
- data/vendor/activemodel/lib/active_model/naming.rb +45 -0
- data/vendor/activemodel/lib/active_model/observing.rb +191 -0
- data/vendor/activemodel/lib/active_model/railtie.rb +2 -0
- data/vendor/activemodel/lib/active_model/serialization.rb +30 -0
- data/vendor/activemodel/lib/active_model/serializers/json.rb +96 -0
- data/vendor/activemodel/lib/active_model/serializers/xml.rb +204 -0
- data/vendor/activemodel/lib/active_model/state_machine/event.rb +62 -0
- data/vendor/activemodel/lib/active_model/state_machine/machine.rb +75 -0
- data/vendor/activemodel/lib/active_model/state_machine/state.rb +47 -0
- data/vendor/activemodel/lib/active_model/state_machine/state_transition.rb +40 -0
- data/vendor/activemodel/lib/active_model/state_machine.rb +70 -0
- data/vendor/activemodel/lib/active_model/test_case.rb +18 -0
- data/vendor/activemodel/lib/active_model/translation.rb +44 -0
- data/vendor/activemodel/lib/active_model/validations/acceptance.rb +55 -0
- data/vendor/activemodel/lib/active_model/validations/confirmation.rb +47 -0
- data/vendor/activemodel/lib/active_model/validations/exclusion.rb +42 -0
- data/vendor/activemodel/lib/active_model/validations/format.rb +64 -0
- data/vendor/activemodel/lib/active_model/validations/inclusion.rb +42 -0
- data/vendor/activemodel/lib/active_model/validations/length.rb +117 -0
- data/vendor/activemodel/lib/active_model/validations/numericality.rb +111 -0
- data/vendor/activemodel/lib/active_model/validations/presence.rb +42 -0
- data/vendor/activemodel/lib/active_model/validations/with.rb +59 -0
- data/vendor/activemodel/lib/active_model/validations.rb +120 -0
- data/vendor/activemodel/lib/active_model/validator.rb +110 -0
- data/vendor/activemodel/lib/active_model/version.rb +9 -0
- data/vendor/activemodel/lib/active_model.rb +61 -0
- data/vendor/activemodel/test/cases/attribute_methods_test.rb +46 -0
- data/vendor/activemodel/test/cases/callbacks_test.rb +70 -0
- data/vendor/activemodel/test/cases/helper.rb +23 -0
- data/vendor/activemodel/test/cases/lint_test.rb +28 -0
- data/vendor/activemodel/test/cases/naming_test.rb +28 -0
- data/vendor/activemodel/test/cases/observing_test.rb +133 -0
- data/vendor/activemodel/test/cases/serializeration/json_serialization_test.rb +83 -0
- data/vendor/activemodel/test/cases/serializeration/xml_serialization_test.rb +110 -0
- data/vendor/activemodel/test/cases/state_machine/event_test.rb +49 -0
- data/vendor/activemodel/test/cases/state_machine/machine_test.rb +43 -0
- data/vendor/activemodel/test/cases/state_machine/state_test.rb +72 -0
- data/vendor/activemodel/test/cases/state_machine/state_transition_test.rb +84 -0
- data/vendor/activemodel/test/cases/state_machine_test.rb +312 -0
- data/vendor/activemodel/test/cases/tests_database.rb +37 -0
- data/vendor/activemodel/test/cases/translation_test.rb +45 -0
- data/vendor/activemodel/test/cases/validations/acceptance_validation_test.rb +71 -0
- data/vendor/activemodel/test/cases/validations/conditional_validation_test.rb +141 -0
- data/vendor/activemodel/test/cases/validations/confirmation_validation_test.rb +58 -0
- data/vendor/activemodel/test/cases/validations/exclusion_validation_test.rb +47 -0
- data/vendor/activemodel/test/cases/validations/format_validation_test.rb +118 -0
- data/vendor/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +175 -0
- data/vendor/activemodel/test/cases/validations/i18n_validation_test.rb +527 -0
- data/vendor/activemodel/test/cases/validations/inclusion_validation_test.rb +71 -0
- data/vendor/activemodel/test/cases/validations/length_validation_test.rb +437 -0
- data/vendor/activemodel/test/cases/validations/numericality_validation_test.rb +180 -0
- data/vendor/activemodel/test/cases/validations/presence_validation_test.rb +70 -0
- data/vendor/activemodel/test/cases/validations/with_validation_test.rb +166 -0
- data/vendor/activemodel/test/cases/validations_test.rb +215 -0
- data/vendor/activemodel/test/config.rb +3 -0
- data/vendor/activemodel/test/fixtures/topics.yml +41 -0
- data/vendor/activemodel/test/models/contact.rb +7 -0
- data/vendor/activemodel/test/models/custom_reader.rb +17 -0
- data/vendor/activemodel/test/models/developer.rb +6 -0
- data/vendor/activemodel/test/models/person.rb +9 -0
- data/vendor/activemodel/test/models/reply.rb +34 -0
- data/vendor/activemodel/test/models/topic.rb +9 -0
- data/vendor/activemodel/test/models/track_back.rb +4 -0
- data/vendor/activemodel/test/schema.rb +14 -0
- data/vendor/activesupport/lib/active_support/autoload.rb +48 -0
- data/vendor/activesupport/lib/active_support/concern.rb +25 -0
- data/vendor/activesupport/lib/active_support/core_ext/array/wrap.rb +20 -0
- data/vendor/activesupport/lib/active_support/core_ext/object/blank.rb +58 -0
- data/vendor/activesupport/lib/active_support/core_ext/object/tap.rb +6 -0
- data/vendor/activesupport/lib/active_support/dependency_module.rb +17 -0
- data/vendor/activesupport/lib/active_support/i18n.rb +2 -0
- data/vendor/activesupport/lib/active_support/locale/en.yml +33 -0
- metadata +230 -0
@@ -0,0 +1,437 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'cases/helper'
|
3
|
+
require 'cases/tests_database'
|
4
|
+
|
5
|
+
require 'models/topic'
|
6
|
+
require 'models/developer'
|
7
|
+
require 'models/person'
|
8
|
+
|
9
|
+
class LengthValidationTest < ActiveModel::TestCase
|
10
|
+
include ActiveModel::TestsDatabase
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
Topic.reset_callbacks(:validate)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_validates_length_of_with_allow_nil
|
17
|
+
Topic.validates_length_of( :title, :is => 5, :allow_nil=>true )
|
18
|
+
|
19
|
+
assert !Topic.create("title" => "ab").valid?
|
20
|
+
assert !Topic.create("title" => "").valid?
|
21
|
+
assert Topic.create("title" => nil).valid?
|
22
|
+
assert Topic.create("title" => "abcde").valid?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_validates_length_of_with_allow_blank
|
26
|
+
Topic.validates_length_of( :title, :is => 5, :allow_blank=>true )
|
27
|
+
|
28
|
+
assert !Topic.create("title" => "ab").valid?
|
29
|
+
assert Topic.create("title" => "").valid?
|
30
|
+
assert Topic.create("title" => nil).valid?
|
31
|
+
assert Topic.create("title" => "abcde").valid?
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_validates_length_of_using_minimum
|
35
|
+
Topic.validates_length_of :title, :minimum => 5
|
36
|
+
|
37
|
+
t = Topic.create("title" => "valid", "content" => "whatever")
|
38
|
+
assert t.valid?
|
39
|
+
|
40
|
+
t.title = "not"
|
41
|
+
assert !t.valid?
|
42
|
+
assert t.errors[:title].any?
|
43
|
+
assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]
|
44
|
+
|
45
|
+
t.title = ""
|
46
|
+
assert !t.valid?
|
47
|
+
assert t.errors[:title].any?
|
48
|
+
assert_equal ["is too short (minimum is 5 characters)"], t.errors[:title]
|
49
|
+
|
50
|
+
t.title = nil
|
51
|
+
assert !t.valid?
|
52
|
+
assert t.errors[:title].any?
|
53
|
+
assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_validates_length_of_using_maximum_should_allow_nil
|
57
|
+
Topic.validates_length_of :title, :maximum => 10
|
58
|
+
t = Topic.create
|
59
|
+
assert t.valid?
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_optionally_validates_length_of_using_minimum
|
63
|
+
Topic.validates_length_of :title, :minimum => 5, :allow_nil => true
|
64
|
+
|
65
|
+
t = Topic.create("title" => "valid", "content" => "whatever")
|
66
|
+
assert t.valid?
|
67
|
+
|
68
|
+
t.title = nil
|
69
|
+
assert t.valid?
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_validates_length_of_using_maximum
|
73
|
+
Topic.validates_length_of :title, :maximum => 5
|
74
|
+
|
75
|
+
t = Topic.create("title" => "valid", "content" => "whatever")
|
76
|
+
assert t.valid?
|
77
|
+
|
78
|
+
t.title = "notvalid"
|
79
|
+
assert !t.valid?
|
80
|
+
assert t.errors[:title].any?
|
81
|
+
assert_equal ["is too long (maximum is 5 characters)"], t.errors[:title]
|
82
|
+
|
83
|
+
t.title = ""
|
84
|
+
assert t.valid?
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_optionally_validates_length_of_using_maximum
|
88
|
+
Topic.validates_length_of :title, :maximum => 5, :allow_nil => true
|
89
|
+
|
90
|
+
t = Topic.create("title" => "valid", "content" => "whatever")
|
91
|
+
assert t.valid?
|
92
|
+
|
93
|
+
t.title = nil
|
94
|
+
assert t.valid?
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_validates_length_of_using_within
|
98
|
+
Topic.validates_length_of(:title, :content, :within => 3..5)
|
99
|
+
|
100
|
+
t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long")
|
101
|
+
assert !t.valid?
|
102
|
+
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
|
103
|
+
assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]
|
104
|
+
|
105
|
+
t.title = nil
|
106
|
+
t.content = nil
|
107
|
+
assert !t.valid?
|
108
|
+
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
|
109
|
+
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:content]
|
110
|
+
|
111
|
+
t.title = "abe"
|
112
|
+
t.content = "mad"
|
113
|
+
assert t.valid?
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_validates_length_of_using_within_with_exclusive_range
|
117
|
+
Topic.validates_length_of(:title, :within => 4...10)
|
118
|
+
|
119
|
+
t = Topic.new("title" => "9 chars!!")
|
120
|
+
assert t.valid?
|
121
|
+
|
122
|
+
t.title = "Now I'm 10"
|
123
|
+
assert !t.valid?
|
124
|
+
assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title]
|
125
|
+
|
126
|
+
t.title = "Four"
|
127
|
+
assert t.valid?
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_optionally_validates_length_of_using_within
|
131
|
+
Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true
|
132
|
+
|
133
|
+
t = Topic.create('title' => 'abc', 'content' => 'abcd')
|
134
|
+
assert t.valid?
|
135
|
+
|
136
|
+
t.title = nil
|
137
|
+
assert t.valid?
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_optionally_validates_length_of_using_within_on_create
|
141
|
+
Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "my string is too long: {{count}}"
|
142
|
+
|
143
|
+
t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
|
144
|
+
assert !t.save
|
145
|
+
assert t.errors[:title].any?
|
146
|
+
assert_equal ["my string is too long: 10"], t.errors[:title]
|
147
|
+
|
148
|
+
t.title = "butthisis"
|
149
|
+
assert t.save
|
150
|
+
|
151
|
+
t.title = "few"
|
152
|
+
assert t.save
|
153
|
+
|
154
|
+
t.content = "andthisislong"
|
155
|
+
assert t.save
|
156
|
+
|
157
|
+
t.content = t.title = "iamfine"
|
158
|
+
assert t.save
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_optionally_validates_length_of_using_within_on_update
|
162
|
+
Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "my string is too short: {{count}}"
|
163
|
+
|
164
|
+
t = Topic.create("title" => "vali", "content" => "whatever")
|
165
|
+
assert !t.save
|
166
|
+
assert t.errors[:title].any?
|
167
|
+
|
168
|
+
t.title = "not"
|
169
|
+
assert !t.save
|
170
|
+
assert t.errors[:title].any?
|
171
|
+
assert_equal ["my string is too short: 5"], t.errors[:title]
|
172
|
+
|
173
|
+
t.title = "valid"
|
174
|
+
t.content = "andthisistoolong"
|
175
|
+
assert !t.save
|
176
|
+
assert t.errors[:content].any?
|
177
|
+
|
178
|
+
t.content = "iamfine"
|
179
|
+
assert t.save
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_validates_length_of_using_is
|
183
|
+
Topic.validates_length_of :title, :is => 5
|
184
|
+
|
185
|
+
t = Topic.create("title" => "valid", "content" => "whatever")
|
186
|
+
assert t.valid?
|
187
|
+
|
188
|
+
t.title = "notvalid"
|
189
|
+
assert !t.valid?
|
190
|
+
assert t.errors[:title].any?
|
191
|
+
assert_equal ["is the wrong length (should be 5 characters)"], t.errors[:title]
|
192
|
+
|
193
|
+
t.title = ""
|
194
|
+
assert !t.valid?
|
195
|
+
|
196
|
+
t.title = nil
|
197
|
+
assert !t.valid?
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_optionally_validates_length_of_using_is
|
201
|
+
Topic.validates_length_of :title, :is => 5, :allow_nil => true
|
202
|
+
|
203
|
+
t = Topic.create("title" => "valid", "content" => "whatever")
|
204
|
+
assert t.valid?
|
205
|
+
|
206
|
+
t.title = nil
|
207
|
+
assert t.valid?
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_validates_length_of_using_bignum
|
211
|
+
bigmin = 2 ** 30
|
212
|
+
bigmax = 2 ** 32
|
213
|
+
bigrange = bigmin...bigmax
|
214
|
+
assert_nothing_raised do
|
215
|
+
Topic.validates_length_of :title, :is => bigmin + 5
|
216
|
+
Topic.validates_length_of :title, :within => bigrange
|
217
|
+
Topic.validates_length_of :title, :in => bigrange
|
218
|
+
Topic.validates_length_of :title, :minimum => bigmin
|
219
|
+
Topic.validates_length_of :title, :maximum => bigmax
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_validates_length_of_nasty_params
|
224
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) }
|
225
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) }
|
226
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :minimum=>9) }
|
227
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :is=>9) }
|
228
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>"a") }
|
229
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>"a") }
|
230
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a") }
|
231
|
+
assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>"a") }
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_validates_length_of_custom_errors_for_minimum_with_message
|
235
|
+
Topic.validates_length_of( :title, :minimum=>5, :message=>"boo {{count}}" )
|
236
|
+
t = Topic.create("title" => "uhoh", "content" => "whatever")
|
237
|
+
assert !t.valid?
|
238
|
+
assert t.errors[:title].any?
|
239
|
+
assert_equal ["boo 5"], t.errors[:title]
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_validates_length_of_custom_errors_for_minimum_with_too_short
|
243
|
+
Topic.validates_length_of( :title, :minimum=>5, :too_short=>"hoo {{count}}" )
|
244
|
+
t = Topic.create("title" => "uhoh", "content" => "whatever")
|
245
|
+
assert !t.valid?
|
246
|
+
assert t.errors[:title].any?
|
247
|
+
assert_equal ["hoo 5"], t.errors[:title]
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_validates_length_of_custom_errors_for_maximum_with_message
|
251
|
+
Topic.validates_length_of( :title, :maximum=>5, :message=>"boo {{count}}" )
|
252
|
+
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
|
253
|
+
assert !t.valid?
|
254
|
+
assert t.errors[:title].any?
|
255
|
+
assert_equal ["boo 5"], t.errors[:title]
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_validates_length_of_custom_errors_for_in
|
259
|
+
Topic.validates_length_of(:title, :in => 10..20, :message => "hoo {{count}}")
|
260
|
+
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
|
261
|
+
assert !t.valid?
|
262
|
+
assert t.errors[:title].any?
|
263
|
+
assert_equal ["hoo 10"], t.errors["title"]
|
264
|
+
|
265
|
+
t = Topic.create("title" => "uhohuhohuhohuhohuhohuhohuhohuhoh", "content" => "whatever")
|
266
|
+
assert !t.valid?
|
267
|
+
assert t.errors[:title].any?
|
268
|
+
assert_equal ["hoo 20"], t.errors["title"]
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_validates_length_of_custom_errors_for_maximum_with_too_long
|
272
|
+
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}" )
|
273
|
+
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
|
274
|
+
assert !t.valid?
|
275
|
+
assert t.errors[:title].any?
|
276
|
+
assert_equal ["hoo 5"], t.errors["title"]
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_validates_length_of_custom_errors_for_is_with_message
|
280
|
+
Topic.validates_length_of( :title, :is=>5, :message=>"boo {{count}}" )
|
281
|
+
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
|
282
|
+
assert !t.valid?
|
283
|
+
assert t.errors[:title].any?
|
284
|
+
assert_equal ["boo 5"], t.errors["title"]
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_validates_length_of_custom_errors_for_is_with_wrong_length
|
288
|
+
Topic.validates_length_of( :title, :is=>5, :wrong_length=>"hoo {{count}}" )
|
289
|
+
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
|
290
|
+
assert !t.valid?
|
291
|
+
assert t.errors[:title].any?
|
292
|
+
assert_equal ["hoo 5"], t.errors["title"]
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_validates_length_of_using_minimum_utf8
|
296
|
+
with_kcode('UTF8') do
|
297
|
+
Topic.validates_length_of :title, :minimum => 5
|
298
|
+
|
299
|
+
t = Topic.create("title" => "一二三四五", "content" => "whatever")
|
300
|
+
assert t.valid?
|
301
|
+
|
302
|
+
t.title = "一二三四"
|
303
|
+
assert !t.valid?
|
304
|
+
assert t.errors[:title].any?
|
305
|
+
assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"]
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_validates_length_of_using_maximum_utf8
|
310
|
+
with_kcode('UTF8') do
|
311
|
+
Topic.validates_length_of :title, :maximum => 5
|
312
|
+
|
313
|
+
t = Topic.create("title" => "一二三四五", "content" => "whatever")
|
314
|
+
assert t.valid?
|
315
|
+
|
316
|
+
t.title = "一二34五六"
|
317
|
+
assert !t.valid?
|
318
|
+
assert t.errors[:title].any?
|
319
|
+
assert_equal ["is too long (maximum is 5 characters)"], t.errors["title"]
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_validates_length_of_using_within_utf8
|
324
|
+
with_kcode('UTF8') do
|
325
|
+
Topic.validates_length_of(:title, :content, :within => 3..5)
|
326
|
+
|
327
|
+
t = Topic.new("title" => "一二", "content" => "12三四五六七")
|
328
|
+
assert !t.valid?
|
329
|
+
assert_equal ["is too short (minimum is 3 characters)"], t.errors[:title]
|
330
|
+
assert_equal ["is too long (maximum is 5 characters)"], t.errors[:content]
|
331
|
+
t.title = "一二三"
|
332
|
+
t.content = "12三"
|
333
|
+
assert t.valid?
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
def test_optionally_validates_length_of_using_within_utf8
|
338
|
+
with_kcode('UTF8') do
|
339
|
+
Topic.validates_length_of :title, :within => 3..5, :allow_nil => true
|
340
|
+
|
341
|
+
t = Topic.create(:title => "一二三四五")
|
342
|
+
assert t.valid?, t.errors.inspect
|
343
|
+
|
344
|
+
t = Topic.create(:title => "一二三")
|
345
|
+
assert t.valid?, t.errors.inspect
|
346
|
+
|
347
|
+
t.title = nil
|
348
|
+
assert t.valid?, t.errors.inspect
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_optionally_validates_length_of_using_within_on_create_utf8
|
353
|
+
with_kcode('UTF8') do
|
354
|
+
Topic.validates_length_of :title, :within => 5..10, :on => :create, :too_long => "長すぎます: {{count}}"
|
355
|
+
|
356
|
+
t = Topic.create("title" => "一二三四五六七八九十A", "content" => "whatever")
|
357
|
+
assert !t.save
|
358
|
+
assert t.errors[:title].any?
|
359
|
+
assert_equal "長すぎます: 10", t.errors[:title].first
|
360
|
+
|
361
|
+
t.title = "一二三四五六七八九"
|
362
|
+
assert t.save
|
363
|
+
|
364
|
+
t.title = "一二3"
|
365
|
+
assert t.save
|
366
|
+
|
367
|
+
t.content = "一二三四五六七八九十"
|
368
|
+
assert t.save
|
369
|
+
|
370
|
+
t.content = t.title = "一二三四五六"
|
371
|
+
assert t.save
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_optionally_validates_length_of_using_within_on_update_utf8
|
376
|
+
with_kcode('UTF8') do
|
377
|
+
Topic.validates_length_of :title, :within => 5..10, :on => :update, :too_short => "短すぎます: {{count}}"
|
378
|
+
|
379
|
+
t = Topic.create("title" => "一二三4", "content" => "whatever")
|
380
|
+
assert !t.save
|
381
|
+
assert t.errors[:title].any?
|
382
|
+
|
383
|
+
t.title = "1二三4"
|
384
|
+
assert !t.save
|
385
|
+
assert t.errors[:title].any?
|
386
|
+
assert_equal ["短すぎます: 5"], t.errors[:title]
|
387
|
+
|
388
|
+
t.title = "一二三四五六七八九十A"
|
389
|
+
assert !t.save
|
390
|
+
assert t.errors[:title].any?
|
391
|
+
|
392
|
+
t.title = "一二345"
|
393
|
+
assert t.save
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_validates_length_of_using_is_utf8
|
398
|
+
with_kcode('UTF8') do
|
399
|
+
Topic.validates_length_of :title, :is => 5
|
400
|
+
|
401
|
+
t = Topic.create("title" => "一二345", "content" => "whatever")
|
402
|
+
assert t.valid?
|
403
|
+
|
404
|
+
t.title = "一二345六"
|
405
|
+
assert !t.valid?
|
406
|
+
assert t.errors[:title].any?
|
407
|
+
assert_equal ["is the wrong length (should be 5 characters)"], t.errors["title"]
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_validates_length_of_with_block
|
412
|
+
Topic.validates_length_of :content, :minimum => 5, :too_short=>"Your essay must be at least {{count}} words.",
|
413
|
+
:tokenizer => lambda {|str| str.scan(/\w+/) }
|
414
|
+
t = Topic.create!(:content => "this content should be long enough")
|
415
|
+
assert t.valid?
|
416
|
+
|
417
|
+
t.content = "not long enough"
|
418
|
+
assert !t.valid?
|
419
|
+
assert t.errors[:content].any?
|
420
|
+
assert_equal ["Your essay must be at least 5 words."], t.errors[:content]
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_validates_length_of_for_ruby_class
|
424
|
+
Person.validates_length_of :karma, :minimum => 5
|
425
|
+
|
426
|
+
p = Person.new
|
427
|
+
p.karma = "Pix"
|
428
|
+
assert p.invalid?
|
429
|
+
|
430
|
+
assert_equal ["is too short (minimum is 5 characters)"], p.errors[:karma]
|
431
|
+
|
432
|
+
p.karma = "The Smiths"
|
433
|
+
assert p.valid?
|
434
|
+
ensure
|
435
|
+
Person.reset_callbacks(:validate)
|
436
|
+
end
|
437
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'cases/helper'
|
3
|
+
require 'cases/tests_database'
|
4
|
+
|
5
|
+
require 'models/topic'
|
6
|
+
require 'models/developer'
|
7
|
+
require 'models/person'
|
8
|
+
|
9
|
+
class NumericalityValidationTest < ActiveModel::TestCase
|
10
|
+
include ActiveModel::TestsDatabase
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
Topic.reset_callbacks(:validate)
|
14
|
+
end
|
15
|
+
|
16
|
+
NIL = [nil]
|
17
|
+
BLANK = ["", " ", " \t \r \n"]
|
18
|
+
BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significent digits
|
19
|
+
FLOAT_STRINGS = %w(0.0 +0.0 -0.0 10.0 10.5 -10.5 -0.0001 -090.1 90.1e1 -90.1e5 -90.1e-5 90e-5)
|
20
|
+
INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090)
|
21
|
+
FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
|
22
|
+
INTEGERS = [0, 10, -10] + INTEGER_STRINGS
|
23
|
+
BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal.new(bd) }
|
24
|
+
JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
|
25
|
+
INFINITY = [1.0/0.0]
|
26
|
+
|
27
|
+
def test_default_validates_numericality_of
|
28
|
+
Topic.validates_numericality_of :approved
|
29
|
+
invalid!(NIL + BLANK + JUNK)
|
30
|
+
valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_validates_numericality_of_with_nil_allowed
|
34
|
+
Topic.validates_numericality_of :approved, :allow_nil => true
|
35
|
+
|
36
|
+
invalid!(JUNK)
|
37
|
+
valid!(NIL + BLANK + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_validates_numericality_of_with_integer_only
|
41
|
+
Topic.validates_numericality_of :approved, :only_integer => true
|
42
|
+
|
43
|
+
invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
|
44
|
+
valid!(INTEGERS)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_validates_numericality_of_with_integer_only_and_nil_allowed
|
48
|
+
Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true
|
49
|
+
|
50
|
+
invalid!(JUNK + FLOATS + BIGDECIMAL + INFINITY)
|
51
|
+
valid!(NIL + BLANK + INTEGERS)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_validates_numericality_with_greater_than
|
55
|
+
Topic.validates_numericality_of :approved, :greater_than => 10
|
56
|
+
|
57
|
+
invalid!([-10, 10], 'must be greater than 10')
|
58
|
+
valid!([11])
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_validates_numericality_with_greater_than_or_equal
|
62
|
+
Topic.validates_numericality_of :approved, :greater_than_or_equal_to => 10
|
63
|
+
|
64
|
+
invalid!([-9, 9], 'must be greater than or equal to 10')
|
65
|
+
valid!([10])
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_validates_numericality_with_equal_to
|
69
|
+
Topic.validates_numericality_of :approved, :equal_to => 10
|
70
|
+
|
71
|
+
invalid!([-10, 11] + INFINITY, 'must be equal to 10')
|
72
|
+
valid!([10])
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_validates_numericality_with_less_than
|
76
|
+
Topic.validates_numericality_of :approved, :less_than => 10
|
77
|
+
|
78
|
+
invalid!([10], 'must be less than 10')
|
79
|
+
valid!([-9, 9])
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_validates_numericality_with_less_than_or_equal_to
|
83
|
+
Topic.validates_numericality_of :approved, :less_than_or_equal_to => 10
|
84
|
+
|
85
|
+
invalid!([11], 'must be less than or equal to 10')
|
86
|
+
valid!([-10, 10])
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_validates_numericality_with_odd
|
90
|
+
Topic.validates_numericality_of :approved, :odd => true
|
91
|
+
|
92
|
+
invalid!([-2, 2], 'must be odd')
|
93
|
+
valid!([-1, 1])
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_validates_numericality_with_even
|
97
|
+
Topic.validates_numericality_of :approved, :even => true
|
98
|
+
|
99
|
+
invalid!([-1, 1], 'must be even')
|
100
|
+
valid!([-2, 2])
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_validates_numericality_with_greater_than_less_than_and_even
|
104
|
+
Topic.validates_numericality_of :approved, :greater_than => 1, :less_than => 4, :even => true
|
105
|
+
|
106
|
+
invalid!([1, 3, 4])
|
107
|
+
valid!([2])
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_validates_numericality_with_proc
|
111
|
+
Topic.send(:define_method, :min_approved, lambda { 5 })
|
112
|
+
Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved }
|
113
|
+
|
114
|
+
invalid!([3, 4])
|
115
|
+
valid!([5, 6])
|
116
|
+
Topic.send(:remove_method, :min_approved)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_validates_numericality_with_symbol
|
120
|
+
Topic.send(:define_method, :max_approved, lambda { 5 })
|
121
|
+
Topic.validates_numericality_of :approved, :less_than_or_equal_to => :max_approved
|
122
|
+
|
123
|
+
invalid!([6])
|
124
|
+
valid!([4, 5])
|
125
|
+
Topic.send(:remove_method, :max_approved)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_validates_numericality_with_numeric_message
|
129
|
+
Topic.validates_numericality_of :approved, :less_than => 4, :message => "smaller than {{count}}"
|
130
|
+
topic = Topic.new("title" => "numeric test", "approved" => 10)
|
131
|
+
|
132
|
+
assert !topic.valid?
|
133
|
+
assert_equal ["smaller than 4"], topic.errors[:approved]
|
134
|
+
|
135
|
+
Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than {{count}}"
|
136
|
+
topic = Topic.new("title" => "numeric test", "approved" => 1)
|
137
|
+
|
138
|
+
assert !topic.valid?
|
139
|
+
assert_equal ["greater than 4"], topic.errors[:approved]
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_validates_numericality_of_for_ruby_class
|
143
|
+
Person.validates_numericality_of :karma, :allow_nil => false
|
144
|
+
|
145
|
+
p = Person.new
|
146
|
+
p.karma = "Pix"
|
147
|
+
assert p.invalid?
|
148
|
+
|
149
|
+
assert_equal ["is not a number"], p.errors[:karma]
|
150
|
+
|
151
|
+
p.karma = "1234"
|
152
|
+
assert p.valid?
|
153
|
+
ensure
|
154
|
+
Person.reset_callbacks(:validate)
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def invalid!(values, error = nil)
|
160
|
+
with_each_topic_approved_value(values) do |topic, value|
|
161
|
+
assert !topic.valid?, "#{value.inspect} not rejected as a number"
|
162
|
+
assert topic.errors[:approved].any?, "FAILED for #{value.inspect}"
|
163
|
+
assert_equal error, topic.errors[:approved].first if error
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def valid!(values)
|
168
|
+
with_each_topic_approved_value(values) do |topic, value|
|
169
|
+
assert topic.valid?, "#{value.inspect} not accepted as a number"
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def with_each_topic_approved_value(values)
|
174
|
+
topic = Topic.new(:title => "numeric test", :content => "whatever")
|
175
|
+
values.each do |value|
|
176
|
+
topic.approved = value
|
177
|
+
yield topic, value
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'cases/helper'
|
3
|
+
require 'cases/tests_database'
|
4
|
+
|
5
|
+
require 'models/topic'
|
6
|
+
require 'models/developer'
|
7
|
+
require 'models/person'
|
8
|
+
require 'models/custom_reader'
|
9
|
+
|
10
|
+
class PresenceValidationTest < ActiveModel::TestCase
|
11
|
+
include ActiveModel::TestsDatabase
|
12
|
+
|
13
|
+
def test_validate_presences
|
14
|
+
Topic.validates_presence_of(:title, :content)
|
15
|
+
|
16
|
+
t = Topic.create
|
17
|
+
assert !t.save
|
18
|
+
assert_equal ["can't be blank"], t.errors[:title]
|
19
|
+
assert_equal ["can't be blank"], t.errors[:content]
|
20
|
+
|
21
|
+
t.title = "something"
|
22
|
+
t.content = " "
|
23
|
+
|
24
|
+
assert !t.save
|
25
|
+
assert_equal ["can't be blank"], t.errors[:content]
|
26
|
+
|
27
|
+
t.content = "like stuff"
|
28
|
+
|
29
|
+
assert t.save
|
30
|
+
ensure
|
31
|
+
Topic.reset_callbacks(:validate)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_validates_acceptance_of_with_custom_error_using_quotes
|
35
|
+
Person.validates_presence_of :karma, :message=> "This string contains 'single' and \"double\" quotes"
|
36
|
+
p = Person.new
|
37
|
+
assert !p.valid?
|
38
|
+
assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
|
39
|
+
ensure
|
40
|
+
Person.reset_callbacks(:validate)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_validates_presence_of_for_ruby_class
|
44
|
+
Person.validates_presence_of :karma
|
45
|
+
|
46
|
+
p = Person.new
|
47
|
+
assert p.invalid?
|
48
|
+
|
49
|
+
assert_equal ["can't be blank"], p.errors[:karma]
|
50
|
+
|
51
|
+
p.karma = "Cold"
|
52
|
+
assert p.valid?
|
53
|
+
ensure
|
54
|
+
Person.reset_callbacks(:validate)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_validates_presence_of_for_ruby_class_with_custom_reader
|
58
|
+
CustomReader.validates_presence_of :karma
|
59
|
+
|
60
|
+
p = CustomReader.new
|
61
|
+
assert p.invalid?
|
62
|
+
|
63
|
+
assert_equal ["can't be blank"], p.errors[:karma]
|
64
|
+
|
65
|
+
p[:karma] = "Cold"
|
66
|
+
assert p.valid?
|
67
|
+
ensure
|
68
|
+
CustomReader.reset_callbacks(:validate)
|
69
|
+
end
|
70
|
+
end
|