diff options
Diffstat (limited to 'activerecord/test')
37 files changed, 279 insertions, 274 deletions
diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 93b01a3934..404b492288 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -69,16 +69,16 @@ module ActiveRecord def test_not_specifying_database_name_for_cross_database_selects begin assert_nothing_raised do - ActiveRecord::Model.establish_connection(ActiveRecord::Base.configurations['arunit'].except(:database)) + ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['arunit'].except(:database)) config = ARTest.connection_config - ActiveRecord::Model.connection.execute( + ActiveRecord::Base.connection.execute( "SELECT #{config['arunit']['database']}.pirates.*, #{config['arunit2']['database']}.courses.* " \ "FROM #{config['arunit']['database']}.pirates, #{config['arunit2']['database']}.courses" ) end ensure - ActiveRecord::Model.establish_connection 'arunit' + ActiveRecord::Base.establish_connection 'arunit' end end end diff --git a/activerecord/test/cases/adapters/mysql/connection_test.rb b/activerecord/test/cases/adapters/mysql/connection_test.rb index 4bccd2cc59..534dc2c2df 100644 --- a/activerecord/test/cases/adapters/mysql/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql/connection_test.rb @@ -3,13 +3,13 @@ require "cases/helper" class MysqlConnectionTest < ActiveRecord::TestCase def setup super - @connection = ActiveRecord::Model.connection + @connection = ActiveRecord::Base.connection end def test_mysql_reconnect_attribute_after_connection_with_reconnect_true run_without_connection do |orig_connection| - ActiveRecord::Model.establish_connection(orig_connection.merge({:reconnect => true})) - assert ActiveRecord::Model.connection.raw_connection.reconnect + ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => true})) + assert ActiveRecord::Base.connection.raw_connection.reconnect end end @@ -25,8 +25,8 @@ class MysqlConnectionTest < ActiveRecord::TestCase def test_mysql_reconnect_attribute_after_connection_with_reconnect_false run_without_connection do |orig_connection| - ActiveRecord::Model.establish_connection(orig_connection.merge({:reconnect => false})) - assert !ActiveRecord::Model.connection.raw_connection.reconnect + ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => false})) + assert !ActiveRecord::Base.connection.raw_connection.reconnect end end @@ -117,7 +117,7 @@ class MysqlConnectionTest < ActiveRecord::TestCase # Test that MySQL allows multiple results for stored procedures if defined?(Mysql) && Mysql.const_defined?(:CLIENT_MULTI_RESULTS) def test_multi_results - rows = ActiveRecord::Model.connection.select_rows('CALL ten();') + rows = ActiveRecord::Base.connection.select_rows('CALL ten();') assert_equal 10, rows[0][0].to_i, "ten() did not return 10 as expected: #{rows.inspect}" assert @connection.active?, "Bad connection use by 'MysqlAdapter.select_rows'" end @@ -130,9 +130,9 @@ class MysqlConnectionTest < ActiveRecord::TestCase def test_mysql_strict_mode_disabled_dont_override_global_sql_mode run_without_connection do |orig_connection| - ActiveRecord::Model.establish_connection(orig_connection.merge({:strict => false})) - global_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@GLOBAL.sql_mode" - session_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode" + ActiveRecord::Base.establish_connection(orig_connection.merge({:strict => false})) + global_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@GLOBAL.sql_mode" + session_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@SESSION.sql_mode" assert_equal global_sql_mode.rows, session_sql_mode.rows end end @@ -140,11 +140,11 @@ class MysqlConnectionTest < ActiveRecord::TestCase private def run_without_connection - original_connection = ActiveRecord::Model.remove_connection + original_connection = ActiveRecord::Base.remove_connection begin yield original_connection ensure - ActiveRecord::Model.establish_connection(original_connection) + ActiveRecord::Base.establish_connection(original_connection) end end end diff --git a/activerecord/test/cases/adapters/mysql/sql_types_test.rb b/activerecord/test/cases/adapters/mysql/sql_types_test.rb new file mode 100644 index 0000000000..1ddb1b91c9 --- /dev/null +++ b/activerecord/test/cases/adapters/mysql/sql_types_test.rb @@ -0,0 +1,14 @@ +require "cases/helper" + +class SqlTypesTest < ActiveRecord::TestCase + def test_binary_types + assert_equal 'varbinary(64)', type_to_sql(:binary, 64) + assert_equal 'varbinary(4095)', type_to_sql(:binary, 4095) + assert_equal 'blob(4096)', type_to_sql(:binary, 4096) + assert_equal 'blob', type_to_sql(:binary) + end + + def type_to_sql(*args) + ActiveRecord::Base.connection.type_to_sql(*args) + end +end diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb index c63e4fe5b6..14c22d2519 100644 --- a/activerecord/test/cases/adapters/mysql2/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb @@ -3,7 +3,7 @@ require "cases/helper" class MysqlConnectionTest < ActiveRecord::TestCase def setup super - @connection = ActiveRecord::Model.connection + @connection = ActiveRecord::Base.connection @connection.extend(LogIntercepter) @connection.intercepted = true end @@ -46,9 +46,9 @@ class MysqlConnectionTest < ActiveRecord::TestCase def test_mysql_strict_mode_disabled_dont_override_global_sql_mode run_without_connection do |orig_connection| - ActiveRecord::Model.establish_connection(orig_connection.merge({:strict => false})) - global_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@GLOBAL.sql_mode" - session_sql_mode = ActiveRecord::Model.connection.exec_query "SELECT @@SESSION.sql_mode" + ActiveRecord::Base.establish_connection(orig_connection.merge({:strict => false})) + global_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@GLOBAL.sql_mode" + session_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@SESSION.sql_mode" assert_equal global_sql_mode.rows, session_sql_mode.rows end end @@ -76,11 +76,11 @@ class MysqlConnectionTest < ActiveRecord::TestCase private def run_without_connection - original_connection = ActiveRecord::Model.remove_connection + original_connection = ActiveRecord::Base.remove_connection begin yield original_connection ensure - ActiveRecord::Model.establish_connection(original_connection) + ActiveRecord::Base.establish_connection(original_connection) end end end diff --git a/activerecord/test/cases/adapters/mysql2/sql_types_test.rb b/activerecord/test/cases/adapters/mysql2/sql_types_test.rb new file mode 100644 index 0000000000..1ddb1b91c9 --- /dev/null +++ b/activerecord/test/cases/adapters/mysql2/sql_types_test.rb @@ -0,0 +1,14 @@ +require "cases/helper" + +class SqlTypesTest < ActiveRecord::TestCase + def test_binary_types + assert_equal 'varbinary(64)', type_to_sql(:binary, 64) + assert_equal 'varbinary(4095)', type_to_sql(:binary, 4095) + assert_equal 'blob(4096)', type_to_sql(:binary, 4096) + assert_equal 'blob', type_to_sql(:binary) + end + + def type_to_sql(*args) + ActiveRecord::Base.connection.type_to_sql(*args) + end +end diff --git a/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb b/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb index 74288a98d1..d03d1dd94c 100644 --- a/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/copy_table_test.rb @@ -32,6 +32,11 @@ class CopyTableTest < ActiveRecord::TestCase end end + def test_copy_table_allows_to_pass_options_to_create_table + @connection.create_table('blocker_table') + test_copy_table('customers', 'blocker_table', force: true) + end + def test_copy_table_with_index test_copy_table('comments', 'comments_with_index') do @connection.add_index('comments_with_index', ['post_id', 'type']) @@ -43,7 +48,9 @@ class CopyTableTest < ActiveRecord::TestCase end def test_copy_table_without_primary_key - test_copy_table('developers_projects', 'programmers_projects') + test_copy_table('developers_projects', 'programmers_projects') do + assert_nil @connection.primary_key('programmers_projects') + end end def test_copy_table_with_id_col_that_is_not_primary_key diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index 4e26c5dda1..003052bac4 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -154,6 +154,12 @@ module ActiveRecord DualEncoding.connection.drop_table('dual_encodings') end + def test_type_cast_should_not_mutate_encoding + name = 'hello'.force_encoding(Encoding::ASCII_8BIT) + Owner.create(name: name) + assert_equal Encoding::ASCII_8BIT, name.encoding + end + def test_execute @conn.execute "INSERT INTO items (number) VALUES (10)" records = @conn.execute "SELECT * FROM items" diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index aad48e7ce9..8c9b4fb921 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -261,10 +261,23 @@ class InverseHasManyTests < ActiveRecord::TestCase def test_parent_instance_should_be_shared_with_first_and_last_child man = Man.first + assert man.interests.first.man.equal? man assert man.interests.last.man.equal? man end + def test_parent_instance_should_be_shared_with_first_n_and_last_n_children + man = Man.first + + interests = man.interests.first(2) + assert interests[0].man.equal? man + assert interests[1].man.equal? man + + interests = man.interests.last(2) + assert interests[0].man.equal? man + assert interests[1].man.equal? man + end + def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.secret_interests } end diff --git a/activerecord/test/cases/attribute_methods/read_test.rb b/activerecord/test/cases/attribute_methods/read_test.rb index da5d9d8c2a..8d8ff2f952 100644 --- a/activerecord/test/cases/attribute_methods/read_test.rb +++ b/activerecord/test/cases/attribute_methods/read_test.rb @@ -11,10 +11,8 @@ module ActiveRecord def setup @klass = Class.new do def self.superclass; Base; end - def self.active_record_super; Base; end def self.base_class; self; end - extend ActiveRecord::Configuration include ActiveRecord::AttributeMethods def self.define_attribute_methods diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index c2b58fd7d1..8b82b79219 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -287,6 +287,12 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert_equal "Don't change the topic", topic[:title] end + def test_read_attribute_raises_missing_attribute_error_when_not_exists + computer = Computer.select('id').first + assert_raises(ActiveModel::MissingAttributeError) { computer[:developer] } + assert_raises(ActiveModel::MissingAttributeError) { computer[:extendedWarranty] } + end + def test_read_attribute_when_false topic = topics(:first) topic.approved = false diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 0f859bf452..203e44857a 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -23,7 +23,6 @@ require 'models/edge' require 'models/joke' require 'models/bulb' require 'models/bird' -require 'models/teapot' require 'rexml/document' require 'active_support/core_ext/exception' @@ -1196,10 +1195,6 @@ class BasicsTest < ActiveRecord::TestCase # Concrete subclasses an abstract class which has a type column. assert !SubStiPost.descends_from_active_record? - - assert Teapot.descends_from_active_record? - assert !OtherTeapot.descends_from_active_record? - assert CoolTeapot.descends_from_active_record? end def test_find_on_abstract_base_class_doesnt_use_type_condition @@ -1453,15 +1448,6 @@ class BasicsTest < ActiveRecord::TestCase assert_equal scope, Bird.uniq end - def test_active_record_super - assert_equal ActiveRecord::Model, ActiveRecord::Base.active_record_super - assert_equal ActiveRecord::Base, Topic.active_record_super - assert_equal Topic, ImportantTopic.active_record_super - assert_equal ActiveRecord::Model, Teapot.active_record_super - assert_equal Teapot, OtherTeapot.active_record_super - assert_equal ActiveRecord::Model, CoolTeapot.active_record_super - end - def test_table_name_with_2_abstract_subclasses assert_equal "photos", Photo.table_name end diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index 12d5245fbd..acb8b5f562 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -136,4 +136,12 @@ class EachTest < ActiveRecord::TestCase assert_equal nick_order_subscribers[1..-1].map(&:id), subscribers.map(&:id) end + + def test_find_in_batches_should_use_any_column_as_primary_key_when_start_is_not_specified + assert_queries(Subscriber.count + 1) do + Subscriber.find_each(:batch_size => 1) do |subscriber| + assert_kind_of Subscriber, subscriber + end + end + end end diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb index a7b63d15c9..2124c256fa 100644 --- a/activerecord/test/cases/column_test.rb +++ b/activerecord/test/cases/column_test.rb @@ -33,6 +33,8 @@ module ActiveRecord assert_equal 0, column.type_cast('bad1') assert_equal 0, column.type_cast('bad') assert_equal 1, column.type_cast(1.7) + assert_equal 0, column.type_cast(false) + assert_equal 1, column.type_cast(true) assert_nil column.type_cast(nil) end @@ -41,11 +43,9 @@ module ActiveRecord assert_raises(NoMethodError) do column.type_cast([]) end + assert_raises(NoMethodError) do - column.type_cast(true) - end - assert_raises(NoMethodError) do - column.type_cast(false) + column.type_cast(Object.new) end end diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index 4467ddfc39..631bf1aaac 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -4,8 +4,8 @@ module ActiveRecord module ConnectionAdapters class ConnectionHandlerTest < ActiveRecord::TestCase def setup - @klass = Class.new { include ActiveRecord::Tag } - @subklass = Class.new(@klass) { include ActiveRecord::Tag } + @klass = Class.new(Base) + @subklass = Class.new(@klass) @handler = ConnectionHandler.new @handler.establish_connection @klass, Base.connection_pool.spec diff --git a/activerecord/test/cases/connection_specification/resolver_test.rb b/activerecord/test/cases/connection_specification/resolver_test.rb index 434d2b7ba5..ee9818678d 100644 --- a/activerecord/test/cases/connection_specification/resolver_test.rb +++ b/activerecord/test/cases/connection_specification/resolver_test.rb @@ -9,7 +9,7 @@ module ActiveRecord end def test_url_host_no_db - skip "only if mysql is available" unless defined?(MysqlAdapter) + skip "only if mysql is available" unless current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter) spec = resolve 'mysql://foo?encoding=utf8' assert_equal({ :adapter => "mysql", @@ -18,7 +18,7 @@ module ActiveRecord end def test_url_host_db - skip "only if mysql is available" unless defined?(MysqlAdapter) + skip "only if mysql is available" unless current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter) spec = resolve 'mysql://foo/bar?encoding=utf8' assert_equal({ :adapter => "mysql", @@ -28,7 +28,7 @@ module ActiveRecord end def test_url_port - skip "only if mysql is available" unless defined?(MysqlAdapter) + skip "only if mysql is available" unless current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter) spec = resolve 'mysql://foo:123?encoding=utf8' assert_equal({ :adapter => "mysql", @@ -36,6 +36,14 @@ module ActiveRecord :host => "foo", :encoding => "utf8" }, spec) end + + def test_encoded_password + skip "only if mysql is available" unless current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter) + password = 'am@z1ng_p@ssw0rd#!' + encoded_password = URI.encode_www_form_component(password) + spec = resolve "mysql://foo:#{encoded_password}@localhost/bar" + assert_equal password, spec[:password] + end end end end diff --git a/activerecord/test/cases/defaults_test.rb b/activerecord/test/cases/defaults_test.rb index 0df872ff10..ed7eedaa27 100644 --- a/activerecord/test/cases/defaults_test.rb +++ b/activerecord/test/cases/defaults_test.rb @@ -52,12 +52,12 @@ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter) self.use_transactional_fixtures = false def using_strict(strict) - connection = ActiveRecord::Model.remove_connection - ActiveRecord::Model.establish_connection connection.merge(strict: strict) + connection = ActiveRecord::Base.remove_connection + ActiveRecord::Base.establish_connection connection.merge(strict: strict) yield ensure - ActiveRecord::Model.remove_connection - ActiveRecord::Model.establish_connection connection + ActiveRecord::Base.remove_connection + ActiveRecord::Base.establish_connection connection end # MySQL cannot have defaults on text/blob columns. It reports the diff --git a/activerecord/test/cases/inclusion_test.rb b/activerecord/test/cases/inclusion_test.rb deleted file mode 100644 index 8f095e4953..0000000000 --- a/activerecord/test/cases/inclusion_test.rb +++ /dev/null @@ -1,133 +0,0 @@ -require 'cases/helper' -require 'models/teapot' - -class BasicInclusionModelTest < ActiveRecord::TestCase - def test_basic_model - Teapot.create!(:name => "Ronnie Kemper") - assert_equal "Ronnie Kemper", Teapot.first.name - end - - def test_initialization - t = Teapot.new(:name => "Bob") - assert_equal "Bob", t.name - end - - def test_inherited_model - teapot = CoolTeapot.create!(:name => "Bob") - teapot.reload - - assert_equal "Bob", teapot.name - assert_equal "mmm", teapot.aaahhh - end - - def test_generated_feature_methods - assert Teapot < Teapot::GeneratedFeatureMethods - end - - def test_exists - t = Teapot.create!(:name => "Ronnie Kemper") - assert Teapot.exists?(t) - end - - def test_predicate_builder - t = Teapot.create!(:name => "Bob") - assert_equal "Bob", Teapot.where(:id => [t]).first.name - assert_equal "Bob", Teapot.where(:id => t).first.name - end - - def test_nested_model - assert_equal "ceiling_teapots", Ceiling::Teapot.table_name - end -end - -class InclusionUnitTest < ActiveRecord::TestCase - def setup - @klass = Class.new { include ActiveRecord::Model } - end - - def test_non_abstract_class - assert !@klass.abstract_class? - end - - def test_abstract_class - @klass.abstract_class = true - assert @klass.abstract_class? - end - - def test_establish_connection - assert @klass.respond_to?(:establish_connection) - assert ActiveRecord::Model.respond_to?(:establish_connection) - end - - def test_adapter_connection - name = "#{ActiveRecord::Base.connection_config[:adapter]}_connection" - assert @klass.respond_to?(name) - assert ActiveRecord::Model.respond_to?(name) - end - - def test_connection_handler - assert_equal ActiveRecord::Base.connection_handler, @klass.connection_handler - end - - def test_mirrored_configuration - ActiveRecord::Base.time_zone_aware_attributes = true - assert @klass.time_zone_aware_attributes - ActiveRecord::Base.time_zone_aware_attributes = false - assert !@klass.time_zone_aware_attributes - ensure - ActiveRecord::Base.time_zone_aware_attributes = false - end - - # Doesn't really test anything, but this is here to ensure warnings don't occur - def test_included_twice - @klass.send :include, ActiveRecord::Model - end - - def test_deprecation_proxy - proxy = ActiveRecord::Model::DeprecationProxy.new - - assert_equal ActiveRecord::Model.name, proxy.name - assert_equal ActiveRecord::Base.superclass, assert_deprecated { proxy.superclass } - - sup, sup2 = nil, nil - ActiveSupport.on_load(:__test_active_record_model_deprecation) do - sup = superclass - sup2 = send(:superclass) - end - assert_deprecated do - ActiveSupport.run_load_hooks(:__test_active_record_model_deprecation, proxy) - end - assert_equal ActiveRecord::Base.superclass, sup - assert_equal ActiveRecord::Base.superclass, sup2 - end - - test "including in deprecation proxy" do - model, base = ActiveRecord::Model.dup, ActiveRecord::Base.dup - proxy = ActiveRecord::Model::DeprecationProxy.new(model, base) - - mod = Module.new - proxy.include mod - assert model < mod - end - - test "extending in deprecation proxy" do - model, base = ActiveRecord::Model.dup, ActiveRecord::Base.dup - proxy = ActiveRecord::Model::DeprecationProxy.new(model, base) - - mod = Module.new - assert_deprecated { proxy.extend mod } - assert base.singleton_class < mod - end -end - -class InclusionFixturesTest < ActiveRecord::TestCase - fixtures :teapots - - def test_fixtured_record - assert_equal "Bob", teapots(:bob).name - end - - def test_timestamped_fixture - assert_not_nil teapots(:bob).created_at - end -end diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index 8fded9159f..aab7aa51dd 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -4,7 +4,6 @@ require 'models/person' require 'models/post' require 'models/project' require 'models/subscriber' -require 'models/teapot' require 'models/vegetables' class InheritanceTest < ActiveRecord::TestCase @@ -81,10 +80,6 @@ class InheritanceTest < ActiveRecord::TestCase assert_equal SubStiPost, SubStiPost.base_class end - def test_active_record_model_included_base_class - assert_equal Teapot, Teapot.base_class - end - def test_abstract_inheritance_base_class assert_equal LoosePerson, LoosePerson.base_class assert_equal LooseDescendant, LooseDescendant.base_class @@ -93,11 +88,7 @@ class InheritanceTest < ActiveRecord::TestCase end def test_base_class_activerecord_error - klass = Class.new { - extend ActiveRecord::Configuration - include ActiveRecord::Inheritance - } - + klass = Class.new { include ActiveRecord::Inheritance } assert_raise(ActiveRecord::ActiveRecordError) { klass.base_class } end diff --git a/activerecord/test/cases/migration/change_schema_test.rb b/activerecord/test/cases/migration/change_schema_test.rb index 17c1634444..86451289e7 100644 --- a/activerecord/test/cases/migration/change_schema_test.rb +++ b/activerecord/test/cases/migration/change_schema_test.rb @@ -132,6 +132,26 @@ module ActiveRecord assert_equal %w(foo testingid), connection.columns(:testings).map(&:name).sort end + def test_create_table_raises_when_redefining_primary_key_column + error = assert_raise(ArgumentError) do + connection.create_table :testings do |t| + t.column :id, :string + end + end + + assert_equal "you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.", error.message + end + + def test_create_table_raises_when_redefining_custom_primary_key_column + error = assert_raise(ArgumentError) do + connection.create_table :testings, primary_key: :testing_id do |t| + t.column :testing_id, :string + end + end + + assert_equal "you can't redefine the primary key column 'testing_id'. To define a custom primary key, pass { id: false } to create_table.", error.message + end + def test_create_table_with_timestamps_should_create_datetime_columns connection.create_table table_name do |t| t.timestamps diff --git a/activerecord/test/cases/multiple_db_test.rb b/activerecord/test/cases/multiple_db_test.rb index 42461e8ecb..2e386a172a 100644 --- a/activerecord/test/cases/multiple_db_test.rb +++ b/activerecord/test/cases/multiple_db_test.rb @@ -96,12 +96,12 @@ class MultipleDbTest < ActiveRecord::TestCase unless in_memory_db? def test_associations_should_work_when_model_has_no_connection begin - ActiveRecord::Model.remove_connection + ActiveRecord::Base.remove_connection assert_nothing_raised ActiveRecord::ConnectionNotEstablished do College.first.courses.first end ensure - ActiveRecord::Model.establish_connection 'arunit' + ActiveRecord::Base.establish_connection 'arunit' end end end diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index b5f32a57b2..4b938da5c4 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -592,6 +592,19 @@ class PersistencesTest < ActiveRecord::TestCase assert_equal 'super_title', t.title end + def test_update_columns_changing_id + topic = Topic.find(1) + topic.update_columns(id: 123) + assert_equal 123, topic.id + topic.reload + assert_equal 123, topic.id + end + + def test_update_columns_returns_boolean + topic = Topic.find(1) + assert_equal true, topic.update_columns(title: "New title") + end + def test_update_attributes topic = Topic.find(1) assert !topic.approved? diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 0a6354f5cc..a8a9b06ec4 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -7,17 +7,17 @@ class PooledConnectionsTest < ActiveRecord::TestCase def setup @per_test_teardown = [] - @connection = ActiveRecord::Model.remove_connection + @connection = ActiveRecord::Base.remove_connection end def teardown - ActiveRecord::Model.clear_all_connections! - ActiveRecord::Model.establish_connection(@connection) + ActiveRecord::Base.clear_all_connections! + ActiveRecord::Base.establish_connection(@connection) @per_test_teardown.each {|td| td.call } end def checkout_connections - ActiveRecord::Model.establish_connection(@connection.merge({:pool => 2, :checkout_timeout => 0.3})) + ActiveRecord::Base.establish_connection(@connection.merge({:pool => 2, :checkout_timeout => 0.3})) @connections = [] @timed_out = 0 @@ -34,15 +34,15 @@ class PooledConnectionsTest < ActiveRecord::TestCase # Will deadlock due to lack of Monitor timeouts in 1.9 def checkout_checkin_connections(pool_size, threads) - ActiveRecord::Model.establish_connection(@connection.merge({:pool => pool_size, :checkout_timeout => 0.5})) + ActiveRecord::Base.establish_connection(@connection.merge({:pool => pool_size, :checkout_timeout => 0.5})) @connection_count = 0 @timed_out = 0 threads.times do Thread.new do begin - conn = ActiveRecord::Model.connection_pool.checkout + conn = ActiveRecord::Base.connection_pool.checkout sleep 0.1 - ActiveRecord::Model.connection_pool.checkin conn + ActiveRecord::Base.connection_pool.checkin conn @connection_count += 1 rescue ActiveRecord::ConnectionTimeoutError @timed_out += 1 @@ -55,13 +55,13 @@ class PooledConnectionsTest < ActiveRecord::TestCase checkout_checkin_connections 1, 2 assert_equal 2, @connection_count assert_equal 0, @timed_out - assert_equal 1, ActiveRecord::Model.connection_pool.connections.size + assert_equal 1, ActiveRecord::Base.connection_pool.connections.size end private def add_record(name) - ActiveRecord::Model.connection_pool.with_connection { Project.create! :name => name } + ActiveRecord::Base.connection_pool.with_connection { Project.create! :name => name } end end unless current_adapter?(:FrontBase) || in_memory_db? diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index e6e50a4cd4..bf8aacc363 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -188,14 +188,14 @@ class PrimaryKeyWithNoConnectionTest < ActiveRecord::TestCase def test_set_primary_key_with_no_connection return skip("disconnect wipes in-memory db") if in_memory_db? - connection = ActiveRecord::Model.remove_connection + connection = ActiveRecord::Base.remove_connection model = Class.new(ActiveRecord::Base) model.primary_key = 'foo' assert_equal 'foo', model.primary_key - ActiveRecord::Model.establish_connection(connection) + ActiveRecord::Base.establish_connection(connection) assert_equal 'foo', model.primary_key end diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index 51a285a2b4..4ff481e6a5 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -184,6 +184,17 @@ class QueryCacheTest < ActiveRecord::TestCase assert_queries(2) { task.lock!; task.lock! } end end + + def test_cache_is_available_when_connection_is_connected + conf = ActiveRecord::Base.configurations + + ActiveRecord::Base.configurations = {} + Task.cache do + assert_queries(1) { Task.find(1); Task.find(1) } + end + ensure + ActiveRecord::Base.configurations = conf + end end class QueryCacheExpiryTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb index 6399111be6..98e278df82 100644 --- a/activerecord/test/cases/relation_test.rb +++ b/activerecord/test/cases/relation_test.rb @@ -256,5 +256,11 @@ module ActiveRecord test 'merge with a proc' do assert_equal [:foo], relation.merge(-> { where(:foo) }).where_values end + + test 'none!' do + assert relation.none!.equal?(relation) + assert_equal [NullRelation], relation.extending_values + assert relation.is_a?(NullRelation) + end end end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 5f96145b47..bc6cac0c6c 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1391,6 +1391,15 @@ class RelationTest < ActiveRecord::TestCase end end + test "loaded relations cannot be mutated by extending!" do + relation = Post.all + relation.to_a + + assert_raises(ActiveRecord::ImmutableRelation) do + relation.extending! Module.new + end + end + test "relations show the records in #inspect" do relation = Post.limit(2) assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 5f13124e5b..7ff0044bd4 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -203,6 +203,12 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{t.text\s+"body",\s+null: false$}, output end + def test_schema_dump_includes_length_for_mysql_binary_fields + output = standard_dump + assert_match %r{t.binary\s+"var_binary",\s+limit: 255$}, output + assert_match %r{t.binary\s+"var_binary_large",\s+limit: 4095$}, output + end + def test_schema_dump_includes_length_for_mysql_blob_and_text_fields output = standard_dump assert_match %r{t.binary\s+"tiny_blob",\s+limit: 255$}, output diff --git a/activerecord/test/cases/serialization_test.rb b/activerecord/test/cases/serialization_test.rb index 25b860878a..eb9cb91e32 100644 --- a/activerecord/test/cases/serialization_test.rb +++ b/activerecord/test/cases/serialization_test.rb @@ -45,4 +45,20 @@ class SerializationTest < ActiveRecord::TestCase assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}" end end + + def test_include_root_in_json_allows_inheritance + original_root_in_json = ActiveRecord::Base.include_root_in_json + ActiveRecord::Base.include_root_in_json = true + + klazz = Class.new(ActiveRecord::Base) + klazz.table_name = 'topics' + assert klazz.include_root_in_json + + klazz.include_root_in_json = false + assert ActiveRecord::Base.include_root_in_json + assert !klazz.include_root_in_json + assert !klazz.new.include_root_in_json + ensure + ActiveRecord::Base.include_root_in_json = original_root_in_json + end end diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index f24ee54cd2..068f3cf3cd 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -56,11 +56,21 @@ class SerializedAttributeTest < ActiveRecord::TestCase def test_serialized_attribute_before_type_cast_returns_unserialized_value Topic.serialize :content, Hash - t = Topic.new(:content => { :foo => :bar }) - assert_equal({ :foo => :bar }, t.content_before_type_cast) + t = Topic.new(content: { foo: :bar }) + assert_equal({ foo: :bar }, t.content_before_type_cast) t.save! t.reload - assert_equal({ :foo => :bar }, t.content_before_type_cast) + assert_equal({ foo: :bar }, t.content_before_type_cast) + end + + def test_serialized_attributes_before_type_cast_returns_unserialized_value + Topic.serialize :content, Hash + + t = Topic.new(content: { foo: :bar }) + assert_equal({ foo: :bar }, t.attributes_before_type_cast["content"]) + t.save! + t.reload + assert_equal({ foo: :bar }, t.attributes_before_type_cast["content"]) end def test_serialized_attribute_calling_dup_method diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index bb4f2c8064..fdca10f4fb 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -14,6 +14,21 @@ class TransactionTest < ActiveRecord::TestCase @first, @second = Topic.find(1, 2).sort_by { |t| t.id } end + def test_raise_after_destroy + refute @first.frozen? + + assert_raises(RuntimeError) { + Topic.transaction do + @first.destroy + assert @first.frozen? + raise + end + } + + assert @first.reload + refute @first.frozen? + end + def test_successful Topic.transaction do @first.approved = true diff --git a/activerecord/test/cases/unconnected_test.rb b/activerecord/test/cases/unconnected_test.rb index 5a69054445..e82ca3f93d 100644 --- a/activerecord/test/cases/unconnected_test.rb +++ b/activerecord/test/cases/unconnected_test.rb @@ -7,13 +7,13 @@ class TestUnconnectedAdapter < ActiveRecord::TestCase self.use_transactional_fixtures = false def setup - @underlying = ActiveRecord::Model.connection - @specification = ActiveRecord::Model.remove_connection + @underlying = ActiveRecord::Base.connection + @specification = ActiveRecord::Base.remove_connection end def teardown @underlying = nil - ActiveRecord::Model.establish_connection(@specification) + ActiveRecord::Base.establish_connection(@specification) load_schema if in_memory_db? end diff --git a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb index 174d96aa4e..32d2bf746f 100644 --- a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb @@ -59,4 +59,18 @@ class I18nGenerateMessageValidationTest < ActiveRecord::TestCase assert_equal "Custom taken message", @topic.errors.generate_message(:title, :taken, :value => 'title') end + test "translation for 'taken' can be overridden in activerecord scope" do + I18n.backend.store_translations "en", {activerecord: {errors: {messages: {taken: "Custom taken message" }}}} + assert_equal "Custom taken message", @topic.errors.generate_message(:title, :taken, :value => 'title') + end + + test "translation for 'taken' can be overridden in activerecord model scope" do + I18n.backend.store_translations "en", {activerecord: {errors: {models: {topic: {taken: "Custom taken message" }}}}} + assert_equal "Custom taken message", @topic.errors.generate_message(:title, :taken, :value => 'title') + end + + test "translation for 'taken' can be overridden in activerecord attributes scope" do + I18n.backend.store_translations "en", {activerecord: {errors: {models: {topic: {attributes: {title: {taken: "Custom taken message" }}}}}}} + assert_equal "Custom taken message", @topic.errors.generate_message(:title, :taken, :value => 'title') + end end diff --git a/activerecord/test/models/teapot.rb b/activerecord/test/models/teapot.rb deleted file mode 100644 index b035b18c1b..0000000000 --- a/activerecord/test/models/teapot.rb +++ /dev/null @@ -1,35 +0,0 @@ -class Teapot - # I'm a little teapot, - # Short and stout, - # Here is my handle - # Here is my spout - # When I get all steamed up, - # Hear me shout, - # Tip me over and pour me out! - # - # HELL YEAH TEAPOT SONG - - include ActiveRecord::Model -end - -class OtherTeapot < Teapot -end - -class OMFGIMATEAPOT - def aaahhh - "mmm" - end -end - -class CoolTeapot < OMFGIMATEAPOT - include ActiveRecord::Model - self.table_name = "teapots" -end - -class Ceiling - include ActiveRecord::Model - - class Teapot - include ActiveRecord::Model - end -end diff --git a/activerecord/test/schema/mysql2_specific_schema.rb b/activerecord/test/schema/mysql2_specific_schema.rb index 24a43d7ece..f25f72c481 100644 --- a/activerecord/test/schema/mysql2_specific_schema.rb +++ b/activerecord/test/schema/mysql2_specific_schema.rb @@ -1,15 +1,19 @@ ActiveRecord::Schema.define do - create_table :binary_fields, :force => true do |t| - t.binary :tiny_blob, :limit => 255 - t.binary :normal_blob, :limit => 65535 - t.binary :medium_blob, :limit => 16777215 - t.binary :long_blob, :limit => 2147483647 - t.text :tiny_text, :limit => 255 - t.text :normal_text, :limit => 65535 - t.text :medium_text, :limit => 16777215 - t.text :long_text, :limit => 2147483647 + create_table :binary_fields, force: true do |t| + t.binary :var_binary, limit: 255 + t.binary :var_binary_large, limit: 4095 + t.column :tiny_blob, 'tinyblob', limit: 255 + t.binary :normal_blob, limit: 65535 + t.binary :medium_blob, limit: 16777215 + t.binary :long_blob, limit: 2147483647 + t.text :tiny_text, limit: 255 + t.text :normal_text, limit: 65535 + t.text :medium_text, limit: 16777215 + t.text :long_text, limit: 2147483647 end + add_index :binary_fields, :var_binary + ActiveRecord::Base.connection.execute <<-SQL DROP PROCEDURE IF EXISTS ten; SQL diff --git a/activerecord/test/schema/mysql_specific_schema.rb b/activerecord/test/schema/mysql_specific_schema.rb index 802c08b819..5401c12ed5 100644 --- a/activerecord/test/schema/mysql_specific_schema.rb +++ b/activerecord/test/schema/mysql_specific_schema.rb @@ -1,15 +1,19 @@ ActiveRecord::Schema.define do - create_table :binary_fields, :force => true do |t| - t.binary :tiny_blob, :limit => 255 - t.binary :normal_blob, :limit => 65535 - t.binary :medium_blob, :limit => 16777215 - t.binary :long_blob, :limit => 2147483647 - t.text :tiny_text, :limit => 255 - t.text :normal_text, :limit => 65535 - t.text :medium_text, :limit => 16777215 - t.text :long_text, :limit => 2147483647 + create_table :binary_fields, force: true do |t| + t.binary :var_binary, limit: 255 + t.binary :var_binary_large, limit: 4095 + t.column :tiny_blob, 'tinyblob', limit: 255 + t.binary :normal_blob, limit: 65535 + t.binary :medium_blob, limit: 16777215 + t.binary :long_blob, limit: 2147483647 + t.text :tiny_text, limit: 255 + t.text :normal_text, limit: 65535 + t.text :medium_text, limit: 16777215 + t.text :long_text, limit: 2147483647 end + add_index :binary_fields, :var_binary + ActiveRecord::Base.connection.execute <<-SQL DROP PROCEDURE IF EXISTS ten; SQL diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 2e4ec96933..eec06754a5 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -648,12 +648,6 @@ ActiveRecord::Schema.define do t.datetime :ending end - create_table :teapots, :force => true do |t| - t.string :name - t.string :type - t.timestamps - end - create_table :topics, :force => true do |t| t.string :title t.string :author_name diff --git a/activerecord/test/support/connection.rb b/activerecord/test/support/connection.rb index bea894a583..196b3a9493 100644 --- a/activerecord/test/support/connection.rb +++ b/activerecord/test/support/connection.rb @@ -13,9 +13,9 @@ module ARTest def self.connect puts "Using #{connection_name}" - ActiveRecord::Model.logger = ActiveSupport::Logger.new("debug.log", 0, 100 * 1024 * 1024) - ActiveRecord::Model.configurations = connection_config - ActiveRecord::Model.establish_connection 'arunit' + ActiveRecord::Base.logger = ActiveSupport::Logger.new("debug.log", 0, 100 * 1024 * 1024) + ActiveRecord::Base.configurations = connection_config + ActiveRecord::Base.establish_connection 'arunit' ARUnit2Model.establish_connection 'arunit2' end end |