2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/topic'
|
|
|
|
require 'models/reply'
|
|
|
|
require 'models/subscriber'
|
|
|
|
require 'models/movie'
|
|
|
|
require 'models/keyboard'
|
|
|
|
require 'models/mixed_case_monkey'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class PrimaryKeysTest < ActiveRecord::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
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
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
|
2008-01-18 02:30:42 -05:00
|
|
|
|
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
|
2007-09-28 10:18:47 -04:00
|
|
|
def test_instance_destroy_should_quote_pkey
|
2007-03-08 22:23:37 -05:00
|
|
|
assert_nothing_raised { MixedCaseMonkey.find(1).destroy }
|
|
|
|
end
|
2009-02-26 10:48:58 -05:00
|
|
|
|
|
|
|
def test_supports_primary_key
|
|
|
|
assert_nothing_raised NoMethodError do
|
|
|
|
ActiveRecord::Base.connection.supports_primary_key?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_primary_key_returns_value_if_it_exists
|
|
|
|
if ActiveRecord::Base.connection.supports_primary_key?
|
|
|
|
assert_equal 'id', ActiveRecord::Base.connection.primary_key('developers')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_primary_key_returns_nil_if_it_does_not_exist
|
|
|
|
if ActiveRecord::Base.connection.supports_primary_key?
|
|
|
|
assert_nil ActiveRecord::Base.connection.primary_key('developers_projects')
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|