From bbf6df78a42345eb5343337372fb8602b736222e Mon Sep 17 00:00:00 2001 From: Girish S Date: Fri, 14 Feb 2014 15:32:10 +0530 Subject: SQLite3Adapter now checks for views in table_exists? fixes: 14041 --- .../test/cases/adapters/sqlite3/sqlite3_adapter_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index 02834edf7b..fc5c1ef859 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -276,6 +276,16 @@ module ActiveRecord assert_equal 0, @conn.select_rows(count_sql).first.first end + def test_views + assert_equal %w{ items }, @conn.tables + + @conn.execute <<-eosql + CREATE VIEW items_view AS + select id from items; + eosql + assert @conn.table_exists?('items_view') + end + def test_tables assert_equal %w{ items }, @conn.tables -- cgit v1.2.3 From 82cb3de461d15c1e59b49465ce714594a45b7f5f Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Fri, 29 Aug 2014 09:28:59 +0200 Subject: Revert "Merge pull request #16254 from zuhao/refactor_activerecord_attribute_decorators_test" This reverts commit 16fe19831548f108c113094d106663497fc190d5, reversing changes made to 4c81c8ce533896be28bdc0b055ff30bb9dee1316. The call to `Model.reset_column_information` was to express the intent of the test. `reset_column_information` itself can trigger SQL queries because it checks for a tables existance. Let's move it outside of the block. /cc @sgrif --- activerecord/test/cases/attribute_decorators_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/attribute_decorators_test.rb b/activerecord/test/cases/attribute_decorators_test.rb index cbc2c4e5d7..32977b805f 100644 --- a/activerecord/test/cases/attribute_decorators_test.rb +++ b/activerecord/test/cases/attribute_decorators_test.rb @@ -44,6 +44,7 @@ module ActiveRecord end test "decoration does not eagerly load existing columns" do + Model.reset_column_information assert_no_queries do Model.decorate_attribute_type(:a_string, :test) { |t| StringDecorator.new(t) } end -- cgit v1.2.3 From 7447e0b9943bcd7ba99921c014c32927c2858343 Mon Sep 17 00:00:00 2001 From: Akira Matsuda & Kohei Suzuki Date: Fri, 29 Aug 2014 19:06:56 +0900 Subject: Don't test PostgreSQL implementation details Asserting that the message contains "QUERY PLAN" is enough for the adapter's test. The plan may vary according to number of tuples etc. but that's out of our concern. --- activerecord/test/cases/adapters/postgresql/explain_test.rb | 3 --- 1 file changed, 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/explain_test.rb b/activerecord/test/cases/adapters/postgresql/explain_test.rb index 416f84cb38..19053b6732 100644 --- a/activerecord/test/cases/adapters/postgresql/explain_test.rb +++ b/activerecord/test/cases/adapters/postgresql/explain_test.rb @@ -11,16 +11,13 @@ module ActiveRecord explain = Developer.where(:id => 1).explain assert_match %(EXPLAIN for: SELECT "developers".* FROM "developers" WHERE "developers"."id" = $1), explain assert_match %(QUERY PLAN), explain - assert_match %(Index Scan using developers_pkey on developers), explain end def test_explain_with_eager_loading explain = Developer.where(:id => 1).includes(:audit_logs).explain assert_match %(QUERY PLAN), explain assert_match %(EXPLAIN for: SELECT "developers".* FROM "developers" WHERE "developers"."id" = $1), explain - assert_match %(Index Scan using developers_pkey on developers), explain assert_match %(EXPLAIN for: SELECT "audit_logs".* FROM "audit_logs" WHERE "audit_logs"."developer_id" IN (1)), explain - assert_match %(Seq Scan on audit_logs), explain end end end -- cgit v1.2.3 From 39f3495ebc40e867ad39c006eac08638ff41398d Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sun, 31 Aug 2014 00:04:08 +0900 Subject: AR#reset_column_information sometimes queries via table_exists? so this assertion causes random test fail --- activerecord/test/cases/serialized_attribute_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index f8d87a3661..c5fb491b10 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -16,8 +16,8 @@ class SerializedAttributeTest < ActiveRecord::TestCase end def test_serialize_does_not_eagerly_load_columns + Topic.reset_column_information assert_no_queries do - Topic.reset_column_information Topic.serialize(:content) end end -- cgit v1.2.3 From d5ad2037455a03261d680d143257ba253d8a7112 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 30 Aug 2014 09:32:50 -0700 Subject: MySQL: set connection collation along with the charset Sets the connection collation to the database collation configured in database.yml. Otherwise, `SET NAMES utf8mb4` will use the default collation for that charset (utf8mb4_general_ci) when you may have chosen a different collation, like utf8mb4_unicode_ci. This only applies to literal string comparisons, not column values, so it is unlikely to affect you. --- activerecord/test/cases/adapters/mysql/connection_test.rb | 5 +++++ activerecord/test/cases/adapters/mysql2/connection_test.rb | 5 +++++ activerecord/test/config.example.yml | 2 ++ 3 files changed, 12 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/mysql/connection_test.rb b/activerecord/test/cases/adapters/mysql/connection_test.rb index a7b0addc1b..3dabb1104a 100644 --- a/activerecord/test/cases/adapters/mysql/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql/connection_test.rb @@ -129,6 +129,11 @@ class MysqlConnectionTest < ActiveRecord::TestCase end end + def test_mysql_connection_collation_is_configured + assert_equal 'utf8_unicode_ci', @connection.show_variable('collation_connection') + assert_equal 'utf8_general_ci', ARUnit2Model.connection.show_variable('collation_connection') + end + def test_mysql_default_in_strict_mode result = @connection.exec_query "SELECT @@SESSION.sql_mode" assert_equal [["STRICT_ALL_TABLES"]], result.rows diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb index beedb4f3a1..0b2f59b0eb 100644 --- a/activerecord/test/cases/adapters/mysql2/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb @@ -52,6 +52,11 @@ class MysqlConnectionTest < ActiveRecord::TestCase assert @connection.active? end + def test_mysql_connection_collation_is_configured + assert_equal 'utf8_unicode_ci', @connection.show_variable('collation_connection') + assert_equal 'utf8_general_ci', ARUnit2Model.connection.show_variable('collation_connection') + end + # TODO: Below is a straight up copy/paste from mysql/connection_test.rb # I'm not sure what the correct way is to share these tests between # adapters in minitest. diff --git a/activerecord/test/config.example.yml b/activerecord/test/config.example.yml index a54914c372..ce30cff9e7 100644 --- a/activerecord/test/config.example.yml +++ b/activerecord/test/config.example.yml @@ -55,6 +55,7 @@ connections: arunit: username: rails encoding: utf8 + collation: utf8_unicode_ci arunit2: username: rails encoding: utf8 @@ -63,6 +64,7 @@ connections: arunit: username: rails encoding: utf8 + collation: utf8_unicode_ci arunit2: username: rails encoding: utf8 -- cgit v1.2.3 From 095241dd76e83087bbd9f0d26def8757aa9dc337 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 1 Sep 2014 09:59:28 +0900 Subject: Clear schema cache before each test --- activerecord/test/cases/adapters/mysql2/boolean_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/mysql2/boolean_test.rb b/activerecord/test/cases/adapters/mysql2/boolean_test.rb index f3c711a64b..03627135b2 100644 --- a/activerecord/test/cases/adapters/mysql2/boolean_test.rb +++ b/activerecord/test/cases/adapters/mysql2/boolean_test.rb @@ -9,6 +9,7 @@ class Mysql2BooleanTest < ActiveRecord::TestCase setup do @connection = ActiveRecord::Base.connection + @connection.clear_cache! @connection.create_table("mysql_booleans") do |t| t.boolean "archived" t.string "published", limit: 1 -- cgit v1.2.3 From a4ecc64959613a5be0fc933fd57beb43c5de27a6 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 1 Sep 2014 20:59:08 +0900 Subject: Clear schema cache before each test --- activerecord/test/cases/adapters/mysql/consistency_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/mysql/consistency_test.rb b/activerecord/test/cases/adapters/mysql/consistency_test.rb index 083d533bb2..e972d6b330 100644 --- a/activerecord/test/cases/adapters/mysql/consistency_test.rb +++ b/activerecord/test/cases/adapters/mysql/consistency_test.rb @@ -12,6 +12,7 @@ class MysqlConsistencyTest < ActiveRecord::TestCase ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false @connection = ActiveRecord::Base.connection + @connection.clear_cache! @connection.create_table("mysql_consistency") do |t| t.boolean "a_bool" t.string "a_string" -- cgit v1.2.3 From 41e48c2874eb8b4c419b7803976646005739d5bd Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 1 Sep 2014 22:35:56 -0700 Subject: MySQL: skip GTID-unsafe statement tests when enforce_gtid_consistency is enabled --- .../test/cases/adapters/mysql2/schema_test.rb | 14 +++++---- activerecord/test/cases/helper.rb | 4 +++ activerecord/test/cases/migration_test.rb | 36 ++++++++++++---------- 3 files changed, 31 insertions(+), 23 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/mysql2/schema_test.rb b/activerecord/test/cases/adapters/mysql2/schema_test.rb index 43c9116b5a..1b7e60565d 100644 --- a/activerecord/test/cases/adapters/mysql2/schema_test.rb +++ b/activerecord/test/cases/adapters/mysql2/schema_test.rb @@ -66,12 +66,14 @@ module ActiveRecord assert_equal :fulltext, index_c.type end - def test_drop_temporary_table - @connection.transaction do - @connection.create_table(:temp_table, temporary: true) - # if it doesn't properly say DROP TEMPORARY TABLE, the transaction commit - # will complain that no transaction is active - @connection.drop_table(:temp_table, temporary: true) + unless mysql_enforcing_gtid_consistency? + def test_drop_temporary_table + @connection.transaction do + @connection.create_table(:temp_table, temporary: true) + # if it doesn't properly say DROP TEMPORARY TABLE, the transaction commit + # will complain that no transaction is active + @connection.drop_table(:temp_table, temporary: true) + end end end end diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 209ef597db..1bccd21f84 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -50,6 +50,10 @@ def mysql_56? ActiveRecord::Base.connection.send(:version).join(".") >= "5.6.0" end +def mysql_enforcing_gtid_consistency? + current_adapter?(:MysqlAdapter, :Mysql2Adapter) && 'ON' == ActiveRecord::Base.connection.show_variable('enforce_gtid_consistency') +end + def supports_savepoints? ActiveRecord::Base.connection.supports_savepoints? end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 633077622c..270e446c69 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -430,30 +430,32 @@ class MigrationTest < ActiveRecord::TestCase Person.connection.drop_table :binary_testings rescue nil end - def test_create_table_with_query - Person.connection.drop_table :table_from_query_testings rescue nil - Person.connection.create_table(:person, force: true) + unless mysql_enforcing_gtid_consistency? + def test_create_table_with_query + Person.connection.drop_table :table_from_query_testings rescue nil + Person.connection.create_table(:person, force: true) - Person.connection.create_table :table_from_query_testings, as: "SELECT id FROM person" + Person.connection.create_table :table_from_query_testings, as: "SELECT id FROM person" - columns = Person.connection.columns(:table_from_query_testings) - assert_equal 1, columns.length - assert_equal "id", columns.first.name + columns = Person.connection.columns(:table_from_query_testings) + assert_equal 1, columns.length + assert_equal "id", columns.first.name - Person.connection.drop_table :table_from_query_testings rescue nil - end + Person.connection.drop_table :table_from_query_testings rescue nil + end - def test_create_table_with_query_from_relation - Person.connection.drop_table :table_from_query_testings rescue nil - Person.connection.create_table(:person, force: true) + def test_create_table_with_query_from_relation + Person.connection.drop_table :table_from_query_testings rescue nil + Person.connection.create_table(:person, force: true) - Person.connection.create_table :table_from_query_testings, as: Person.select(:id) + Person.connection.create_table :table_from_query_testings, as: Person.select(:id) - columns = Person.connection.columns(:table_from_query_testings) - assert_equal 1, columns.length - assert_equal "id", columns.first.name + columns = Person.connection.columns(:table_from_query_testings) + assert_equal 1, columns.length + assert_equal "id", columns.first.name - Person.connection.drop_table :table_from_query_testings rescue nil + Person.connection.drop_table :table_from_query_testings rescue nil + end end if current_adapter? :OracleAdapter -- cgit v1.2.3 From 2f52f969885b2834198de0045748436a4651a94e Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Tue, 2 Sep 2014 23:48:23 +0930 Subject: Leave all our tests as order_dependent! for now We're seeing too many failures to believe otherwise. This reverts commits bc116a55ca3dd9f63a1f1ca7ade3623885adcc57, cbde413df3839e06dd14e3c220e9800af91e83ab, bf0a67931dd8e58f6f878b9510ae818ae1f29a3a, and 2440933fe2c27b27bcafcd9019717800db2641aa. --- activerecord/test/cases/helper.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 1bccd21f84..1a31c2ec4a 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -208,3 +208,8 @@ module InTimeZone end require 'mocha/setup' # FIXME: stop using mocha + +# FIXME: we have tests that depend on run order, we should fix that and +# remove this method call. +require 'active_support/test_case' +ActiveSupport::TestCase.my_tests_are_order_dependent! -- cgit v1.2.3 From 252165ea46932f8a7304ce40ba6e30dc1b490586 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Wed, 3 Sep 2014 02:51:27 +0900 Subject: Remove 'if exists' from drop table statement then use `table_exists?` Since 'drop table if exists' statement does not always work with some databases such as Oracle. --- activerecord/test/cases/associations/required_test.rb | 4 ++-- activerecord/test/cases/attribute_decorators_test.rb | 2 +- activerecord/test/cases/migration/foreign_key_test.rb | 4 ++-- activerecord/test/cases/schema_dumper_test.rb | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/required_test.rb b/activerecord/test/cases/associations/required_test.rb index a6934a056e..24a332bb4a 100644 --- a/activerecord/test/cases/associations/required_test.rb +++ b/activerecord/test/cases/associations/required_test.rb @@ -18,8 +18,8 @@ class RequiredAssociationsTest < ActiveRecord::TestCase end teardown do - @connection.execute("DROP TABLE IF EXISTS parents") - @connection.execute("DROP TABLE IF EXISTS children") + @connection.execute("DROP TABLE parents") if @connection.table_exists? 'parents' + @connection.execute("DROP TABLE children") if @connection.table_exists? 'children' end test "belongs_to associations are not required by default" do diff --git a/activerecord/test/cases/attribute_decorators_test.rb b/activerecord/test/cases/attribute_decorators_test.rb index 32977b805f..644876752e 100644 --- a/activerecord/test/cases/attribute_decorators_test.rb +++ b/activerecord/test/cases/attribute_decorators_test.rb @@ -28,7 +28,7 @@ module ActiveRecord teardown do return unless @connection - @connection.execute 'DROP TABLE IF EXISTS attribute_decorators_model' + @connection.execute 'DROP TABLE attribute_decorators_model' if @connection.table_exists? 'attribute_decorators_model' Model.attribute_type_decorations.clear Model.reset_column_information end diff --git a/activerecord/test/cases/migration/foreign_key_test.rb b/activerecord/test/cases/migration/foreign_key_test.rb index c985092b4c..ba28d55e4c 100644 --- a/activerecord/test/cases/migration/foreign_key_test.rb +++ b/activerecord/test/cases/migration/foreign_key_test.rb @@ -29,8 +29,8 @@ module ActiveRecord teardown do if defined?(@connection) - @connection.execute "DROP TABLE IF EXISTS astronauts" - @connection.execute "DROP TABLE IF EXISTS rockets" + @connection.execute "DROP TABLE astronauts" if @connection.table_exists? 'astronauts' + @connection.execute "DROP TABLE rockets" if @connection.table_exists? 'rockets' end end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 066e6b6468..ff47a7a355 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -441,7 +441,7 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase teardown do return unless @connection - @connection.execute 'DROP TABLE IF EXISTS defaults' + @connection.execute 'DROP TABLE defaults' if @connection.table_exists? 'defaults' end def test_schema_dump_defaults_with_universally_supported_types -- cgit v1.2.3 From 3cce7628e4e3a4f43ed53db1f2756c4d9d1f1e00 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 3 Sep 2014 11:17:27 +0900 Subject: Reset ActiveRecord::SchemaDumper.ignore_tables value after changed in tests --- activerecord/test/cases/schema_dumper_test.rb | 38 +++++++--------------- activerecord/test/support/schema_dumping_helper.rb | 9 +++++ 2 files changed, 21 insertions(+), 26 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index ff47a7a355..d5584ad06c 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -2,6 +2,7 @@ require "cases/helper" require 'support/schema_dumping_helper' class SchemaDumperTest < ActiveRecord::TestCase + include SchemaDumpingHelper self.use_transactional_fixtures = false setup do @@ -10,9 +11,11 @@ class SchemaDumperTest < ActiveRecord::TestCase def standard_dump @stream = StringIO.new - ActiveRecord::SchemaDumper.ignore_tables = [] + old_ignore_tables, ActiveRecord::SchemaDumper.ignore_tables = ActiveRecord::SchemaDumper.ignore_tables, [] ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, @stream) @stream.string + ensure + ActiveRecord::SchemaDumper.ignore_tables = old_ignore_tables end def test_dump_schema_information_outputs_lexically_ordered_versions @@ -90,20 +93,12 @@ class SchemaDumperTest < ActiveRecord::TestCase end def test_schema_dump_includes_not_null_columns - stream = StringIO.new - - ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string + output = dump_all_table_schema([/^[^r]/]) assert_match %r{null: false}, output end def test_schema_dump_includes_limit_constraint_for_integer_columns - stream = StringIO.new - - ActiveRecord::SchemaDumper.ignore_tables = [/^(?!integer_limits)/] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string + output = dump_all_table_schema([/^(?!integer_limits)/]) if current_adapter?(:PostgreSQLAdapter) assert_match %r{c_int_1.*limit: 2}, output @@ -150,22 +145,14 @@ class SchemaDumperTest < ActiveRecord::TestCase end def test_schema_dump_with_string_ignored_table - stream = StringIO.new - - ActiveRecord::SchemaDumper.ignore_tables = ['accounts'] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string + output = dump_all_table_schema(['accounts']) assert_no_match %r{create_table "accounts"}, output assert_match %r{create_table "authors"}, output assert_no_match %r{create_table "schema_migrations"}, output end def test_schema_dump_with_regexp_ignored_table - stream = StringIO.new - - ActiveRecord::SchemaDumper.ignore_tables = [/^account/] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string + output = dump_all_table_schema([/^account/]) assert_no_match %r{create_table "accounts"}, output assert_match %r{create_table "authors"}, output assert_no_match %r{create_table "schema_migrations"}, output @@ -173,10 +160,12 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_schema_dump_illegal_ignored_table_value stream = StringIO.new - ActiveRecord::SchemaDumper.ignore_tables = [5] + old_ignore_tables, ActiveRecord::SchemaDumper.ignore_tables = ActiveRecord::SchemaDumper.ignore_tables, [5] assert_raise(StandardError) do ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) end + ensure + ActiveRecord::SchemaDumper.ignore_tables = old_ignore_tables end def test_schema_dumps_index_columns_in_right_order @@ -245,10 +234,7 @@ class SchemaDumperTest < ActiveRecord::TestCase end def test_schema_dump_includes_decimal_options - stream = StringIO.new - ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - output = stream.string + output = dump_all_table_schema([/^[^n]/]) assert_match %r{precision: 3,[[:space:]]+scale: 2,[[:space:]]+default: 2.78}, output end diff --git a/activerecord/test/support/schema_dumping_helper.rb b/activerecord/test/support/schema_dumping_helper.rb index 2ae8d299e5..2d1651454d 100644 --- a/activerecord/test/support/schema_dumping_helper.rb +++ b/activerecord/test/support/schema_dumping_helper.rb @@ -8,4 +8,13 @@ module SchemaDumpingHelper ensure ActiveRecord::SchemaDumper.ignore_tables = old_ignore_tables end + + def dump_all_table_schema(ignore_tables) + old_ignore_tables, ActiveRecord::SchemaDumper.ignore_tables = ActiveRecord::SchemaDumper.ignore_tables, ignore_tables + stream = StringIO.new + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + stream.string + ensure + ActiveRecord::SchemaDumper.ignore_tables = old_ignore_tables + end end -- cgit v1.2.3 From ded17a498aaed63a80fe0bbf4d184aa3e468b023 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 3 Sep 2014 17:31:38 +0200 Subject: schema loading rake tasks maintain database connection for current env. [Joshua Cody & Yves Senn] Closes #16757. Prior to this patch schema loading rake tasks had the potential to leak a connection to a different database. This had side-effects when rake tasks operating on the current connection (like `db:seed`) were chained. --- activerecord/test/cases/tasks/database_tasks_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index 01d373b691..2fa033ed45 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -309,6 +309,7 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.expects(:purge). with('database' => 'prod-db') + ActiveRecord::Base.expects(:establish_connection).with(:production) ActiveRecord::Tasks::DatabaseTasks.purge_current('production') end -- cgit v1.2.3 From 94b7328b08fcc55e82bfcaa34a25ae718921ae1c Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Thu, 4 Sep 2014 00:26:27 -0700 Subject: Enums shouldn't ruin people's anniversaries Added a few more methods on Module/Class to the dangerous class methods blacklist. (Technically, allocate and new are already protected currently because we happen to redefine them in the current implantation.) Closes #16792 --- activerecord/test/cases/enum_test.rb | 3 ++- activerecord/test/cases/scoping/named_scoping_test.rb | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 3b2f0dfe07..346fcab6ea 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -194,7 +194,8 @@ class EnumTest < ActiveRecord::TestCase :valid, # generates #valid?, which conflicts with an AR method :save, # generates #save!, which conflicts with an AR method :proposed, # same value as an existing enum - :public, :private, :protected, # generates a method that conflict with ruby words + :public, :private, :protected, # some important methods on Module and Class + :name, :parent, :superclass ] conflicts.each_with_index do |value, i| diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index 59ec2dd6a4..d3546bd471 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -291,9 +291,12 @@ class NamedScopingTest < ActiveRecord::TestCase :relation, # private class method on AR::Base :new, # redefined class method on AR::Base :all, # a default scope - :public, + :public, # some imporant methods on Module and Class :protected, - :private + :private, + :name, + :parent, + :superclass ] non_conflicts = [ -- cgit v1.2.3 From 4abbdbdf1656e9bed6f744723a901cbaf6878df4 Mon Sep 17 00:00:00 2001 From: Sammy Larbi Date: Sun, 31 Aug 2014 14:18:02 -0500 Subject: Skip StatementCache for eager loaded associations (Fixes #16761) Eagerly loaded collection and singular associations are ignored by the StatementCache, which causes errors when the queries they generate reference columns that were not eagerly loaded. This commit skips the creation of the StatementCache as a fix for these scenarios. --- .../test/cases/scoping/default_scoping_test.rb | 21 ++++++++++++++++++++- activerecord/test/models/comment.rb | 5 +++++ activerecord/test/models/post.rb | 6 ++++++ 3 files changed, 31 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/scoping/default_scoping_test.rb b/activerecord/test/cases/scoping/default_scoping_test.rb index 9a4d8c6740..a5c4404175 100644 --- a/activerecord/test/cases/scoping/default_scoping_test.rb +++ b/activerecord/test/cases/scoping/default_scoping_test.rb @@ -1,9 +1,10 @@ require 'cases/helper' require 'models/post' +require 'models/comment' require 'models/developer' class DefaultScopingTest < ActiveRecord::TestCase - fixtures :developers, :posts + fixtures :developers, :posts, :comments def test_default_scope expected = Developer.all.merge!(:order => 'salary DESC').to_a.collect { |dev| dev.salary } @@ -378,6 +379,24 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal 1, DeveloperWithIncludes.where(:audit_logs => { :message => 'foo' }).count end + def test_default_scope_with_references_works_through_collection_association + post = PostWithCommentWithDefaultScopeReferencesAssociation.create!(title: "Hello World", body: "Here we go.") + comment = post.comment_with_default_scope_references_associations.create!(body: "Great post.", developer_id: Developer.first.id) + assert_equal comment, post.comment_with_default_scope_references_associations.to_a.first + end + + def test_default_scope_with_references_works_through_association + post = PostWithCommentWithDefaultScopeReferencesAssociation.create!(title: "Hello World", body: "Here we go.") + comment = post.comment_with_default_scope_references_associations.create!(body: "Great post.", developer_id: Developer.first.id) + assert_equal comment, post.first_comment + end + + def test_default_scope_with_references_works_with_find_by + post = PostWithCommentWithDefaultScopeReferencesAssociation.create!(title: "Hello World", body: "Here we go.") + comment = post.comment_with_default_scope_references_associations.create!(body: "Great post.", developer_id: Developer.first.id) + assert_equal comment, CommentWithDefaultScopeReferencesAssociation.find_by(id: comment.id) + end + unless in_memory_db? def test_default_scope_is_threadsafe threads = [] diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 7a88299d08..b38b17e90e 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -52,3 +52,8 @@ class CommentThatAutomaticallyAltersPostBody < Comment comment.post.update_attributes(body: "Automatically altered") end end + +class CommentWithDefaultScopeReferencesAssociation < Comment + default_scope ->{ includes(:developer).order('developers.name').references(:developer) } + belongs_to :developer +end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 56a31011da..256b720c9a 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -218,3 +218,9 @@ class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base post.comments.load end end + +class PostWithCommentWithDefaultScopeReferencesAssociation < ActiveRecord::Base + self.table_name = 'posts' + has_many :comment_with_default_scope_references_associations, foreign_key: :post_id + has_one :first_comment, class_name: "CommentWithDefaultScopeReferencesAssociation", foreign_key: :post_id +end -- cgit v1.2.3 From 2e6625fb775783cdbc721391be18a073a5b9a9c8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 4 Sep 2014 14:40:10 -0700 Subject: always reorder bind parameters. fixes #15920 --- activerecord/test/cases/associations_test.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index f663b5490c..635c657d9b 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -23,7 +23,7 @@ require 'models/interest' class AssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects, - :computers, :people, :readers + :computers, :people, :readers, :authors, :author_favorites def test_eager_loading_should_not_change_count_of_children liquid = Liquid.create(:name => 'salty') @@ -35,6 +35,13 @@ class AssociationsTest < ActiveRecord::TestCase assert_equal 1, liquids[0].molecules.length end + def test_subselect + author = authors :david + favs = author.author_favorites + fav2 = author.author_favorites.where(:author => Author.where(id: author.id)).to_a + assert_equal favs, fav2 + end + def test_clear_association_cache_stored firm = Firm.find(1) assert_kind_of Firm, firm -- cgit v1.2.3 From 312e4cda78e04d04308a3b9bee822f540d11103f Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Fri, 5 Sep 2014 09:49:22 +0900 Subject: Load :developers fixtures where expecting a Developer to be there --- activerecord/test/cases/relation/merging_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relation/merging_test.rb b/activerecord/test/cases/relation/merging_test.rb index 0d537fbfe3..8f40a1890d 100644 --- a/activerecord/test/cases/relation/merging_test.rb +++ b/activerecord/test/cases/relation/merging_test.rb @@ -117,7 +117,7 @@ class RelationMergingTest < ActiveRecord::TestCase end class MergingDifferentRelationsTest < ActiveRecord::TestCase - fixtures :posts, :authors + fixtures :posts, :authors, :developers test "merging where relations" do hello_by_bob = Post.where(body: "hello").joins(:author). -- cgit v1.2.3 From a8827cb9afdf122c003fbff1bfcc28bda2994d04 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Fri, 5 Sep 2014 04:27:54 -0700 Subject: Fixed regression with referencing polymorphic assoc in eager-load This is cased by 03118bc + 9b5d603. The first commit referenced the undefined local variable `column` when it should be using `reflection.type` as the lookup key. The second commit changed `build_arel` to not modify the `bind_values` in- place so we need to combine the arel's `bind_values` with the relation's when building the SQL. Fixes #16591 Related #15821 / #15892 / 7aeca50 --- activerecord/test/cases/associations/eager_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 21912fdf0f..b852bd3536 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1290,4 +1290,14 @@ class EagerAssociationTest < ActiveRecord::TestCase david = Author.where(id: "1").eager_load(:readonly_comments).first! assert david.readonly_comments.first.readonly? end + + test "preloading a polymorphic association with references to the associated table" do + post = Post.includes(:tags).references(:tags).where('tags.name = ?', 'General').first + assert_equal posts(:welcome), post + end + + test "eager-loading a polymorphic association with references to the associated table" do + post = Post.eager_load(:tags).where('tags.name = ?', 'General').first + assert_equal posts(:welcome), post + end end -- cgit v1.2.3 From 0ab9301f2acb5e14846c4241701a9d9d57ce35ce Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Wed, 9 Jul 2014 21:10:49 +0000 Subject: Added enable_extension! to helper --- activerecord/test/cases/adapters/postgresql/array_test.rb | 8 ++------ activerecord/test/cases/adapters/postgresql/citext_test.rb | 9 ++------- activerecord/test/cases/adapters/postgresql/ltree_test.rb | 4 +--- activerecord/test/cases/adapters/postgresql/uuid_test.rb | 11 ++++++----- activerecord/test/cases/helper.rb | 14 +++++++++++--- activerecord/test/cases/migration/rename_table_test.rb | 3 ++- activerecord/test/schema/schema.rb | 2 +- 7 files changed, 25 insertions(+), 26 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb index 8df1b7d18c..ff553c3f1a 100644 --- a/activerecord/test/cases/adapters/postgresql/array_test.rb +++ b/activerecord/test/cases/adapters/postgresql/array_test.rb @@ -12,12 +12,7 @@ class PostgresqlArrayTest < ActiveRecord::TestCase def setup @connection = ActiveRecord::Base.connection - unless @connection.extension_enabled?('hstore') - @connection.enable_extension 'hstore' - @connection.commit_db_transaction - end - - @connection.reconnect! + enable_extension!('hstore', @connection) @connection.transaction do @connection.create_table('pg_arrays') do |t| @@ -32,6 +27,7 @@ class PostgresqlArrayTest < ActiveRecord::TestCase teardown do @connection.execute 'drop table if exists pg_arrays' + disable_extension!('hstore', @connection) end def test_column diff --git a/activerecord/test/cases/adapters/postgresql/citext_test.rb b/activerecord/test/cases/adapters/postgresql/citext_test.rb index 2acb64f81c..cb024463c9 100644 --- a/activerecord/test/cases/adapters/postgresql/citext_test.rb +++ b/activerecord/test/cases/adapters/postgresql/citext_test.rb @@ -10,12 +10,7 @@ if ActiveRecord::Base.connection.supports_extensions? def setup @connection = ActiveRecord::Base.connection - unless @connection.extension_enabled?('citext') - @connection.enable_extension 'citext' - @connection.commit_db_transaction - end - - @connection.reconnect! + enable_extension!('citext', @connection) @connection.create_table('citexts') do |t| t.citext 'cival' @@ -24,7 +19,7 @@ if ActiveRecord::Base.connection.supports_extensions? teardown do @connection.execute 'DROP TABLE IF EXISTS citexts;' - @connection.execute 'DROP EXTENSION IF EXISTS citext CASCADE;' + disable_extension!('citext', @connection) end def test_citext_enabled diff --git a/activerecord/test/cases/adapters/postgresql/ltree_test.rb b/activerecord/test/cases/adapters/postgresql/ltree_test.rb index 889e369bd6..2968109346 100644 --- a/activerecord/test/cases/adapters/postgresql/ltree_test.rb +++ b/activerecord/test/cases/adapters/postgresql/ltree_test.rb @@ -9,9 +9,7 @@ class PostgresqlLtreeTest < ActiveRecord::TestCase def setup @connection = ActiveRecord::Base.connection - unless @connection.extension_enabled?('ltree') - @connection.enable_extension 'ltree' - end + enable_extension!('ltree', @connection) @connection.transaction do @connection.create_table('ltrees') do |t| diff --git a/activerecord/test/cases/adapters/postgresql/uuid_test.rb b/activerecord/test/cases/adapters/postgresql/uuid_test.rb index 6e7920b814..ae5b409f99 100644 --- a/activerecord/test/cases/adapters/postgresql/uuid_test.rb +++ b/activerecord/test/cases/adapters/postgresql/uuid_test.rb @@ -118,7 +118,7 @@ class PostgresqlUUIDGenerationTest < ActiveRecord::TestCase end setup do - enable_uuid_ossp!(connection) + enable_extension!('uuid-ossp', connection) connection.create_table('pg_uuids', id: :uuid, default: 'uuid_generate_v1()') do |t| t.string 'name' @@ -144,6 +144,7 @@ class PostgresqlUUIDGenerationTest < ActiveRecord::TestCase drop_table "pg_uuids" drop_table 'pg_uuids_2' connection.execute 'DROP FUNCTION IF EXISTS my_uuid_generator();' + disable_extension!('uuid-ossp', connection) end if ActiveRecord::Base.connection.supports_extensions? @@ -189,7 +190,7 @@ class PostgresqlUUIDTestNilDefault < ActiveRecord::TestCase include PostgresqlUUIDHelper setup do - enable_uuid_ossp!(connection) + enable_extension!('uuid-ossp', connection) connection.create_table('pg_uuids', id: false) do |t| t.primary_key :id, :uuid, default: nil @@ -199,6 +200,7 @@ class PostgresqlUUIDTestNilDefault < ActiveRecord::TestCase teardown do drop_table "pg_uuids" + disable_extension!('uuid-ossp', connection) end if ActiveRecord::Base.connection.supports_extensions? @@ -226,7 +228,7 @@ class PostgresqlUUIDTestInverseOf < ActiveRecord::TestCase end setup do - enable_uuid_ossp!(connection) + enable_extension!('uuid-ossp', connection) connection.transaction do connection.create_table('pg_uuid_posts', id: :uuid) do |t| @@ -240,10 +242,9 @@ class PostgresqlUUIDTestInverseOf < ActiveRecord::TestCase end teardown do - connection.transaction do drop_table "pg_uuid_comments" drop_table "pg_uuid_posts" - end + disable_extension!('uuid-ossp', connection) end if ActiveRecord::Base.connection.supports_extensions? diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 1a31c2ec4a..3d9328b198 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -119,15 +119,23 @@ def verify_default_timezone_config end end -def enable_uuid_ossp!(connection) +def enable_extension!(extension, connection) return false unless connection.supports_extensions? - return connection.reconnect! if connection.extension_enabled?('uuid-ossp') + return connection.reconnect! if connection.extension_enabled?(extension) - connection.enable_extension 'uuid-ossp' + connection.enable_extension extension connection.commit_db_transaction connection.reconnect! end +def disable_extension!(extension, connection) + return false unless connection.supports_extensions? + return true unless connection.extension_enabled?(extension) + + connection.disable_extension extension + connection.reconnect! +end + unless ENV['FIXTURE_DEBUG'] module ActiveRecord::TestFixtures::ClassMethods def try_to_load_dependency_with_silence(*args) diff --git a/activerecord/test/cases/migration/rename_table_test.rb b/activerecord/test/cases/migration/rename_table_test.rb index ba39fb1dec..c8b3f75e10 100644 --- a/activerecord/test/cases/migration/rename_table_test.rb +++ b/activerecord/test/cases/migration/rename_table_test.rb @@ -78,11 +78,12 @@ module ActiveRecord end def test_renaming_table_doesnt_attempt_to_rename_non_existent_sequences - enable_uuid_ossp!(connection) + enable_extension!('uuid-ossp', connection) connection.create_table :cats, id: :uuid assert_nothing_raised { rename_table :cats, :felines } assert connection.table_exists? :felines ensure + disable_extension!('uuid-ossp', connection) connection.drop_table :cats if connection.table_exists? :cats connection.drop_table :felines if connection.table_exists? :felines end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 0584df87c6..3161edd58e 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -10,7 +10,7 @@ ActiveRecord::Schema.define do #put adapter specific setup here case adapter_name when "PostgreSQL" - enable_uuid_ossp!(ActiveRecord::Base.connection) + enable_extension!('uuid-ossp', ActiveRecord::Base.connection) create_table :uuid_parents, id: :uuid, force: true do |t| t.string :name end -- cgit v1.2.3 From ad4ea980900b96a92e8248bdc6c09973d6bd8254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 5 Sep 2014 18:43:10 -0300 Subject: Do not mark object as persisted after an association is saved Callback order in Active Record objects are important. Users should not define callbacks before the association definition or surprising behaviours like the described at #3798 will happen. This callback order dependency is documented at https://github.com/rails/rails/blob/31bfcdc77ca0d8cec9b5fe513bdc6f05814dd4f1/activerecord/lib/active_record/associations.rb#L1222-1227. This reverts #15728. Fixes #16620. --- .../cases/associations/has_many_associations_test.rb | 14 ++++++++++++++ .../has_many_through_associations_test.rb | 19 ------------------- 2 files changed, 14 insertions(+), 19 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 1d28c8dac9..e34b993029 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -28,6 +28,7 @@ require 'models/college' require 'models/student' require 'models/pirate' require 'models/ship' +require 'models/tyre' class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase fixtures :authors, :posts, :comments @@ -1941,4 +1942,17 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [], authors(:david).posts_with_signature.map(&:title) end + + test 'associations autosaves when object is already persited' do + bulb = Bulb.create! + tyre = Tyre.create! + + car = Car.create! do |c| + c.bulbs << bulb + c.tyres << tyre + end + + assert_equal 1, car.bulbs.count + assert_equal 1, car.tyres.count + 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 d9659a72c5..cddf1a1f72 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -1147,23 +1147,4 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase club.members << member assert_equal 1, SuperMembership.where(member_id: member.id, club_id: club.id).count end - - class ClubWithCallbacks < ActiveRecord::Base - self.table_name = 'clubs' - after_create :add_a_member - - has_many :memberships, inverse_of: :club, foreign_key: :club_id - has_many :members, through: :memberships - - def add_a_member - members << Member.last - end - end - - def test_has_many_with_callback_before_association - Member.create! - club = ClubWithCallbacks.create! - - assert_equal 1, club.reload.memberships.count - end end -- cgit v1.2.3 From da2f61947db287b5ba0343905da9316eecfd43f3 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 6 Sep 2014 20:06:40 +0900 Subject: Dynamically modified schema and association would not be correctly reset This fixes <"SQLite3::SQLException: no such column: legacy_things.person_id: SELECT \"legacy_things\".* FROM \"legacy_things\" WHERE \"legacy_things\".\"person_id\" = ?"> in OptimisticLockingTest#test_lock_destroy --- activerecord/test/cases/locking_test.rb | 25 ++++++++--------------- activerecord/test/models/person.rb | 2 ++ activerecord/test/models/personal_legacy_thing.rb | 4 ++++ activerecord/test/schema/schema.rb | 6 ++++++ 4 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 activerecord/test/models/personal_legacy_thing.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 713999df84..5a4b1fb919 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -5,6 +5,7 @@ require 'models/job' require 'models/reader' require 'models/ship' require 'models/legacy_thing' +require 'models/personal_legacy_thing' require 'models/reference' require 'models/string_key_object' require 'models/car' @@ -311,32 +312,24 @@ class OptimisticLockingWithSchemaChangeTest < ActiveRecord::TestCase # See Lighthouse ticket #1966 def test_destroy_dependents - # Establish dependent relationship between People and LegacyThing - add_counter_column_to(Person, 'legacy_things_count') - LegacyThing.connection.add_column LegacyThing.table_name, 'person_id', :integer - LegacyThing.reset_column_information - LegacyThing.class_eval do - belongs_to :person, :counter_cache => true - end - Person.class_eval do - has_many :legacy_things, :dependent => :destroy - end + # Establish dependent relationship between Person and PersonalLegacyThing + add_counter_column_to(Person, 'personal_legacy_things_count') + PersonalLegacyThing.reset_column_information # Make sure that counter incrementing doesn't cause problems p1 = Person.new(:first_name => 'fjord') p1.save! - t = LegacyThing.new(:person => p1) + t = PersonalLegacyThing.new(:person => p1) t.save! p1.reload - assert_equal 1, p1.legacy_things_count + assert_equal 1, p1.personal_legacy_things_count assert p1.destroy assert_equal true, p1.frozen? assert_raises(ActiveRecord::RecordNotFound) { Person.find(p1.id) } - assert_raises(ActiveRecord::RecordNotFound) { LegacyThing.find(t.id) } + assert_raises(ActiveRecord::RecordNotFound) { PersonalLegacyThing.find(t.id) } ensure - remove_counter_column_from(Person, 'legacy_things_count') - LegacyThing.connection.remove_column LegacyThing.table_name, 'person_id' - LegacyThing.reset_column_information + remove_counter_column_from(Person, 'personal_legacy_things_count') + PersonalLegacyThing.reset_column_information end private diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index c7e54e7b63..ad12f00d42 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -30,6 +30,8 @@ class Person < ActiveRecord::Base has_many :agents_of_agents, :through => :agents, :source => :agents belongs_to :number1_fan, :class_name => 'Person' + has_many :personal_legacy_things, :dependent => :destroy + has_many :agents_posts, :through => :agents, :source => :posts has_many :agents_posts_authors, :through => :agents_posts, :source => :author has_many :essays, primary_key: "first_name", foreign_key: "writer_id" diff --git a/activerecord/test/models/personal_legacy_thing.rb b/activerecord/test/models/personal_legacy_thing.rb new file mode 100644 index 0000000000..a7ee3a0bca --- /dev/null +++ b/activerecord/test/models/personal_legacy_thing.rb @@ -0,0 +1,4 @@ +class PersonalLegacyThing < ActiveRecord::Base + self.locking_column = :version + belongs_to :person, :counter_cache => true +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 3161edd58e..10de3d34cb 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -546,6 +546,12 @@ ActiveRecord::Schema.define do t.column :treasure_id, :integer end + create_table :personal_legacy_things, force: true do |t| + t.integer :tps_report_number + t.integer :person_id + t.integer :version, null: false, default: 0 + end + create_table :pets, primary_key: :pet_id, force: true do |t| t.string :name t.integer :owner_id, :integer -- cgit v1.2.3 From 5663f06656f0c2e8f559e1c137794b5f48c1b8ce Mon Sep 17 00:00:00 2001 From: Kuldeep Aggarwal Date: Sat, 6 Sep 2014 21:44:08 +0530 Subject: disable migrations logging while running test cases for AR schema tests --- activerecord/test/cases/ar_schema_test.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb index 3f5858714a..8e3d22eb57 100644 --- a/activerecord/test/cases/ar_schema_test.rb +++ b/activerecord/test/cases/ar_schema_test.rb @@ -6,6 +6,7 @@ if ActiveRecord::Base.connection.supports_migrations? self.use_transactional_fixtures = false def setup + ActiveRecord::Migration.verbose = false @connection = ActiveRecord::Base.connection ActiveRecord::SchemaMigration.drop_table end @@ -16,6 +17,7 @@ if ActiveRecord::Base.connection.supports_migrations? @connection.drop_table :nep_schema_migrations rescue nil @connection.drop_table :has_timestamps rescue nil ActiveRecord::SchemaMigration.delete_all rescue nil + ActiveRecord::Migration.verbose = true end def test_has_no_primary_key -- cgit v1.2.3 From 72d1663bc7353813f3ec5f4e841a2243baedb473 Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Sat, 6 Sep 2014 10:25:36 +0300 Subject: Fix query with nested array in Active Record `User.where(id: [[1,2],3])` was equal to `User.where(id:[1, 2, 3])` in Rails 4.1.x but because of some refactoring in Arel this stopped working in 4.2.0. This fixes it in Rails. [Dan Olson & Cristian Bica] --- activerecord/test/cases/finder_test.rb | 28 ++++++++++++++++++++++++++ activerecord/test/cases/relation/where_test.rb | 4 +++- activerecord/test/fixtures/developers.yml | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index befbec4e1b..fc91b728c2 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -521,6 +521,34 @@ class FinderTest < ActiveRecord::TestCase assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [1..2, 3, 5, 6..8, 9]).to_a.map(&:id).sort end + def test_find_on_hash_conditions_with_array_of_ranges + assert_equal [1,2,6,7,8], Comment.where(id: [1..2, 6..8]).to_a.map(&:id).sort + end + + def test_find_on_hash_conditions_with_nested_array_of_integers_and_ranges + assert_deprecated do + assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [[1..2], 3, [5], 6..8, 9]).to_a.map(&:id).sort + end + end + + def test_find_on_hash_conditions_with_array_of_integers_and_arrays + assert_deprecated do + assert_equal [1,2,3,5,6,7,8,9], Comment.where(id: [[1, 2], 3, 5, [6, [7], 8], 9]).to_a.map(&:id).sort + end + end + + def test_find_on_hash_conditions_with_nested_array_of_integers_and_ranges_and_nils + assert_deprecated do + assert_equal [1,3,4,5], Topic.where(parent_id: [[2..6], nil]).to_a.map(&:id).sort + end + end + + def test_find_on_hash_conditions_with_nested_array_of_integers_and_ranges_and_more_nils + assert_deprecated do + assert_equal [], Topic.where(parent_id: [[7..10, nil, [nil]], [nil]]).to_a.map(&:id).sort + end + end + def test_find_on_multiple_hash_conditions assert Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: false).find(1) assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: true).find(1) } diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index 580ea98910..39c8afdd23 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -181,7 +181,9 @@ module ActiveRecord end def test_where_with_table_name_and_nested_empty_array - assert_equal [], Post.where(:id => [[]]).to_a + assert_deprecated do + assert_equal [], Post.where(:id => [[]]).to_a + end end def test_where_with_empty_hash_and_no_foreign_key diff --git a/activerecord/test/fixtures/developers.yml b/activerecord/test/fixtures/developers.yml index 3656564f63..1a74563dc6 100644 --- a/activerecord/test/fixtures/developers.yml +++ b/activerecord/test/fixtures/developers.yml @@ -18,4 +18,4 @@ dev_<%= digit %>: poor_jamis: id: 11 name: Jamis - salary: 9000 \ No newline at end of file + salary: 9000 -- cgit v1.2.3 From 2b41343c34bcbe809537590152506690b84832df Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Mon, 8 Sep 2014 05:32:16 -0700 Subject: Default to sorting user's test cases for now Goals: 1. Default to :random for newly generated applications 2. Default to :sorted for existing applications with a warning 3. Only show the warning once 4. Only show the warning if the app actually uses AS::TestCase Fixes #16769 --- activerecord/test/cases/helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 3d9328b198..be635aeef9 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -220,4 +220,4 @@ require 'mocha/setup' # FIXME: stop using mocha # FIXME: we have tests that depend on run order, we should fix that and # remove this method call. require 'active_support/test_case' -ActiveSupport::TestCase.my_tests_are_order_dependent! +ActiveSupport::TestCase.test_order = :sorted -- cgit v1.2.3 From d5580b91b64daeb114875f24f6d54bbf88428018 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 09:44:48 +0200 Subject: Allow included modules to override association methods. Closes #16684. This is achieved by always generating `GeneratedAssociationMethods` when `ActiveRecord::Base` is subclassed. When some of the included modules of `ActiveRecord::Base` were reordered this behavior was broken as `Core#initialize_generated_modules` was no longer called. Meaning that the module was generated on first access. --- activerecord/test/cases/associations_test.rb | 14 ++++++++++++++ activerecord/test/cases/attribute_methods/read_test.rb | 1 + 2 files changed, 15 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 635c657d9b..9b0cf4c18f 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -357,4 +357,18 @@ class GeneratedMethodsTest < ActiveRecord::TestCase def test_model_method_overrides_association_method assert_equal(comments(:greetings).body, posts(:welcome).first_comment) end + + module MyModule + def comments; :none end + end + + class MyArticle < ActiveRecord::Base + self.table_name = "articles" + include MyModule + has_many :comments, inverse_of: false + end + + def test_included_module_overwrites_association_methods + assert_equal :none, MyArticle.new.comments + end end diff --git a/activerecord/test/cases/attribute_methods/read_test.rb b/activerecord/test/cases/attribute_methods/read_test.rb index 4741ee8799..e38b32d7fc 100644 --- a/activerecord/test/cases/attribute_methods/read_test.rb +++ b/activerecord/test/cases/attribute_methods/read_test.rb @@ -13,6 +13,7 @@ module ActiveRecord def self.superclass; Base; end def self.base_class; self; end def self.decorate_matching_attribute_types(*); end + def self.initialize_generated_modules; end include ActiveRecord::AttributeMethods -- cgit v1.2.3 From ae9412e857a78236b359eb1b636511d07fe45cf3 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 11:21:23 +0200 Subject: introduce `connection.supports_views?` and basic view tests. `AbstractAdapter#supports_views?` defaults to `false` so we have to turn it on in adapter subclasses. Currently the flag only controls test execution. /cc @yahonda --- activerecord/test/cases/view_test.rb | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 activerecord/test/cases/view_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb new file mode 100644 index 0000000000..aef6ad2296 --- /dev/null +++ b/activerecord/test/cases/view_test.rb @@ -0,0 +1,42 @@ +require "cases/helper" +require "models/book" + +if ActiveRecord::Base.connection.supports_views? +class ViewWithPrimaryKeyTest < ActiveRecord::TestCase + fixtures :books + + class Ebook < ActiveRecord::Base + self.primary_key = "id" + end + + setup do + @connection = ActiveRecord::Base.connection + @connection.execute <<-SQL + CREATE VIEW ebooks + AS SELECT id, name, status FROM books WHERE format = 'ebook' + SQL + end + + teardown do + @connection.execute "DROP VIEW IF EXISTS ebooks" + end + + def test_reading + books = Ebook.all + assert_equal [books(:rfr).id], books.map(&:id) + assert_equal ["Ruby for Rails"], books.map(&:name) + end + + def test_table_exists + skip "SQLite does not currently treat views as tables" if current_adapter?(:SQLite3Adapter) + view_name = Ebook.table_name + assert @connection.table_exists?(view_name), "'#{view_name}' table should exist" + end + + def test_column_definitions + assert_equal([["id", :integer], + ["name", :string], + ["status", :integer]], Ebook.columns.map { |c| [c.name, c.type] }) + end +end +end -- cgit v1.2.3 From ec6eee5db0994f92ac2c3c6d2a922c6edd35b2b1 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 12:00:01 +0200 Subject: add test-cases for primary-key-less-views. Closes #16555. --- activerecord/test/cases/view_test.rb | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb index b79dd03676..2f23d12de5 100644 --- a/activerecord/test/cases/view_test.rb +++ b/activerecord/test/cases/view_test.rb @@ -37,5 +37,48 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase ["name", :string], ["status", :integer]], Ebook.columns.map { |c| [c.name, c.type] }) end + + def test_attributes + assert_equal({"id" => 2, "name" => "Ruby for Rails", "status" => 0}, + Ebook.first.attributes) + end +end + +class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase + fixtures :books + + class Paperback < ActiveRecord::Base; end + + setup do + @connection = ActiveRecord::Base.connection + @connection.execute <<-SQL + CREATE VIEW paperbacks + AS SELECT name, status FROM books WHERE format = 'paperback' + SQL + end + + teardown do + @connection.execute "DROP VIEW IF EXISTS paperbacks" + end + + def test_reading + books = Paperback.all + assert_equal ["Agile Web Development with Rails"], books.map(&:name) + end + + def test_table_exists + view_name = Paperback.table_name + assert @connection.table_exists?(view_name), "'#{view_name}' table should exist" + end + + def test_column_definitions + assert_equal([["name", :string], + ["status", :integer]], Paperback.columns.map { |c| [c.name, c.type] }) + end + + def test_attributes + assert_equal({"name" => "Agile Web Development with Rails", "status" => 0}, + Paperback.first.attributes) + end end end -- cgit v1.2.3 From fde0d0219d844bcf71d8f6d9c9420fb75cf9ece9 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 15:17:46 +0200 Subject: models backed by views don't assume "id" columns are the primary key. Closes #10247. The same goes for tables with an "id" column but without primary key constraint. Reading from the view works without configuration. If you have an updateable view you need to use `self.primary_key = ''`. --- activerecord/test/cases/view_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb index 2f23d12de5..c2e71612a3 100644 --- a/activerecord/test/cases/view_test.rb +++ b/activerecord/test/cases/view_test.rb @@ -42,6 +42,13 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase assert_equal({"id" => 2, "name" => "Ruby for Rails", "status" => 0}, Ebook.first.attributes) end + + def test_does_not_assume_id_column_as_primary_key + model = Class.new(ActiveRecord::Base) do + self.table_name = "ebooks" + end + assert_nil model.primary_key + end end class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase @@ -80,5 +87,9 @@ class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase assert_equal({"name" => "Agile Web Development with Rails", "status" => 0}, Paperback.first.attributes) end + + def test_does_not_have_a_primary_key + assert_nil Paperback.primary_key + end end end -- cgit v1.2.3 From e6001cbac62130eac9193cdc2ac3b4ff99379c41 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 15:33:23 +0200 Subject: Oracle does not support `IF EXISTS` for `DROP VIEW`. /cc @yahonda --- activerecord/test/cases/view_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb index c2e71612a3..357265aa05 100644 --- a/activerecord/test/cases/view_test.rb +++ b/activerecord/test/cases/view_test.rb @@ -18,7 +18,7 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase end teardown do - @connection.execute "DROP VIEW IF EXISTS ebooks" + @connection.execute "DROP VIEW ebooks" if @connection.table_exists? "ebooks" end def test_reading @@ -65,7 +65,7 @@ class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase end teardown do - @connection.execute "DROP VIEW IF EXISTS paperbacks" + @connection.execute "DROP VIEW paperbacks" if @connection.table_exists? "paperbacks" end def test_reading -- cgit v1.2.3 From cb8b4167641484a9f7fe5757c872216e945bb1fb Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Thu, 11 Sep 2014 01:43:41 +0900 Subject: Replace drop sql statement to drop_table method to drop sequences at the same time each tables dropped for Oracle --- activerecord/test/cases/associations/required_test.rb | 4 ++-- activerecord/test/cases/attribute_decorators_test.rb | 2 +- activerecord/test/cases/migration/foreign_key_test.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/required_test.rb b/activerecord/test/cases/associations/required_test.rb index 24a332bb4a..321fb6c8dd 100644 --- a/activerecord/test/cases/associations/required_test.rb +++ b/activerecord/test/cases/associations/required_test.rb @@ -18,8 +18,8 @@ class RequiredAssociationsTest < ActiveRecord::TestCase end teardown do - @connection.execute("DROP TABLE parents") if @connection.table_exists? 'parents' - @connection.execute("DROP TABLE children") if @connection.table_exists? 'children' + @connection.drop_table 'parents' if @connection.table_exists? 'parents' + @connection.drop_table 'children' if @connection.table_exists? 'children' end test "belongs_to associations are not required by default" do diff --git a/activerecord/test/cases/attribute_decorators_test.rb b/activerecord/test/cases/attribute_decorators_test.rb index 644876752e..53bd58e22e 100644 --- a/activerecord/test/cases/attribute_decorators_test.rb +++ b/activerecord/test/cases/attribute_decorators_test.rb @@ -28,7 +28,7 @@ module ActiveRecord teardown do return unless @connection - @connection.execute 'DROP TABLE attribute_decorators_model' if @connection.table_exists? 'attribute_decorators_model' + @connection.drop_table 'attribute_decorators_model' if @connection.table_exists? 'attribute_decorators_model' Model.attribute_type_decorations.clear Model.reset_column_information end diff --git a/activerecord/test/cases/migration/foreign_key_test.rb b/activerecord/test/cases/migration/foreign_key_test.rb index ba28d55e4c..406dd70c37 100644 --- a/activerecord/test/cases/migration/foreign_key_test.rb +++ b/activerecord/test/cases/migration/foreign_key_test.rb @@ -29,8 +29,8 @@ module ActiveRecord teardown do if defined?(@connection) - @connection.execute "DROP TABLE astronauts" if @connection.table_exists? 'astronauts' - @connection.execute "DROP TABLE rockets" if @connection.table_exists? 'rockets' + @connection.drop_table "astronauts" if @connection.table_exists? 'astronauts' + @connection.drop_table "rockets" if @connection.table_exists? 'rockets' end end -- cgit v1.2.3 From 39303c0221d6854387c0e9038551e8e6d4533239 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 10 Sep 2014 14:01:52 -0700 Subject: MySQL: correct LONGTEXT and LONGBLOB limits from 2GB to their true 4GB --- activerecord/test/cases/schema_dumper_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index d5584ad06c..79fad06565 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -219,11 +219,11 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{t.binary\s+"tiny_blob",\s+limit: 255$}, output assert_match %r{t.binary\s+"normal_blob"$}, output assert_match %r{t.binary\s+"medium_blob",\s+limit: 16777215$}, output - assert_match %r{t.binary\s+"long_blob",\s+limit: 2147483647$}, output + assert_match %r{t.binary\s+"long_blob",\s+limit: 4294967295$}, output assert_match %r{t.text\s+"tiny_text",\s+limit: 255$}, output assert_match %r{t.text\s+"normal_text"$}, output assert_match %r{t.text\s+"medium_text",\s+limit: 16777215$}, output - assert_match %r{t.text\s+"long_text",\s+limit: 2147483647$}, output + assert_match %r{t.text\s+"long_text",\s+limit: 4294967295$}, output end def test_schema_dumps_index_type -- cgit v1.2.3 From da74eeb29426dd7940d1329f91a3894d2d6fb2cb Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 10 Sep 2014 14:03:34 -0700 Subject: MySQL: schema.rb now includes TEXT and BLOB column limits. --- activerecord/test/cases/schema_dumper_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 79fad06565..c8fc297a7a 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -203,9 +203,9 @@ class SchemaDumperTest < ActiveRecord::TestCase end if current_adapter?(:MysqlAdapter, :Mysql2Adapter) - def test_schema_dump_should_not_add_default_value_for_mysql_text_field + def test_schema_dump_should_add_default_value_for_mysql_text_field output = standard_dump - assert_match %r{t.text\s+"body",\s+null: false$}, output + assert_match %r{t.text\s+"body",\s+limit: 65535,\s+null: false$}, output end def test_schema_dump_includes_length_for_mysql_binary_fields @@ -217,11 +217,11 @@ class SchemaDumperTest < ActiveRecord::TestCase 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 - assert_match %r{t.binary\s+"normal_blob"$}, output + assert_match %r{t.binary\s+"normal_blob",\s+limit: 65535$}, output assert_match %r{t.binary\s+"medium_blob",\s+limit: 16777215$}, output assert_match %r{t.binary\s+"long_blob",\s+limit: 4294967295$}, output assert_match %r{t.text\s+"tiny_text",\s+limit: 255$}, output - assert_match %r{t.text\s+"normal_text"$}, output + assert_match %r{t.text\s+"normal_text",\s+limit: 65535$}, output assert_match %r{t.text\s+"medium_text",\s+limit: 16777215$}, output assert_match %r{t.text\s+"long_text",\s+limit: 4294967295$}, output end -- cgit v1.2.3 From 2c76793f087e212ff4c6d835656a9a95c8bfeaa5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 10 Sep 2014 14:22:41 -0700 Subject: Include default column limits in schema.rb Allows :limit defaults to be changed without pulling the rug out from under old migrations that omitted :limit because it matched the default at the time. --- activerecord/test/cases/schema_dumper_test.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index c8fc297a7a..72fb296825 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -100,7 +100,11 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_schema_dump_includes_limit_constraint_for_integer_columns output = dump_all_table_schema([/^(?!integer_limits)/]) + assert_match %r{c_int_without_limit}, output + if current_adapter?(:PostgreSQLAdapter) + assert_no_match %r{c_int_without_limit.*limit:}, output + assert_match %r{c_int_1.*limit: 2}, output assert_match %r{c_int_2.*limit: 2}, output @@ -111,6 +115,8 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{c_int_4.*}, output assert_no_match %r{c_int_4.*limit:}, output elsif current_adapter?(:MysqlAdapter, :Mysql2Adapter) + assert_match %r{c_int_without_limit.*limit: 4}, output + assert_match %r{c_int_1.*limit: 1}, output assert_match %r{c_int_2.*limit: 2}, output assert_match %r{c_int_3.*limit: 3}, output @@ -118,13 +124,13 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{c_int_4.*}, output assert_no_match %r{c_int_4.*:limit}, output elsif current_adapter?(:SQLite3Adapter) + assert_no_match %r{c_int_without_limit.*limit:}, output + assert_match %r{c_int_1.*limit: 1}, output assert_match %r{c_int_2.*limit: 2}, output assert_match %r{c_int_3.*limit: 3}, output assert_match %r{c_int_4.*limit: 4}, output end - assert_match %r{c_int_without_limit.*}, output - assert_no_match %r{c_int_without_limit.*limit:}, output if current_adapter?(:SQLite3Adapter) assert_match %r{c_int_5.*limit: 5}, output @@ -354,7 +360,7 @@ class SchemaDumperTest < ActiveRecord::TestCase match = output.match(%r{create_table "goofy_string_id"(.*)do.*\n(.*)\n}) assert_not_nil(match, "goofy_string_id table not found") assert_match %r(id: false), match[1], "no table id not preserved" - assert_match %r{t.string[[:space:]]+"id",[[:space:]]+null: false$}, match[2], "non-primary key id column not preserved" + assert_match %r{t.string\s+"id",.*?null: false$}, match[2], "non-primary key id column not preserved" end def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added @@ -433,7 +439,7 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase def test_schema_dump_defaults_with_universally_supported_types output = dump_table_schema('defaults') - assert_match %r{t\.string\s+"string_with_default",\s+default: "Hello!"}, output + assert_match %r{t\.string\s+"string_with_default",.*?default: "Hello!"}, output assert_match %r{t\.date\s+"date_with_default",\s+default: '2014-06-05'}, output assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: '2014-06-05 07:17:04'}, output assert_match %r{t\.time\s+"time_with_default",\s+default: '2000-01-01 07:17:04'}, output -- cgit v1.2.3 From fdba1a791bb32bc890a50a3655d7369f80377caa Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 10 Sep 2014 15:32:29 -0700 Subject: Speed up schema dumper tests Dump the standard schema once instead of redoing it per test --- activerecord/test/cases/schema_dumper_test.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 72fb296825..d7ee56374d 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -10,12 +10,11 @@ class SchemaDumperTest < ActiveRecord::TestCase end def standard_dump - @stream = StringIO.new - old_ignore_tables, ActiveRecord::SchemaDumper.ignore_tables = ActiveRecord::SchemaDumper.ignore_tables, [] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, @stream) - @stream.string - ensure - ActiveRecord::SchemaDumper.ignore_tables = old_ignore_tables + @@standard_dump ||= perform_schema_dump + end + + def perform_schema_dump + dump_all_table_schema [] end def test_dump_schema_information_outputs_lexically_ordered_versions @@ -31,8 +30,7 @@ class SchemaDumperTest < ActiveRecord::TestCase end def test_magic_comment - output = standard_dump - assert_match "# encoding: #{@stream.external_encoding.name}", output + assert_match "# encoding: #{Encoding.default_external.name}", standard_dump end def test_schema_dump @@ -255,12 +253,12 @@ class SchemaDumperTest < ActiveRecord::TestCase connection = ActiveRecord::Base.connection connection.stubs(:extensions).returns(['hstore']) - output = standard_dump + output = perform_schema_dump assert_match "# These are extensions that must be enabled", output assert_match %r{enable_extension "hstore"}, output connection.stubs(:extensions).returns([]) - output = standard_dump + output = perform_schema_dump assert_no_match "# These are extensions that must be enabled", output assert_no_match %r{enable_extension}, output end @@ -401,7 +399,7 @@ class SchemaDumperTest < ActiveRecord::TestCase migration = CreateDogMigration.new migration.migrate(:up) - output = standard_dump + output = perform_schema_dump assert_no_match %r{create_table "foo_.+_bar"}, output assert_no_match %r{add_index "foo_.+_bar"}, output assert_no_match %r{create_table "schema_migrations"}, output -- cgit v1.2.3 From 16868b53f9533ba48ca33bb694fade390f0ffe7e Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 9 Sep 2014 19:16:26 +0200 Subject: A `NullRelation` should represent nothing. Closes #15176. [Matthew Draper & Yves Senn] Closes #16860. (pull request to discuss the implementation) --- activerecord/test/cases/relation/mutation_test.rb | 4 ++++ activerecord/test/cases/relations_test.rb | 5 +++++ 2 files changed, 9 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relation/mutation_test.rb b/activerecord/test/cases/relation/mutation_test.rb index 1da5c36e1c..4c94c2fd0d 100644 --- a/activerecord/test/cases/relation/mutation_test.rb +++ b/activerecord/test/cases/relation/mutation_test.rb @@ -18,6 +18,10 @@ module ActiveRecord def attribute_alias?(name) false end + + def sanitize_sql(sql) + sql + end end def relation diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 7163697a68..3bfbf95b0a 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -420,6 +420,11 @@ class RelationTest < ActiveRecord::TestCase assert_equal nil, ac.engines.maximum(:id) end + def test_null_relation_in_where_condition + assert_operator 0, :<, Comment.count # precondition, make sure there are comments. + assert_equal 0, Comment.where(post_id: Post.none).to_a.size + end + def test_joins_with_nil_argument assert_nothing_raised { DependentFirm.joins(nil).first } end -- cgit v1.2.3 From e581b011165766a64a6749fb1465d76e90c14cb5 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 11 Sep 2014 08:07:35 +0200 Subject: switch `assert_operator` arguments as discussed in #16860. Working with two different machines is hard :sweat: --- activerecord/test/cases/relations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 3bfbf95b0a..410b5ba5a3 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -421,7 +421,7 @@ class RelationTest < ActiveRecord::TestCase end def test_null_relation_in_where_condition - assert_operator 0, :<, Comment.count # precondition, make sure there are comments. + assert_operator Comment.count, :>, 0 # precondition, make sure there are comments. assert_equal 0, Comment.where(post_id: Post.none).to_a.size end -- cgit v1.2.3 From 395573b344b0bb0579a325d8f958f692f8f66cc8 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 11 Sep 2014 13:14:37 +0200 Subject: reuse view test-cases for pg materialized view tests. --- .../test/cases/adapters/postgresql/view_test.rb | 67 +++------------------- activerecord/test/cases/view_test.rb | 36 +++++++++--- 2 files changed, 36 insertions(+), 67 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/view_test.rb b/activerecord/test/cases/adapters/postgresql/view_test.rb index 47b7d38eda..f6f96bb3ba 100644 --- a/activerecord/test/cases/adapters/postgresql/view_test.rb +++ b/activerecord/test/cases/adapters/postgresql/view_test.rb @@ -1,67 +1,18 @@ require "cases/helper" +require "cases/view_test" -module ViewTestConcern - extend ActiveSupport::Concern - - included do - self.use_transactional_fixtures = false - mattr_accessor :view_type - end - - SCHEMA_NAME = 'test_schema' - TABLE_NAME = 'things' - COLUMNS = [ - 'id integer', - 'name character varying(50)', - 'email character varying(50)', - 'moment timestamp without time zone' - ] - - class ThingView < ActiveRecord::Base - end - - def setup - super - ThingView.table_name = "#{SCHEMA_NAME}.#{view_type}_things" +if ActiveRecord::Base.connection.supports_materialized_views? +class MaterializedViewTest < ActiveRecord::TestCase + include ViewBehavior - @connection = ActiveRecord::Base.connection - @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})" - @connection.execute "CREATE #{view_type.humanize} #{ThingView.table_name} AS SELECT * FROM #{SCHEMA_NAME}.#{TABLE_NAME}" + private + def create_view(name, query) + @connection.execute "CREATE MATERIALIZED VIEW #{name} AS #{query}" end - def teardown - super - @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE" - end + def drop_view(name) + @connection.execute "DROP MATERIALIZED VIEW #{name}" if @connection.table_exists? name - def test_table_exists - name = ThingView.table_name - assert @connection.table_exists?(name), "'#{name}' table should exist" end - - def test_column_definitions - assert_nothing_raised do - assert_equal COLUMNS, columns(ThingView.table_name) - end - end - - private - def columns(table_name) - @connection.send(:column_definitions, table_name).map do |name, type, default| - "#{name} #{type}" + (default ? " default #{default}" : '') - end - end - end - -class ViewTest < ActiveRecord::TestCase - include ViewTestConcern - self.view_type = 'view' -end - -if ActiveRecord::Base.connection.supports_materialized_views? - class MaterializedViewTest < ActiveRecord::TestCase - include ViewTestConcern - self.view_type = 'materialized_view' - end end diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb index 357265aa05..3aed90ba36 100644 --- a/activerecord/test/cases/view_test.rb +++ b/activerecord/test/cases/view_test.rb @@ -1,24 +1,28 @@ require "cases/helper" require "models/book" -if ActiveRecord::Base.connection.supports_views? -class ViewWithPrimaryKeyTest < ActiveRecord::TestCase - fixtures :books +module ViewBehavior + extend ActiveSupport::Concern + + included do + fixtures :books + end class Ebook < ActiveRecord::Base self.primary_key = "id" end - setup do + def setup + super @connection = ActiveRecord::Base.connection - @connection.execute <<-SQL - CREATE VIEW ebooks - AS SELECT id, name, status FROM books WHERE format = 'ebook' + create_view "ebooks", <<-SQL + SELECT id, name, status FROM books WHERE format = 'ebook' SQL end - teardown do - @connection.execute "DROP VIEW ebooks" if @connection.table_exists? "ebooks" + def teardown + super + drop_view "ebooks" end def test_reading @@ -51,6 +55,20 @@ class ViewWithPrimaryKeyTest < ActiveRecord::TestCase end end +if ActiveRecord::Base.connection.supports_views? +class ViewWithPrimaryKeyTest < ActiveRecord::TestCase + include ViewBehavior + + private + def create_view(name, query) + @connection.execute "CREATE VIEW #{name} AS #{query}" + end + + def drop_view(name) + @connection.execute "DROP VIEW #{name}" if @connection.table_exists? name + end +end + class ViewWithoutPrimaryKeyTest < ActiveRecord::TestCase fixtures :books -- cgit v1.2.3 From 516f431ab0618a052f53eb5b14e2c6204da244dd Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 11 Sep 2014 13:37:52 +0200 Subject: pg, add test cases for updateable views. --- .../test/cases/adapters/postgresql/view_test.rb | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/view_test.rb b/activerecord/test/cases/adapters/postgresql/view_test.rb index f6f96bb3ba..8a8e1d3b17 100644 --- a/activerecord/test/cases/adapters/postgresql/view_test.rb +++ b/activerecord/test/cases/adapters/postgresql/view_test.rb @@ -1,6 +1,51 @@ require "cases/helper" require "cases/view_test" +class UpdateableViewTest < ActiveRecord::TestCase + fixtures :books + + class PrintedBook < ActiveRecord::Base + self.primary_key = "id" + end + + setup do + @connection = ActiveRecord::Base.connection + @connection.execute <<-SQL + CREATE VIEW printed_books + AS SELECT id, name, status, format FROM books WHERE format = 'paperback' + SQL + end + + teardown do + @connection.execute "DROP VIEW printed_books" if @connection.table_exists? "printed_books" + end + + def test_update_record + book = PrintedBook.first + book.name = "AWDwR" + book.save! + book.reload + assert_equal "AWDwR", book.name + end + + def test_insert_record + PrintedBook.create! name: "Rails in Action", status: 0, format: "paperback" + + new_book = PrintedBook.last + assert_equal "Rails in Action", new_book.name + end + + def test_update_record_to_fail_view_conditions + book = PrintedBook.first + book.format = "ebook" + book.save! + + assert_raises ActiveRecord::RecordNotFound do + book.reload + end + end +end + if ActiveRecord::Base.connection.supports_materialized_views? class MaterializedViewTest < ActiveRecord::TestCase include ViewBehavior -- cgit v1.2.3