cassandra_object 0.6.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/lib/cassandra_object/associations/one_to_many.rb +136 -0
  2. data/lib/cassandra_object/associations/one_to_one.rb +77 -0
  3. data/lib/cassandra_object/associations.rb +35 -0
  4. data/lib/cassandra_object/attributes.rb +93 -0
  5. data/lib/cassandra_object/base.rb +104 -0
  6. data/lib/cassandra_object/callbacks.rb +10 -0
  7. data/lib/cassandra_object/collection.rb +8 -0
  8. data/lib/cassandra_object/cursor.rb +86 -0
  9. data/lib/cassandra_object/dirty.rb +27 -0
  10. data/lib/cassandra_object/identity/abstract_key_factory.rb +36 -0
  11. data/lib/cassandra_object/identity/key.rb +20 -0
  12. data/lib/cassandra_object/identity/natural_key_factory.rb +51 -0
  13. data/lib/cassandra_object/identity/uuid_key_factory.rb +37 -0
  14. data/lib/cassandra_object/identity.rb +61 -0
  15. data/lib/cassandra_object/indexes.rb +129 -0
  16. data/lib/cassandra_object/legacy_callbacks.rb +33 -0
  17. data/lib/cassandra_object/migrations.rb +72 -0
  18. data/lib/cassandra_object/mocking.rb +15 -0
  19. data/lib/cassandra_object/persistence.rb +193 -0
  20. data/lib/cassandra_object/serialization.rb +6 -0
  21. data/lib/cassandra_object/type_registration.rb +7 -0
  22. data/lib/cassandra_object/types.rb +128 -0
  23. data/lib/cassandra_object/validation.rb +58 -0
  24. data/lib/cassandra_object.rb +30 -0
  25. data/vendor/active_support_shims.rb +4 -0
  26. data/vendor/activemodel/CHANGELOG +13 -0
  27. data/vendor/activemodel/CHANGES +12 -0
  28. data/vendor/activemodel/MIT-LICENSE +21 -0
  29. data/vendor/activemodel/README +21 -0
  30. data/vendor/activemodel/Rakefile +52 -0
  31. data/vendor/activemodel/activemodel.gemspec +19 -0
  32. data/vendor/activemodel/examples/validations.rb +29 -0
  33. data/vendor/activemodel/lib/active_model/attribute_methods.rb +291 -0
  34. data/vendor/activemodel/lib/active_model/callbacks.rb +91 -0
  35. data/vendor/activemodel/lib/active_model/conversion.rb +8 -0
  36. data/vendor/activemodel/lib/active_model/deprecated_error_methods.rb +33 -0
  37. data/vendor/activemodel/lib/active_model/dirty.rb +126 -0
  38. data/vendor/activemodel/lib/active_model/errors.rb +162 -0
  39. data/vendor/activemodel/lib/active_model/lint.rb +91 -0
  40. data/vendor/activemodel/lib/active_model/locale/en.yml +27 -0
  41. data/vendor/activemodel/lib/active_model/naming.rb +45 -0
  42. data/vendor/activemodel/lib/active_model/observing.rb +191 -0
  43. data/vendor/activemodel/lib/active_model/railtie.rb +2 -0
  44. data/vendor/activemodel/lib/active_model/serialization.rb +30 -0
  45. data/vendor/activemodel/lib/active_model/serializers/json.rb +96 -0
  46. data/vendor/activemodel/lib/active_model/serializers/xml.rb +204 -0
  47. data/vendor/activemodel/lib/active_model/state_machine/event.rb +62 -0
  48. data/vendor/activemodel/lib/active_model/state_machine/machine.rb +75 -0
  49. data/vendor/activemodel/lib/active_model/state_machine/state.rb +47 -0
  50. data/vendor/activemodel/lib/active_model/state_machine/state_transition.rb +40 -0
  51. data/vendor/activemodel/lib/active_model/state_machine.rb +70 -0
  52. data/vendor/activemodel/lib/active_model/test_case.rb +18 -0
  53. data/vendor/activemodel/lib/active_model/translation.rb +44 -0
  54. data/vendor/activemodel/lib/active_model/validations/acceptance.rb +55 -0
  55. data/vendor/activemodel/lib/active_model/validations/confirmation.rb +47 -0
  56. data/vendor/activemodel/lib/active_model/validations/exclusion.rb +42 -0
  57. data/vendor/activemodel/lib/active_model/validations/format.rb +64 -0
  58. data/vendor/activemodel/lib/active_model/validations/inclusion.rb +42 -0
  59. data/vendor/activemodel/lib/active_model/validations/length.rb +117 -0
  60. data/vendor/activemodel/lib/active_model/validations/numericality.rb +111 -0
  61. data/vendor/activemodel/lib/active_model/validations/presence.rb +42 -0
  62. data/vendor/activemodel/lib/active_model/validations/with.rb +59 -0
  63. data/vendor/activemodel/lib/active_model/validations.rb +120 -0
  64. data/vendor/activemodel/lib/active_model/validator.rb +110 -0
  65. data/vendor/activemodel/lib/active_model/version.rb +9 -0
  66. data/vendor/activemodel/lib/active_model.rb +61 -0
  67. data/vendor/activemodel/test/cases/attribute_methods_test.rb +46 -0
  68. data/vendor/activemodel/test/cases/callbacks_test.rb +70 -0
  69. data/vendor/activemodel/test/cases/helper.rb +23 -0
  70. data/vendor/activemodel/test/cases/lint_test.rb +28 -0
  71. data/vendor/activemodel/test/cases/naming_test.rb +28 -0
  72. data/vendor/activemodel/test/cases/observing_test.rb +133 -0
  73. data/vendor/activemodel/test/cases/serializeration/json_serialization_test.rb +83 -0
  74. data/vendor/activemodel/test/cases/serializeration/xml_serialization_test.rb +110 -0
  75. data/vendor/activemodel/test/cases/state_machine/event_test.rb +49 -0
  76. data/vendor/activemodel/test/cases/state_machine/machine_test.rb +43 -0
  77. data/vendor/activemodel/test/cases/state_machine/state_test.rb +72 -0
  78. data/vendor/activemodel/test/cases/state_machine/state_transition_test.rb +84 -0
  79. data/vendor/activemodel/test/cases/state_machine_test.rb +312 -0
  80. data/vendor/activemodel/test/cases/tests_database.rb +37 -0
  81. data/vendor/activemodel/test/cases/translation_test.rb +45 -0
  82. data/vendor/activemodel/test/cases/validations/acceptance_validation_test.rb +71 -0
  83. data/vendor/activemodel/test/cases/validations/conditional_validation_test.rb +141 -0
  84. data/vendor/activemodel/test/cases/validations/confirmation_validation_test.rb +58 -0
  85. data/vendor/activemodel/test/cases/validations/exclusion_validation_test.rb +47 -0
  86. data/vendor/activemodel/test/cases/validations/format_validation_test.rb +118 -0
  87. data/vendor/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +175 -0
  88. data/vendor/activemodel/test/cases/validations/i18n_validation_test.rb +527 -0
  89. data/vendor/activemodel/test/cases/validations/inclusion_validation_test.rb +71 -0
  90. data/vendor/activemodel/test/cases/validations/length_validation_test.rb +437 -0
  91. data/vendor/activemodel/test/cases/validations/numericality_validation_test.rb +180 -0
  92. data/vendor/activemodel/test/cases/validations/presence_validation_test.rb +70 -0
  93. data/vendor/activemodel/test/cases/validations/with_validation_test.rb +166 -0
  94. data/vendor/activemodel/test/cases/validations_test.rb +215 -0
  95. data/vendor/activemodel/test/config.rb +3 -0
  96. data/vendor/activemodel/test/fixtures/topics.yml +41 -0
  97. data/vendor/activemodel/test/models/contact.rb +7 -0
  98. data/vendor/activemodel/test/models/custom_reader.rb +17 -0
  99. data/vendor/activemodel/test/models/developer.rb +6 -0
  100. data/vendor/activemodel/test/models/person.rb +9 -0
  101. data/vendor/activemodel/test/models/reply.rb +34 -0
  102. data/vendor/activemodel/test/models/topic.rb +9 -0
  103. data/vendor/activemodel/test/models/track_back.rb +4 -0
  104. data/vendor/activemodel/test/schema.rb +14 -0
  105. data/vendor/activesupport/lib/active_support/autoload.rb +48 -0
  106. data/vendor/activesupport/lib/active_support/concern.rb +25 -0
  107. data/vendor/activesupport/lib/active_support/core_ext/array/wrap.rb +20 -0
  108. data/vendor/activesupport/lib/active_support/core_ext/object/blank.rb +58 -0
  109. data/vendor/activesupport/lib/active_support/core_ext/object/tap.rb +6 -0
  110. data/vendor/activesupport/lib/active_support/dependency_module.rb +17 -0
  111. data/vendor/activesupport/lib/active_support/i18n.rb +2 -0
  112. data/vendor/activesupport/lib/active_support/locale/en.yml +33 -0
  113. metadata +230 -0
