Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / pk_test.rb
1 require "cases/helper"
2 require 'models/topic'
3 require 'models/reply'
4 require 'models/subscriber'
5 require 'models/movie'
6 require 'models/keyboard'
7 require 'models/mixed_case_monkey'
8
9 class PrimaryKeysTest < ActiveRecord::TestCase
10 fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
11
12 def test_integer_key
13 topic = Topic.find(1)
14 assert_equal(topics(:first).author_name, topic.author_name)
15 topic = Topic.find(2)
16 assert_equal(topics(:second).author_name, topic.author_name)
17
18 topic = Topic.new
19 topic.title = "New Topic"
20 assert_equal(nil, topic.id)
21 assert_nothing_raised { topic.save! }
22 id = topic.id
23
24 topicReloaded = Topic.find(id)
25 assert_equal("New Topic", topicReloaded.title)
26 end
27
28 def test_customized_primary_key_auto_assigns_on_save
29 Keyboard.delete_all
30 keyboard = Keyboard.new(:name => 'HHKB')
31 assert_nothing_raised { keyboard.save! }
32 assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
33 end
34
35 def test_customized_primary_key_can_be_get_before_saving
36 keyboard = Keyboard.new
37 assert_nil keyboard.id
38 assert_nothing_raised { assert_nil keyboard.key_number }
39 end
40
41 def test_customized_string_primary_key_settable_before_save
42 subscriber = Subscriber.new
43 assert_nothing_raised { subscriber.id = 'webster123' }
44 assert_equal 'webster123', subscriber.id
45 assert_equal 'webster123', subscriber.nick
46 end
47
48 def test_string_key
49 subscriber = Subscriber.find(subscribers(:first).nick)
50 assert_equal(subscribers(:first).name, subscriber.name)
51 subscriber = Subscriber.find(subscribers(:second).nick)
52 assert_equal(subscribers(:second).name, subscriber.name)
53
54 subscriber = Subscriber.new
55 subscriber.id = "jdoe"
56 assert_equal("jdoe", subscriber.id)
57 subscriber.name = "John Doe"
58 assert_nothing_raised { subscriber.save! }
59 assert_equal("jdoe", subscriber.id)
60
61 subscriberReloaded = Subscriber.find("jdoe")
62 assert_equal("John Doe", subscriberReloaded.name)
63 end
64
65 def test_find_with_more_than_one_string_key
66 assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
67 end
68
69 def test_primary_key_prefix
70 ActiveRecord::Base.primary_key_prefix_type = :table_name
71 Topic.reset_primary_key
72 assert_equal "topicid", Topic.primary_key
73
74 ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
75 Topic.reset_primary_key
76 assert_equal "topic_id", Topic.primary_key
77
78 ActiveRecord::Base.primary_key_prefix_type = nil
79 Topic.reset_primary_key
80 assert_equal "id", Topic.primary_key
81 end
82
83 def test_delete_should_quote_pkey
84 assert_nothing_raised { MixedCaseMonkey.delete(1) }
85 end
86 def test_update_counters_should_quote_pkey_and_quote_counter_columns
87 assert_nothing_raised { MixedCaseMonkey.update_counters(1, :fleaCount => 99) }
88 end
89 def test_find_with_one_id_should_quote_pkey
90 assert_nothing_raised { MixedCaseMonkey.find(1) }
91 end
92 def test_find_with_multiple_ids_should_quote_pkey
93 assert_nothing_raised { MixedCaseMonkey.find([1,2]) }
94 end
95 def test_instance_update_should_quote_pkey
96 assert_nothing_raised { MixedCaseMonkey.find(1).save }
97 end
98 def test_instance_destroy_should_quote_pkey
99 assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
100 end
101 end