Skip to content

Commit

Permalink
Add test coverage for customized primary keys including a failing tes…
Browse files Browse the repository at this point in the history
…t for #2444.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2540 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
Marcel Molina committed Oct 12, 2005
1 parent de42f26 commit f0b2b63
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion activerecord/test/fixtures/db_definitions/db2.drop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/db2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/mysql.drop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions activerecord/test/fixtures/db_definitions/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
2 changes: 2 additions & 0 deletions activerecord/test/fixtures/db_definitions/oci.drop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/oci.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
1 change: 1 addition & 0 deletions activerecord/test/fixtures/db_definitions/sqlite.drop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/db_definitions/sqlserver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/keyboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Keyboard < ActiveRecord::Base
set_primary_key 'key_number'
end
6 changes: 2 additions & 4 deletions activerecord/test/fixtures/subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class Subscriber < ActiveRecord::Base
def self.primary_key
"nick"
end
set_primary_key 'nick'
end

class SpecialSubscriber < Subscriber
end
end
2 changes: 1 addition & 1 deletion activerecord/test/inheritance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions activerecord/test/pk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit f0b2b63

Please sign in to comment.