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
|
|
|
|
2010-02-20 21:05:28 -05:00
|
|
|
def test_to_key_with_default_primary_key
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
topic = Topic.new
|
2010-08-16 21:29:57 -04:00
|
|
|
assert_nil topic.to_key
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
topic = Topic.find(1)
|
2010-07-16 22:24:18 -04:00
|
|
|
assert_equal [1], topic.to_key
|
2010-02-20 21:05:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_key_with_customized_primary_key
|
|
|
|
keyboard = Keyboard.new
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_nil keyboard.to_key
|
2010-02-20 21:05:28 -05:00
|
|
|
keyboard.save
|
|
|
|
assert_equal keyboard.to_key, [keyboard.id]
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
end
|
|
|
|
|
2010-03-29 19:04:34 -04:00
|
|
|
def test_to_key_with_primary_key_after_destroy
|
|
|
|
topic = Topic.find(1)
|
|
|
|
topic.destroy
|
2010-08-15 10:33:21 -04:00
|
|
|
assert_equal [1], topic.to_key
|
2010-03-29 19:04:34 -04:00
|
|
|
end
|
|
|
|
|
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"
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_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
|