diff options
Diffstat (limited to 'activerecord/test/cases')
13 files changed, 165 insertions, 39 deletions
diff --git a/activerecord/test/cases/arel/nodes/node_test.rb b/activerecord/test/cases/arel/nodes/node_test.rb index f1e0ce1ea9..8a9ecd84ca 100644 --- a/activerecord/test/cases/arel/nodes/node_test.rb +++ b/activerecord/test/cases/arel/nodes/node_test.rb @@ -14,7 +14,7 @@ module Arel }.grep(Class).each do |klass| next if Nodes::SqlLiteral == klass next if Nodes::BindParam == klass - next if klass.name =~ /^Arel::Nodes::(?:Test|.*Test$)/ + next if /^Arel::Nodes::(?:Test|.*Test$)/.match?(klass.name) assert klass.ancestors.include?(Nodes::Node), klass.name end end diff --git a/activerecord/test/cases/arel/select_manager_test.rb b/activerecord/test/cases/arel/select_manager_test.rb index 526fe6787a..4512d8e8a6 100644 --- a/activerecord/test/cases/arel/select_manager_test.rb +++ b/activerecord/test/cases/arel/select_manager_test.rb @@ -514,7 +514,7 @@ module Arel assert_equal "bar", join.right end - it "should create join nodes with a outer join klass" do + it "should create join nodes with an outer join klass" do relation = Arel::SelectManager.new join = relation.create_join "foo", "bar", Arel::Nodes::OuterJoin assert_kind_of Arel::Nodes::OuterJoin, join diff --git a/activerecord/test/cases/arel/visitors/mysql_test.rb b/activerecord/test/cases/arel/visitors/mysql_test.rb index 5f37587957..05dccd126e 100644 --- a/activerecord/test/cases/arel/visitors/mysql_test.rb +++ b/activerecord/test/cases/arel/visitors/mysql_test.rb @@ -104,6 +104,52 @@ module Arel sql.must_be_like %{ NOT "users"."name" <=> NULL } end end + + describe "Nodes::Regexp" do + before do + @table = Table.new(:users) + @attr = @table[:id] + end + + it "should know how to visit" do + node = @table[:name].matches_regexp("foo.*") + node.must_be_kind_of Nodes::Regexp + compile(node).must_be_like %{ + "users"."name" REGEXP 'foo.*' + } + end + + it "can handle subqueries" do + subquery = @table.project(:id).where(@table[:name].matches_regexp("foo.*")) + node = @attr.in subquery + compile(node).must_be_like %{ + "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" REGEXP 'foo.*') + } + end + end + + describe "Nodes::NotRegexp" do + before do + @table = Table.new(:users) + @attr = @table[:id] + end + + it "should know how to visit" do + node = @table[:name].does_not_match_regexp("foo.*") + node.must_be_kind_of Nodes::NotRegexp + compile(node).must_be_like %{ + "users"."name" NOT REGEXP 'foo.*' + } + end + + it "can handle subqueries" do + subquery = @table.project(:id).where(@table[:name].does_not_match_regexp("foo.*")) + node = @attr.in subquery + compile(node).must_be_like %{ + "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT REGEXP 'foo.*') + } + end + end end end end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 3525fa2ab8..6bd305306f 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -60,11 +60,8 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase end def test_belongs_to_does_not_use_order_by - ActiveRecord::SQLCounter.clear_log - Client.find(3).firm - ensure - sql_log = ActiveRecord::SQLCounter.log - assert sql_log.all? { |sql| /order by/i !~ sql }, "ORDER BY was used in the query: #{sql_log}" + sql_log = capture_sql { Client.find(3).firm } + assert sql_log.all? { |sql| !/order by/i.match?(sql) }, "ORDER BY was used in the query: #{sql_log}" end def test_belongs_to_with_primary_key diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 25cfa0a723..de9742b250 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -700,10 +700,17 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal ["id"], developers(:david).projects.select(:id).first.attributes.keys end + def test_join_middle_table_alias + assert_equal( + 2, + Project.includes(:developers_projects).where.not("developers_projects.joined_on": nil).to_a.size + ) + end + def test_join_table_alias assert_equal( 3, - Developer.includes(projects: :developers).where.not("projects_developers_projects_join.joined_on": nil).to_a.size + Developer.includes(projects: :developers).where.not("developers_projects_projects_join.joined_on": nil).to_a.size ) end @@ -716,7 +723,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal( 3, - Developer.includes(projects: :developers).where.not("projects_developers_projects_join.joined_on": nil).group(group.join(",")).to_a.size + Developer.includes(projects: :developers).where.not("developers_projects_projects_join.joined_on": nil).group(group.join(",")).to_a.size ) end diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index b1fcf47528..6c2a09c296 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -37,11 +37,8 @@ class HasOneAssociationsTest < ActiveRecord::TestCase end def test_has_one_does_not_use_order_by - ActiveRecord::SQLCounter.clear_log - companies(:first_firm).account - ensure - sql_log = ActiveRecord::SQLCounter.log - assert sql_log.all? { |sql| /order by/i !~ sql }, "ORDER BY was used in the query: #{sql_log}" + sql_log = capture_sql { companies(:first_firm).account } + assert sql_log.all? { |sql| !/order by/i.match?(sql) }, "ORDER BY was used in the query: #{sql_log}" end def test_has_one_cache_nils diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index e0dac01f4a..b65af4b819 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -13,7 +13,7 @@ require "models/tag" class InnerJoinAssociationTest < ActiveRecord::TestCase fixtures :authors, :author_addresses, :essays, :posts, :comments, :categories, :categories_posts, :categorizations, - :taggings, :tags + :taggings, :tags, :people def test_construct_finder_sql_applies_aliases_tables_on_association_conditions result = Author.joins(:thinking_posts, :welcome_posts).to_a @@ -36,16 +36,37 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase end def test_construct_finder_sql_does_not_table_name_collide_with_string_joins - sql = Person.joins(:agents).joins("JOIN people agents_people ON agents_people.primary_contact_id = people.id").to_sql - assert_match(/agents_people_2/i, sql) + string_join = <<~SQL + JOIN people agents_people ON agents_people.primary_contact_id = agents_people_2.id AND agents_people.id > agents_people_2.id + SQL + + expected = people(:susan) + assert_sql(/agents_people_2/i) do + assert_equal [expected], Person.joins(:agents).joins(string_join) + end end def test_construct_finder_sql_does_not_table_name_collide_with_aliased_joins - people = Person.arel_table - agents = people.alias("agents_people") - constraint = agents[:primary_contact_id].eq(people[:id]) - sql = Person.joins(:agents).joins(agents.create_join(agents, agents.create_on(constraint))).to_sql - assert_match(/agents_people_2/i, sql) + agents = Person.arel_table.alias("agents_people") + agents_2 = Person.arel_table.alias("agents_people_2") + constraint = agents[:primary_contact_id].eq(agents_2[:id]).and(agents[:id].gt(agents_2[:id])) + + expected = people(:susan) + assert_sql(/agents_people_2/i) do + assert_equal [expected], Person.joins(:agents).joins(agents.create_join(agents, agents.create_on(constraint))) + end + end + + def test_user_supplied_joins_order_should_be_preserved + string_join = <<~SQL + JOIN people agents_people_2 ON agents_people_2.primary_contact_id = people.id + SQL + agents = Person.arel_table.alias("agents_people") + agents_2 = Person.arel_table.alias("agents_people_2") + constraint = agents[:primary_contact_id].eq(agents_2[:id]).and(agents[:id].gt(agents_2[:id])) + + expected = people(:susan) + assert_equal [expected], Person.joins(string_join).joins(agents.create_join(agents, agents.create_on(constraint))) end def test_construct_finder_sql_ignores_empty_joins_hash diff --git a/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb b/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb index 95e57f42e3..ee2972101f 100644 --- a/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +++ b/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb @@ -334,6 +334,8 @@ module ActiveRecord } } + configs = ActiveRecord::DatabaseConfigurations.new(config) + actual = configs.configs_for(env_name: "default_env", spec_name: "primary").config expected = { "adapter" => "postgresql", "database" => "foo", @@ -341,11 +343,37 @@ module ActiveRecord "pool" => 5 } - ["primary", "animals"].each do |spec_name| - configs = ActiveRecord::DatabaseConfigurations.new(config) - actual = configs.configs_for(env_name: "default_env", spec_name: spec_name).config - assert_equal expected, actual - end + assert_equal expected, actual + + configs = ActiveRecord::DatabaseConfigurations.new(config) + actual = configs.configs_for(env_name: "default_env", spec_name: "animals").config + expected = { "pool" => 5 } + + assert_equal expected, actual + end + + def test_separate_database_env_vars + ENV["DATABASE_URL"] = "postgres://localhost/foo" + ENV["PRIMARY_DATABASE_URL"] = "postgres://localhost/primary" + ENV["ANIMALS_DATABASE_URL"] = "postgres://localhost/animals" + + config = { + "default_env" => { + "primary" => { "pool" => 5 }, + "animals" => { "pool" => 5 } + } + } + + configs = ActiveRecord::DatabaseConfigurations.new(config) + actual = configs.configs_for(env_name: "default_env", spec_name: "primary").config + assert_equal "primary", actual["database"] + + configs = ActiveRecord::DatabaseConfigurations.new(config) + actual = configs.configs_for(env_name: "default_env", spec_name: "animals").config + assert_equal "animals", actual["database"] + ensure + ENV.delete("PRIMARY_DATABASE_URL") + ENV.delete("ANIMALS_DATABASE_URL") end def test_does_not_change_other_environments diff --git a/activerecord/test/cases/database_configurations_test.rb b/activerecord/test/cases/database_configurations_test.rb index ed8151f01a..714b9d75c5 100644 --- a/activerecord/test/cases/database_configurations_test.rb +++ b/activerecord/test/cases/database_configurations_test.rb @@ -80,17 +80,20 @@ class LegacyDatabaseConfigurationsTest < ActiveRecord::TestCase def test_each_is_deprecated assert_deprecated do - ActiveRecord::Base.configurations.each do |db_config| - assert_equal "primary", db_config.spec_name + all_configs = ActiveRecord::Base.configurations.values + ActiveRecord::Base.configurations.each do |env_name, config| + assert_includes ["arunit", "arunit2", "arunit_without_prepared_statements"], env_name + assert_includes all_configs, config end end end def test_first_is_deprecated + first_config = ActiveRecord::Base.configurations.configurations.map(&:config).first assert_deprecated do - db_config = ActiveRecord::Base.configurations.first - assert_equal "arunit", db_config.env_name - assert_equal "primary", db_config.spec_name + env_name, config = ActiveRecord::Base.configurations.first + assert_equal "arunit", env_name + assert_equal first_config, config end end diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 8673a99c45..0ae156320a 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -580,7 +580,9 @@ class EnumTest < ActiveRecord::TestCase def self.name "Book" end - enum status: [:sent, :not_sent] + silence_warnings do + enum status: [:sent, :not_sent] + end end assert_match(expected_message, logger.logged(:warn).first) diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index a7f01e898e..0861d938c5 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -67,7 +67,7 @@ class FixturesTest < ActiveRecord::TestCase end def call(_, _, _, _, values) - @events << values[:sql] if values[:sql] =~ /INSERT/ + @events << values[:sql] if /INSERT/.match?(values[:sql]) end end @@ -1279,6 +1279,33 @@ class CustomNameForFixtureOrModelTest < ActiveRecord::TestCase end end +class IgnoreFixturesTest < ActiveRecord::TestCase + fixtures :other_books, :parrots + + test "ignores books fixtures" do + assert_raise(StandardError) { other_books(:published) } + assert_raise(StandardError) { other_books(:published_paperback) } + assert_raise(StandardError) { other_books(:published_ebook) } + + assert_equal 2, Book.count + assert_equal "Agile Web Development with Rails", other_books(:awdr).name + assert_equal "published", other_books(:awdr).status + assert_equal "paperback", other_books(:awdr).format + assert_equal "english", other_books(:awdr).language + + assert_equal "Ruby for Rails", other_books(:rfr).name + assert_equal "ebook", other_books(:rfr).format + assert_equal "published", other_books(:rfr).status + end + + test "ignores parrots fixtures" do + assert_raise(StandardError) { parrots(:DEFAULT) } + assert_raise(StandardError) { parrots(:DEAD_PARROT) } + + assert_equal "DeadParrot", parrots(:polly).parrot_sti_class + end +end + class FixturesWithDefaultScopeTest < ActiveRecord::TestCase fixtures :bulbs diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb index ac3c5bc26e..4b039395e8 100644 --- a/activerecord/test/cases/tasks/mysql_rake_test.rb +++ b/activerecord/test/cases/tasks/mysql_rake_test.rb @@ -93,11 +93,9 @@ if current_adapter?(:Mysql2Adapter) with_stubbed_connection_establish_connection do ActiveRecord::Base.connection.stub( :create_database, - proc { raise ActiveRecord::StatementInvalid } + proc { raise ActiveRecord::DatabaseAlreadyExists } ) do - ActiveRecord::Base.connection.stub(:error_number, 1007) do - ActiveRecord::Tasks::DatabaseTasks.create @configuration - end + ActiveRecord::Tasks::DatabaseTasks.create @configuration assert_equal "Database 'my-app-db' already exists\n", $stderr.string end diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index f9df650687..d74ba0580d 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -129,7 +129,7 @@ if current_adapter?(:PostgreSQLAdapter) with_stubbed_connection_establish_connection do ActiveRecord::Base.connection.stub( :create_database, - proc { raise ActiveRecord::Tasks::DatabaseAlreadyExists } + proc { raise ActiveRecord::DatabaseAlreadyExists } ) do ActiveRecord::Tasks::DatabaseTasks.create @configuration |