ibm_db 2.5.27-x86-mingw32 → 2.6.0-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +5 -0
- data/MANIFEST +14 -14
- data/README +225 -225
- data/ext/Makefile.nt32 +181 -181
- data/ext/Makefile.nt32.191 +212 -212
- data/ext/extconf.rb +264 -264
- data/ext/extconf_MacOS.rb +269 -0
- data/ext/ibm_db.c +1 -1
- data/ext/ruby_ibm_db.h +241 -241
- data/init.rb +41 -41
- data/lib/IBM_DB.rb +27 -3
- data/lib/active_record/connection_adapters/ibm_db_adapter.rb +3290 -3290
- data/lib/active_record/connection_adapters/ibmdb_adapter.rb +1 -1
- data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -328
- data/lib/mswin32/ibm_db.rb +104 -20
- data/lib/mswin32/rb19x/ibm_db.so +0 -0
- data/lib/mswin32/rb21x/i386/ibm_db.so +0 -0
- data/lib/mswin32/rb22x/i386/ibm_db.so +0 -0
- data/lib/mswin32/rb2x/i386/ibm_db.so +0 -0
- data/test/cases/adapter_test.rb +207 -207
- data/test/cases/associations/belongs_to_associations_test.rb +711 -711
- data/test/cases/associations/cascaded_eager_loading_test.rb +181 -181
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +851 -851
- data/test/cases/associations/join_model_test.rb +743 -743
- data/test/cases/attribute_methods_test.rb +822 -822
- data/test/cases/base_test.rb +2133 -2133
- data/test/cases/calculations_test.rb +482 -482
- data/test/cases/migration_test.rb +2408 -2408
- data/test/cases/persistence_test.rb +642 -642
- data/test/cases/query_cache_test.rb +257 -257
- data/test/cases/relations_test.rb +1182 -1182
- data/test/cases/schema_dumper_test.rb +256 -256
- data/test/cases/transaction_callbacks_test.rb +300 -300
- data/test/cases/validations/uniqueness_validation_test.rb +299 -299
- data/test/cases/xml_serialization_test.rb +408 -408
- data/test/config.yml +154 -154
- data/test/connections/native_ibm_db/connection.rb +43 -43
- data/test/ibm_db_test.rb +24 -24
- data/test/models/warehouse_thing.rb +4 -4
- data/test/schema/schema.rb +751 -751
- metadata +31 -16
@@ -1,642 +1,642 @@
|
|
1
|
-
require "cases/helper"
|
2
|
-
require 'models/post'
|
3
|
-
require 'models/comment'
|
4
|
-
require 'models/author'
|
5
|
-
require 'models/topic'
|
6
|
-
require 'models/reply'
|
7
|
-
require 'models/category'
|
8
|
-
require 'models/company'
|
9
|
-
require 'models/developer'
|
10
|
-
require 'models/project'
|
11
|
-
require 'models/minimalistic'
|
12
|
-
require 'models/warehouse_thing'
|
13
|
-
require 'models/parrot'
|
14
|
-
require 'models/minivan'
|
15
|
-
require 'models/person'
|
16
|
-
require 'rexml/document'
|
17
|
-
require 'active_support/core_ext/exception'
|
18
|
-
|
19
|
-
class PersistencesTest < ActiveRecord::TestCase
|
20
|
-
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics,
|
21
|
-
'warehouse_things', :authors, :categorizations, :categories, :posts, :minivans
|
22
|
-
|
23
|
-
# Skip databases that don't support UPDATE + ORDER BY
|
24
|
-
unless current_adapter?(:OracleAdapter, :PostgreSQLAdapter)
|
25
|
-
def test_update_all_ignores_order_without_limit_from_association
|
26
|
-
author = authors(:david)
|
27
|
-
assert_nothing_raised do
|
28
|
-
assert_equal author.posts_with_comments_and_categories.length,
|
29
|
-
author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_update_all_doesnt_ignore_order
|
34
|
-
assert_equal authors(:david).id + 1, authors(:mary).id # make sure there is going to be a duplicate PK error
|
35
|
-
test_update_with_order_succeeds = lambda do |order|
|
36
|
-
begin
|
37
|
-
Author.order(order).update_all('id = id + 1')
|
38
|
-
rescue ActiveRecord::ActiveRecordError
|
39
|
-
false
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
if !current_adapter?(:IBM_DBAdapter) && test_update_with_order_succeeds.call('id DESC')
|
44
|
-
# The update below goes on successfully in DB2.
|
45
|
-
assert !test_update_with_order_succeeds.call('id ASC') # test that this wasn't a fluke and using an incorrect order results in an exception
|
46
|
-
else
|
47
|
-
# test that we're failing because the current Arel's engine doesn't support UPDATE ORDER BY queries is using subselects instead
|
48
|
-
assert_sql(/\AUPDATE .+ \(SELECT .* ORDER BY id DESC\)\Z/i) do
|
49
|
-
test_update_with_order_succeeds.call('id DESC')
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_update_all_with_order_and_limit_updates_subset_only
|
55
|
-
author = authors(:david)
|
56
|
-
assert_nothing_raised do
|
57
|
-
assert_equal 1, author.posts_sorted_by_id_limited.size
|
58
|
-
assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size
|
59
|
-
assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ])
|
60
|
-
assert_equal "bulk update!", posts(:welcome).body
|
61
|
-
assert_not_equal "bulk update!", posts(:thinking).body
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_update_many
|
67
|
-
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
|
68
|
-
updated = Topic.update(topic_data.keys, topic_data.values)
|
69
|
-
|
70
|
-
assert_equal 2, updated.size
|
71
|
-
assert_equal "1 updated", Topic.find(1).content
|
72
|
-
assert_equal "2 updated", Topic.find(2).content
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_delete_all
|
76
|
-
assert Topic.count > 0
|
77
|
-
|
78
|
-
assert_equal Topic.count, Topic.delete_all
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_update_by_condition
|
82
|
-
Topic.update_all "content = 'bulk updated!'", ["approved = ?", true]
|
83
|
-
assert_equal "Have a nice day", Topic.find(1).content
|
84
|
-
assert_equal "bulk updated!", Topic.find(2).content
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_increment_attribute
|
88
|
-
assert_equal 50, accounts(:signals37).credit_limit
|
89
|
-
accounts(:signals37).increment! :credit_limit
|
90
|
-
assert_equal 51, accounts(:signals37, :reload).credit_limit
|
91
|
-
|
92
|
-
accounts(:signals37).increment(:credit_limit).increment!(:credit_limit)
|
93
|
-
assert_equal 53, accounts(:signals37, :reload).credit_limit
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_increment_nil_attribute
|
97
|
-
assert_nil topics(:first).parent_id
|
98
|
-
topics(:first).increment! :parent_id
|
99
|
-
assert_equal 1, topics(:first).parent_id
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_increment_attribute_by
|
103
|
-
assert_equal 50, accounts(:signals37).credit_limit
|
104
|
-
accounts(:signals37).increment! :credit_limit, 5
|
105
|
-
assert_equal 55, accounts(:signals37, :reload).credit_limit
|
106
|
-
|
107
|
-
accounts(:signals37).increment(:credit_limit, 1).increment!(:credit_limit, 3)
|
108
|
-
assert_equal 59, accounts(:signals37, :reload).credit_limit
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_destroy_all
|
112
|
-
conditions = "author_name = 'Mary'"
|
113
|
-
topics_by_mary = Topic.all(:conditions => conditions, :order => 'id')
|
114
|
-
assert ! topics_by_mary.empty?
|
115
|
-
|
116
|
-
assert_difference('Topic.count', -topics_by_mary.size) do
|
117
|
-
destroyed = Topic.destroy_all(conditions).sort_by(&:id)
|
118
|
-
assert_equal topics_by_mary, destroyed
|
119
|
-
assert destroyed.all? { |topic| topic.frozen? }, "destroyed topics should be frozen"
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_destroy_many
|
124
|
-
clients = Client.find([2, 3], :order => 'id')
|
125
|
-
|
126
|
-
assert_difference('Client.count', -2) do
|
127
|
-
destroyed = Client.destroy([2, 3]).sort_by(&:id)
|
128
|
-
assert_equal clients, destroyed
|
129
|
-
assert destroyed.all? { |client| client.frozen? }, "destroyed clients should be frozen"
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_delete_many
|
134
|
-
original_count = Topic.count
|
135
|
-
Topic.delete(deleting = [1, 2])
|
136
|
-
assert_equal original_count - deleting.size, Topic.count
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_decrement_attribute
|
140
|
-
assert_equal 50, accounts(:signals37).credit_limit
|
141
|
-
|
142
|
-
accounts(:signals37).decrement!(:credit_limit)
|
143
|
-
assert_equal 49, accounts(:signals37, :reload).credit_limit
|
144
|
-
|
145
|
-
accounts(:signals37).decrement(:credit_limit).decrement!(:credit_limit)
|
146
|
-
assert_equal 47, accounts(:signals37, :reload).credit_limit
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_decrement_attribute_by
|
150
|
-
assert_equal 50, accounts(:signals37).credit_limit
|
151
|
-
accounts(:signals37).decrement! :credit_limit, 5
|
152
|
-
assert_equal 45, accounts(:signals37, :reload).credit_limit
|
153
|
-
|
154
|
-
accounts(:signals37).decrement(:credit_limit, 1).decrement!(:credit_limit, 3)
|
155
|
-
assert_equal 41, accounts(:signals37, :reload).credit_limit
|
156
|
-
end
|
157
|
-
|
158
|
-
def test_create
|
159
|
-
topic = Topic.new
|
160
|
-
topic.title = "New Topic"
|
161
|
-
topic.save
|
162
|
-
topic_reloaded = Topic.find(topic.id)
|
163
|
-
assert_equal("New Topic", topic_reloaded.title)
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_save!
|
167
|
-
topic = Topic.new(:title => "New Topic")
|
168
|
-
assert topic.save!
|
169
|
-
|
170
|
-
reply = WrongReply.new
|
171
|
-
assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
|
172
|
-
end
|
173
|
-
|
174
|
-
def test_save_null_string_attributes
|
175
|
-
topic = Topic.find(1)
|
176
|
-
topic.attributes = { "title" => "null", "author_name" => "null" }
|
177
|
-
topic.save!
|
178
|
-
topic.reload
|
179
|
-
assert_equal("null", topic.title)
|
180
|
-
assert_equal("null", topic.author_name)
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_save_nil_string_attributes
|
184
|
-
topic = Topic.find(1)
|
185
|
-
topic.title = nil
|
186
|
-
topic.save!
|
187
|
-
topic.reload
|
188
|
-
assert_nil topic.title
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_save_for_record_with_only_primary_key
|
192
|
-
minimalistic = Minimalistic.new
|
193
|
-
assert_nothing_raised { minimalistic.save }
|
194
|
-
end
|
195
|
-
|
196
|
-
def test_save_for_record_with_only_primary_key_that_is_provided
|
197
|
-
assert_nothing_raised { Minimalistic.create!(:id => 2) }
|
198
|
-
end
|
199
|
-
|
200
|
-
def test_create_many
|
201
|
-
topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
|
202
|
-
assert_equal 2, topics.size
|
203
|
-
assert_equal "first", topics.first.title
|
204
|
-
end
|
205
|
-
|
206
|
-
def test_create_columns_not_equal_attributes
|
207
|
-
topic = Topic.allocate.init_with(
|
208
|
-
'attributes' => {
|
209
|
-
'title' => 'Another New Topic',
|
210
|
-
'does_not_exist' => 'test'
|
211
|
-
}
|
212
|
-
)
|
213
|
-
assert_nothing_raised { topic.save }
|
214
|
-
end
|
215
|
-
|
216
|
-
def test_create_through_factory_with_block
|
217
|
-
topic = Topic.create("title" => "New Topic") do |t|
|
218
|
-
t.author_name = "David"
|
219
|
-
end
|
220
|
-
assert_equal("New Topic", topic.title)
|
221
|
-
assert_equal("David", topic.author_name)
|
222
|
-
end
|
223
|
-
|
224
|
-
def test_create_many_through_factory_with_block
|
225
|
-
topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t|
|
226
|
-
t.author_name = "David"
|
227
|
-
end
|
228
|
-
assert_equal 2, topics.size
|
229
|
-
topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id)
|
230
|
-
assert_equal "first", topic1.title
|
231
|
-
assert_equal "David", topic1.author_name
|
232
|
-
assert_equal "second", topic2.title
|
233
|
-
assert_equal "David", topic2.author_name
|
234
|
-
end
|
235
|
-
|
236
|
-
def test_update
|
237
|
-
topic = Topic.new
|
238
|
-
topic.title = "Another New Topic"
|
239
|
-
topic.written_on = "2003-12-12 23:23:00"
|
240
|
-
topic.save
|
241
|
-
topicReloaded = Topic.find(topic.id)
|
242
|
-
assert_equal("Another New Topic", topicReloaded.title)
|
243
|
-
|
244
|
-
topicReloaded.title = "Updated topic"
|
245
|
-
topicReloaded.save
|
246
|
-
|
247
|
-
topicReloadedAgain = Topic.find(topic.id)
|
248
|
-
|
249
|
-
assert_equal("Updated topic", topicReloadedAgain.title)
|
250
|
-
end
|
251
|
-
|
252
|
-
def test_update_columns_not_equal_attributes
|
253
|
-
topic = Topic.new
|
254
|
-
topic.title = "Still another topic"
|
255
|
-
topic.save
|
256
|
-
|
257
|
-
topicReloaded = Topic.allocate
|
258
|
-
topicReloaded.init_with(
|
259
|
-
'attributes' => topic.attributes.merge('does_not_exist' => 'test')
|
260
|
-
)
|
261
|
-
topicReloaded.title = 'A New Topic'
|
262
|
-
assert_nothing_raised { topicReloaded.save }
|
263
|
-
end
|
264
|
-
|
265
|
-
def test_update_for_record_with_only_primary_key
|
266
|
-
minimalistic = minimalistics(:first)
|
267
|
-
assert_nothing_raised { minimalistic.save }
|
268
|
-
end
|
269
|
-
|
270
|
-
def test_update_sti_type
|
271
|
-
assert_instance_of Reply, topics(:second)
|
272
|
-
|
273
|
-
topic = topics(:second).becomes(Topic)
|
274
|
-
assert_instance_of Topic, topic
|
275
|
-
topic.save!
|
276
|
-
assert_instance_of Topic, Topic.find(topic.id)
|
277
|
-
end
|
278
|
-
|
279
|
-
def test_delete
|
280
|
-
topic = Topic.find(1)
|
281
|
-
assert_equal topic, topic.delete, 'topic.delete did not return self'
|
282
|
-
assert topic.frozen?, 'topic not frozen after delete'
|
283
|
-
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
|
284
|
-
end
|
285
|
-
|
286
|
-
def test_delete_doesnt_run_callbacks
|
287
|
-
Topic.find(1).delete
|
288
|
-
assert_not_nil Topic.find(2)
|
289
|
-
end
|
290
|
-
|
291
|
-
def test_destroy
|
292
|
-
topic = Topic.find(1)
|
293
|
-
assert_equal topic, topic.destroy, 'topic.destroy did not return self'
|
294
|
-
assert topic.frozen?, 'topic not frozen after destroy'
|
295
|
-
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
|
296
|
-
end
|
297
|
-
|
298
|
-
def test_record_not_found_exception
|
299
|
-
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(99999) }
|
300
|
-
end
|
301
|
-
|
302
|
-
def test_update_all
|
303
|
-
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
|
304
|
-
assert_equal "bulk updated!", Topic.find(1).content
|
305
|
-
assert_equal "bulk updated!", Topic.find(2).content
|
306
|
-
|
307
|
-
assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!'])
|
308
|
-
assert_equal "bulk updated again!", Topic.find(1).content
|
309
|
-
assert_equal "bulk updated again!", Topic.find(2).content
|
310
|
-
|
311
|
-
assert_equal Topic.count, Topic.update_all(['content = ?', nil])
|
312
|
-
assert_nil Topic.find(1).content
|
313
|
-
end
|
314
|
-
|
315
|
-
def test_update_all_with_hash
|
316
|
-
assert_not_nil Topic.find(1).last_read
|
317
|
-
assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil)
|
318
|
-
assert_equal "bulk updated with hash!", Topic.find(1).content
|
319
|
-
assert_equal "bulk updated with hash!", Topic.find(2).content
|
320
|
-
assert_nil Topic.find(1).last_read
|
321
|
-
assert_nil Topic.find(2).last_read
|
322
|
-
end
|
323
|
-
|
324
|
-
def test_update_all_with_non_standard_table_name
|
325
|
-
assert_equal 1, WarehouseThing.update_all(['value = ?', 0], ['id = ?', 1])
|
326
|
-
assert_equal 0, WarehouseThing.find(1).value
|
327
|
-
end
|
328
|
-
|
329
|
-
def test_delete_new_record
|
330
|
-
client = Client.new
|
331
|
-
client.delete
|
332
|
-
assert client.frozen?
|
333
|
-
end
|
334
|
-
|
335
|
-
def test_delete_record_with_associations
|
336
|
-
client = Client.find(3)
|
337
|
-
client.delete
|
338
|
-
assert client.frozen?
|
339
|
-
assert_kind_of Firm, client.firm
|
340
|
-
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
|
341
|
-
end
|
342
|
-
|
343
|
-
def test_destroy_new_record
|
344
|
-
client = Client.new
|
345
|
-
client.destroy
|
346
|
-
assert client.frozen?
|
347
|
-
end
|
348
|
-
|
349
|
-
def test_destroy_record_with_associations
|
350
|
-
client = Client.find(3)
|
351
|
-
client.destroy
|
352
|
-
assert client.frozen?
|
353
|
-
assert_kind_of Firm, client.firm
|
354
|
-
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
|
355
|
-
end
|
356
|
-
|
357
|
-
def test_update_attribute
|
358
|
-
assert !Topic.find(1).approved?
|
359
|
-
Topic.find(1).update_attribute("approved", true)
|
360
|
-
assert Topic.find(1).approved?
|
361
|
-
|
362
|
-
Topic.find(1).update_attribute(:approved, false)
|
363
|
-
assert !Topic.find(1).approved?
|
364
|
-
end
|
365
|
-
|
366
|
-
def test_update_attribute_does_not_choke_on_nil
|
367
|
-
assert Topic.find(1).update_attributes(nil)
|
368
|
-
end
|
369
|
-
|
370
|
-
def test_update_attribute_for_readonly_attribute
|
371
|
-
minivan = Minivan.find('m1')
|
372
|
-
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
|
373
|
-
end
|
374
|
-
|
375
|
-
# This test is correct, but it is hard to fix it since
|
376
|
-
# update_attribute trigger simply call save! that triggers
|
377
|
-
# all callbacks.
|
378
|
-
# def test_update_attribute_with_one_changed_and_one_updated
|
379
|
-
# t = Topic.order('id').limit(1).first
|
380
|
-
# title, author_name = t.title, t.author_name
|
381
|
-
# t.author_name = 'John'
|
382
|
-
# t.update_attribute(:title, 'super_title')
|
383
|
-
# assert_equal 'John', t.author_name
|
384
|
-
# assert_equal 'super_title', t.title
|
385
|
-
# assert t.changed?, "topic should have changed"
|
386
|
-
# assert t.author_name_changed?, "author_name should have changed"
|
387
|
-
# assert !t.title_changed?, "title should not have changed"
|
388
|
-
# assert_nil t.title_change, 'title change should be nil'
|
389
|
-
# assert_equal ['author_name'], t.changed
|
390
|
-
#
|
391
|
-
# t.reload
|
392
|
-
# assert_equal 'David', t.author_name
|
393
|
-
# assert_equal 'super_title', t.title
|
394
|
-
# end
|
395
|
-
|
396
|
-
def test_update_attribute_with_one_updated
|
397
|
-
t = Topic.first
|
398
|
-
title = t.title
|
399
|
-
t.update_attribute(:title, 'super_title')
|
400
|
-
assert_equal 'super_title', t.title
|
401
|
-
assert !t.changed?, "topic should not have changed"
|
402
|
-
assert !t.title_changed?, "title should not have changed"
|
403
|
-
assert_nil t.title_change, 'title change should be nil'
|
404
|
-
|
405
|
-
t.reload
|
406
|
-
assert_equal 'super_title', t.title
|
407
|
-
end
|
408
|
-
|
409
|
-
def test_update_attribute_for_updated_at_on
|
410
|
-
developer = Developer.find(1)
|
411
|
-
prev_month = Time.now.prev_month
|
412
|
-
|
413
|
-
developer.update_attribute(:updated_at, prev_month)
|
414
|
-
assert_equal prev_month, developer.updated_at
|
415
|
-
|
416
|
-
developer.update_attribute(:salary, 80001)
|
417
|
-
assert_not_equal prev_month, developer.updated_at
|
418
|
-
|
419
|
-
developer.reload
|
420
|
-
assert_not_equal prev_month, developer.updated_at
|
421
|
-
end
|
422
|
-
|
423
|
-
def test_update_column
|
424
|
-
topic = Topic.find(1)
|
425
|
-
topic.update_column("approved", true)
|
426
|
-
assert topic.approved?
|
427
|
-
topic.reload
|
428
|
-
assert topic.approved?
|
429
|
-
|
430
|
-
topic.update_column(:approved, false)
|
431
|
-
assert !topic.approved?
|
432
|
-
topic.reload
|
433
|
-
assert !topic.approved?
|
434
|
-
end
|
435
|
-
|
436
|
-
def test_update_column_should_not_use_setter_method
|
437
|
-
dev = Developer.find(1)
|
438
|
-
dev.instance_eval { def salary=(value); write_attribute(:salary, value * 2); end }
|
439
|
-
|
440
|
-
dev.update_column(:salary, 80000)
|
441
|
-
assert_equal 80000, dev.salary
|
442
|
-
|
443
|
-
dev.reload
|
444
|
-
assert_equal 80000, dev.salary
|
445
|
-
end
|
446
|
-
|
447
|
-
def test_update_column_should_raise_exception_if_new_record
|
448
|
-
topic = Topic.new
|
449
|
-
assert_raises(ActiveRecord::ActiveRecordError) { topic.update_column("approved", false) }
|
450
|
-
end
|
451
|
-
|
452
|
-
def test_update_column_should_not_leave_the_object_dirty
|
453
|
-
topic = Topic.find(1)
|
454
|
-
topic.update_attribute("content", "Have a nice day")
|
455
|
-
|
456
|
-
topic.reload
|
457
|
-
topic.update_column(:content, "You too")
|
458
|
-
assert_equal [], topic.changed
|
459
|
-
|
460
|
-
topic.reload
|
461
|
-
topic.update_column("content", "Have a nice day")
|
462
|
-
assert_equal [], topic.changed
|
463
|
-
end
|
464
|
-
|
465
|
-
def test_update_column_with_model_having_primary_key_other_than_id
|
466
|
-
minivan = Minivan.find('m1')
|
467
|
-
new_name = 'sebavan'
|
468
|
-
|
469
|
-
minivan.update_column(:name, new_name)
|
470
|
-
assert_equal new_name, minivan.name
|
471
|
-
end
|
472
|
-
|
473
|
-
def test_update_column_for_readonly_attribute
|
474
|
-
minivan = Minivan.find('m1')
|
475
|
-
prev_color = minivan.color
|
476
|
-
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_column(:color, 'black') }
|
477
|
-
assert_equal prev_color, minivan.color
|
478
|
-
end
|
479
|
-
|
480
|
-
def test_update_column_should_not_modify_updated_at
|
481
|
-
developer = Developer.find(1)
|
482
|
-
prev_month = Time.now.prev_month
|
483
|
-
|
484
|
-
developer.update_column(:updated_at, prev_month)
|
485
|
-
assert_equal prev_month, developer.updated_at
|
486
|
-
|
487
|
-
developer.update_column(:salary, 80001)
|
488
|
-
assert_equal prev_month, developer.updated_at
|
489
|
-
|
490
|
-
developer.reload
|
491
|
-
assert_equal prev_month.to_i, developer.updated_at.to_i
|
492
|
-
end
|
493
|
-
|
494
|
-
def test_update_column_with_one_changed_and_one_updated
|
495
|
-
t = Topic.order('id').limit(1).first
|
496
|
-
title, author_name = t.title, t.author_name
|
497
|
-
t.author_name = 'John'
|
498
|
-
t.update_column(:title, 'super_title')
|
499
|
-
assert_equal 'John', t.author_name
|
500
|
-
assert_equal 'super_title', t.title
|
501
|
-
assert t.changed?, "topic should have changed"
|
502
|
-
assert t.author_name_changed?, "author_name should have changed"
|
503
|
-
|
504
|
-
t.reload
|
505
|
-
assert_equal author_name, t.author_name
|
506
|
-
assert_equal 'super_title', t.title
|
507
|
-
end
|
508
|
-
|
509
|
-
def test_update_attributes
|
510
|
-
topic = Topic.find(1)
|
511
|
-
assert !topic.approved?
|
512
|
-
assert_equal "The First Topic", topic.title
|
513
|
-
|
514
|
-
topic.update_attributes("approved" => true, "title" => "The First Topic Updated")
|
515
|
-
topic.reload
|
516
|
-
assert topic.approved?
|
517
|
-
assert_equal "The First Topic Updated", topic.title
|
518
|
-
|
519
|
-
topic.update_attributes(:approved => false, :title => "The First Topic")
|
520
|
-
topic.reload
|
521
|
-
assert !topic.approved?
|
522
|
-
assert_equal "The First Topic", topic.title
|
523
|
-
end
|
524
|
-
|
525
|
-
def test_update_attributes_as_admin
|
526
|
-
person = TightPerson.create({ "first_name" => 'Joshua' })
|
527
|
-
person.update_attributes({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :as => :admin)
|
528
|
-
person.reload
|
529
|
-
|
530
|
-
assert_equal 'Josh', person.first_name
|
531
|
-
assert_equal 'm', person.gender
|
532
|
-
assert_equal 'from NZ', person.comments
|
533
|
-
end
|
534
|
-
|
535
|
-
def test_update_attributes_without_protection
|
536
|
-
person = TightPerson.create({ "first_name" => 'Joshua' })
|
537
|
-
person.update_attributes({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :without_protection => true)
|
538
|
-
person.reload
|
539
|
-
|
540
|
-
assert_equal 'Josh', person.first_name
|
541
|
-
assert_equal 'm', person.gender
|
542
|
-
assert_equal 'from NZ', person.comments
|
543
|
-
end
|
544
|
-
|
545
|
-
def test_update_attributes!
|
546
|
-
Reply.validates_presence_of(:title)
|
547
|
-
reply = Reply.find(2)
|
548
|
-
assert_equal "The Second Topic of the day", reply.title
|
549
|
-
assert_equal "Have a nice day", reply.content
|
550
|
-
|
551
|
-
reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening")
|
552
|
-
reply.reload
|
553
|
-
assert_equal "The Second Topic of the day updated", reply.title
|
554
|
-
assert_equal "Have a nice evening", reply.content
|
555
|
-
|
556
|
-
reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day")
|
557
|
-
reply.reload
|
558
|
-
assert_equal "The Second Topic of the day", reply.title
|
559
|
-
assert_equal "Have a nice day", reply.content
|
560
|
-
|
561
|
-
assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") }
|
562
|
-
ensure
|
563
|
-
Reply.reset_callbacks(:validate)
|
564
|
-
end
|
565
|
-
|
566
|
-
def test_update_attributes_with_bang_as_admin
|
567
|
-
person = TightPerson.create({ "first_name" => 'Joshua' })
|
568
|
-
person.update_attributes!({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :as => :admin)
|
569
|
-
person.reload
|
570
|
-
|
571
|
-
assert_equal 'Josh', person.first_name
|
572
|
-
assert_equal 'm', person.gender
|
573
|
-
assert_equal 'from NZ', person.comments
|
574
|
-
end
|
575
|
-
|
576
|
-
def test_update_attributestes_with_bang_without_protection
|
577
|
-
person = TightPerson.create({ "first_name" => 'Joshua' })
|
578
|
-
person.update_attributes!({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :without_protection => true)
|
579
|
-
person.reload
|
580
|
-
|
581
|
-
assert_equal 'Josh', person.first_name
|
582
|
-
assert_equal 'm', person.gender
|
583
|
-
assert_equal 'from NZ', person.comments
|
584
|
-
end
|
585
|
-
|
586
|
-
def test_destroyed_returns_boolean
|
587
|
-
developer = Developer.first
|
588
|
-
assert_equal false, developer.destroyed?
|
589
|
-
developer.destroy
|
590
|
-
assert_equal true, developer.destroyed?
|
591
|
-
|
592
|
-
developer = Developer.last
|
593
|
-
assert_equal false, developer.destroyed?
|
594
|
-
developer.delete
|
595
|
-
assert_equal true, developer.destroyed?
|
596
|
-
end
|
597
|
-
|
598
|
-
def test_persisted_returns_boolean
|
599
|
-
developer = Developer.new(:name => "Jose")
|
600
|
-
assert_equal false, developer.persisted?
|
601
|
-
developer.save!
|
602
|
-
assert_equal true, developer.persisted?
|
603
|
-
|
604
|
-
developer = Developer.first
|
605
|
-
assert_equal true, developer.persisted?
|
606
|
-
developer.destroy
|
607
|
-
assert_equal false, developer.persisted?
|
608
|
-
|
609
|
-
developer = Developer.last
|
610
|
-
assert_equal true, developer.persisted?
|
611
|
-
developer.delete
|
612
|
-
assert_equal false, developer.persisted?
|
613
|
-
end
|
614
|
-
|
615
|
-
def test_class_level_destroy
|
616
|
-
should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
|
617
|
-
Topic.find(1).replies << should_be_destroyed_reply
|
618
|
-
|
619
|
-
Topic.destroy(1)
|
620
|
-
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
|
621
|
-
assert_raise(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) }
|
622
|
-
end
|
623
|
-
|
624
|
-
def test_class_level_delete
|
625
|
-
should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
|
626
|
-
Topic.find(1).replies << should_be_destroyed_reply
|
627
|
-
|
628
|
-
Topic.delete(1)
|
629
|
-
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
|
630
|
-
assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
|
631
|
-
end
|
632
|
-
|
633
|
-
def test_create_with_custom_timestamps
|
634
|
-
custom_datetime = 1.hour.ago.beginning_of_day
|
635
|
-
|
636
|
-
%w(created_at created_on updated_at updated_on).each do |attribute|
|
637
|
-
parrot = LiveParrot.create(:name => "colombian", attribute => custom_datetime)
|
638
|
-
assert_equal custom_datetime, parrot[attribute]
|
639
|
-
end
|
640
|
-
end
|
641
|
-
|
642
|
-
end
|
1
|
+
require "cases/helper"
|
2
|
+
require 'models/post'
|
3
|
+
require 'models/comment'
|
4
|
+
require 'models/author'
|
5
|
+
require 'models/topic'
|
6
|
+
require 'models/reply'
|
7
|
+
require 'models/category'
|
8
|
+
require 'models/company'
|
9
|
+
require 'models/developer'
|
10
|
+
require 'models/project'
|
11
|
+
require 'models/minimalistic'
|
12
|
+
require 'models/warehouse_thing'
|
13
|
+
require 'models/parrot'
|
14
|
+
require 'models/minivan'
|
15
|
+
require 'models/person'
|
16
|
+
require 'rexml/document'
|
17
|
+
require 'active_support/core_ext/exception'
|
18
|
+
|
19
|
+
class PersistencesTest < ActiveRecord::TestCase
|
20
|
+
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics,
|
21
|
+
'warehouse_things', :authors, :categorizations, :categories, :posts, :minivans
|
22
|
+
|
23
|
+
# Skip databases that don't support UPDATE + ORDER BY
|
24
|
+
unless current_adapter?(:OracleAdapter, :PostgreSQLAdapter)
|
25
|
+
def test_update_all_ignores_order_without_limit_from_association
|
26
|
+
author = authors(:david)
|
27
|
+
assert_nothing_raised do
|
28
|
+
assert_equal author.posts_with_comments_and_categories.length,
|
29
|
+
author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_update_all_doesnt_ignore_order
|
34
|
+
assert_equal authors(:david).id + 1, authors(:mary).id # make sure there is going to be a duplicate PK error
|
35
|
+
test_update_with_order_succeeds = lambda do |order|
|
36
|
+
begin
|
37
|
+
Author.order(order).update_all('id = id + 1')
|
38
|
+
rescue ActiveRecord::ActiveRecordError
|
39
|
+
false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
if !current_adapter?(:IBM_DBAdapter) && test_update_with_order_succeeds.call('id DESC')
|
44
|
+
# The update below goes on successfully in DB2.
|
45
|
+
assert !test_update_with_order_succeeds.call('id ASC') # test that this wasn't a fluke and using an incorrect order results in an exception
|
46
|
+
else
|
47
|
+
# test that we're failing because the current Arel's engine doesn't support UPDATE ORDER BY queries is using subselects instead
|
48
|
+
assert_sql(/\AUPDATE .+ \(SELECT .* ORDER BY id DESC\)\Z/i) do
|
49
|
+
test_update_with_order_succeeds.call('id DESC')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_update_all_with_order_and_limit_updates_subset_only
|
55
|
+
author = authors(:david)
|
56
|
+
assert_nothing_raised do
|
57
|
+
assert_equal 1, author.posts_sorted_by_id_limited.size
|
58
|
+
assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size
|
59
|
+
assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ])
|
60
|
+
assert_equal "bulk update!", posts(:welcome).body
|
61
|
+
assert_not_equal "bulk update!", posts(:thinking).body
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_update_many
|
67
|
+
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
|
68
|
+
updated = Topic.update(topic_data.keys, topic_data.values)
|
69
|
+
|
70
|
+
assert_equal 2, updated.size
|
71
|
+
assert_equal "1 updated", Topic.find(1).content
|
72
|
+
assert_equal "2 updated", Topic.find(2).content
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_delete_all
|
76
|
+
assert Topic.count > 0
|
77
|
+
|
78
|
+
assert_equal Topic.count, Topic.delete_all
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_update_by_condition
|
82
|
+
Topic.update_all "content = 'bulk updated!'", ["approved = ?", true]
|
83
|
+
assert_equal "Have a nice day", Topic.find(1).content
|
84
|
+
assert_equal "bulk updated!", Topic.find(2).content
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_increment_attribute
|
88
|
+
assert_equal 50, accounts(:signals37).credit_limit
|
89
|
+
accounts(:signals37).increment! :credit_limit
|
90
|
+
assert_equal 51, accounts(:signals37, :reload).credit_limit
|
91
|
+
|
92
|
+
accounts(:signals37).increment(:credit_limit).increment!(:credit_limit)
|
93
|
+
assert_equal 53, accounts(:signals37, :reload).credit_limit
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_increment_nil_attribute
|
97
|
+
assert_nil topics(:first).parent_id
|
98
|
+
topics(:first).increment! :parent_id
|
99
|
+
assert_equal 1, topics(:first).parent_id
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_increment_attribute_by
|
103
|
+
assert_equal 50, accounts(:signals37).credit_limit
|
104
|
+
accounts(:signals37).increment! :credit_limit, 5
|
105
|
+
assert_equal 55, accounts(:signals37, :reload).credit_limit
|
106
|
+
|
107
|
+
accounts(:signals37).increment(:credit_limit, 1).increment!(:credit_limit, 3)
|
108
|
+
assert_equal 59, accounts(:signals37, :reload).credit_limit
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_destroy_all
|
112
|
+
conditions = "author_name = 'Mary'"
|
113
|
+
topics_by_mary = Topic.all(:conditions => conditions, :order => 'id')
|
114
|
+
assert ! topics_by_mary.empty?
|
115
|
+
|
116
|
+
assert_difference('Topic.count', -topics_by_mary.size) do
|
117
|
+
destroyed = Topic.destroy_all(conditions).sort_by(&:id)
|
118
|
+
assert_equal topics_by_mary, destroyed
|
119
|
+
assert destroyed.all? { |topic| topic.frozen? }, "destroyed topics should be frozen"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_destroy_many
|
124
|
+
clients = Client.find([2, 3], :order => 'id')
|
125
|
+
|
126
|
+
assert_difference('Client.count', -2) do
|
127
|
+
destroyed = Client.destroy([2, 3]).sort_by(&:id)
|
128
|
+
assert_equal clients, destroyed
|
129
|
+
assert destroyed.all? { |client| client.frozen? }, "destroyed clients should be frozen"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_delete_many
|
134
|
+
original_count = Topic.count
|
135
|
+
Topic.delete(deleting = [1, 2])
|
136
|
+
assert_equal original_count - deleting.size, Topic.count
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_decrement_attribute
|
140
|
+
assert_equal 50, accounts(:signals37).credit_limit
|
141
|
+
|
142
|
+
accounts(:signals37).decrement!(:credit_limit)
|
143
|
+
assert_equal 49, accounts(:signals37, :reload).credit_limit
|
144
|
+
|
145
|
+
accounts(:signals37).decrement(:credit_limit).decrement!(:credit_limit)
|
146
|
+
assert_equal 47, accounts(:signals37, :reload).credit_limit
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_decrement_attribute_by
|
150
|
+
assert_equal 50, accounts(:signals37).credit_limit
|
151
|
+
accounts(:signals37).decrement! :credit_limit, 5
|
152
|
+
assert_equal 45, accounts(:signals37, :reload).credit_limit
|
153
|
+
|
154
|
+
accounts(:signals37).decrement(:credit_limit, 1).decrement!(:credit_limit, 3)
|
155
|
+
assert_equal 41, accounts(:signals37, :reload).credit_limit
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_create
|
159
|
+
topic = Topic.new
|
160
|
+
topic.title = "New Topic"
|
161
|
+
topic.save
|
162
|
+
topic_reloaded = Topic.find(topic.id)
|
163
|
+
assert_equal("New Topic", topic_reloaded.title)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_save!
|
167
|
+
topic = Topic.new(:title => "New Topic")
|
168
|
+
assert topic.save!
|
169
|
+
|
170
|
+
reply = WrongReply.new
|
171
|
+
assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_save_null_string_attributes
|
175
|
+
topic = Topic.find(1)
|
176
|
+
topic.attributes = { "title" => "null", "author_name" => "null" }
|
177
|
+
topic.save!
|
178
|
+
topic.reload
|
179
|
+
assert_equal("null", topic.title)
|
180
|
+
assert_equal("null", topic.author_name)
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_save_nil_string_attributes
|
184
|
+
topic = Topic.find(1)
|
185
|
+
topic.title = nil
|
186
|
+
topic.save!
|
187
|
+
topic.reload
|
188
|
+
assert_nil topic.title
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_save_for_record_with_only_primary_key
|
192
|
+
minimalistic = Minimalistic.new
|
193
|
+
assert_nothing_raised { minimalistic.save }
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_save_for_record_with_only_primary_key_that_is_provided
|
197
|
+
assert_nothing_raised { Minimalistic.create!(:id => 2) }
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_create_many
|
201
|
+
topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
|
202
|
+
assert_equal 2, topics.size
|
203
|
+
assert_equal "first", topics.first.title
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_create_columns_not_equal_attributes
|
207
|
+
topic = Topic.allocate.init_with(
|
208
|
+
'attributes' => {
|
209
|
+
'title' => 'Another New Topic',
|
210
|
+
'does_not_exist' => 'test'
|
211
|
+
}
|
212
|
+
)
|
213
|
+
assert_nothing_raised { topic.save }
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_create_through_factory_with_block
|
217
|
+
topic = Topic.create("title" => "New Topic") do |t|
|
218
|
+
t.author_name = "David"
|
219
|
+
end
|
220
|
+
assert_equal("New Topic", topic.title)
|
221
|
+
assert_equal("David", topic.author_name)
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_create_many_through_factory_with_block
|
225
|
+
topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t|
|
226
|
+
t.author_name = "David"
|
227
|
+
end
|
228
|
+
assert_equal 2, topics.size
|
229
|
+
topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id)
|
230
|
+
assert_equal "first", topic1.title
|
231
|
+
assert_equal "David", topic1.author_name
|
232
|
+
assert_equal "second", topic2.title
|
233
|
+
assert_equal "David", topic2.author_name
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_update
|
237
|
+
topic = Topic.new
|
238
|
+
topic.title = "Another New Topic"
|
239
|
+
topic.written_on = "2003-12-12 23:23:00"
|
240
|
+
topic.save
|
241
|
+
topicReloaded = Topic.find(topic.id)
|
242
|
+
assert_equal("Another New Topic", topicReloaded.title)
|
243
|
+
|
244
|
+
topicReloaded.title = "Updated topic"
|
245
|
+
topicReloaded.save
|
246
|
+
|
247
|
+
topicReloadedAgain = Topic.find(topic.id)
|
248
|
+
|
249
|
+
assert_equal("Updated topic", topicReloadedAgain.title)
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_update_columns_not_equal_attributes
|
253
|
+
topic = Topic.new
|
254
|
+
topic.title = "Still another topic"
|
255
|
+
topic.save
|
256
|
+
|
257
|
+
topicReloaded = Topic.allocate
|
258
|
+
topicReloaded.init_with(
|
259
|
+
'attributes' => topic.attributes.merge('does_not_exist' => 'test')
|
260
|
+
)
|
261
|
+
topicReloaded.title = 'A New Topic'
|
262
|
+
assert_nothing_raised { topicReloaded.save }
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_update_for_record_with_only_primary_key
|
266
|
+
minimalistic = minimalistics(:first)
|
267
|
+
assert_nothing_raised { minimalistic.save }
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_update_sti_type
|
271
|
+
assert_instance_of Reply, topics(:second)
|
272
|
+
|
273
|
+
topic = topics(:second).becomes(Topic)
|
274
|
+
assert_instance_of Topic, topic
|
275
|
+
topic.save!
|
276
|
+
assert_instance_of Topic, Topic.find(topic.id)
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_delete
|
280
|
+
topic = Topic.find(1)
|
281
|
+
assert_equal topic, topic.delete, 'topic.delete did not return self'
|
282
|
+
assert topic.frozen?, 'topic not frozen after delete'
|
283
|
+
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_delete_doesnt_run_callbacks
|
287
|
+
Topic.find(1).delete
|
288
|
+
assert_not_nil Topic.find(2)
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_destroy
|
292
|
+
topic = Topic.find(1)
|
293
|
+
assert_equal topic, topic.destroy, 'topic.destroy did not return self'
|
294
|
+
assert topic.frozen?, 'topic not frozen after destroy'
|
295
|
+
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_record_not_found_exception
|
299
|
+
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(99999) }
|
300
|
+
end
|
301
|
+
|
302
|
+
def test_update_all
|
303
|
+
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
|
304
|
+
assert_equal "bulk updated!", Topic.find(1).content
|
305
|
+
assert_equal "bulk updated!", Topic.find(2).content
|
306
|
+
|
307
|
+
assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!'])
|
308
|
+
assert_equal "bulk updated again!", Topic.find(1).content
|
309
|
+
assert_equal "bulk updated again!", Topic.find(2).content
|
310
|
+
|
311
|
+
assert_equal Topic.count, Topic.update_all(['content = ?', nil])
|
312
|
+
assert_nil Topic.find(1).content
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_update_all_with_hash
|
316
|
+
assert_not_nil Topic.find(1).last_read
|
317
|
+
assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil)
|
318
|
+
assert_equal "bulk updated with hash!", Topic.find(1).content
|
319
|
+
assert_equal "bulk updated with hash!", Topic.find(2).content
|
320
|
+
assert_nil Topic.find(1).last_read
|
321
|
+
assert_nil Topic.find(2).last_read
|
322
|
+
end
|
323
|
+
|
324
|
+
def test_update_all_with_non_standard_table_name
|
325
|
+
assert_equal 1, WarehouseThing.update_all(['value = ?', 0], ['id = ?', 1])
|
326
|
+
assert_equal 0, WarehouseThing.find(1).value
|
327
|
+
end
|
328
|
+
|
329
|
+
def test_delete_new_record
|
330
|
+
client = Client.new
|
331
|
+
client.delete
|
332
|
+
assert client.frozen?
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_delete_record_with_associations
|
336
|
+
client = Client.find(3)
|
337
|
+
client.delete
|
338
|
+
assert client.frozen?
|
339
|
+
assert_kind_of Firm, client.firm
|
340
|
+
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
|
341
|
+
end
|
342
|
+
|
343
|
+
def test_destroy_new_record
|
344
|
+
client = Client.new
|
345
|
+
client.destroy
|
346
|
+
assert client.frozen?
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_destroy_record_with_associations
|
350
|
+
client = Client.find(3)
|
351
|
+
client.destroy
|
352
|
+
assert client.frozen?
|
353
|
+
assert_kind_of Firm, client.firm
|
354
|
+
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_update_attribute
|
358
|
+
assert !Topic.find(1).approved?
|
359
|
+
Topic.find(1).update_attribute("approved", true)
|
360
|
+
assert Topic.find(1).approved?
|
361
|
+
|
362
|
+
Topic.find(1).update_attribute(:approved, false)
|
363
|
+
assert !Topic.find(1).approved?
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_update_attribute_does_not_choke_on_nil
|
367
|
+
assert Topic.find(1).update_attributes(nil)
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_update_attribute_for_readonly_attribute
|
371
|
+
minivan = Minivan.find('m1')
|
372
|
+
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
|
373
|
+
end
|
374
|
+
|
375
|
+
# This test is correct, but it is hard to fix it since
|
376
|
+
# update_attribute trigger simply call save! that triggers
|
377
|
+
# all callbacks.
|
378
|
+
# def test_update_attribute_with_one_changed_and_one_updated
|
379
|
+
# t = Topic.order('id').limit(1).first
|
380
|
+
# title, author_name = t.title, t.author_name
|
381
|
+
# t.author_name = 'John'
|
382
|
+
# t.update_attribute(:title, 'super_title')
|
383
|
+
# assert_equal 'John', t.author_name
|
384
|
+
# assert_equal 'super_title', t.title
|
385
|
+
# assert t.changed?, "topic should have changed"
|
386
|
+
# assert t.author_name_changed?, "author_name should have changed"
|
387
|
+
# assert !t.title_changed?, "title should not have changed"
|
388
|
+
# assert_nil t.title_change, 'title change should be nil'
|
389
|
+
# assert_equal ['author_name'], t.changed
|
390
|
+
#
|
391
|
+
# t.reload
|
392
|
+
# assert_equal 'David', t.author_name
|
393
|
+
# assert_equal 'super_title', t.title
|
394
|
+
# end
|
395
|
+
|
396
|
+
def test_update_attribute_with_one_updated
|
397
|
+
t = Topic.first
|
398
|
+
title = t.title
|
399
|
+
t.update_attribute(:title, 'super_title')
|
400
|
+
assert_equal 'super_title', t.title
|
401
|
+
assert !t.changed?, "topic should not have changed"
|
402
|
+
assert !t.title_changed?, "title should not have changed"
|
403
|
+
assert_nil t.title_change, 'title change should be nil'
|
404
|
+
|
405
|
+
t.reload
|
406
|
+
assert_equal 'super_title', t.title
|
407
|
+
end
|
408
|
+
|
409
|
+
def test_update_attribute_for_updated_at_on
|
410
|
+
developer = Developer.find(1)
|
411
|
+
prev_month = Time.now.prev_month
|
412
|
+
|
413
|
+
developer.update_attribute(:updated_at, prev_month)
|
414
|
+
assert_equal prev_month, developer.updated_at
|
415
|
+
|
416
|
+
developer.update_attribute(:salary, 80001)
|
417
|
+
assert_not_equal prev_month, developer.updated_at
|
418
|
+
|
419
|
+
developer.reload
|
420
|
+
assert_not_equal prev_month, developer.updated_at
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_update_column
|
424
|
+
topic = Topic.find(1)
|
425
|
+
topic.update_column("approved", true)
|
426
|
+
assert topic.approved?
|
427
|
+
topic.reload
|
428
|
+
assert topic.approved?
|
429
|
+
|
430
|
+
topic.update_column(:approved, false)
|
431
|
+
assert !topic.approved?
|
432
|
+
topic.reload
|
433
|
+
assert !topic.approved?
|
434
|
+
end
|
435
|
+
|
436
|
+
def test_update_column_should_not_use_setter_method
|
437
|
+
dev = Developer.find(1)
|
438
|
+
dev.instance_eval { def salary=(value); write_attribute(:salary, value * 2); end }
|
439
|
+
|
440
|
+
dev.update_column(:salary, 80000)
|
441
|
+
assert_equal 80000, dev.salary
|
442
|
+
|
443
|
+
dev.reload
|
444
|
+
assert_equal 80000, dev.salary
|
445
|
+
end
|
446
|
+
|
447
|
+
def test_update_column_should_raise_exception_if_new_record
|
448
|
+
topic = Topic.new
|
449
|
+
assert_raises(ActiveRecord::ActiveRecordError) { topic.update_column("approved", false) }
|
450
|
+
end
|
451
|
+
|
452
|
+
def test_update_column_should_not_leave_the_object_dirty
|
453
|
+
topic = Topic.find(1)
|
454
|
+
topic.update_attribute("content", "Have a nice day")
|
455
|
+
|
456
|
+
topic.reload
|
457
|
+
topic.update_column(:content, "You too")
|
458
|
+
assert_equal [], topic.changed
|
459
|
+
|
460
|
+
topic.reload
|
461
|
+
topic.update_column("content", "Have a nice day")
|
462
|
+
assert_equal [], topic.changed
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_update_column_with_model_having_primary_key_other_than_id
|
466
|
+
minivan = Minivan.find('m1')
|
467
|
+
new_name = 'sebavan'
|
468
|
+
|
469
|
+
minivan.update_column(:name, new_name)
|
470
|
+
assert_equal new_name, minivan.name
|
471
|
+
end
|
472
|
+
|
473
|
+
def test_update_column_for_readonly_attribute
|
474
|
+
minivan = Minivan.find('m1')
|
475
|
+
prev_color = minivan.color
|
476
|
+
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_column(:color, 'black') }
|
477
|
+
assert_equal prev_color, minivan.color
|
478
|
+
end
|
479
|
+
|
480
|
+
def test_update_column_should_not_modify_updated_at
|
481
|
+
developer = Developer.find(1)
|
482
|
+
prev_month = Time.now.prev_month
|
483
|
+
|
484
|
+
developer.update_column(:updated_at, prev_month)
|
485
|
+
assert_equal prev_month, developer.updated_at
|
486
|
+
|
487
|
+
developer.update_column(:salary, 80001)
|
488
|
+
assert_equal prev_month, developer.updated_at
|
489
|
+
|
490
|
+
developer.reload
|
491
|
+
assert_equal prev_month.to_i, developer.updated_at.to_i
|
492
|
+
end
|
493
|
+
|
494
|
+
def test_update_column_with_one_changed_and_one_updated
|
495
|
+
t = Topic.order('id').limit(1).first
|
496
|
+
title, author_name = t.title, t.author_name
|
497
|
+
t.author_name = 'John'
|
498
|
+
t.update_column(:title, 'super_title')
|
499
|
+
assert_equal 'John', t.author_name
|
500
|
+
assert_equal 'super_title', t.title
|
501
|
+
assert t.changed?, "topic should have changed"
|
502
|
+
assert t.author_name_changed?, "author_name should have changed"
|
503
|
+
|
504
|
+
t.reload
|
505
|
+
assert_equal author_name, t.author_name
|
506
|
+
assert_equal 'super_title', t.title
|
507
|
+
end
|
508
|
+
|
509
|
+
def test_update_attributes
|
510
|
+
topic = Topic.find(1)
|
511
|
+
assert !topic.approved?
|
512
|
+
assert_equal "The First Topic", topic.title
|
513
|
+
|
514
|
+
topic.update_attributes("approved" => true, "title" => "The First Topic Updated")
|
515
|
+
topic.reload
|
516
|
+
assert topic.approved?
|
517
|
+
assert_equal "The First Topic Updated", topic.title
|
518
|
+
|
519
|
+
topic.update_attributes(:approved => false, :title => "The First Topic")
|
520
|
+
topic.reload
|
521
|
+
assert !topic.approved?
|
522
|
+
assert_equal "The First Topic", topic.title
|
523
|
+
end
|
524
|
+
|
525
|
+
def test_update_attributes_as_admin
|
526
|
+
person = TightPerson.create({ "first_name" => 'Joshua' })
|
527
|
+
person.update_attributes({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :as => :admin)
|
528
|
+
person.reload
|
529
|
+
|
530
|
+
assert_equal 'Josh', person.first_name
|
531
|
+
assert_equal 'm', person.gender
|
532
|
+
assert_equal 'from NZ', person.comments
|
533
|
+
end
|
534
|
+
|
535
|
+
def test_update_attributes_without_protection
|
536
|
+
person = TightPerson.create({ "first_name" => 'Joshua' })
|
537
|
+
person.update_attributes({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :without_protection => true)
|
538
|
+
person.reload
|
539
|
+
|
540
|
+
assert_equal 'Josh', person.first_name
|
541
|
+
assert_equal 'm', person.gender
|
542
|
+
assert_equal 'from NZ', person.comments
|
543
|
+
end
|
544
|
+
|
545
|
+
def test_update_attributes!
|
546
|
+
Reply.validates_presence_of(:title)
|
547
|
+
reply = Reply.find(2)
|
548
|
+
assert_equal "The Second Topic of the day", reply.title
|
549
|
+
assert_equal "Have a nice day", reply.content
|
550
|
+
|
551
|
+
reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening")
|
552
|
+
reply.reload
|
553
|
+
assert_equal "The Second Topic of the day updated", reply.title
|
554
|
+
assert_equal "Have a nice evening", reply.content
|
555
|
+
|
556
|
+
reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day")
|
557
|
+
reply.reload
|
558
|
+
assert_equal "The Second Topic of the day", reply.title
|
559
|
+
assert_equal "Have a nice day", reply.content
|
560
|
+
|
561
|
+
assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") }
|
562
|
+
ensure
|
563
|
+
Reply.reset_callbacks(:validate)
|
564
|
+
end
|
565
|
+
|
566
|
+
def test_update_attributes_with_bang_as_admin
|
567
|
+
person = TightPerson.create({ "first_name" => 'Joshua' })
|
568
|
+
person.update_attributes!({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :as => :admin)
|
569
|
+
person.reload
|
570
|
+
|
571
|
+
assert_equal 'Josh', person.first_name
|
572
|
+
assert_equal 'm', person.gender
|
573
|
+
assert_equal 'from NZ', person.comments
|
574
|
+
end
|
575
|
+
|
576
|
+
def test_update_attributestes_with_bang_without_protection
|
577
|
+
person = TightPerson.create({ "first_name" => 'Joshua' })
|
578
|
+
person.update_attributes!({ "first_name" => 'Josh', "gender" => 'm', "comments" => 'from NZ' }, :without_protection => true)
|
579
|
+
person.reload
|
580
|
+
|
581
|
+
assert_equal 'Josh', person.first_name
|
582
|
+
assert_equal 'm', person.gender
|
583
|
+
assert_equal 'from NZ', person.comments
|
584
|
+
end
|
585
|
+
|
586
|
+
def test_destroyed_returns_boolean
|
587
|
+
developer = Developer.first
|
588
|
+
assert_equal false, developer.destroyed?
|
589
|
+
developer.destroy
|
590
|
+
assert_equal true, developer.destroyed?
|
591
|
+
|
592
|
+
developer = Developer.last
|
593
|
+
assert_equal false, developer.destroyed?
|
594
|
+
developer.delete
|
595
|
+
assert_equal true, developer.destroyed?
|
596
|
+
end
|
597
|
+
|
598
|
+
def test_persisted_returns_boolean
|
599
|
+
developer = Developer.new(:name => "Jose")
|
600
|
+
assert_equal false, developer.persisted?
|
601
|
+
developer.save!
|
602
|
+
assert_equal true, developer.persisted?
|
603
|
+
|
604
|
+
developer = Developer.first
|
605
|
+
assert_equal true, developer.persisted?
|
606
|
+
developer.destroy
|
607
|
+
assert_equal false, developer.persisted?
|
608
|
+
|
609
|
+
developer = Developer.last
|
610
|
+
assert_equal true, developer.persisted?
|
611
|
+
developer.delete
|
612
|
+
assert_equal false, developer.persisted?
|
613
|
+
end
|
614
|
+
|
615
|
+
def test_class_level_destroy
|
616
|
+
should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
|
617
|
+
Topic.find(1).replies << should_be_destroyed_reply
|
618
|
+
|
619
|
+
Topic.destroy(1)
|
620
|
+
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
|
621
|
+
assert_raise(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) }
|
622
|
+
end
|
623
|
+
|
624
|
+
def test_class_level_delete
|
625
|
+
should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
|
626
|
+
Topic.find(1).replies << should_be_destroyed_reply
|
627
|
+
|
628
|
+
Topic.delete(1)
|
629
|
+
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(1) }
|
630
|
+
assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
|
631
|
+
end
|
632
|
+
|
633
|
+
def test_create_with_custom_timestamps
|
634
|
+
custom_datetime = 1.hour.ago.beginning_of_day
|
635
|
+
|
636
|
+
%w(created_at created_on updated_at updated_on).each do |attribute|
|
637
|
+
parrot = LiveParrot.create(:name => "colombian", attribute => custom_datetime)
|
638
|
+
assert_equal custom_datetime, parrot[attribute]
|
639
|
+
end
|
640
|
+
end
|
641
|
+
|
642
|
+
end
|