@@ -0,0 +1,527 @@
1
+ require "cases/helper"
2
+ require 'cases/tests_database'
3
+ require 'models/person'
4
+
5
+ class I18nValidationTest < ActiveModel::TestCase
6
+ include ActiveModel::TestsDatabase
7
+
8
+ def setup
9
+ Person.reset_callbacks(:validate)
10
+ @person = Person.new
11
+
12
+ @old_load_path, @old_backend = I18n.load_path, I18n.backend
13
+ I18n.load_path.clear
14
+ I18n.backend = I18n::Backend::Simple.new
15
+ I18n.backend.store_translations('en', :activemodel => {:errors => {:messages => {:custom => nil}}})
16
+ end
17
+
18
+ def teardown
19
+ Person.reset_callbacks(:validate)
20
+ I18n.load_path.replace @old_load_path
21
+ I18n.backend = @old_backend
22
+ end
23
+
24
+ def test_errors_add_on_empty_generates_message
25
+ @person.errors.expects(:generate_message).with(:title, :empty, {:default => nil})
26
+ @person.errors.add_on_empty :title
27
+ end
28
+
29
+ def test_errors_add_on_empty_generates_message_with_custom_default_message
30
+ @person.errors.expects(:generate_message).with(:title, :empty, {:default => 'custom'})
31
+ @person.errors.add_on_empty :title, 'custom'
32
+ end
33
+
34
+ def test_errors_add_on_blank_generates_message
35
+ @person.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
36
+ @person.errors.add_on_blank :title
37
+ end
38
+
39
+ def test_errors_add_on_blank_generates_message_with_custom_default_message
40
+ @person.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
41
+ @person.errors.add_on_blank :title, 'custom'
42
+ end
43
+
44
+ def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
45
+ @person.errors.add('name', 'empty')
46
+ I18n.expects(:translate).with(:"person.name", :default => ['Name', 'Name'], :scope => [:activemodel, :attributes], :count => 1).returns('Name')
47
+ @person.errors.full_messages
48
+ end
49
+
50
+ def test_errors_full_messages_uses_format
51
+ I18n.backend.store_translations('en', :activemodel => {:errors => {:format => "Field {{attribute}} {{message}}"}})
52
+ @person.errors.add('name', 'empty')
53
+ assert_equal ["Field Name empty"], @person.errors.full_messages
54
+ end
55
+
56
+ # ActiveRecord::Validations
57
+ # validates_confirmation_of w/ mocha
58
+ def test_validates_confirmation_of_generates_message
59
+ Person.validates_confirmation_of :title
60
+ @person.title_confirmation = 'foo'
61
+ @person.errors.expects(:generate_message).with(:title, :confirmation, {:default => nil})
62
+ @person.valid?
63
+ end
64
+
65
+ def test_validates_confirmation_of_generates_message_with_custom_default_message
66
+ Person.validates_confirmation_of :title, :message => 'custom'
67
+ @person.title_confirmation = 'foo'
68
+ @person.errors.expects(:generate_message).with(:title, :confirmation, {:default => 'custom'})
69
+ @person.valid?
70
+ end
71
+
72
+ # validates_acceptance_of w/ mocha
73
+
74
+ def test_validates_acceptance_of_generates_message
75
+ Person.validates_acceptance_of :title, :allow_nil => false
76
+ @person.errors.expects(:generate_message).with(:title, :accepted, {:default => nil})
77
+ @person.valid?
78
+ end
79
+
80
+ def test_validates_acceptance_of_generates_message_with_custom_default_message
81
+ Person.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
82
+ @person.errors.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
83
+ @person.valid?
84
+ end
85
+
86
+ # validates_presence_of w/ mocha
87
+
88
+ def test_validates_presence_of_generates_message
89
+ Person.validates_presence_of :title
90
+ @person.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
91
+ @person.valid?
92
+ end
93
+
94
+ def test_validates_presence_of_generates_message_with_custom_default_message
95
+ Person.validates_presence_of :title, :message => 'custom'
96
+ @person.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
97
+ @person.valid?
98
+ end
99
+
100
+ # validates_length_of :within w/ mocha
101
+
102
+ def test_validates_length_of_within_generates_message_with_title_too_short
103
+ Person.validates_length_of :title, :within => 3..5
104
+ @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
105
+ @person.valid?
106
+ end
107
+
108
+ def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
109
+ Person.validates_length_of :title, :within => 3..5, :too_short => 'custom'
110
+ @person.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
111
+ @person.valid?
112
+ end
113
+
114
+ def test_validates_length_of_within_generates_message_with_title_too_long
115
+ Person.validates_length_of :title, :within => 3..5
116
+ @person.title = 'this title is too long'
117
+ @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
118
+ @person.valid?
119
+ end
120
+
121
+ def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
122
+ Person.validates_length_of :title, :within => 3..5, :too_long => 'custom'
123
+ @person.title = 'this title is too long'
124
+ @person.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
125
+ @person.valid?
126
+ end
127
+
128
+ # validates_length_of :is w/ mocha
129
+
130
+ def test_validates_length_of_is_generates_message
131
+ Person.validates_length_of :title, :is => 5
132
+ @person.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => nil})
133
+ @person.valid?
134
+ end
135
+
136
+ def test_validates_length_of_is_generates_message_with_custom_default_message
137
+ Person.validates_length_of :title, :is => 5, :message => 'custom'
138
+ @person.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => 'custom'})
139
+ @person.valid?
140
+ end
141
+
142
+ # validates_format_of w/ mocha
143
+
144
+ def test_validates_format_of_generates_message
145
+ Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
146
+ @person.title = '72x'
147
+ @person.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => nil})
148
+ @person.valid?
149
+ end
150
+
151
+ def test_validates_format_of_generates_message_with_custom_default_message
152
+ Person.validates_format_of :title, :with => /^[1-9][0-9]*$/, :message => 'custom'
153
+ @person.title = '72x'
154
+ @person.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => 'custom'})
155
+ @person.valid?
156
+ end
157
+
158
+ # validates_inclusion_of w/ mocha
159
+
160
+ def test_validates_inclusion_of_generates_message
161
+ Person.validates_inclusion_of :title, :in => %w(a b c)
162
+ @person.title = 'z'
163
+ @person.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => nil})
164
+ @person.valid?
165
+ end
166
+
167
+ def test_validates_inclusion_of_generates_message_with_custom_default_message
168
+ Person.validates_inclusion_of :title, :in => %w(a b c), :message => 'custom'
169
+ @person.title = 'z'
170
+ @person.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => 'custom'})
171
+ @person.valid?
172
+ end
173
+
174
+ # validates_exclusion_of w/ mocha
175
+
176
+ def test_validates_exclusion_of_generates_message
177
+ Person.validates_exclusion_of :title, :in => %w(a b c)
178
+ @person.title = 'a'
179
+ @person.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => nil})
180
+ @person.valid?
181
+ end
182
+
183
+ def test_validates_exclusion_of_generates_message_with_custom_default_message
184
+ Person.validates_exclusion_of :title, :in => %w(a b c), :message => 'custom'
185
+ @person.title = 'a'
186
+ @person.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => 'custom'})
187
+ @person.valid?
188
+ end
189
+
190
+ # validates_numericality_of without :only_integer w/ mocha
191
+
192
+ def test_validates_numericality_of_generates_message
193
+ Person.validates_numericality_of :title
194
+ @person.title = 'a'
195
+ @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
196
+ @person.valid?
197
+ end
198
+
199
+ def test_validates_numericality_of_generates_message_with_custom_default_message
200
+ Person.validates_numericality_of :title, :message => 'custom'
201
+ @person.title = 'a'
202
+ @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
203
+ @person.valid?
204
+ end
205
+
206
+ # validates_numericality_of with :only_integer w/ mocha
207
+
208
+ def test_validates_numericality_of_only_integer_generates_message
209
+ Person.validates_numericality_of :title, :only_integer => true
210
+ @person.title = 'a'
211
+ @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
212
+ @person.valid?
213
+ end
214
+
215
+ def test_validates_numericality_of_only_integer_generates_message_with_custom_default_message
216
+ Person.validates_numericality_of :title, :only_integer => true, :message => 'custom'
217
+ @person.title = 'a'
218
+ @person.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
219
+ @person.valid?
220
+ end
221
+
222
+ # validates_numericality_of :odd w/ mocha
223
+
224
+ def test_validates_numericality_of_odd_generates_message
225
+ Person.validates_numericality_of :title, :only_integer => true, :odd => true
226
+ @person.title = 0
227
+ @person.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => nil})
228
+ @person.valid?
229
+ end
230
+
231
+ def test_validates_numericality_of_odd_generates_message_with_custom_default_message
232
+ Person.validates_numericality_of :title, :only_integer => true, :odd => true, :message => 'custom'
233
+ @person.title = 0
234
+ @person.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => 'custom'})
235
+ @person.valid?
236
+ end
237
+
238
+ # validates_numericality_of :less_than w/ mocha
239
+
240
+ def test_validates_numericality_of_less_than_generates_message
241
+ Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
242
+ @person.title = 1
243
+ @person.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => nil})
244
+ @person.valid?
245
+ end
246
+
247
+ def test_validates_numericality_of_less_than_odd_generates_message_with_custom_default_message
248
+ Person.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom'
249
+ @person.title = 1
250
+ @person.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => 'custom'})
251
+ @person.valid?
252
+ end
253
+
254
+ # validates_confirmation_of w/o mocha
255
+
256
+ def test_validates_confirmation_of_finds_custom_model_key_translation
257
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}}}
258
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}}
259
+
260
+ Person.validates_confirmation_of :title
261
+ @person.title_confirmation = 'foo'
262
+ @person.valid?
263
+ assert_equal ['custom message'], @person.errors[:title]
264
+ end
265
+
266
+ def test_validates_confirmation_of_finds_global_default_translation
267
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}}
268
+
269
+ Person.validates_confirmation_of :title
270
+ @person.title_confirmation = 'foo'
271
+ @person.valid?
272
+ assert_equal ['global message'], @person.errors[:title]
273
+ end
274
+
275
+ # validates_acceptance_of w/o mocha
276
+
277
+ def test_validates_acceptance_of_finds_custom_model_key_translation
278
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}}}
279
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}}
280
+
281
+ Person.validates_acceptance_of :title, :allow_nil => false
282
+ @person.valid?
283
+ assert_equal ['custom message'], @person.errors[:title]
284
+ end
285
+
286
+ def test_validates_acceptance_of_finds_global_default_translation
287
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}}
288
+
289
+ Person.validates_acceptance_of :title, :allow_nil => false
290
+ @person.valid?
291
+ assert_equal ['global message'], @person.errors[:title]
292
+ end
293
+
294
+ # validates_presence_of w/o mocha
295
+
296
+ def test_validates_presence_of_finds_custom_model_key_translation
297
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}}}
298
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}}
299
+
300
+ Person.validates_presence_of :title
301
+ @person.valid?
302
+ assert_equal ['custom message'], @person.errors[:title]
303
+ end
304
+
305
+ def test_validates_presence_of_finds_global_default_translation
306
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}}
307
+
308
+ Person.validates_presence_of :title
309
+ @person.valid?
310
+ assert_equal ['global message'], @person.errors[:title]
311
+ end
312
+
313
+ # validates_length_of :within w/o mocha
314
+
315
+ def test_validates_length_of_within_finds_custom_model_key_translation
316
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}}}
317
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}}
318
+
319
+ Person.validates_length_of :title, :within => 3..5
320
+ @person.valid?
321
+ assert_equal ['custom message'], @person.errors[:title]
322
+ end
323
+
324
+ def test_validates_length_of_within_finds_global_default_translation
325
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}}
326
+
327
+ Person.validates_length_of :title, :within => 3..5
328
+ @person.valid?
329
+ assert_equal ['global message'], @person.errors[:title]
330
+ end
331
+
332
+ # validates_length_of :is w/o mocha
333
+
334
+ def test_validates_length_of_is_finds_custom_model_key_translation
335
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
336
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}}
337
+
338
+ Person.validates_length_of :title, :is => 5
339
+ @person.valid?
340
+ assert_equal ['custom message'], @person.errors[:title]
341
+ end
342
+
343
+ def test_validates_length_of_is_finds_global_default_translation
344
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}}
345
+
346
+ Person.validates_length_of :title, :is => 5
347
+ @person.valid?
348
+ assert_equal ['global message'], @person.errors[:title]
349
+ end
350
+
351
+ # validates_format_of w/o mocha
352
+
353
+ def test_validates_format_of_finds_custom_model_key_translation
354
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}}}
355
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}}
356
+
357
+ Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
358
+ @person.valid?
359
+ assert_equal ['custom message'], @person.errors[:title]
360
+ end
361
+
362
+ def test_validates_format_of_finds_global_default_translation
363
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}}
364
+
365
+ Person.validates_format_of :title, :with => /^[1-9][0-9]*$/
366
+ @person.valid?
367
+ assert_equal ['global message'], @person.errors[:title]
368
+ end
369
+
370
+ # validates_inclusion_of w/o mocha
371
+
372
+ def test_validates_inclusion_of_finds_custom_model_key_translation
373
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}}}
374
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}}
375
+
376
+ Person.validates_inclusion_of :title, :in => %w(a b c)
377
+ @person.valid?
378
+ assert_equal ['custom message'], @person.errors[:title]
379
+ end
380
+
381
+ def test_validates_inclusion_of_finds_global_default_translation
382
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}}
383
+
384
+ Person.validates_inclusion_of :title, :in => %w(a b c)
385
+ @person.valid?
386
+ assert_equal ['global message'], @person.errors[:title]
387
+ end
388
+
389
+ # validates_exclusion_of w/o mocha
390
+
391
+ def test_validates_exclusion_of_finds_custom_model_key_translation
392
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}}}
393
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}}
394
+
395
+ Person.validates_exclusion_of :title, :in => %w(a b c)
396
+ @person.title = 'a'
397
+ @person.valid?
398
+ assert_equal ['custom message'], @person.errors[:title]
399
+ end
400
+
401
+ def test_validates_exclusion_of_finds_global_default_translation
402
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}}
403
+
404
+ Person.validates_exclusion_of :title, :in => %w(a b c)
405
+ @person.title = 'a'
406
+ @person.valid?
407
+ assert_equal ['global message'], @person.errors[:title]
408
+ end
409
+
410
+ # validates_numericality_of without :only_integer w/o mocha
411
+
412
+ def test_validates_numericality_of_finds_custom_model_key_translation
413
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
414
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
415
+
416
+ Person.validates_numericality_of :title
417
+ @person.title = 'a'
418
+ @person.valid?
419
+ assert_equal ['custom message'], @person.errors[:title]
420
+ end
421
+
422
+ def test_validates_numericality_of_finds_global_default_translation
423
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
424
+
425
+ Person.validates_numericality_of :title, :only_integer => true
426
+ @person.title = 'a'
427
+ @person.valid?
428
+ assert_equal ['global message'], @person.errors[:title]
429
+ end
430
+
431
+ # validates_numericality_of with :only_integer w/o mocha
432
+
433
+ def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
434
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
435
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
436
+
437
+ Person.validates_numericality_of :title, :only_integer => true
438
+ @person.title = 'a'
439
+ @person.valid?
440
+ assert_equal ['custom message'], @person.errors[:title]
441
+ end
442
+
443
+ def test_validates_numericality_of_only_integer_finds_global_default_translation
444
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}}
445
+
446
+ Person.validates_numericality_of :title, :only_integer => true
447
+ @person.title = 'a'
448
+ @person.valid?
449
+ assert_equal ['global message'], @person.errors[:title]
450
+ end
451
+
452
+ # validates_numericality_of :odd w/o mocha
453
+
454
+ def test_validates_numericality_of_odd_finds_custom_model_key_translation
455
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}}}
456
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}}
457
+
458
+ Person.validates_numericality_of :title, :only_integer => true, :odd => true
459
+ @person.title = 0
460
+ @person.valid?
461
+ assert_equal ['custom message'], @person.errors[:title]
462
+ end
463
+
464
+ def test_validates_numericality_of_odd_finds_global_default_translation
465
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}}
466
+
467
+ Person.validates_numericality_of :title, :only_integer => true, :odd => true
468
+ @person.title = 0
469
+ @person.valid?
470
+ assert_equal ['global message'], @person.errors[:title]
471
+ end
472
+
473
+ # validates_numericality_of :less_than w/o mocha
474
+
475
+ def test_validates_numericality_of_less_than_finds_custom_model_key_translation
476
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}}}
477
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}}
478
+
479
+ Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
480
+ @person.title = 1
481
+ @person.valid?
482
+ assert_equal ['custom message'], @person.errors[:title]
483
+ end
484
+
485
+ def test_validates_numericality_of_less_than_finds_global_default_translation
486
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}}
487
+
488
+ Person.validates_numericality_of :title, :only_integer => true, :less_than => 0
489
+ @person.title = 1
490
+ @person.valid?
491
+ assert_equal ['global message'], @person.errors[:title]
492
+ end
493
+
494
+ # test with validates_with
495
+
496
+ def test_validations_with_message_symbol_must_translate
497
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:custom_error => "I am a custom error"}}}
498
+ Person.validates_presence_of :title, :message => :custom_error
499
+ @person.title = nil
500
+ @person.valid?
501
+ assert_equal ["I am a custom error"], @person.errors[:title]
502
+ end
503
+
504
+ def test_validates_with_message_symbol_must_translate_per_attribute
505
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}}
506
+ Person.validates_presence_of :title, :message => :custom_error
507
+ @person.title = nil
508
+ @person.valid?
509
+ assert_equal ["I am a custom error"], @person.errors[:title]
510
+ end
511
+
512
+ def test_validates_with_message_symbol_must_translate_per_model
513
+ I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:custom_error => "I am a custom error"}}}}
514
+ Person.validates_presence_of :title, :message => :custom_error
515
+ @person.title = nil
516
+ @person.valid?
517
+ assert_equal ["I am a custom error"], @person.errors[:title]
518
+ end
519
+
520
+ def test_validates_with_message_string
521
+ Person.validates_presence_of :title, :message => "I am a custom error"
522
+ @person.title = nil
523
+ @person.valid?
524
+ assert_equal ["I am a custom error"], @person.errors[:title]
525
+ end
526
+
527
+ end
@@ -0,0 +1,71 @@
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 InclusionValidationTest < ActiveModel::TestCase
10
+ include ActiveModel::TestsDatabase
11
+
12
+ def teardown
13
+ Topic.reset_callbacks(:validate)
14
+ end
15
+
16
+ def test_validates_inclusion_of
17
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )
18
+
19
+ assert !Topic.create("title" => "a!", "content" => "abc").valid?
20
+ assert !Topic.create("title" => "a b", "content" => "abc").valid?
21
+ assert !Topic.create("title" => nil, "content" => "def").valid?
22
+
23
+ t = Topic.create("title" => "a", "content" => "I know you are but what am I?")
24
+ assert t.valid?
25
+ t.title = "uhoh"
26
+ assert !t.valid?
27
+ assert t.errors[:title].any?
28
+ assert_equal ["is not included in the list"], t.errors[:title]
29
+
30
+ assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => nil ) }
31
+ assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => 0) }
32
+
33
+ assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => "hi!" ) }
34
+ assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => {} ) }
35
+ assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) }
36
+ end
37
+
38
+ def test_validates_inclusion_of_with_allow_nil
39
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :allow_nil=>true )
40
+
41
+ assert !Topic.create("title" => "a!", "content" => "abc").valid?
42
+ assert !Topic.create("title" => "", "content" => "abc").valid?
43
+ assert Topic.create("title" => nil, "content" => "abc").valid?
44
+ end
45
+
46
+ def test_validates_inclusion_of_with_formatted_message
47
+ Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option {{value}} is not in the list" )
48
+
49
+ assert Topic.create("title" => "a", "content" => "abc").valid?
50
+
51
+ t = Topic.create("title" => "uhoh", "content" => "abc")
52
+ assert !t.valid?
53
+ assert t.errors[:title].any?
54
+ assert_equal ["option uhoh is not in the list"], t.errors[:title]
55
+ end
56
+
57
+ def test_validates_inclusion_of_for_ruby_class
58
+ Person.validates_inclusion_of :karma, :in => %w( abe monkey )
59
+
60
+ p = Person.new
61
+ p.karma = "Lifo"
62
+ assert p.invalid?
63
+
64
+ assert_equal ["is not included in the list"], p.errors[:karma]
65
+
66
+ p.karma = "monkey"
67
+ assert p.valid?
68
+ ensure
69
+ Person.reset_callbacks(:validate)
70
+ end
71
+ end