提交 f0b2b637 编写于 作者: M Marcel Molina

Add test coverage for customized primary keys including a failing test for #2444.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2540 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 de42f26d
...@@ -24,4 +24,4 @@ DROP TABLE categories; ...@@ -24,4 +24,4 @@ DROP TABLE categories;
DROP TABLE categories_posts; DROP TABLE categories_posts;
DROP TABLE fk_test_has_pk; DROP TABLE fk_test_has_pk;
DROP TABLE fk_test_has_fk; DROP TABLE fk_test_has_fk;
DROP TABLE keyboards;
...@@ -179,6 +179,11 @@ CREATE TABLE categories_posts ( ...@@ -179,6 +179,11 @@ CREATE TABLE categories_posts (
post_id int NOT NULL post_id int NOT NULL
); );
CREATE TABLE keyboards (
key_number int generated by default as identity (start with +10000),
name VARCHAR(255)
);
CREATE TABLE fk_test_has_pk ( CREATE TABLE fk_test_has_pk (
id INTEGER NOT NULL PRIMARY KEY id INTEGER NOT NULL PRIMARY KEY
); );
......
...@@ -24,3 +24,4 @@ DROP TABLE categories; ...@@ -24,3 +24,4 @@ DROP TABLE categories;
DROP TABLE categories_posts; DROP TABLE categories_posts;
DROP TABLE fk_test_has_fk; DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk; DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
...@@ -191,3 +191,9 @@ CREATE TABLE `fk_test_has_fk` ( ...@@ -191,3 +191,9 @@ CREATE TABLE `fk_test_has_fk` (
FOREIGN KEY (`fk_id`) REFERENCES `fk_test_has_pk`(`id`) FOREIGN KEY (`fk_id`) REFERENCES `fk_test_has_pk`(`id`)
) TYPE=InnoDB; ) TYPE=InnoDB;
CREATE TABLE `keyboards` (
`key_number` int(11) NOT NULL auto_increment primary key,
`name` varchar(50) default NULL
);
...@@ -25,6 +25,7 @@ drop table categories; ...@@ -25,6 +25,7 @@ drop table categories;
drop table posts; drop table posts;
drop table fk_test_has_pk; drop table fk_test_has_pk;
drop table fk_test_has_fk; drop table fk_test_has_fk;
drop table keyboards;
drop sequence accounts_seq; drop sequence accounts_seq;
drop sequence companies_nonstd_seq; drop sequence companies_nonstd_seq;
drop sequence topics_seq; drop sequence topics_seq;
...@@ -51,3 +52,4 @@ drop sequence categories_seq; ...@@ -51,3 +52,4 @@ drop sequence categories_seq;
drop sequence categories_posts_seq; drop sequence categories_posts_seq;
drop sequence fk_test_has_pk_seq; drop sequence fk_test_has_pk_seq;
drop sequence fk_test_has_fk_seq; drop sequence fk_test_has_fk_seq;
drop sequence keyboards_seq;
...@@ -254,3 +254,8 @@ create table fk_test_has_fk ( ...@@ -254,3 +254,8 @@ create table fk_test_has_fk (
fk_id integer not null references fk_test_has_fk initially deferred disable fk_id integer not null references fk_test_has_fk initially deferred disable
); );
create sequence fk_test_has_fk_seq minvalue 10000; create sequence fk_test_has_fk_seq minvalue 10000;
create table keyboards (
key_number integer not null,
name varchar(50) default null
);
...@@ -26,3 +26,4 @@ DROP TABLE defaults; ...@@ -26,3 +26,4 @@ DROP TABLE defaults;
DROP TABLE fk_test_has_fk; DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk; DROP TABLE fk_test_has_pk;
DROP TABLE geometrics; DROP TABLE geometrics;
DROP TABLE keyboards;
...@@ -218,3 +218,8 @@ CREATE TABLE geometrics ( ...@@ -218,3 +218,8 @@ CREATE TABLE geometrics (
a_polygon polygon, a_polygon polygon,
a_circle circle a_circle circle
); );
CREATE TABLE keyboards (
key_number serial primary key,
"name" character varying(50)
);
...@@ -24,3 +24,4 @@ DROP TABLE categories; ...@@ -24,3 +24,4 @@ DROP TABLE categories;
DROP TABLE categories_posts; DROP TABLE categories_posts;
DROP TABLE fk_test_has_fk; DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk; DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
...@@ -176,3 +176,8 @@ CREATE TABLE 'fk_test_has_fk' ( ...@@ -176,3 +176,8 @@ CREATE TABLE 'fk_test_has_fk' (
FOREIGN KEY ('fk_id') REFERENCES 'fk_test_has_pk'('id') FOREIGN KEY ('fk_id') REFERENCES 'fk_test_has_pk'('id')
); );
CREATE TABLE 'keyboards' (
'key_number' INTEGER PRIMARY KEY NOT NULL,
'name' VARCHAR(255) DEFAULT NULL
);
...@@ -24,3 +24,4 @@ DROP TABLE categories; ...@@ -24,3 +24,4 @@ DROP TABLE categories;
DROP TABLE categories_posts; DROP TABLE categories_posts;
DROP TABLE fk_test_has_pd; DROP TABLE fk_test_has_pd;
DROP TABLE fk_test_has_fk; DROP TABLE fk_test_has_fk;
DROP TABLE keyboards;
...@@ -176,3 +176,8 @@ CREATE TABLE fk_test_has_fk ( ...@@ -176,3 +176,8 @@ CREATE TABLE fk_test_has_fk (
FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id) FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
); );
CREATE TABLE keyboards (
key_number int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
name varchar(50) default NULL
);
class Keyboard < ActiveRecord::Base
set_primary_key 'key_number'
end
class Subscriber < ActiveRecord::Base class Subscriber < ActiveRecord::Base
def self.primary_key set_primary_key 'nick'
"nick"
end
end end
class SpecialSubscriber < Subscriber class SpecialSubscriber < Subscriber
end end
\ No newline at end of file
...@@ -127,7 +127,7 @@ def test_alt_complex_inheritance ...@@ -127,7 +127,7 @@ def test_alt_complex_inheritance
def test_inheritance_without_mapping def test_inheritance_without_mapping
assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132") assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
assert_nothing_raised { SpecialSubscriber.create("name" => "And breaaaaathe!", "id" => "smartass") } assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save }
end end
private private
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
require 'fixtures/topic' require 'fixtures/topic'
require 'fixtures/subscriber' require 'fixtures/subscriber'
require 'fixtures/movie' require 'fixtures/movie'
require 'fixtures/keyboard'
class PrimaryKeysTest < Test::Unit::TestCase class PrimaryKeysTest < Test::Unit::TestCase
fixtures :topics, :subscribers, :movies fixtures :topics, :subscribers, :movies
...@@ -22,6 +23,26 @@ def test_integer_key ...@@ -22,6 +23,26 @@ def test_integer_key
assert_equal("New Topic", topicReloaded.title) assert_equal("New Topic", topicReloaded.title)
end end
def test_customized_primary_key_auto_assigns_on_save
keyboard = Keyboard.new(:name => 'HHKB')
assert_nothing_raised { keyboard.save }
assert keyboard.id
assert_equal keyboard.id, Keyboard.find_by_name('HHKB').id
end
def test_customized_primary_key_can_be_set_before_saving
keyboard = Keyboard.new
assert_respond_to(keyboard, :key_number)
assert_nothing_raised { keyboard.key_number = 1 }
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
def test_string_key def test_string_key
subscriber = Subscriber.find(subscribers(:first).nick) subscriber = Subscriber.find(subscribers(:first).nick)
assert_equal(subscribers(:first).name, subscriber.name) assert_equal(subscribers(:first).name, subscriber.name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册