Permalink
Browse files

Add test coverage for customized primary keys including a failing tes…

…t for #2444.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2540 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent de42f26 commit f0b2b637ea9b7133fce95f1fc10eec6b1c7304d9 Marcel Molina committed Oct 12, 2005
@@ -24,4 +24,4 @@ DROP TABLE categories;
DROP TABLE categories_posts;
DROP TABLE fk_test_has_pk;
DROP TABLE fk_test_has_fk;
-
+DROP TABLE keyboards;
@@ -179,6 +179,11 @@ CREATE TABLE categories_posts (
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 (
id INTEGER NOT NULL PRIMARY KEY
);
@@ -24,3 +24,4 @@ DROP TABLE categories;
DROP TABLE categories_posts;
DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk;
+DROP TABLE keyboards;
@@ -191,3 +191,9 @@ CREATE TABLE `fk_test_has_fk` (
FOREIGN KEY (`fk_id`) REFERENCES `fk_test_has_pk`(`id`)
) 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;
drop table posts;
drop table fk_test_has_pk;
drop table fk_test_has_fk;
+drop table keyboards;
drop sequence accounts_seq;
drop sequence companies_nonstd_seq;
drop sequence topics_seq;
@@ -51,3 +52,4 @@ drop sequence categories_seq;
drop sequence categories_posts_seq;
drop sequence fk_test_has_pk_seq;
drop sequence fk_test_has_fk_seq;
+drop sequence keyboards_seq;
@@ -254,3 +254,8 @@ create table fk_test_has_fk (
fk_id integer not null references fk_test_has_fk initially deferred disable
);
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;
DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk;
DROP TABLE geometrics;
+DROP TABLE keyboards;
@@ -218,3 +218,8 @@ CREATE TABLE geometrics (
a_polygon polygon,
a_circle circle
);
+
+CREATE TABLE keyboards (
+ key_number serial primary key,
+ "name" character varying(50)
+);
@@ -24,3 +24,4 @@ DROP TABLE categories;
DROP TABLE categories_posts;
DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk;
+DROP TABLE keyboards;
@@ -176,3 +176,8 @@ CREATE TABLE 'fk_test_has_fk' (
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;
DROP TABLE categories_posts;
DROP TABLE fk_test_has_pd;
DROP TABLE fk_test_has_fk;
+DROP TABLE keyboards;
@@ -176,3 +176,8 @@ CREATE TABLE fk_test_has_fk (
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
+);
@@ -0,0 +1,3 @@
+class Keyboard < ActiveRecord::Base
+ set_primary_key 'key_number'
+end
@@ -1,8 +1,6 @@
class Subscriber < ActiveRecord::Base
- def self.primary_key
- "nick"
- end
+ set_primary_key 'nick'
end
class SpecialSubscriber < Subscriber
-end
+end
@@ -127,7 +127,7 @@ def test_alt_complex_inheritance
def test_inheritance_without_mapping
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
private
@@ -2,6 +2,7 @@
require 'fixtures/topic'
require 'fixtures/subscriber'
require 'fixtures/movie'
+require 'fixtures/keyboard'
class PrimaryKeysTest < Test::Unit::TestCase
fixtures :topics, :subscribers, :movies
@@ -22,6 +23,26 @@ def test_integer_key
assert_equal("New Topic", topicReloaded.title)
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
subscriber = Subscriber.find(subscribers(:first).nick)
assert_equal(subscribers(:first).name, subscriber.name)

0 comments on commit f0b2b63

Please sign in to comment.