2005-10-24 12:45:22 -04:00
|
|
|
require "#{File.dirname(__FILE__)}/abstract_unit"
|
2004-11-23 20:04:44 -05:00
|
|
|
require 'fixtures/topic'
|
|
|
|
require 'fixtures/subscriber'
|
|
|
|
require 'fixtures/movie'
|
2005-10-12 15:55:46 -04:00
|
|
|
require 'fixtures/keyboard'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
class PrimaryKeysTest < Test::Unit::TestCase
|
2005-06-10 10:58:02 -04:00
|
|
|
fixtures :topics, :subscribers, :movies
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def test_integer_key
|
|
|
|
topic = Topic.find(1)
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal(topics(:first).author_name, topic.author_name)
|
2004-11-23 20:04:44 -05:00
|
|
|
topic = Topic.find(2)
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal(topics(:second).author_name, topic.author_name)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
topic = Topic.new
|
|
|
|
topic.title = "New Topic"
|
|
|
|
assert_equal(nil, topic.id)
|
2005-10-24 12:45:22 -04:00
|
|
|
assert_nothing_raised { topic.save! }
|
2004-11-23 20:04:44 -05:00
|
|
|
id = topic.id
|
|
|
|
|
|
|
|
topicReloaded = Topic.find(id)
|
|
|
|
assert_equal("New Topic", topicReloaded.title)
|
|
|
|
end
|
|
|
|
|
2005-10-12 15:55:46 -04:00
|
|
|
def test_customized_primary_key_auto_assigns_on_save
|
2005-10-25 14:14:09 -04:00
|
|
|
Keyboard.delete_all
|
2005-10-12 15:55:46 -04:00
|
|
|
keyboard = Keyboard.new(:name => 'HHKB')
|
2005-10-24 12:45:22 -04:00
|
|
|
assert_nothing_raised { keyboard.save! }
|
2005-10-12 15:55:46 -04:00
|
|
|
assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
|
|
|
|
end
|
|
|
|
|
2005-10-13 16:44:39 -04:00
|
|
|
def test_customized_primary_key_can_be_get_before_saving
|
2005-10-12 15:55:46 -04:00
|
|
|
keyboard = Keyboard.new
|
2005-10-24 12:45:22 -04:00
|
|
|
assert_nil keyboard.id
|
|
|
|
assert_nothing_raised { assert_nil keyboard.key_number }
|
2005-10-12 15:55:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_customized_string_primary_key_settable_before_save
|
|
|
|
subscriber = Subscriber.new
|
|
|
|
assert_nothing_raised { subscriber.id = 'webster123' }
|
|
|
|
assert_equal 'webster123', subscriber.id
|
|
|
|
assert_equal 'webster123', subscriber.nick
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_string_key
|
2005-06-10 10:58:02 -04:00
|
|
|
subscriber = Subscriber.find(subscribers(:first).nick)
|
|
|
|
assert_equal(subscribers(:first).name, subscriber.name)
|
|
|
|
subscriber = Subscriber.find(subscribers(:second).nick)
|
|
|
|
assert_equal(subscribers(:second).name, subscriber.name)
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
subscriber = Subscriber.new
|
|
|
|
subscriber.id = "jdoe"
|
|
|
|
assert_equal("jdoe", subscriber.id)
|
|
|
|
subscriber.name = "John Doe"
|
2005-10-15 23:45:39 -04:00
|
|
|
assert_nothing_raised { subscriber.save! }
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
subscriberReloaded = Subscriber.find("jdoe")
|
|
|
|
assert_equal("John Doe", subscriberReloaded.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_with_more_than_one_string_key
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal 2, Subscriber.find(subscribers(:first).nick, subscribers(:second).nick).length
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_primary_key_prefix
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = :table_name
|
2005-10-06 19:19:55 -04:00
|
|
|
Topic.reset_primary_key
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "topicid", Topic.primary_key
|
|
|
|
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
|
2005-10-06 19:19:55 -04:00
|
|
|
Topic.reset_primary_key
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "topic_id", Topic.primary_key
|
|
|
|
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = nil
|
2005-10-06 19:19:55 -04:00
|
|
|
Topic.reset_primary_key
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "id", Topic.primary_key
|
|
|
|
end
|
|
|
|
end
|