aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMarcel Molina <marcel@vernix.org>2005-10-12 19:55:46 +0000
committerMarcel Molina <marcel@vernix.org>2005-10-12 19:55:46 +0000
commitf0b2b637ea9b7133fce95f1fc10eec6b1c7304d9 (patch)
tree9de4101cc210b7e8dbbb4b1d1c3e0dbebeef3fce /activerecord
parentde42f26df787af135baef65f59aa145873714aa5 (diff)
downloadrails-f0b2b637ea9b7133fce95f1fc10eec6b1c7304d9.tar.gz
rails-f0b2b637ea9b7133fce95f1fc10eec6b1c7304d9.tar.bz2
rails-f0b2b637ea9b7133fce95f1fc10eec6b1c7304d9.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/test/fixtures/db_definitions/db2.drop.sql2
-rw-r--r--activerecord/test/fixtures/db_definitions/db2.sql5
-rw-r--r--activerecord/test/fixtures/db_definitions/mysql.drop.sql1
-rwxr-xr-xactiverecord/test/fixtures/db_definitions/mysql.sql6
-rw-r--r--activerecord/test/fixtures/db_definitions/oci.drop.sql2
-rw-r--r--activerecord/test/fixtures/db_definitions/oci.sql5
-rw-r--r--activerecord/test/fixtures/db_definitions/postgresql.drop.sql1
-rw-r--r--activerecord/test/fixtures/db_definitions/postgresql.sql5
-rw-r--r--activerecord/test/fixtures/db_definitions/sqlite.drop.sql1
-rw-r--r--activerecord/test/fixtures/db_definitions/sqlite.sql5
-rw-r--r--activerecord/test/fixtures/db_definitions/sqlserver.drop.sql1
-rw-r--r--activerecord/test/fixtures/db_definitions/sqlserver.sql5
-rw-r--r--activerecord/test/fixtures/keyboard.rb3
-rw-r--r--activerecord/test/fixtures/subscriber.rb6
-rwxr-xr-xactiverecord/test/inheritance_test.rb2
-rw-r--r--activerecord/test/pk_test.rb21
16 files changed, 65 insertions, 6 deletions
diff --git a/activerecord/test/fixtures/db_definitions/db2.drop.sql b/activerecord/test/fixtures/db_definitions/db2.drop.sql
index 5937b09515..07bffb6019 100644
--- a/activerecord/test/fixtures/db_definitions/db2.drop.sql
+++ b/activerecord/test/fixtures/db_definitions/db2.drop.sql
@@ -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;
diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql
index 9c41f7eede..49f31aba67 100644
--- a/activerecord/test/fixtures/db_definitions/db2.sql
+++ b/activerecord/test/fixtures/db_definitions/db2.sql
@@ -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
);
diff --git a/activerecord/test/fixtures/db_definitions/mysql.drop.sql b/activerecord/test/fixtures/db_definitions/mysql.drop.sql
index b4be3c625e..8a37e2deae 100644
--- a/activerecord/test/fixtures/db_definitions/mysql.drop.sql
+++ b/activerecord/test/fixtures/db_definitions/mysql.drop.sql
@@ -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;
diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql
index 393a6e808e..d9b98042ae 100755
--- a/activerecord/test/fixtures/db_definitions/mysql.sql
+++ b/activerecord/test/fixtures/db_definitions/mysql.sql
@@ -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
+);
diff --git a/activerecord/test/fixtures/db_definitions/oci.drop.sql b/activerecord/test/fixtures/db_definitions/oci.drop.sql
index 08534b8d92..5a3a663703 100644
--- a/activerecord/test/fixtures/db_definitions/oci.drop.sql
+++ b/activerecord/test/fixtures/db_definitions/oci.drop.sql
@@ -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;
diff --git a/activerecord/test/fixtures/db_definitions/oci.sql b/activerecord/test/fixtures/db_definitions/oci.sql
index f6e958d7c2..8a8dcd0681 100644
--- a/activerecord/test/fixtures/db_definitions/oci.sql
+++ b/activerecord/test/fixtures/db_definitions/oci.sql
@@ -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
+);
diff --git a/activerecord/test/fixtures/db_definitions/postgresql.drop.sql b/activerecord/test/fixtures/db_definitions/postgresql.drop.sql
index 3a883e973f..d8d41cf974 100644
--- a/activerecord/test/fixtures/db_definitions/postgresql.drop.sql
+++ b/activerecord/test/fixtures/db_definitions/postgresql.drop.sql
@@ -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;
diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql
index 89cc89ab78..feea20b827 100644
--- a/activerecord/test/fixtures/db_definitions/postgresql.sql
+++ b/activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -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)
+);
diff --git a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql
index b4be3c625e..8a37e2deae 100644
--- a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql
+++ b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql
@@ -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;
diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql
index ebc5cb3a37..2e643f5930 100644
--- a/activerecord/test/fixtures/db_definitions/sqlite.sql
+++ b/activerecord/test/fixtures/db_definitions/sqlite.sql
@@ -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
+);
diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
index 242f8d464c..4d78d6fda1 100644
--- a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
+++ b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
@@ -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;
diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql
index e43b7db1c6..e27364091f 100644
--- a/activerecord/test/fixtures/db_definitions/sqlserver.sql
+++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql
@@ -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
+);
diff --git a/activerecord/test/fixtures/keyboard.rb b/activerecord/test/fixtures/keyboard.rb
new file mode 100644
index 0000000000..32a4a7fad0
--- /dev/null
+++ b/activerecord/test/fixtures/keyboard.rb
@@ -0,0 +1,3 @@
+class Keyboard < ActiveRecord::Base
+ set_primary_key 'key_number'
+end
diff --git a/activerecord/test/fixtures/subscriber.rb b/activerecord/test/fixtures/subscriber.rb
index e5e11724fd..51335a8f20 100644
--- a/activerecord/test/fixtures/subscriber.rb
+++ b/activerecord/test/fixtures/subscriber.rb
@@ -1,8 +1,6 @@
class Subscriber < ActiveRecord::Base
- def self.primary_key
- "nick"
- end
+ set_primary_key 'nick'
end
class SpecialSubscriber < Subscriber
-end \ No newline at end of file
+end
diff --git a/activerecord/test/inheritance_test.rb b/activerecord/test/inheritance_test.rb
index 7c309a7ba3..11a2c3d7a9 100755
--- a/activerecord/test/inheritance_test.rb
+++ b/activerecord/test/inheritance_test.rb
@@ -127,7 +127,7 @@ class InheritanceTest < Test::Unit::TestCase
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
diff --git a/activerecord/test/pk_test.rb b/activerecord/test/pk_test.rb
index fb07194743..24f9bc4097 100644
--- a/activerecord/test/pk_test.rb
+++ b/activerecord/test/pk_test.rb
@@ -2,6 +2,7 @@ require 'abstract_unit'
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 @@ class PrimaryKeysTest < Test::Unit::TestCase
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)