aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/extension_test.rb5
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb40
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb12
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb15
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb31
-rw-r--r--activerecord/test/cases/base_test.rb46
-rw-r--r--activerecord/test/cases/coders/yaml_column_test.rb45
-rw-r--r--activerecord/test/cases/column_definition_test.rb206
-rw-r--r--activerecord/test/cases/migration/command_recorder_test.rb2
-rw-r--r--activerecord/test/cases/migration_test.rb140
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb9
-rw-r--r--activerecord/test/cases/serialization_test.rb6
-rw-r--r--activerecord/test/cases/session_store/session_test.rb1
-rw-r--r--activerecord/test/cases/timestamp_test.rb30
-rw-r--r--activerecord/test/models/bulb.rb11
-rw-r--r--activerecord/test/models/club.rb3
-rw-r--r--activerecord/test/models/comment.rb1
-rw-r--r--activerecord/test/models/contact.rb4
-rw-r--r--activerecord/test/models/member.rb3
-rw-r--r--activerecord/test/models/pirate.rb2
20 files changed, 496 insertions, 116 deletions
diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb
index efaab8569e..e1f5b16eca 100644
--- a/activerecord/test/cases/associations/extension_test.rb
+++ b/activerecord/test/cases/associations/extension_test.rb
@@ -29,6 +29,11 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase
assert_equal projects(:action_controller), developers(:david).projects_extended_by_name_and_block.find_most_recent
assert_equal projects(:active_record), developers(:david).projects_extended_by_name_and_block.find_least_recent
end
+
+ def test_extension_with_scopes
+ assert_equal comments(:greetings), posts(:welcome).comments.offset(1).find_most_recent
+ assert_equal comments(:greetings), posts(:welcome).comments.not_again.find_most_recent
+ end
def test_marshalling_extensions
david = developers(:david)
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 1ce91d7211..e36124a055 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -43,7 +43,7 @@ end
class HasManyAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :categories, :companies, :developers, :projects,
:developers_projects, :topics, :authors, :comments,
- :people, :posts, :readers, :taggings
+ :people, :posts, :readers, :taggings, :cars
def setup
Client.destroyed_client_ids.clear
@@ -66,14 +66,36 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 'exotic', bulb.name
end
+ # When creating objects on the association, we must not do it within a scope (even though it
+ # would be convenient), because this would cause that scope to be applied to any callbacks etc.
+ def test_build_and_create_should_not_happen_within_scope
+ car = cars(:honda)
+ original_scoped_methods = Bulb.scoped_methods
+
+ bulb = car.bulbs.build
+ assert_equal original_scoped_methods, bulb.scoped_methods_after_initialize
+
+ bulb = car.bulbs.create
+ assert_equal original_scoped_methods, bulb.scoped_methods_after_initialize
+
+ bulb = car.bulbs.create!
+ assert_equal original_scoped_methods, bulb.scoped_methods_after_initialize
+ end
+
def test_no_sql_should_be_fired_if_association_already_loaded
Car.create(:name => 'honda')
bulbs = Car.first.bulbs
bulbs.inspect # to load all instances of bulbs
+
assert_no_queries do
bulbs.first()
bulbs.first({})
end
+
+ assert_no_queries do
+ bulbs.last()
+ bulbs.last({})
+ end
end
def test_create_resets_cached_counters
@@ -640,8 +662,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_delete_all
force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
- assert_equal 2, companies(:first_firm).clients_of_firm.size
- companies(:first_firm).clients_of_firm.delete_all
+ clients = companies(:first_firm).clients_of_firm.to_a
+ assert_equal 2, clients.count
+ deleted = companies(:first_firm).clients_of_firm.delete_all
+ assert_equal clients.sort_by(&:id), deleted.sort_by(&:id)
assert_equal 0, companies(:first_firm).clients_of_firm.size
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
end
@@ -661,11 +685,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
client_id = firm.clients_of_firm.first.id
assert_equal 1, firm.clients_of_firm.size
- firm.clients_of_firm.clear
+ cleared = firm.clients_of_firm.clear
assert_equal 0, firm.clients_of_firm.size
assert_equal 0, firm.clients_of_firm(true).size
assert_equal [], Client.destroyed_client_ids[firm.id]
+ assert_equal firm.clients_of_firm.object_id, cleared.object_id
# Should not be destroyed since the association is not dependent.
assert_nothing_raised do
@@ -1327,4 +1352,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal reply.id, first.id
assert_equal true, first.approved?
end
+
+ def test_to_a_should_dup_target
+ ary = topics(:first).replies.to_a
+ target = topics(:first).replies.target
+
+ assert_not_equal target.object_id, ary.object_id
+ end
end
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 7235631b5a..96f4597726 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -20,10 +20,13 @@ require 'models/subscription'
require 'models/categorization'
require 'models/category'
require 'models/essay'
+require 'models/member'
+require 'models/membership'
+require 'models/club'
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors, :categories,
- :owners, :pets, :toys, :jobs, :references, :companies,
+ :owners, :pets, :toys, :jobs, :references, :companies, :members,
:subscribers, :books, :subscriptions, :developers, :categorizations
# Dummies to force column loads so query counts are clean.
@@ -557,4 +560,11 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert proxy.stale_target?
assert_equal authors(:david).categorizations.sort_by(&:id), post.author_categorizations.sort_by(&:id)
end
+
+ def test_create_with_conditions_hash_on_through_association
+ member = members(:groucho)
+ club = member.clubs.create!
+
+ assert_equal true, club.reload.membership.favourite
+ end
end
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index d9b6694dd8..dbf6dfe20d 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -4,6 +4,7 @@ require 'models/project'
require 'models/company'
require 'models/ship'
require 'models/pirate'
+require 'models/bulb'
class HasOneAssociationsTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false unless supports_savepoints?
@@ -167,6 +168,20 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal account, firm.account
end
+ def test_build_and_create_should_not_happen_within_scope
+ pirate = pirates(:blackbeard)
+ original_scoped_methods = Bulb.scoped_methods.dup
+
+ bulb = pirate.build_bulb
+ assert_equal original_scoped_methods, bulb.scoped_methods_after_initialize
+
+ bulb = pirate.create_bulb
+ assert_equal original_scoped_methods, bulb.scoped_methods_after_initialize
+
+ bulb = pirate.create_bulb!
+ assert_equal original_scoped_methods, bulb.scoped_methods_after_initialize
+ end
+
def test_create_association
firm = Firm.create(:name => "GlobalMegaCorp")
account = firm.create_account(:credit_limit => 1000)
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index a29e4349d6..7e3e204626 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -116,24 +116,23 @@ class AttributeMethodsTest < ActiveRecord::TestCase
end
end
- unless current_adapter?(:Mysql2Adapter)
- def test_read_attributes_before_type_cast_on_datetime
- developer = Developer.find(:first)
- # Oracle adapter returns Time before type cast
- unless current_adapter?(:OracleAdapter)
- assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s
- else
- assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s(:db)
+ def test_read_attributes_before_type_cast_on_datetime
+ developer = Developer.find(:first)
+ if current_adapter?(:Mysql2Adapter, :OracleAdapter)
+ # Mysql2 and Oracle adapters keep the value in Time instance
+ assert_equal developer.created_at.to_s(:db), developer.attributes_before_type_cast["created_at"].to_s(:db)
+ else
+ assert_equal developer.created_at.to_s(:db), developer.attributes_before_type_cast["created_at"].to_s
+ end
- developer.created_at = "345643456"
- assert_equal developer.created_at_before_type_cast, "345643456"
- assert_equal developer.created_at, nil
+ developer.created_at = "345643456"
- developer.created_at = "2010-03-21 21:23:32"
- assert_equal developer.created_at_before_type_cast.to_s, "2010-03-21 21:23:32"
- assert_equal developer.created_at, Time.parse("2010-03-21 21:23:32")
- end
- end
+ assert_equal developer.created_at_before_type_cast, "345643456"
+ assert_equal developer.created_at, nil
+
+ developer.created_at = "2010-03-21 21:23:32"
+ assert_equal developer.created_at_before_type_cast.to_s, "2010-03-21 21:23:32"
+ assert_equal developer.created_at, Time.parse("2010-03-21 21:23:32")
end
def test_hash_content
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index a58d5dec81..5cbc52732b 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -1018,6 +1018,52 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal topic.content, false
end
+ def test_serialize_with_coder
+ coder = Class.new {
+ # Identity
+ def load(thing)
+ thing
+ end
+
+ # base 64
+ def dump(thing)
+ [thing].pack('m')
+ end
+ }.new
+
+ Topic.serialize(:content, coder)
+ s = 'hello world'
+ topic = Topic.new(:content => s)
+ assert topic.save
+ topic = topic.reload
+ assert_equal [s].pack('m'), topic.content
+ ensure
+ Topic.serialize(:content)
+ end
+
+ def test_serialize_with_bcrypt_coder
+ crypt_coder = Class.new {
+ def load(thing)
+ return unless thing
+ BCrypt::Password.new thing
+ end
+
+ def dump(thing)
+ BCrypt::Password.create(thing).to_s
+ end
+ }.new
+
+ Topic.serialize(:content, crypt_coder)
+ password = 'password'
+ topic = Topic.new(:content => password)
+ assert topic.save
+ topic = topic.reload
+ assert_kind_of BCrypt::Password, topic.content
+ assert_equal(true, topic.content == password, 'password should equal')
+ ensure
+ Topic.serialize(:content)
+ end
+
def test_quote
author_name = "\\ \001 ' \n \\n \""
topic = Topic.create('author_name' => author_name)
diff --git a/activerecord/test/cases/coders/yaml_column_test.rb b/activerecord/test/cases/coders/yaml_column_test.rb
new file mode 100644
index 0000000000..f85f11b57f
--- /dev/null
+++ b/activerecord/test/cases/coders/yaml_column_test.rb
@@ -0,0 +1,45 @@
+require "cases/helper"
+
+module ActiveRecord
+ module Coders
+ class YAMLColumnTest < ActiveRecord::TestCase
+ def test_initialize_takes_class
+ coder = YAMLColumn.new(Object)
+ assert_equal Object, coder.object_class
+ end
+
+ def test_type_mismatch_on_different_classes
+ coder = YAMLColumn.new(Array)
+ assert_raises(SerializationTypeMismatch) do
+ coder.load "--- foo"
+ end
+ end
+
+ def test_nil_is_ok
+ coder = YAMLColumn.new
+ assert_nil coder.load "--- "
+ end
+
+ def test_nil_is_ok_with_different_class
+ coder = YAMLColumn.new SerializationTypeMismatch
+ assert_nil coder.load "--- "
+ end
+
+ def test_returns_string_unless_starts_with_dash
+ coder = YAMLColumn.new
+ assert_equal 'foo', coder.load("foo")
+ end
+
+ def test_load_handles_other_classes
+ coder = YAMLColumn.new
+ assert_equal [], coder.load([])
+ end
+
+ def test_load_swallows_yaml_exceptions
+ coder = YAMLColumn.new
+ bad_yaml = '--- {'
+ assert_equal bad_yaml, coder.load(bad_yaml)
+ end
+ end
+ end
+end
diff --git a/activerecord/test/cases/column_definition_test.rb b/activerecord/test/cases/column_definition_test.rb
index cc6a6b44f2..d1dddd4c2c 100644
--- a/activerecord/test/cases/column_definition_test.rb
+++ b/activerecord/test/cases/column_definition_test.rb
@@ -1,121 +1,145 @@
require "cases/helper"
-class ColumnDefinitionTest < ActiveRecord::TestCase
- def setup
- @adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(nil)
- def @adapter.native_database_types
- {:string => "varchar"}
- end
- end
+module ActiveRecord
+ module ConnectionAdapters
+ class ColumnDefinitionTest < ActiveRecord::TestCase
+ def setup
+ @adapter = AbstractAdapter.new(nil)
+ def @adapter.native_database_types
+ {:string => "varchar"}
+ end
+ end
- # Avoid column definitions in create table statements like:
- # `title` varchar(255) DEFAULT NULL
- def test_should_not_include_default_clause_when_default_is_null
- column = ActiveRecord::ConnectionAdapters::Column.new("title", nil, "varchar(20)")
- column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
- @adapter, column.name, "string",
- column.limit, column.precision, column.scale, column.default, column.null)
- assert_equal "title varchar(20)", column_def.to_sql
- end
+ def test_can_set_coder
+ column = Column.new("title", nil, "varchar(20)")
+ column.coder = YAML
+ assert_equal YAML, column.coder
+ end
- def test_should_include_default_clause_when_default_is_present
- column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)")
- column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
- @adapter, column.name, "string",
- column.limit, column.precision, column.scale, column.default, column.null)
- assert_equal %Q{title varchar(20) DEFAULT 'Hello'}, column_def.to_sql
- end
+ def test_encoded?
+ column = Column.new("title", nil, "varchar(20)")
+ assert !column.encoded?
- def test_should_specify_not_null_if_null_option_is_false
- column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)", false)
- column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
- @adapter, column.name, "string",
- column.limit, column.precision, column.scale, column.default, column.null)
- assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, column_def.to_sql
- end
+ column.coder = YAML
+ assert column.encoded?
+ end
- if current_adapter?(:MysqlAdapter)
- def test_should_set_default_for_mysql_binary_data_types
- binary_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "binary(1)")
- assert_equal "a", binary_column.default
+ def test_type_case_coded_column
+ column = Column.new("title", nil, "varchar(20)")
+ column.coder = YAML
+ assert_equal "hello", column.type_cast("--- hello")
+ end
- varbinary_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "varbinary(1)")
- assert_equal "a", varbinary_column.default
- end
+ # Avoid column definitions in create table statements like:
+ # `title` varchar(255) DEFAULT NULL
+ def test_should_not_include_default_clause_when_default_is_null
+ column = Column.new("title", nil, "varchar(20)")
+ column_def = ColumnDefinition.new(
+ @adapter, column.name, "string",
+ column.limit, column.precision, column.scale, column.default, column.null)
+ assert_equal "title varchar(20)", column_def.to_sql
+ end
- def test_should_not_set_default_for_blob_and_text_data_types
- assert_raise ArgumentError do
- ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "blob")
+ def test_should_include_default_clause_when_default_is_present
+ column = Column.new("title", "Hello", "varchar(20)")
+ column_def = ColumnDefinition.new(
+ @adapter, column.name, "string",
+ column.limit, column.precision, column.scale, column.default, column.null)
+ assert_equal %Q{title varchar(20) DEFAULT 'Hello'}, column_def.to_sql
end
- assert_raise ArgumentError do
- ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "Hello", "text")
+ def test_should_specify_not_null_if_null_option_is_false
+ column = Column.new("title", "Hello", "varchar(20)", false)
+ column_def = ColumnDefinition.new(
+ @adapter, column.name, "string",
+ column.limit, column.precision, column.scale, column.default, column.null)
+ assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, column_def.to_sql
end
- text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text")
- assert_equal nil, text_column.default
+ if current_adapter?(:MysqlAdapter)
+ def test_should_set_default_for_mysql_binary_data_types
+ binary_column = MysqlColumn.new("title", "a", "binary(1)")
+ assert_equal "a", binary_column.default
- not_null_text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text", false)
- assert_equal "", not_null_text_column.default
- end
+ varbinary_column = MysqlColumn.new("title", "a", "varbinary(1)")
+ assert_equal "a", varbinary_column.default
+ end
- def test_has_default_should_return_false_for_blog_and_test_data_types
- blob_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "blob")
- assert !blob_column.has_default?
+ def test_should_not_set_default_for_blob_and_text_data_types
+ assert_raise ArgumentError do
+ MysqlColumn.new("title", "a", "blob")
+ end
- text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text")
- assert !text_column.has_default?
- end
- end
+ assert_raise ArgumentError do
+ MysqlColumn.new("title", "Hello", "text")
+ end
- if current_adapter?(:Mysql2Adapter)
- def test_should_set_default_for_mysql_binary_data_types
- binary_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "binary(1)")
- assert_equal "a", binary_column.default
+ text_column = MysqlColumn.new("title", nil, "text")
+ assert_equal nil, text_column.default
- varbinary_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "varbinary(1)")
- assert_equal "a", varbinary_column.default
- end
+ not_null_text_column = MysqlColumn.new("title", nil, "text", false)
+ assert_equal "", not_null_text_column.default
+ end
- def test_should_not_set_default_for_blob_and_text_data_types
- assert_raise ArgumentError do
- ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "blob")
- end
+ def test_has_default_should_return_false_for_blog_and_test_data_types
+ blob_column = MysqlColumn.new("title", nil, "blob")
+ assert !blob_column.has_default?
- assert_raise ArgumentError do
- ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "Hello", "text")
+ text_column = MysqlColumn.new("title", nil, "text")
+ assert !text_column.has_default?
+ end
end
- text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text")
- assert_equal nil, text_column.default
+ if current_adapter?(:Mysql2Adapter)
+ def test_should_set_default_for_mysql_binary_data_types
+ binary_column = Mysql2Column.new("title", "a", "binary(1)")
+ assert_equal "a", binary_column.default
- not_null_text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text", false)
- assert_equal "", not_null_text_column.default
- end
+ varbinary_column = Mysql2Column.new("title", "a", "varbinary(1)")
+ assert_equal "a", varbinary_column.default
+ end
- def test_has_default_should_return_false_for_blog_and_test_data_types
- blob_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "blob")
- assert !blob_column.has_default?
+ def test_should_not_set_default_for_blob_and_text_data_types
+ assert_raise ArgumentError do
+ Mysql2Column.new("title", "a", "blob")
+ end
- text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text")
- assert !text_column.has_default?
- end
- end
+ assert_raise ArgumentError do
+ Mysql2Column.new("title", "Hello", "text")
+ end
- if current_adapter?(:PostgreSQLAdapter)
- def test_bigint_column_should_map_to_integer
- bigint_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new('number', nil, "bigint")
- assert_equal :integer, bigint_column.type
- end
+ text_column = Mysql2Column.new("title", nil, "text")
+ assert_equal nil, text_column.default
- def test_smallint_column_should_map_to_integer
- smallint_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new('number', nil, "smallint")
- assert_equal :integer, smallint_column.type
- end
+ not_null_text_column = Mysql2Column.new("title", nil, "text", false)
+ assert_equal "", not_null_text_column.default
+ end
+
+ def test_has_default_should_return_false_for_blog_and_test_data_types
+ blob_column = Mysql2Column.new("title", nil, "blob")
+ assert !blob_column.has_default?
- def test_uuid_column_should_map_to_string
- uuid_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new('unique_id', nil, "uuid")
- assert_equal :string, uuid_column.type
+ text_column = Mysql2Column.new("title", nil, "text")
+ assert !text_column.has_default?
+ end
+ end
+
+ if current_adapter?(:PostgreSQLAdapter)
+ def test_bigint_column_should_map_to_integer
+ bigint_column = PostgreSQLColumn.new('number', nil, "bigint")
+ assert_equal :integer, bigint_column.type
+ end
+
+ def test_smallint_column_should_map_to_integer
+ smallint_column = PostgreSQLColumn.new('number', nil, "smallint")
+ assert_equal :integer, smallint_column.type
+ end
+
+ def test_uuid_column_should_map_to_string
+ uuid_column = PostgreSQLColumn.new('unique_id', nil, "uuid")
+ assert_equal :string, uuid_column.type
+ end
+ end
end
end
end
diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb
index ea2292dda5..ae531ebb4c 100644
--- a/activerecord/test/cases/migration/command_recorder_test.rb
+++ b/activerecord/test/cases/migration/command_recorder_test.rb
@@ -16,7 +16,7 @@ module ActiveRecord
def test_send_calls_super
assert_raises(NoMethodError) do
- @recorder.send(:create_table, :horses)
+ @recorder.send(:non_existing_method, :horses)
end
end
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index a5a9965c3a..6f0f73e3bd 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -1923,6 +1923,146 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ if ActiveRecord::Base.connection.supports_bulk_alter?
+ class BulkAlterTableMigrationsTest < ActiveRecord::TestCase
+ def setup
+ @connection = Person.connection
+ @connection.create_table(:delete_me, :force => true) {|t| }
+ end
+
+ def teardown
+ Person.connection.drop_table(:delete_me) rescue nil
+ end
+
+ def test_adding_multiple_columns
+ assert_queries(1) do
+ with_bulk_change_table do |t|
+ t.column :name, :string
+ t.string :qualification, :experience
+ t.integer :age, :default => 0
+ t.date :birthdate
+ t.timestamps
+ end
+ end
+
+ assert_equal 8, columns.size
+ [:name, :qualification, :experience].each {|s| assert_equal :string, column(s).type }
+ assert_equal 0, column(:age).default
+ end
+
+ def test_removing_columns
+ with_bulk_change_table do |t|
+ t.string :qualification, :experience
+ end
+
+ [:qualification, :experience].each {|c| assert column(c) }
+
+ assert_queries(1) do
+ with_bulk_change_table do |t|
+ t.remove :qualification, :experience
+ t.string :qualification_experience
+ end
+ end
+
+ [:qualification, :experience].each {|c| assert ! column(c) }
+ assert column(:qualification_experience)
+ end
+
+ def test_adding_indexes
+ with_bulk_change_table do |t|
+ t.string :username
+ t.string :name
+ t.integer :age
+ end
+
+ # Adding an index fires a query everytime to check if an index already exists or not
+ assert_queries(3) do
+ with_bulk_change_table do |t|
+ t.index :username, :unique => true, :name => :awesome_username_index
+ t.index [:name, :age]
+ end
+ end
+
+ assert_equal 2, indexes.size
+
+ name_age_index = index(:index_delete_me_on_name_and_age)
+ assert_equal ['name', 'age'].sort, name_age_index.columns.sort
+ assert ! name_age_index.unique
+
+ assert index(:awesome_username_index).unique
+ end
+
+ def test_removing_index
+ with_bulk_change_table do |t|
+ t.string :name
+ t.index :name
+ end
+
+ assert index(:index_delete_me_on_name)
+
+ assert_queries(3) do
+ with_bulk_change_table do |t|
+ t.remove_index :name
+ t.index :name, :name => :new_name_index, :unique => true
+ end
+ end
+
+ assert ! index(:index_delete_me_on_name)
+
+ new_name_index = index(:new_name_index)
+ assert new_name_index.unique
+ end
+
+ def test_changing_columns
+ with_bulk_change_table do |t|
+ t.string :name
+ t.date :birthdate
+ end
+
+ assert ! column(:name).default
+ assert_equal :date, column(:birthdate).type
+
+ assert_queries(1) do
+ with_bulk_change_table do |t|
+ t.change :name, :string, :default => 'NONAME'
+ t.change :birthdate, :datetime
+ end
+ end
+
+ assert_equal 'NONAME', column(:name).default
+ assert_equal :datetime, column(:birthdate).type
+ end
+
+ protected
+
+ def with_bulk_change_table
+ # Reset columns/indexes cache as we're changing the table
+ @columns = @indexes = nil
+
+ Person.connection.change_table(:delete_me, :bulk => true) do |t|
+ yield t
+ end
+ end
+
+ def column(name)
+ columns.detect {|c| c.name == name.to_s }
+ end
+
+ def columns
+ @columns ||= Person.connection.columns('delete_me')
+ end
+
+ def index(name)
+ indexes.detect {|i| i.name == name.to_s }
+ end
+
+ def indexes
+ @indexes ||= Person.connection.indexes('delete_me')
+ end
+ end # AlterTableMigrationsTest
+
+ end
+
class CopyMigrationsTest < ActiveRecord::TestCase
def setup
end
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index e1f938be84..d1afe7376a 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -147,6 +147,15 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase
pirate.ship_attributes = { :id => "" }
assert_nothing_raised(ActiveRecord::RecordNotFound) { pirate.save! }
end
+
+ def test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record
+ Man.accepts_nested_attributes_for(:interests)
+ man = Man.create(:name => "John")
+ interest = man.interests.create :topic => 'gardning'
+ man = Man.find man.id
+ man.interests_attributes = [{:id => interest.id, :topic => 'gardening'}]
+ assert_equal man.interests.first.topic, man.interests[0].topic
+ end
end
class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
diff --git a/activerecord/test/cases/serialization_test.rb b/activerecord/test/cases/serialization_test.rb
index 25dbcc9fc2..677d659f39 100644
--- a/activerecord/test/cases/serialization_test.rb
+++ b/activerecord/test/cases/serialization_test.rb
@@ -23,6 +23,12 @@ class SerializationTest < ActiveRecord::TestCase
@contact = Contact.new(@contact_attributes)
end
+ def test_serialized_init_with
+ topic = Topic.allocate
+ topic.init_with('attributes' => { 'content' => '--- foo' })
+ assert_equal 'foo', topic.content
+ end
+
def test_to_xml
xml = REXML::Document.new(topics(:first).to_xml(:indent => 0))
bonus_time_in_current_timezone = topics(:first).bonus_time.xmlschema
diff --git a/activerecord/test/cases/session_store/session_test.rb b/activerecord/test/cases/session_store/session_test.rb
index f906bda8c3..cee5ddd003 100644
--- a/activerecord/test/cases/session_store/session_test.rb
+++ b/activerecord/test/cases/session_store/session_test.rb
@@ -60,6 +60,7 @@ module ActiveRecord
end
def test_loaded?
+ Session.create_table!
s = Session.new
assert !s.loaded?, 'session is not loaded'
end
diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb
index 70c098bc6d..1c21f0f3b6 100644
--- a/activerecord/test/cases/timestamp_test.rb
+++ b/activerecord/test/cases/timestamp_test.rb
@@ -140,4 +140,34 @@ class TimestampTest < ActiveRecord::TestCase
ensure
Toy.belongs_to :pet
end
+
+ def test_timestamp_attributes_for_create
+ toy = Toy.first
+ assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on]
+ end
+
+ def test_timestamp_attributes_for_update
+ toy = Toy.first
+ assert_equal toy.send(:timestamp_attributes_for_update), [:updated_at, :updated_on]
+ end
+
+ def test_all_timestamp_attributes
+ toy = Toy.first
+ assert_equal toy.send(:all_timestamp_attributes), [:created_at, :created_on, :updated_at, :updated_on]
+ end
+
+ def test_timestamp_attributes_for_create_in_model
+ toy = Toy.first
+ assert_equal toy.send(:timestamp_attributes_for_create_in_model), [:created_at]
+ end
+
+ def test_timestamp_attributes_for_update_in_model
+ toy = Toy.first
+ assert_equal toy.send(:timestamp_attributes_for_update_in_model), [:updated_at]
+ end
+
+ def test_all_timestamp_attributes_in_model
+ toy = Toy.first
+ assert_equal toy.send(:all_timestamp_attributes_in_model), [:created_at, :updated_at]
+ end
end
diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb
index 9eefc5803a..7178bb0d00 100644
--- a/activerecord/test/models/bulb.rb
+++ b/activerecord/test/models/bulb.rb
@@ -1,7 +1,14 @@
class Bulb < ActiveRecord::Base
-
+
default_scope :conditions => {:name => 'defaulty' }
-
+
belongs_to :car
+ attr_reader :scoped_methods_after_initialize
+
+ after_initialize :record_scoped_methods_after_initialize
+ def record_scoped_methods_after_initialize
+ @scoped_methods_after_initialize = self.class.scoped_methods.dup
+ end
+
end
diff --git a/activerecord/test/models/club.rb b/activerecord/test/models/club.rb
index 6e7cdd643a..c432a6ace8 100644
--- a/activerecord/test/models/club.rb
+++ b/activerecord/test/models/club.rb
@@ -1,4 +1,5 @@
class Club < ActiveRecord::Base
+ has_one :membership
has_many :memberships
has_many :members, :through => :memberships
has_many :current_memberships
@@ -10,4 +11,4 @@ class Club < ActiveRecord::Base
def private_method
"I'm sorry sir, this is a *private* club, not a *pirate* club"
end
-end \ No newline at end of file
+end
diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb
index a9aa0afced..ff533717cc 100644
--- a/activerecord/test/models/comment.rb
+++ b/activerecord/test/models/comment.rb
@@ -1,6 +1,7 @@
class Comment < ActiveRecord::Base
scope :limit_by, lambda {|l| limit(l) }
scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'"
+ scope :not_again, where("comments.body NOT LIKE '%again%'")
scope :for_first_post, :conditions => { :post_id => 1 }
scope :for_first_author,
:joins => :post,
diff --git a/activerecord/test/models/contact.rb b/activerecord/test/models/contact.rb
index 975a885331..5bbe7ebb12 100644
--- a/activerecord/test/models/contact.rb
+++ b/activerecord/test/models/contact.rb
@@ -1,4 +1,8 @@
class Contact < ActiveRecord::Base
+ def self.columns
+ @columns
+ end
+
# mock out self.columns so no pesky db is needed for these tests
def self.column(name, sql_type = nil, options = {})
@columns ||= []
diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb
index 15ad6aedd3..e6e78f9e45 100644
--- a/activerecord/test/models/member.rb
+++ b/activerecord/test/models/member.rb
@@ -13,4 +13,7 @@ class Member < ActiveRecord::Base
has_many :current_memberships
has_one :club_through_many, :through => :current_memberships, :source => :club
+
+ has_many :current_memberships, :conditions => { :favourite => true }
+ has_many :clubs, :through => :current_memberships
end
diff --git a/activerecord/test/models/pirate.rb b/activerecord/test/models/pirate.rb
index b0490f754e..0d3f62bb33 100644
--- a/activerecord/test/models/pirate.rb
+++ b/activerecord/test/models/pirate.rb
@@ -34,6 +34,8 @@ class Pirate < ActiveRecord::Base
:after_remove => proc {|p,b| p.ship_log << "after_removing_proc_bird_#{b.id}"}
has_many :birds_with_reject_all_blank, :class_name => "Bird"
+ has_one :bulb, :foreign_key => :car_id
+
accepts_nested_attributes_for :parrots, :birds, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
accepts_nested_attributes_for :update_only_ship, :update_only => true