-
Notifications
You must be signed in to change notification settings - Fork 21.6k
/
base_test.rb
executable file
·2166 lines (1804 loc) · 74 KB
/
base_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "cases/helper"
require 'models/post'
require 'models/event_author'
require 'models/topic'
require 'models/reply'
require 'models/category'
require 'models/company'
require 'models/customer'
require 'models/developer'
require 'models/project'
require 'models/default'
require 'models/auto_id'
require 'models/column_name'
require 'models/subscriber'
require 'models/keyboard'
require 'models/comment'
require 'models/minimalistic'
require 'models/warehouse_thing'
require 'models/parrot'
require 'rexml/document'
class Category < ActiveRecord::Base; end
class Categorization < ActiveRecord::Base; end
class Smarts < ActiveRecord::Base; end
class CreditCard < ActiveRecord::Base
class PinNumber < ActiveRecord::Base
class CvvCode < ActiveRecord::Base; end
class SubCvvCode < CvvCode; end
end
class SubPinNumber < PinNumber; end
class Brand < Category; end
end
class MasterCreditCard < ActiveRecord::Base; end
class Post < ActiveRecord::Base; end
class Computer < ActiveRecord::Base; end
class NonExistentTable < ActiveRecord::Base; end
class TestOracleDefault < ActiveRecord::Base; end
class LoosePerson < ActiveRecord::Base
self.table_name = 'people'
self.abstract_class = true
attr_protected :credit_rating, :administrator
end
class LooseDescendant < LoosePerson
attr_protected :phone_number
end
class LooseDescendantSecond< LoosePerson
attr_protected :phone_number
attr_protected :name
end
class TightPerson < ActiveRecord::Base
self.table_name = 'people'
attr_accessible :name, :address
end
class TightDescendant < TightPerson
attr_accessible :phone_number
end
class ReadonlyTitlePost < Post
attr_readonly :title
end
class Booleantest < ActiveRecord::Base; end
class Task < ActiveRecord::Base
attr_protected :starting
end
class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord::Base
self.table_name = 'topics'
attr_accessible :author_name
attr_protected :content
end
class BasicsTest < ActiveRecord::TestCase
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts
def test_column_names_are_escaped
conn = ActiveRecord::Base.connection
classname = conn.class.name[/[^:]*$/]
badchar = {
'SQLite3Adapter' => '"',
'MysqlAdapter' => '`',
'Mysql2Adapter' => '`',
'PostgreSQLAdapter' => '"',
'OracleAdapter' => '"',
}.fetch(classname) {
raise "need a bad char for #{classname}"
}
quoted = conn.quote_column_name "foo#{badchar}bar"
assert_equal("#{badchar}foo#{badchar * 2}bar#{badchar}", quoted)
end
def test_table_exists
assert !NonExistentTable.table_exists?
assert Topic.table_exists?
end
def test_set_attributes
topic = Topic.find(1)
topic.attributes = { "title" => "Budget", "author_name" => "Jason" }
topic.save
assert_equal("Budget", topic.title)
assert_equal("Jason", topic.author_name)
assert_equal(topics(:first).author_email_address, Topic.find(1).author_email_address)
end
def test_integers_as_nil
test = AutoId.create('value' => '')
assert_nil AutoId.find(test.id).value
end
def test_set_attributes_with_block
topic = Topic.new do |t|
t.title = "Budget"
t.author_name = "Jason"
end
assert_equal("Budget", topic.title)
assert_equal("Jason", topic.author_name)
end
def test_respond_to?
topic = Topic.find(1)
assert topic.respond_to?("title")
assert topic.respond_to?("title?")
assert topic.respond_to?("title=")
assert topic.respond_to?(:title)
assert topic.respond_to?(:title?)
assert topic.respond_to?(:title=)
assert topic.respond_to?("author_name")
assert topic.respond_to?("attribute_names")
assert !topic.respond_to?("nothingness")
assert !topic.respond_to?(:nothingness)
end
def test_array_content
topic = Topic.new
topic.content = %w( one two three )
topic.save
assert_equal(%w( one two three ), Topic.find(topic.id).content)
end
def test_read_attributes_before_type_cast
category = Category.new({:name=>"Test categoty", :type => nil})
category_attrs = {"name"=>"Test categoty", "type" => nil, "categorizations_count" => nil}
assert_equal category_attrs , category.attributes_before_type_cast
end
if current_adapter?(:MysqlAdapter)
def test_read_attributes_before_type_cast_on_boolean
bool = Booleantest.create({ "value" => false })
assert_equal "0", bool.reload.attributes_before_type_cast["value"]
end
end
def test_read_attributes_before_type_cast_on_datetime
developer = Developer.find(:first)
assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"]
end
def test_hash_content
topic = Topic.new
topic.content = { "one" => 1, "two" => 2 }
topic.save
assert_equal 2, Topic.find(topic.id).content["two"]
topic.content_will_change!
topic.content["three"] = 3
topic.save
assert_equal 3, Topic.find(topic.id).content["three"]
end
def test_update_array_content
topic = Topic.new
topic.content = %w( one two three )
topic.content.push "four"
assert_equal(%w( one two three four ), topic.content)
topic.save
topic = Topic.find(topic.id)
topic.content << "five"
assert_equal(%w( one two three four five ), topic.content)
end
def test_case_sensitive_attributes_hash
# DB2 is not case-sensitive
return true if current_adapter?(:DB2Adapter)
assert_equal @loaded_fixtures['computers']['workstation'].to_hash, Computer.find(:first).attributes
end
def test_create
topic = Topic.new
topic.title = "New Topic"
topic.save
topic_reloaded = Topic.find(topic.id)
assert_equal("New Topic", topic_reloaded.title)
end
def test_save!
topic = Topic.new(:title => "New Topic")
assert topic.save!
reply = Reply.new
assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
end
def test_save_null_string_attributes
topic = Topic.find(1)
topic.attributes = { "title" => "null", "author_name" => "null" }
topic.save!
topic.reload
assert_equal("null", topic.title)
assert_equal("null", topic.author_name)
end
def test_save_nil_string_attributes
topic = Topic.find(1)
topic.title = nil
topic.save!
topic.reload
assert_nil topic.title
end
def test_save_for_record_with_only_primary_key
minimalistic = Minimalistic.new
assert_nothing_raised { minimalistic.save }
end
def test_save_for_record_with_only_primary_key_that_is_provided
assert_nothing_raised { Minimalistic.create!(:id => 2) }
end
def test_hashes_not_mangled
new_topic = { :title => "New Topic" }
new_topic_values = { :title => "AnotherTopic" }
topic = Topic.new(new_topic)
assert_equal new_topic[:title], topic.title
topic.attributes= new_topic_values
assert_equal new_topic_values[:title], topic.title
end
def test_create_many
topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
assert_equal 2, topics.size
assert_equal "first", topics.first.title
end
def test_create_columns_not_equal_attributes
topic = Topic.new
topic.title = 'Another New Topic'
topic.send :write_attribute, 'does_not_exist', 'test'
assert_nothing_raised { topic.save }
end
def test_create_through_factory
topic = Topic.create("title" => "New Topic")
topicReloaded = Topic.find(topic.id)
assert_equal(topic, topicReloaded)
end
def test_create_through_factory_with_block
topic = Topic.create("title" => "New Topic") do |t|
t.author_name = "David"
end
topicReloaded = Topic.find(topic.id)
assert_equal("New Topic", topic.title)
assert_equal("David", topic.author_name)
end
def test_create_many_through_factory_with_block
topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t|
t.author_name = "David"
end
assert_equal 2, topics.size
topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id)
assert_equal "first", topic1.title
assert_equal "David", topic1.author_name
assert_equal "second", topic2.title
assert_equal "David", topic2.author_name
end
def test_update
topic = Topic.new
topic.title = "Another New Topic"
topic.written_on = "2003-12-12 23:23:00"
topic.save
topicReloaded = Topic.find(topic.id)
assert_equal("Another New Topic", topicReloaded.title)
topicReloaded.title = "Updated topic"
topicReloaded.save
topicReloadedAgain = Topic.find(topic.id)
assert_equal("Updated topic", topicReloadedAgain.title)
end
def test_update_columns_not_equal_attributes
topic = Topic.new
topic.title = "Still another topic"
topic.save
topicReloaded = Topic.find(topic.id)
topicReloaded.title = "A New Topic"
topicReloaded.send :write_attribute, 'does_not_exist', 'test'
assert_nothing_raised { topicReloaded.save }
end
def test_update_for_record_with_only_primary_key
minimalistic = minimalistics(:first)
assert_nothing_raised { minimalistic.save }
end
def test_write_attribute
topic = Topic.new
topic.send(:write_attribute, :title, "Still another topic")
assert_equal "Still another topic", topic.title
topic.send(:write_attribute, "title", "Still another topic: part 2")
assert_equal "Still another topic: part 2", topic.title
end
def test_read_attribute
topic = Topic.new
topic.title = "Don't change the topic"
assert_equal "Don't change the topic", topic.send(:read_attribute, "title")
assert_equal "Don't change the topic", topic["title"]
assert_equal "Don't change the topic", topic.send(:read_attribute, :title)
assert_equal "Don't change the topic", topic[:title]
end
def test_read_attribute_when_false
topic = topics(:first)
topic.approved = false
assert !topic.approved?, "approved should be false"
topic.approved = "false"
assert !topic.approved?, "approved should be false"
end
def test_read_attribute_when_true
topic = topics(:first)
topic.approved = true
assert topic.approved?, "approved should be true"
topic.approved = "true"
assert topic.approved?, "approved should be true"
end
def test_read_write_boolean_attribute
topic = Topic.new
# puts ""
# puts "New Topic"
# puts topic.inspect
topic.approved = "false"
# puts "Expecting false"
# puts topic.inspect
assert !topic.approved?, "approved should be false"
topic.approved = "false"
# puts "Expecting false"
# puts topic.inspect
assert !topic.approved?, "approved should be false"
topic.approved = "true"
# puts "Expecting true"
# puts topic.inspect
assert topic.approved?, "approved should be true"
topic.approved = "true"
# puts "Expecting true"
# puts topic.inspect
assert topic.approved?, "approved should be true"
# puts ""
end
def test_query_attribute_string
[nil, "", " "].each do |value|
assert_equal false, Topic.new(:author_name => value).author_name?
end
assert_equal true, Topic.new(:author_name => "Name").author_name?
end
def test_query_attribute_number
[nil, 0, "0"].each do |value|
assert_equal false, Developer.new(:salary => value).salary?
end
assert_equal true, Developer.new(:salary => 1).salary?
assert_equal true, Developer.new(:salary => "1").salary?
end
def test_query_attribute_boolean
[nil, "", false, "false", "f", 0].each do |value|
assert_equal false, Topic.new(:approved => value).approved?
end
[true, "true", "1", 1].each do |value|
assert_equal true, Topic.new(:approved => value).approved?
end
end
def test_query_attribute_with_custom_fields
object = Company.find_by_sql(<<-SQL).first
SELECT c1.*, c2.ruby_type as string_value, c2.rating as int_value
FROM companies c1, companies c2
WHERE c1.firm_id = c2.id
AND c1.id = 2
SQL
assert_equal "Firm", object.string_value
assert object.string_value?
object.string_value = " "
assert !object.string_value?
assert_equal 1, object.int_value.to_i
assert object.int_value?
object.int_value = "0"
assert !object.int_value?
end
def test_reader_for_invalid_column_names
Topic.send(:define_read_method, "mumub-jumbo".to_sym, "mumub-jumbo", nil)
assert !Topic.generated_methods.include?("mumub-jumbo")
end
def test_non_attribute_access_and_assignment
topic = Topic.new
assert !topic.respond_to?("mumbo")
assert_raise(NoMethodError) { topic.mumbo }
assert_raise(NoMethodError) { topic.mumbo = 5 }
end
def test_preserving_date_objects
if current_adapter?(:SybaseAdapter, :OracleAdapter)
# Sybase ctlib does not (yet?) support the date type; use datetime instead.
# Oracle treats all dates/times as Time.
assert_kind_of(
Time, Topic.find(1).last_read,
"The last_read attribute should be of the Time class"
)
else
assert_kind_of(
Date, Topic.find(1).last_read,
"The last_read attribute should be of the Date class"
)
end
end
def test_preserving_time_objects
assert_kind_of(
Time, Topic.find(1).bonus_time,
"The bonus_time attribute should be of the Time class"
)
assert_kind_of(
Time, Topic.find(1).written_on,
"The written_on attribute should be of the Time class"
)
# For adapters which support microsecond resolution.
if current_adapter?(:PostgreSQLAdapter)
assert_equal 11, Topic.find(1).written_on.sec
assert_equal 223300, Topic.find(1).written_on.usec
assert_equal 9900, Topic.find(2).written_on.usec
end
end
def test_custom_mutator
topic = Topic.find(1)
# This mutator is protected in the class definition
topic.send(:approved=, true)
assert topic.instance_variable_get("@custom_approved")
end
def test_delete
topic = Topic.find(1)
assert_equal topic, topic.delete, 'topic.delete did not return self'
assert topic.frozen?, 'topic not frozen after delete'
assert topic.destroyed?, 'topic not marked as being destroyed'
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
end
def test_delete_doesnt_run_callbacks
Topic.find(1).delete
assert_not_nil Topic.find(2)
end
def test_destroy
topic = Topic.find(1)
assert_equal topic, topic.destroy, 'topic.destroy did not return self'
assert topic.frozen?, 'topic not frozen after destroy'
assert topic.destroyed?, 'topic not marked as being destroyed'
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
end
def test_record_not_found_exception
assert_raise(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(99999) }
end
def test_initialize_with_attributes
topic = Topic.new({
"title" => "initialized from attributes", "written_on" => "2003-12-12 23:23"
})
assert_equal("initialized from attributes", topic.title)
end
def test_initialize_with_invalid_attribute
begin
topic = Topic.new({ "title" => "test",
"last_read(1i)" => "2005", "last_read(2i)" => "2", "last_read(3i)" => "31"})
rescue ActiveRecord::MultiparameterAssignmentErrors => ex
assert_equal(1, ex.errors.size)
assert_equal("last_read", ex.errors[0].attribute)
end
end
def test_load
topics = Topic.find(:all, :order => 'id')
assert_equal(4, topics.size)
assert_equal(topics(:first).title, topics.first.title)
end
def test_load_with_condition
topics = Topic.find(:all, :conditions => "author_name = 'Mary'")
assert_equal(1, topics.size)
assert_equal(topics(:second).title, topics.first.title)
end
def test_table_name_guesses
classes = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]
assert_equal "topics", Topic.table_name
assert_equal "categories", Category.table_name
assert_equal "smarts", Smarts.table_name
assert_equal "credit_cards", CreditCard.table_name
assert_equal "credit_card_pin_numbers", CreditCard::PinNumber.table_name
assert_equal "credit_card_pin_number_cvv_codes", CreditCard::PinNumber::CvvCode.table_name
assert_equal "credit_card_pin_numbers", CreditCard::SubPinNumber.table_name
assert_equal "categories", CreditCard::Brand.table_name
assert_equal "master_credit_cards", MasterCreditCard.table_name
ActiveRecord::Base.pluralize_table_names = false
classes.each(&:reset_table_name)
assert_equal "category", Category.table_name
assert_equal "smarts", Smarts.table_name
assert_equal "credit_card", CreditCard.table_name
assert_equal "credit_card_pin_number", CreditCard::PinNumber.table_name
assert_equal "credit_card_pin_number_cvv_code", CreditCard::PinNumber::CvvCode.table_name
assert_equal "credit_card_pin_number", CreditCard::SubPinNumber.table_name
assert_equal "category", CreditCard::Brand.table_name
assert_equal "master_credit_card", MasterCreditCard.table_name
ActiveRecord::Base.pluralize_table_names = true
classes.each(&:reset_table_name)
ActiveRecord::Base.table_name_prefix = "test_"
Category.reset_table_name
assert_equal "test_categories", Category.table_name
ActiveRecord::Base.table_name_suffix = "_test"
Category.reset_table_name
assert_equal "test_categories_test", Category.table_name
ActiveRecord::Base.table_name_prefix = ""
Category.reset_table_name
assert_equal "categories_test", Category.table_name
ActiveRecord::Base.table_name_suffix = ""
Category.reset_table_name
assert_equal "categories", Category.table_name
ActiveRecord::Base.pluralize_table_names = false
ActiveRecord::Base.table_name_prefix = "test_"
Category.reset_table_name
assert_equal "test_category", Category.table_name
ActiveRecord::Base.table_name_suffix = "_test"
Category.reset_table_name
assert_equal "test_category_test", Category.table_name
ActiveRecord::Base.table_name_prefix = ""
Category.reset_table_name
assert_equal "category_test", Category.table_name
ActiveRecord::Base.table_name_suffix = ""
Category.reset_table_name
assert_equal "category", Category.table_name
ActiveRecord::Base.pluralize_table_names = true
classes.each(&:reset_table_name)
end
def test_destroy_all
conditions = "author_name = 'Mary'"
topics_by_mary = Topic.all(:conditions => conditions, :order => 'id')
assert ! topics_by_mary.empty?
assert_difference('Topic.count', -topics_by_mary.size) do
destroyed = Topic.destroy_all(conditions).sort_by(&:id)
assert_equal topics_by_mary, destroyed
assert destroyed.all? { |topic| topic.frozen? }
end
end
def test_destroy_many
clients = Client.find([2, 3], :order => 'id')
assert_difference('Client.count', -2) do
destroyed = Client.destroy([2, 3]).sort_by(&:id)
assert_equal clients, destroyed
assert destroyed.all? { |client| client.frozen? }
end
end
def test_delete_many
original_count = Topic.count
Topic.delete(deleting = [1, 2])
assert_equal original_count - deleting.size, Topic.count
end
def test_boolean_attributes
assert ! Topic.find(1).approved?
assert Topic.find(2).approved?
end
def test_update_all
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
assert_equal "bulk updated!", Topic.find(1).content
assert_equal "bulk updated!", Topic.find(2).content
assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!'])
assert_equal "bulk updated again!", Topic.find(1).content
assert_equal "bulk updated again!", Topic.find(2).content
assert_equal Topic.count, Topic.update_all(['content = ?', nil])
assert_nil Topic.find(1).content
end
def test_update_all_with_hash
assert_not_nil Topic.find(1).last_read
assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil)
assert_equal "bulk updated with hash!", Topic.find(1).content
assert_equal "bulk updated with hash!", Topic.find(2).content
assert_nil Topic.find(1).last_read
assert_nil Topic.find(2).last_read
end
def test_update_all_with_non_standard_table_name
assert_equal 1, WarehouseThing.update_all(['value = ?', 0], ['id = ?', 1])
assert_equal 0, WarehouseThing.find(1).value
end
if current_adapter?(:MysqlAdapter)
def test_update_all_with_order_and_limit
assert_equal 1, Topic.update_all("content = 'bulk updated!'", nil, :limit => 1, :order => 'id DESC')
end
end
def test_update_all_ignores_order_without_limit_from_association
author = authors(:david)
assert_nothing_raised do
assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ])
end
end
def test_update_all_with_order_and_limit_updates_subset_only
author = authors(:david)
assert_nothing_raised do
assert_equal 1, author.posts_sorted_by_id_limited.size
assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size
assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ])
assert_equal "bulk update!", posts(:welcome).body
assert_not_equal "bulk update!", posts(:thinking).body
end
end
def test_update_many
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
updated = Topic.update(topic_data.keys, topic_data.values)
assert_equal 2, updated.size
assert_equal "1 updated", Topic.find(1).content
assert_equal "2 updated", Topic.find(2).content
end
def test_delete_all
assert Topic.count > 0
assert_equal Topic.count, Topic.delete_all
end
def test_update_by_condition
Topic.update_all "content = 'bulk updated!'", ["approved = ?", true]
assert_equal "Have a nice day", Topic.find(1).content
assert_equal "bulk updated!", Topic.find(2).content
end
def test_attribute_present
t = Topic.new
t.title = "hello there!"
t.written_on = Time.now
assert t.attribute_present?("title")
assert t.attribute_present?("written_on")
assert !t.attribute_present?("content")
end
def test_attribute_keys_on_new_instance
t = Topic.new
assert_equal nil, t.title, "The topics table has a title column, so it should be nil"
assert_raise(NoMethodError) { t.title2 }
end
def test_class_name
ActiveSupport::Deprecation.silence do
assert_equal "Firm", ActiveRecord::Base.class_name("firms")
assert_equal "Category", ActiveRecord::Base.class_name("categories")
assert_equal "AccountHolder", ActiveRecord::Base.class_name("account_holder")
ActiveRecord::Base.pluralize_table_names = false
assert_equal "Firms", ActiveRecord::Base.class_name( "firms" )
ActiveRecord::Base.pluralize_table_names = true
ActiveRecord::Base.table_name_prefix = "test_"
assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms" )
ActiveRecord::Base.table_name_suffix = "_tests"
assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms_tests" )
ActiveRecord::Base.table_name_prefix = ""
assert_equal "Firm", ActiveRecord::Base.class_name( "firms_tests" )
ActiveRecord::Base.table_name_suffix = ""
assert_equal "Firm", ActiveRecord::Base.class_name( "firms" )
end
end
def test_null_fields
assert_nil Topic.find(1).parent_id
assert_nil Topic.create("title" => "Hey you").parent_id
end
def test_default_values
topic = Topic.new
assert topic.approved?
assert_nil topic.written_on
assert_nil topic.bonus_time
assert_nil topic.last_read
topic.save
topic = Topic.find(topic.id)
assert topic.approved?
assert_nil topic.last_read
# Oracle has some funky default handling, so it requires a bit of
# extra testing. See ticket #2788.
if current_adapter?(:OracleAdapter)
test = TestOracleDefault.new
assert_equal "X", test.test_char
assert_equal "hello", test.test_string
assert_equal 3, test.test_int
end
end
# Oracle, and Sybase do not have a TIME datatype.
unless current_adapter?(:OracleAdapter, :SybaseAdapter)
def test_utc_as_time_zone
Topic.default_timezone = :utc
attributes = { "bonus_time" => "5:42:00AM" }
topic = Topic.find(1)
topic.attributes = attributes
assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
Topic.default_timezone = :local
end
def test_utc_as_time_zone_and_new
Topic.default_timezone = :utc
attributes = { "bonus_time(1i)"=>"2000",
"bonus_time(2i)"=>"1",
"bonus_time(3i)"=>"1",
"bonus_time(4i)"=>"10",
"bonus_time(5i)"=>"35",
"bonus_time(6i)"=>"50" }
topic = Topic.new(attributes)
assert_equal Time.utc(2000, 1, 1, 10, 35, 50), topic.bonus_time
Topic.default_timezone = :local
end
end
def test_default_values_on_empty_strings
topic = Topic.new
topic.approved = nil
topic.last_read = nil
topic.save
topic = Topic.find(topic.id)
assert_nil topic.last_read
# Sybase adapter does not allow nulls in boolean columns
if current_adapter?(:SybaseAdapter)
assert topic.approved == false
else
assert_nil topic.approved
end
end
def test_equality
assert_equal Topic.find(1), Topic.find(2).topic
end
def test_equality_of_new_records
assert_not_equal Topic.new, Topic.new
end
def test_hashing
assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
end
def test_delete_new_record
client = Client.new
client.delete
assert client.frozen?
end
def test_delete_record_with_associations
client = Client.find(3)
client.delete
assert client.frozen?
assert_kind_of Firm, client.firm
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
end
def test_destroy_new_record
client = Client.new
client.destroy
assert client.frozen?
end
def test_destroy_record_with_associations
client = Client.find(3)
client.destroy
assert client.frozen?
assert_kind_of Firm, client.firm
assert_raise(ActiveSupport::FrozenObjectError) { client.name = "something else" }
end
def test_update_attribute
assert !Topic.find(1).approved?
Topic.find(1).update_attribute("approved", true)
assert Topic.find(1).approved?
Topic.find(1).update_attribute(:approved, false)
assert !Topic.find(1).approved?
end
def test_update_attributes
topic = Topic.find(1)
assert !topic.approved?
assert_equal "The First Topic", topic.title
topic.update_attributes("approved" => true, "title" => "The First Topic Updated")
topic.reload
assert topic.approved?
assert_equal "The First Topic Updated", topic.title
topic.update_attributes(:approved => false, :title => "The First Topic")
topic.reload
assert !topic.approved?
assert_equal "The First Topic", topic.title
end
def test_update_attributes!
reply = Reply.find(2)
assert_equal "The Second Topic of the day", reply.title
assert_equal "Have a nice day", reply.content
reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening")
reply.reload
assert_equal "The Second Topic of the day updated", reply.title
assert_equal "Have a nice evening", reply.content
reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day")
reply.reload
assert_equal "The Second Topic of the day", reply.title
assert_equal "Have a nice day", reply.content
assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") }
end
def test_mass_assignment_should_raise_exception_if_accessible_and_protected_attribute_writers_are_both_used
topic = TopicWithProtectedContentAndAccessibleAuthorName.new
assert_raise(RuntimeError) { topic.attributes = { "author_name" => "me" } }
assert_raise(RuntimeError) { topic.attributes = { "content" => "stuff" } }
end
def test_mass_assignment_protection
firm = Firm.new
firm.attributes = { "name" => "Next Angle", "rating" => 5 }
assert_equal 1, firm.rating
end
def test_mass_assignment_protection_against_class_attribute_writers
[:logger, :configurations, :primary_key_prefix_type, :table_name_prefix, :table_name_suffix, :pluralize_table_names, :colorize_logging,
:default_timezone, :schema_format, :lock_optimistically, :record_timestamps].each do |method|
assert Task.respond_to?(method)
assert Task.respond_to?("#{method}=")
assert Task.new.respond_to?(method)
assert !Task.new.respond_to?("#{method}=")
end
end
def test_customized_primary_key_remains_protected
subscriber = Subscriber.new(:nick => 'webster123', :name => 'nice try')
assert_nil subscriber.id
keyboard = Keyboard.new(:key_number => 9, :name => 'nice try')
assert_nil keyboard.id
end
def test_customized_primary_key_remains_protected_when_referred_to_as_id
subscriber = Subscriber.new(:id => 'webster123', :name => 'nice try')
assert_nil subscriber.id
keyboard = Keyboard.new(:id => 9, :name => 'nice try')
assert_nil keyboard.id
end
def test_mass_assigning_invalid_attribute
firm = Firm.new
assert_raise(ActiveRecord::UnknownAttributeError) do
firm.attributes = { "id" => 5, "type" => "Client", "i_dont_even_exist" => 20 }
end
end
def test_mass_assignment_protection_on_defaults
firm = Firm.new
firm.attributes = { "id" => 5, "type" => "Client" }
assert_nil firm.id
assert_equal "Firm", firm[:type]
end
def test_mass_assignment_accessible
reply = Reply.new("title" => "hello", "content" => "world", "approved" => true)
reply.save
assert reply.approved?
reply.approved = false
reply.save
assert !reply.approved?
end
def test_mass_assignment_protection_inheritance
assert_nil LoosePerson.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator' ]), LoosePerson.protected_attributes
assert_nil LooseDescendant.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number' ]), LooseDescendant.protected_attributes
assert_nil LooseDescendantSecond.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number', 'name' ]), LooseDescendantSecond.protected_attributes, 'Running attr_protected twice in one class should merge the protections'
assert_nil TightPerson.protected_attributes
assert_equal Set.new([ 'name', 'address' ]), TightPerson.accessible_attributes
assert_nil TightDescendant.protected_attributes
assert_equal Set.new([ 'name', 'address', 'phone_number' ]), TightDescendant.accessible_attributes
end
def test_readonly_attributes
assert_equal Set.new([ 'title' , 'comments_count' ]), ReadonlyTitlePost.readonly_attributes
post = ReadonlyTitlePost.create(:title => "cannot change this", :body => "changeable")
post.reload
assert_equal "cannot change this", post.title
post.update_attributes(:title => "try to change", :body => "changed")
post.reload
assert_equal "cannot change this", post.title
assert_equal "changed", post.body
end
def test_multiparameter_attribute_assignment_via_association_proxy
multiparameter_date_attribute = {
"ends_on(1i)" => "2004", "ends_on(2i)" => "6", "ends_on(3i)" => "24",
"ends_on(4i)" => "16", "ends_on(5i)" => "24", "ends_on(6i)" => "00"
}