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'
|
2006-04-27 18:39:45 -04:00
|
|
|
require 'fixtures/reply'
|
2004-11-23 20:04:44 -05:00
|
|
|
require 'fixtures/subscriber'
|
|
|
|
require 'fixtures/movie'
|
2005-10-12 15:55:46 -04:00
|
|
|
require 'fixtures/keyboard'
|
2007-03-08 22:23:37 -05:00
|
|
|
require 'fixtures/mixed_case_monkey'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
class PrimaryKeysTest < Test::Unit::TestCase
|
2007-03-08 22:23:37 -05:00
|
|
|
fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
|
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! }
|
2005-11-14 17:24:55 -05:00
|
|
|
assert_equal("jdoe", subscriber.id)
|
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
|
2007-03-08 22:23:37 -05:00
|
|
|
|
|
|
|
def test_delete_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.delete(1) }
|
|
|
|
end
|
|
|
|
def test_update_counters_should_quote_pkey_and_quote_counter_columns
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.update_counters(1, :fleaCount => 99) }
|
|
|
|
end
|
|
|
|
def test_find_with_one_id_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.find(1) }
|
|
|
|
end
|
|
|
|
def test_find_with_multiple_ids_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.find([1,2]) }
|
|
|
|
end
|
|
|
|
def test_instance_update_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.find(1).save }
|
|
|
|
end
|
|
|
|
def test_instance_destry_should_quote_pkey
|
|
|
|
assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|