4
require 'models/subscriber'
6
require 'models/keyboard'
7
require 'models/mixed_case_monkey'
9
class PrimaryKeysTest < ActiveRecord::TestCase
10
fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
14
assert_equal(topics(:first).author_name, topic.author_name)
16
assert_equal(topics(:second).author_name, topic.author_name)
19
topic.title = "New Topic"
20
assert_equal(nil, topic.id)
21
assert_nothing_raised { topic.save! }
24
topicReloaded = Topic.find(id)
25
assert_equal("New Topic", topicReloaded.title)
28
def test_customized_primary_key_auto_assigns_on_save
30
keyboard = Keyboard.new(:name => 'HHKB')
31
assert_nothing_raised { keyboard.save! }
32
assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
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 }
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
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)
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)
61
subscriberReloaded = Subscriber.find("jdoe")
62
assert_equal("John Doe", subscriberReloaded.name)
65
def test_find_with_more_than_one_string_key
66
assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
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
74
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
75
Topic.reset_primary_key
76
assert_equal "topic_id", Topic.primary_key
78
ActiveRecord::Base.primary_key_prefix_type = nil
79
Topic.reset_primary_key
80
assert_equal "id", Topic.primary_key
83
def test_delete_should_quote_pkey
84
assert_nothing_raised { MixedCaseMonkey.delete(1) }
86
def test_update_counters_should_quote_pkey_and_quote_counter_columns
87
assert_nothing_raised { MixedCaseMonkey.update_counters(1, :fleaCount => 99) }
89
def test_find_with_one_id_should_quote_pkey
90
assert_nothing_raised { MixedCaseMonkey.find(1) }
92
def test_find_with_multiple_ids_should_quote_pkey
93
assert_nothing_raised { MixedCaseMonkey.find([1,2]) }
95
def test_instance_update_should_quote_pkey
96
assert_nothing_raised { MixedCaseMonkey.find(1).save }
98
def test_instance_destroy_should_quote_pkey
99
assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
102
def test_supports_primary_key
103
assert_nothing_raised NoMethodError do
104
ActiveRecord::Base.connection.supports_primary_key?
108
def test_primary_key_returns_value_if_it_exists
109
if ActiveRecord::Base.connection.supports_primary_key?
110
assert_equal 'id', ActiveRecord::Base.connection.primary_key('developers')
114
def test_primary_key_returns_nil_if_it_does_not_exist
115
if ActiveRecord::Base.connection.supports_primary_key?
116
assert_nil ActiveRecord::Base.connection.primary_key('developers_projects')