cassandra_object 0.6.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,312 @@
1
+ require 'cases/helper'
2
+
3
+ class StateMachineSubject
4
+ include ActiveModel::StateMachine
5
+
6
+ state_machine do
7
+ state :open, :exit => :exit
8
+ state :closed, :enter => :enter
9
+
10
+ event :close, :success => :success_callback do
11
+ transitions :to => :closed, :from => [:open]
12
+ end
13
+
14
+ event :null do
15
+ transitions :to => :closed, :from => [:open], :guard => :always_false
16
+ end
17
+ end
18
+
19
+ state_machine :bar do
20
+ state :read
21
+ state :ended
22
+
23
+ event :foo do
24
+ transitions :to => :ended, :from => [:read]
25
+ end
26
+ end
27
+
28
+ def always_false
29
+ false
30
+ end
31
+
32
+ def success_callback
33
+ end
34
+
35
+ def enter
36
+ end
37
+ def exit
38
+ end
39
+ end
40
+
41
+ class StateMachineSubjectSubclass < StateMachineSubject
42
+ end
43
+
44
+ class StateMachineClassLevelTest < ActiveModel::TestCase
45
+ test 'defines a class level #state_machine method on its including class' do
46
+ assert StateMachineSubject.respond_to?(:state_machine)
47
+ end
48
+
49
+ test 'defines a class level #state_machines method on its including class' do
50
+ assert StateMachineSubject.respond_to?(:state_machines)
51
+ end
52
+
53
+ test 'class level #state_machine returns machine instance' do
54
+ assert_kind_of ActiveModel::StateMachine::Machine, StateMachineSubject.state_machine
55
+ end
56
+
57
+ test 'class level #state_machine returns machine instance with given name' do
58
+ assert_kind_of ActiveModel::StateMachine::Machine, StateMachineSubject.state_machine(:default)
59
+ end
60
+
61
+ test 'class level #state_machines returns hash of machine instances' do
62
+ assert_kind_of ActiveModel::StateMachine::Machine, StateMachineSubject.state_machines[:default]
63
+ end
64
+
65
+ test "should return a select friendly array of states in the form of [['Friendly name', 'state_name']]" do
66
+ assert_equal [['Open', 'open'], ['Closed', 'closed']], StateMachineSubject.state_machine.states_for_select
67
+ end
68
+ end
69
+
70
+ class StateMachineInstanceLevelTest < ActiveModel::TestCase
71
+ def setup
72
+ @foo = StateMachineSubject.new
73
+ end
74
+
75
+ test 'defines an accessor for the current state' do
76
+ assert @foo.respond_to?(:current_state)
77
+ end
78
+
79
+ test 'defines a state querying instance method on including class' do
80
+ assert @foo.respond_to?(:open?)
81
+ end
82
+
83
+ test 'defines an event! instance method' do
84
+ assert @foo.respond_to?(:close!)
85
+ end
86
+
87
+ test 'defines an event instance method' do
88
+ assert @foo.respond_to?(:close)
89
+ end
90
+ end
91
+
92
+ class StateMachineInitialStatesTest < ActiveModel::TestCase
93
+ def setup
94
+ @foo = StateMachineSubject.new
95
+ end
96
+
97
+ test 'sets the initial state' do
98
+ assert_equal :open, @foo.current_state
99
+ end
100
+
101
+ test '#open? should be initially true' do
102
+ assert @foo.open?
103
+ end
104
+
105
+ test '#closed? should be initially false' do
106
+ assert !@foo.closed?
107
+ end
108
+
109
+ test 'uses the first state defined if no initial state is given' do
110
+ assert_equal :read, @foo.current_state(:bar)
111
+ end
112
+ end
113
+
114
+ class StateMachineEventFiringWithPersistenceTest < ActiveModel::TestCase
115
+ def setup
116
+ @subj = StateMachineSubject.new
117
+ end
118
+
119
+ test 'updates the current state' do
120
+ @subj.close!
121
+
122
+ assert_equal :closed, @subj.current_state
123
+ end
124
+
125
+ test 'fires the Event' do
126
+ @subj.class.state_machine.events[:close].expects(:fire).with(@subj)
127
+ @subj.close!
128
+ end
129
+
130
+ test 'calls the success callback if one was provided' do
131
+ @subj.expects(:success_callback)
132
+ @subj.close!
133
+ end
134
+
135
+ test 'attempts to persist if write_state is defined' do
136
+ def @subj.write_state
137
+ end
138
+
139
+ @subj.expects(:write_state)
140
+ @subj.close!
141
+ end
142
+ end
143
+
144
+ class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
145
+ test 'updates the current state' do
146
+ subj = StateMachineSubject.new
147
+ assert_equal :open, subj.current_state
148
+ subj.close
149
+ assert_equal :closed, subj.current_state
150
+ end
151
+
152
+ test 'fires the Event' do
153
+ subj = StateMachineSubject.new
154
+
155
+ StateMachineSubject.state_machine.events[:close].expects(:fire).with(subj)
156
+ subj.close
157
+ end
158
+
159
+ test 'attempts to persist if write_state is defined' do
160
+ subj = StateMachineSubject.new
161
+
162
+ def subj.write_state
163
+ end
164
+
165
+ subj.expects(:write_state_without_persistence)
166
+
167
+ subj.close
168
+ end
169
+ end
170
+
171
+ class StateMachinePersistenceTest < ActiveModel::TestCase
172
+ test 'reads the state if it has not been set and read_state is defined' do
173
+ subj = StateMachineSubject.new
174
+ def subj.read_state
175
+ end
176
+
177
+ subj.expects(:read_state).with(StateMachineSubject.state_machine)
178
+
179
+ subj.current_state
180
+ end
181
+ end
182
+
183
+ class StateMachineEventCallbacksTest < ActiveModel::TestCase
184
+ test 'should call aasm_event_fired if defined and successful for bang fire' do
185
+ subj = StateMachineSubject.new
186
+ def subj.aasm_event_fired(from, to)
187
+ end
188
+
189
+ subj.expects(:event_fired)
190
+
191
+ subj.close!
192
+ end
193
+
194
+ test 'should call aasm_event_fired if defined and successful for non-bang fire' do
195
+ subj = StateMachineSubject.new
196
+ def subj.aasm_event_fired(from, to)
197
+ end
198
+
199
+ subj.expects(:event_fired)
200
+
201
+ subj.close
202
+ end
203
+
204
+ test 'should call aasm_event_failed if defined and transition failed for bang fire' do
205
+ subj = StateMachineSubject.new
206
+ def subj.event_failed(event)
207
+ end
208
+
209
+ subj.expects(:event_failed)
210
+
211
+ subj.null!
212
+ end
213
+
214
+ test 'should call aasm_event_failed if defined and transition failed for non-bang fire' do
215
+ subj = StateMachineSubject.new
216
+ def subj.aasm_event_failed(event)
217
+ end
218
+
219
+ subj.expects(:event_failed)
220
+
221
+ subj.null
222
+ end
223
+ end
224
+
225
+ class StateMachineStateActionsTest < ActiveModel::TestCase
226
+ test "calls enter when entering state" do
227
+ subj = StateMachineSubject.new
228
+ subj.expects(:enter)
229
+ subj.close
230
+ end
231
+
232
+ test "calls exit when exiting state" do
233
+ subj = StateMachineSubject.new
234
+ subj.expects(:exit)
235
+ subj.close
236
+ end
237
+ end
238
+
239
+ class StateMachineInheritanceTest < ActiveModel::TestCase
240
+ test "has the same states as its parent" do
241
+ assert_equal StateMachineSubject.state_machine.states, StateMachineSubjectSubclass.state_machine.states
242
+ end
243
+
244
+ test "has the same events as its parent" do
245
+ assert_equal StateMachineSubject.state_machine.events, StateMachineSubjectSubclass.state_machine.events
246
+ end
247
+ end
248
+
249
+ class StateMachineSubject
250
+ state_machine :chetan_patil, :initial => :sleeping do
251
+ state :sleeping
252
+ state :showering
253
+ state :working
254
+ state :dating
255
+
256
+ event :wakeup do
257
+ transitions :from => :sleeping, :to => [:showering, :working]
258
+ end
259
+
260
+ event :dress do
261
+ transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes
262
+ transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) }
263
+ end
264
+ end
265
+
266
+ def wear_clothes(shirt_color, trouser_type)
267
+ end
268
+ end
269
+
270
+ class StateMachineWithComplexTransitionsTest < ActiveModel::TestCase
271
+ def setup
272
+ @subj = StateMachineSubject.new
273
+ end
274
+
275
+ test 'transitions to specified next state (sleeping to showering)' do
276
+ @subj.wakeup! :showering
277
+
278
+ assert_equal :showering, @subj.current_state(:chetan_patil)
279
+ end
280
+
281
+ test 'transitions to specified next state (sleeping to working)' do
282
+ @subj.wakeup! :working
283
+
284
+ assert_equal :working, @subj.current_state(:chetan_patil)
285
+ end
286
+
287
+ test 'transitions to default (first or showering) state' do
288
+ @subj.wakeup!
289
+
290
+ assert_equal :showering, @subj.current_state(:chetan_patil)
291
+ end
292
+
293
+ test 'transitions to default state when on_transition invoked' do
294
+ @subj.dress!(nil, 'purple', 'dressy')
295
+
296
+ assert_equal :working, @subj.current_state(:chetan_patil)
297
+ end
298
+
299
+ test 'calls on_transition method with args' do
300
+ @subj.wakeup! :showering
301
+
302
+ @subj.expects(:wear_clothes).with('blue', 'jeans')
303
+ @subj.dress! :working, 'blue', 'jeans'
304
+ end
305
+
306
+ test 'calls on_transition proc' do
307
+ @subj.wakeup! :showering
308
+
309
+ @subj.expects(:wear_clothes).with('purple', 'slacks')
310
+ @subj.dress!(:dating, 'purple', 'slacks')
311
+ end
312
+ end
@@ -0,0 +1,37 @@
1
+ require 'logger'
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../../../activerecord/lib')
4
+ require 'active_record'
5
+ require 'active_record/test_case'
6
+ require 'active_record/fixtures'
7
+
8
+ module ActiveModel
9
+ module TestsDatabase
10
+ mattr_accessor :connected
11
+
12
+ def self.included(base)
13
+ unless self.connected
14
+ setup_connection
15
+ setup_schema
16
+ end
17
+
18
+ base.send :include, ActiveRecord::TestFixtures
19
+ end
20
+
21
+ def self.setup_schema
22
+ original, $stdout = $stdout, StringIO.new
23
+ load(SCHEMA_FILE)
24
+ ensure
25
+ $stdout = original
26
+ self.connected = true
27
+ end
28
+
29
+ def self.setup_connection
30
+ defaults = { :database => ':memory:' }
31
+
32
+ adapter = defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3'
33
+ options = defaults.merge :adapter => adapter, :timeout => 500
34
+ ActiveRecord::Base.establish_connection(options)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,45 @@
1
+ require 'cases/helper'
2
+ require 'models/person'
3
+
4
+ class ActiveModelI18nTests < ActiveModel::TestCase
5
+
6
+ def setup
7
+ I18n.backend = I18n::Backend::Simple.new
8
+ end
9
+
10
+ def test_translated_model_attributes
11
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } }
12
+ assert_equal 'person name attribute', Person.human_attribute_name('name')
13
+ end
14
+
15
+ def test_translated_model_attributes_with_symbols
16
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } }
17
+ assert_equal 'person name attribute', Person.human_attribute_name(:name)
18
+ end
19
+
20
+ def test_translated_model_attributes_with_ancestor
21
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:child => {:name => 'child name attribute'} } }
22
+ assert_equal 'child name attribute', Child.human_attribute_name('name')
23
+ end
24
+
25
+ def test_translated_model_attributes_with_ancestors_fallback
26
+ I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } }
27
+ assert_equal 'person name attribute', Child.human_attribute_name('name')
28
+ end
29
+
30
+ def test_translated_model_names
31
+ I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} }
32
+ assert_equal 'person model', Person.model_name.human
33
+ end
34
+
35
+ def test_translated_model_names_with_sti
36
+ I18n.backend.store_translations 'en', :activemodel => {:models => {:child => 'child model'} }
37
+ assert_equal 'child model', Child.model_name.human
38
+ end
39
+
40
+ def test_translated_model_names_with_ancestors_fallback
41
+ I18n.backend.store_translations 'en', :activemodel => {:models => {:person => 'person model'} }
42
+ assert_equal 'person model', Child.model_name.human
43
+ end
44
+ end
45
+
@@ -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/reply'
7
+ require 'models/developer'
8
+ require 'models/person'
9
+
10
+ class AcceptanceValidationTest < ActiveModel::TestCase
11
+ include ActiveModel::TestsDatabase
12
+
13
+ def teardown
14
+ Topic.reset_callbacks(:validate)
15
+ end
16
+
17
+ def test_terms_of_service_agreement_no_acceptance
18
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create)
19
+
20
+ t = Topic.create("title" => "We should not be confirmed")
21
+ assert t.save
22
+ end
23
+
24
+ def test_terms_of_service_agreement
25
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create)
26
+
27
+ t = Topic.create("title" => "We should be confirmed","terms_of_service" => "")
28
+ assert !t.save
29
+ assert_equal ["must be accepted"], t.errors[:terms_of_service]
30
+
31
+ t.terms_of_service = "1"
32
+ assert t.save
33
+ end
34
+
35
+ def test_eula
36
+ Topic.validates_acceptance_of(:eula, :message => "must be abided", :on => :create)
37
+
38
+ t = Topic.create("title" => "We should be confirmed","eula" => "")
39
+ assert !t.save
40
+ assert_equal ["must be abided"], t.errors[:eula]
41
+
42
+ t.eula = "1"
43
+ assert t.save
44
+ end
45
+
46
+ def test_terms_of_service_agreement_with_accept_value
47
+ Topic.validates_acceptance_of(:terms_of_service, :on => :create, :accept => "I agree.")
48
+
49
+ t = Topic.create("title" => "We should be confirmed", "terms_of_service" => "")
50
+ assert !t.save
51
+ assert_equal ["must be accepted"], t.errors[:terms_of_service]
52
+
53
+ t.terms_of_service = "I agree."
54
+ assert t.save
55
+ end
56
+
57
+ def test_validates_acceptance_of_for_ruby_class
58
+ Person.validates_acceptance_of :karma
59
+
60
+ p = Person.new
61
+ p.karma = ""
62
+
63
+ assert p.invalid?
64
+ assert_equal ["must be accepted"], p.errors[:karma]
65
+
66
+ p.karma = "1"
67
+ assert p.valid?
68
+ ensure
69
+ Person.reset_callbacks(:validate)
70
+ end
71
+ end
@@ -0,0 +1,141 @@
1
+ # encoding: utf-8
2
+ require 'cases/helper'
3
+ require 'cases/tests_database'
4
+
5
+ require 'models/topic'
6
+
7
+ class ConditionalValidationTest < ActiveModel::TestCase
8
+ include ActiveModel::TestsDatabase
9
+
10
+ def teardown
11
+ Topic.reset_callbacks(:validate)
12
+ end
13
+
14
+ def test_if_validation_using_method_true
15
+ # When the method returns true
16
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => :condition_is_true )
17
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
18
+ assert !t.valid?
19
+ assert t.errors[:title].any?
20
+ assert_equal ["hoo 5"], t.errors["title"]
21
+ end
22
+
23
+ def test_unless_validation_using_method_true
24
+ # When the method returns true
25
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => :condition_is_true )
26
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
27
+ assert t.valid?
28
+ assert !t.errors[:title].any?
29
+ end
30
+
31
+ def test_if_validation_using_method_false
32
+ # When the method returns false
33
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => :condition_is_true_but_its_not )
34
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
35
+ assert t.valid?
36
+ assert t.errors[:title].empty?
37
+ end
38
+
39
+ def test_unless_validation_using_method_false
40
+ # When the method returns false
41
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => :condition_is_true_but_its_not )
42
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
43
+ assert !t.valid?
44
+ assert t.errors[:title].any?
45
+ assert_equal ["hoo 5"], t.errors["title"]
46
+ end
47
+
48
+ def test_if_validation_using_string_true
49
+ # When the evaluated string returns true
50
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => "a = 1; a == 1" )
51
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
52
+ assert !t.valid?
53
+ assert t.errors[:title].any?
54
+ assert_equal ["hoo 5"], t.errors["title"]
55
+ end
56
+
57
+ def test_unless_validation_using_string_true
58
+ # When the evaluated string returns true
59
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => "a = 1; a == 1" )
60
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
61
+ assert t.valid?
62
+ assert t.errors[:title].empty?
63
+ end
64
+
65
+ def test_if_validation_using_string_false
66
+ # When the evaluated string returns false
67
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => "false")
68
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
69
+ assert t.valid?
70
+ assert t.errors[:title].empty?
71
+ end
72
+
73
+ def test_unless_validation_using_string_false
74
+ # When the evaluated string returns false
75
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => "false")
76
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
77
+ assert !t.valid?
78
+ assert t.errors[:title].any?
79
+ assert_equal ["hoo 5"], t.errors["title"]
80
+ end
81
+
82
+ def test_if_validation_using_block_true
83
+ # When the block returns true
84
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
85
+ :if => Proc.new { |r| r.content.size > 4 } )
86
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
87
+ assert !t.valid?
88
+ assert t.errors[:title].any?
89
+ assert_equal ["hoo 5"], t.errors["title"]
90
+ end
91
+
92
+ def test_unless_validation_using_block_true
93
+ # When the block returns true
94
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
95
+ :unless => Proc.new { |r| r.content.size > 4 } )
96
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
97
+ assert t.valid?
98
+ assert t.errors[:title].empty?
99
+ end
100
+
101
+ def test_if_validation_using_block_false
102
+ # When the block returns false
103
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
104
+ :if => Proc.new { |r| r.title != "uhohuhoh"} )
105
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
106
+ assert t.valid?
107
+ assert t.errors[:title].empty?
108
+ end
109
+
110
+ def test_unless_validation_using_block_false
111
+ # When the block returns false
112
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
113
+ :unless => Proc.new { |r| r.title != "uhohuhoh"} )
114
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
115
+ assert !t.valid?
116
+ assert t.errors[:title].any?
117
+ assert_equal ["hoo 5"], t.errors["title"]
118
+ end
119
+
120
+ # previous implementation of validates_presence_of eval'd the
121
+ # string with the wrong binding, this regression test is to
122
+ # ensure that it works correctly
123
+ def test_validation_with_if_as_string
124
+ Topic.validates_presence_of(:title)
125
+ Topic.validates_presence_of(:author_name, :if => "title.to_s.match('important')")
126
+
127
+ t = Topic.new
128
+ assert t.invalid?, "A topic without a title should not be valid"
129
+ assert t.errors[:author_name].empty?, "A topic without an 'important' title should not require an author"
130
+
131
+ t.title = "Just a title"
132
+ assert t.valid?, "A topic with a basic title should be valid"
133
+
134
+ t.title = "A very important title"
135
+ assert !t.valid?, "A topic with an important title, but without an author, should not be valid"
136
+ assert t.errors[:author_name].any?, "A topic with an 'important' title should require an author"
137
+
138
+ t.author_name = "Hubert J. Farnsworth"
139
+ assert t.valid?, "A topic with an important title and author should be valid"
140
+ end
141
+ end
@@ -0,0 +1,58 @@
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 ConfirmationValidationTest < ActiveModel::TestCase
10
+ include ActiveModel::TestsDatabase
11
+
12
+ def teardown
13
+ Topic.reset_callbacks(:validate)
14
+ end
15
+
16
+ def test_no_title_confirmation
17
+ Topic.validates_confirmation_of(:title)
18
+
19
+ t = Topic.new(:author_name => "Plutarch")
20
+ assert t.valid?
21
+
22
+ t.title_confirmation = "Parallel Lives"
23
+ assert !t.valid?
24
+
25
+ t.title_confirmation = nil
26
+ t.title = "Parallel Lives"
27
+ assert t.valid?
28
+
29
+ t.title_confirmation = "Parallel Lives"
30
+ assert t.valid?
31
+ end
32
+
33
+ def test_title_confirmation
34
+ Topic.validates_confirmation_of(:title)
35
+
36
+ t = Topic.create("title" => "We should be confirmed","title_confirmation" => "")
37
+ assert !t.save
38
+
39
+ t.title_confirmation = "We should be confirmed"
40
+ assert t.save
41
+ end
42
+
43
+ def test_validates_confirmation_of_for_ruby_class
44
+ Person.validates_confirmation_of :karma
45
+
46
+ p = Person.new
47
+ p.karma_confirmation = "None"
48
+ assert p.invalid?
49
+
50
+ assert_equal ["doesn't match confirmation"], p.errors[:karma]
51
+
52
+ p.karma = "None"
53
+ assert p.valid?
54
+ ensure
55
+ Person.reset_callbacks(:validate)
56
+ end
57
+
58
+ end