From a982443ae5bd12535405dbdb40f27df2d612256e Mon Sep 17 00:00:00 2001 From: Daniel Schierbeck Date: Sat, 9 Jul 2011 14:24:28 +0200 Subject: Make #extract_schema_and_table an instance method in Utils Also, move the utils test into its own test case. --- .../test/cases/adapters/postgresql/schema_test.rb | 15 --------------- .../test/cases/adapters/postgresql/utils_test.rb | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 activerecord/test/cases/adapters/postgresql/utils_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/postgresql/schema_test.rb b/activerecord/test/cases/adapters/postgresql/schema_test.rb index 3a7f1badf0..4c6d865d59 100644 --- a/activerecord/test/cases/adapters/postgresql/schema_test.rb +++ b/activerecord/test/cases/adapters/postgresql/schema_test.rb @@ -219,21 +219,6 @@ class SchemaTest < ActiveRecord::TestCase end end - def test_extract_schema_and_table - { - %(table_name) => [nil,'table_name'], - %("table.name") => [nil,'table.name'], - %(schema.table_name) => %w{schema table_name}, - %("schema".table_name) => %w{schema table_name}, - %(schema."table_name") => %w{schema table_name}, - %("schema"."table_name") => %w{schema table_name}, - %("even spaces".table) => ['even spaces','table'], - %(schema."table.name") => ['schema', 'table.name'] - }.each do |given, expect| - assert_equal expect, ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils.extract_schema_and_table(given) - end - end - def test_current_schema { %('$user',public) => 'public', diff --git a/activerecord/test/cases/adapters/postgresql/utils_test.rb b/activerecord/test/cases/adapters/postgresql/utils_test.rb new file mode 100644 index 0000000000..5f08f79171 --- /dev/null +++ b/activerecord/test/cases/adapters/postgresql/utils_test.rb @@ -0,0 +1,18 @@ +class PostgreSQLUtilsTest < ActiveSupport::TestCase + include ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils + + def test_extract_schema_and_table + { + %(table_name) => [nil,'table_name'], + %("table.name") => [nil,'table.name'], + %(schema.table_name) => %w{schema table_name}, + %("schema".table_name) => %w{schema table_name}, + %(schema."table_name") => %w{schema table_name}, + %("schema"."table_name") => %w{schema table_name}, + %("even spaces".table) => ['even spaces','table'], + %(schema."table.name") => ['schema', 'table.name'] + }.each do |given, expect| + assert_equal expect, extract_schema_and_table(given) + end + end +end -- cgit v1.2.3 From 41902e6932d46e3142185ee8d75dd861d0424d4e Mon Sep 17 00:00:00 2001 From: Franck Verrot Date: Tue, 26 Jul 2011 11:36:18 +0200 Subject: Simplify the test by using id and name. `id` will be the only real sort criteria in any case as it's unique. --- 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 6363cae371..821da91f0a 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -963,6 +963,6 @@ class RelationTest < ActiveRecord::TestCase end def test_ordering_with_extra_spaces - assert_equal authors(:david), Author.order('organization_id ASC , owned_essay_id DESC').last + assert_equal authors(:david), Author.order('id DESC , name DESC').last end end -- cgit v1.2.3 From 04cc446d178653d362510e79a22db5300d463161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awosz=20S=C5=82awi=C5=84ski?= Date: Sat, 23 Jul 2011 21:35:16 +0200 Subject: allow select to have multiple arguments --- activerecord/test/cases/base_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index f2f5b73626..84b66fdf49 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -123,6 +123,11 @@ class BasicsTest < ActiveRecord::TestCase assert_equal Topic.all.map(&:id).sort, topic_ids end + def test_select_symbol_for_many_arguments + topics = Topic.select(:id, :author_name).map{|topic| [topic.id, topic.author_name]}.sort + assert_equal Topic.all.map{|topic| [topic.id,topic.author_name]}.sort, topics + end + def test_table_exists assert !NonExistentTable.table_exists? assert Topic.table_exists? -- cgit v1.2.3 From 14c6fca9cd2353e3b949e2683b69fac7e356097e Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 26 Jul 2011 16:12:17 +0200 Subject: Revert "allow select to have multiple arguments" This reverts commit 04cc446d178653d362510e79a22db5300d463161. I reverted it because apparently we want to use: select([:a, :b]) instead of select(:a, :b), but there was no tests for that form. --- activerecord/test/cases/base_test.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 84b66fdf49..f2f5b73626 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -123,11 +123,6 @@ class BasicsTest < ActiveRecord::TestCase assert_equal Topic.all.map(&:id).sort, topic_ids end - def test_select_symbol_for_many_arguments - topics = Topic.select(:id, :author_name).map{|topic| [topic.id, topic.author_name]}.sort - assert_equal Topic.all.map{|topic| [topic.id,topic.author_name]}.sort, topics - end - def test_table_exists assert !NonExistentTable.table_exists? assert Topic.table_exists? -- cgit v1.2.3 From 1e69f0b84a17fffdcb9aa2cba156117ae47fa92f Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Tue, 26 Jul 2011 23:39:17 +0530 Subject: remove deprication warning: ambiguous first argument; put parentheses or even spaces --- activerecord/test/cases/base_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index f2f5b73626..12101c1683 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1867,6 +1867,6 @@ class BasicsTest < ActiveRecord::TestCase def test_cache_key_format_for_existing_record_with_nil_updated_at dev = Developer.first dev.update_attribute(:updated_at, nil) - assert_match /\/#{dev.id}$/, dev.cache_key + assert_match(/\/#{dev.id}$/, dev.cache_key) end end -- cgit v1.2.3 From 24f0a872e660647fe1abf187748660a77da3fe0a Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 27 Jul 2011 12:36:00 +0100 Subject: Add a proxy_association method to association proxies, which can be called by association extensions to access information about the association. This replaces proxy_owner etc with proxy_association.owner. --- activerecord/test/cases/associations_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 49d82ba2df..ffe2993e0f 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -203,6 +203,11 @@ class AssociationProxyTest < ActiveRecord::TestCase assert_equal david.projects, david.projects.reload.reload end end + + def test_proxy_association_accessor + david = developers(:david) + assert_equal david.association(:projects), david.projects.proxy_association + end end class OverridingAssociationsTest < ActiveRecord::TestCase -- cgit v1.2.3 From 9a298a162c16e019fe6971e563e7f4916e86ced6 Mon Sep 17 00:00:00 2001 From: Dmitriy Kiriyenko Date: Tue, 5 Jul 2011 13:17:39 +0300 Subject: Fixed failing query when performing calculation with having based on select. --- activerecord/test/cases/calculations_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 224b3f3d1f..42f98b3d42 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -170,6 +170,13 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 60, c[2] end + def test_should_group_by_summed_field_having_condition_from_select + c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit) + assert_nil c[1] + assert_equal 60, c[2] + assert_equal 53, c[9] + end + def test_should_group_by_summed_association c = Account.sum(:credit_limit, :group => :firm) assert_equal 50, c[companies(:first_firm)] -- cgit v1.2.3 From 18d307ed94ee9a8cd2dca117597f9e6afd1b6faa Mon Sep 17 00:00:00 2001 From: Samer Masry Date: Wed, 27 Jul 2011 10:17:25 -0700 Subject: Reverse order fix when using function for ActiveRecord::QueryMethods Fixes #1697 --- activerecord/test/cases/relations_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 821da91f0a..8a2cf0ef85 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -911,6 +911,11 @@ class RelationTest < ActiveRecord::TestCase assert_equal 'zyke', FastCar.order_using_old_style.limit(1).first.name end + def test_order_with_function_and_last + authors = Author.scoped + assert_equal authors(:bob), authors.order( "id asc, MAX( organization_id, owned_essay_id)" ).last + end + def test_order_using_scoping car1 = CoolCar.order('id DESC').scoping do CoolCar.find(:first, :order => 'id asc') -- cgit v1.2.3 From 971a74b81f37ec82e06056b738a7215185c51ee6 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 28 Jul 2011 13:14:11 +0100 Subject: Revert "Merge pull request #2309 from smasry/master" This reverts commit 9d396ee8195e31f646e0b89158ed96f4db4ab38f, reversing changes made to fa2bfd832c1d1e997d93c2269a485cc74782c86d. Reason: the change broke the build. --- activerecord/test/cases/relations_test.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 8a2cf0ef85..821da91f0a 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -911,11 +911,6 @@ class RelationTest < ActiveRecord::TestCase assert_equal 'zyke', FastCar.order_using_old_style.limit(1).first.name end - def test_order_with_function_and_last - authors = Author.scoped - assert_equal authors(:bob), authors.order( "id asc, MAX( organization_id, owned_essay_id)" ).last - end - def test_order_using_scoping car1 = CoolCar.order('id DESC').scoping do CoolCar.find(:first, :order => 'id asc') -- cgit v1.2.3 From 3d6e1872550f284dd387ffc87a984a9036376062 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 29 Jul 2011 12:23:37 -0700 Subject: dump IO encoding value along with schema.rb so the file can be reloaded. fixes #1592 --- activerecord/test/cases/schema_dumper_test.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 4adecf8e83..5da3f59a1f 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -3,11 +3,20 @@ require 'stringio' class SchemaDumperTest < ActiveRecord::TestCase + def setup + @stream = StringIO.new + end + def standard_dump - stream = StringIO.new + @stream = StringIO.new ActiveRecord::SchemaDumper.ignore_tables = [] - ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) - stream.string + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, @stream) + @stream.string + end + + def test_magic_comment + skip "only test magic comments on 1.9" if RUBY_VERSION < '1.9' + assert_match "# encoding: #{@stream.external_encoding.name}", standard_dump end def test_schema_dump -- cgit v1.2.3 From e7330f3d4fa97c683a49457239402f826d8016b7 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Sat, 30 Jul 2011 12:08:26 -0400 Subject: Resolve warnings by instantizing @attrubtes as nil --- activerecord/test/cases/attribute_methods_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index b0896fb236..dbf5a1ba76 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -113,6 +113,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase # by inspecting it. def test_allocated_object_can_be_inspected topic = Topic.allocate + topic.instance_eval { @attributes = nil } assert_nothing_raised { topic.inspect } assert topic.inspect, "#" end -- cgit v1.2.3 From 329409decdcbaa80695b7b65a6252e70a829091d Mon Sep 17 00:00:00 2001 From: Arun Agrawal Date: Sat, 30 Jul 2011 09:19:58 +0530 Subject: magic comment test only if encoding_aware?. --- activerecord/test/cases/schema_dumper_test.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 5da3f59a1f..99e7ef6c03 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -14,9 +14,10 @@ class SchemaDumperTest < ActiveRecord::TestCase @stream.string end - def test_magic_comment - skip "only test magic comments on 1.9" if RUBY_VERSION < '1.9' - assert_match "# encoding: #{@stream.external_encoding.name}", standard_dump + if "string".encoding_aware? + def test_magic_comment + assert_match "# encoding: #{@stream.external_encoding.name}", standard_dump + end end def test_schema_dump -- cgit v1.2.3 From 624c4571fd364bc71e2d9f5de20a664cfc8f7e15 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Sun, 31 Jul 2011 18:05:05 +0530 Subject: remove extra require for 'stringio' as it is required in helper.rb --- activerecord/test/cases/schema_dumper_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 99e7ef6c03..71ff727b7f 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -1,5 +1,4 @@ require "cases/helper" -require 'stringio' class SchemaDumperTest < ActiveRecord::TestCase -- cgit v1.2.3 From 74d7bfb200e4590e244558554e147a31d30990df Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Fri, 29 Jul 2011 21:26:21 -0400 Subject: Support backwards compatible interface for migration down/up with rails 3.0.x. --- .../test/cases/invertible_migration_test.rb | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/invertible_migration_test.rb b/activerecord/test/cases/invertible_migration_test.rb index afec64750e..acba4a134e 100644 --- a/activerecord/test/cases/invertible_migration_test.rb +++ b/activerecord/test/cases/invertible_migration_test.rb @@ -27,6 +27,19 @@ module ActiveRecord end end + class LegacyMigration < ActiveRecord::Migration + def self.up + create_table("horses") do |t| + t.column :content, :text + t.column :remind_at, :datetime + end + end + + def self.down + drop_table("horses") + end + end + def teardown if ActiveRecord::Base.connection.table_exists?("horses") ActiveRecord::Base.connection.drop_table("horses") @@ -53,5 +66,16 @@ module ActiveRecord migration.migrate :down assert !migration.connection.table_exists?("horses") end + + def test_legacy_up + LegacyMigration.migrate :up + assert ActiveRecord::Base.connection.table_exists?("horses"), "horses should exist" + end + + def test_legacy_down + LegacyMigration.migrate :up + LegacyMigration.migrate :down + assert !ActiveRecord::Base.connection.table_exists?("horses"), "horses should not exist" + end end end -- cgit v1.2.3 From 43fc81407495a31465bc3559bc0703fd328f2308 Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Fri, 29 Jul 2011 23:17:12 -0400 Subject: Ensure that .up and .down work as well. --- activerecord/test/cases/invertible_migration_test.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/invertible_migration_test.rb b/activerecord/test/cases/invertible_migration_test.rb index acba4a134e..3ae7b63dff 100644 --- a/activerecord/test/cases/invertible_migration_test.rb +++ b/activerecord/test/cases/invertible_migration_test.rb @@ -54,13 +54,13 @@ module ActiveRecord end end - def test_up + def test_migrate_up migration = InvertibleMigration.new migration.migrate(:up) assert migration.connection.table_exists?("horses"), "horses should exist" end - def test_down + def test_migrate_down migration = InvertibleMigration.new migration.migrate :up migration.migrate :down @@ -77,5 +77,16 @@ module ActiveRecord LegacyMigration.migrate :down assert !ActiveRecord::Base.connection.table_exists?("horses"), "horses should not exist" end + + def test_up + LegacyMigration.up + assert ActiveRecord::Base.connection.table_exists?("horses"), "horses should exist" + end + + def test_down + LegacyMigration.up + LegacyMigration.down + assert !ActiveRecord::Base.connection.table_exists?("horses"), "horses should not exist" + end end end -- cgit v1.2.3 From f000d4e5faeabafece59a46911a74e876785b785 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 4 Aug 2011 00:23:58 +0100 Subject: Quote these dates to prevent intermittent test failure. Suppose local time is 00:50 GMT+1. Without the quoting, the YAML parser would parse this as 00:50 UTC, into the local time of 01:50 GMT+1. Then, it would get written into the database in local time as 01:50. When it came back out the UTC date from the database and the UTC date of two weeks ago would be compared. The former would be 23:50, and the latter would be 00:50, so the two dates would differ, causing the assertion to fail. Quoting it prevents the YAML parser from getting involved. --- activerecord/test/cases/fixtures_test.rb | 4 ++-- activerecord/test/fixtures/pirates.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 842e8a0049..913f6a3340 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -587,8 +587,8 @@ class FoxyFixturesTest < ActiveRecord::TestCase end def test_preserves_existing_fixture_data - assert_equal(2.weeks.ago.utc.to_date, pirates(:redbeard).created_on.utc.to_date) - assert_equal(2.weeks.ago.utc.to_date, pirates(:redbeard).updated_on.utc.to_date) + assert_equal(2.weeks.ago.to_date, pirates(:redbeard).created_on.to_date) + assert_equal(2.weeks.ago.to_date, pirates(:redbeard).updated_on.to_date) end def test_generates_unique_ids diff --git a/activerecord/test/fixtures/pirates.yml b/activerecord/test/fixtures/pirates.yml index abb91101da..6004f390a4 100644 --- a/activerecord/test/fixtures/pirates.yml +++ b/activerecord/test/fixtures/pirates.yml @@ -5,5 +5,5 @@ blackbeard: redbeard: catchphrase: "Avast!" parrot: louis - created_on: <%= 2.weeks.ago.to_s(:db) %> - updated_on: <%= 2.weeks.ago.to_s(:db) %> + created_on: "<%= 2.weeks.ago.to_s(:db) %>" + updated_on: "<%= 2.weeks.ago.to_s(:db) %>" -- cgit v1.2.3 From 5a05207d99b7e2678f9b42db2d9ffc21ec2c8c3b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 5 Aug 2011 11:20:15 -0700 Subject: pg does not allow aliases in the having clause, but functions are fine --- activerecord/test/cases/calculations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 42f98b3d42..c38814713a 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -171,7 +171,7 @@ class CalculationsTest < ActiveRecord::TestCase end def test_should_group_by_summed_field_having_condition_from_select - c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("min_credit_limit > 50").sum(:credit_limit) + c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit) assert_nil c[1] assert_equal 60, c[2] assert_equal 53, c[9] -- cgit v1.2.3 From e0a6ec214987882a47722e709a91b17b8395bec9 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 5 Aug 2011 16:48:46 -0300 Subject: Fix test for Rubinius --- .../test/cases/associations/nested_through_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb index dd450a2a8e..80c6e41169 100644 --- a/activerecord/test/cases/associations/nested_through_associations_test.rb +++ b/activerecord/test/cases/associations/nested_through_associations_test.rb @@ -247,7 +247,7 @@ class NestedThroughAssociationsTest < ActiveRecord::TestCase def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload_via_joins assert_includes_and_joins_equal( - Category.where('comments.id' => comments(:more_greetings).id).order('comments.id'), + Category.where('comments.id' => comments(:more_greetings).id).order('categories.id'), [categories(:general), categories(:technology)], :post_comments ) end -- cgit v1.2.3 From 1b676fc76074047f4784502a9597fb529fd74dd7 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 5 Aug 2011 18:42:41 -0300 Subject: Revert "to_key on a destroyed model should return nil". Closes #2440 This reverts commit c5448721b5054b8a467958d60427fdee15eac604. --- activerecord/test/cases/primary_keys_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 7e3da145e5..05a41d8a0a 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -26,7 +26,7 @@ class PrimaryKeysTest < ActiveRecord::TestCase def test_to_key_with_primary_key_after_destroy topic = Topic.find(1) topic.destroy - assert_equal nil, topic.to_key + assert_equal [1], topic.to_key end def test_integer_key -- cgit v1.2.3 From 58af0d46cfa4481800bb1c4c26950c717ea1ebdd Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Mon, 8 Aug 2011 22:32:26 +0300 Subject: Fixed test_disable_referential_integrity on Oracle On Oracle disable_referential_integrity before execution of block will disable foreign key constraints and after block will enable them but when constraints are enabled then they are validated. Therefore created record with invalid foreign key should be deleted before enabling foreign key constraints. --- activerecord/test/cases/adapter_test.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 3942e7bb41..f1023ed7ef 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -151,6 +151,9 @@ class AdapterTest < ActiveRecord::TestCase else @connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)" end + # should deleted created record as otherwise disable_referential_integrity will try to enable contraints after executed block + # and will fail (at least on Oracle) + @connection.execute "DELETE FROM fk_test_has_fk" end end end -- cgit v1.2.3 From 7db90aa7c7dfe5033ad012b8ee13e6f15d1c66f0 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 8 Aug 2011 23:27:54 +0100 Subject: Make it the responsibility of the connection to hold onto an ARel visitor for generating SQL. This improves the code architecture generally, and solves some problems with marshalling. Adapter authors please take note: you now need to define an Adapter.visitor_for method, but it degrades gracefully with a deprecation warning for now. --- activerecord/test/cases/adapter_test.rb | 10 ++++++++++ activerecord/test/cases/base_test.rb | 19 ------------------- activerecord/test/cases/connection_pool_test.rb | 4 ++++ activerecord/test/cases/method_scoping_test.rb | 8 ++++---- activerecord/test/cases/relation_scoping_test.rb | 2 +- 5 files changed, 19 insertions(+), 24 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index f1023ed7ef..94497e37c7 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -157,4 +157,14 @@ class AdapterTest < ActiveRecord::TestCase end end end + + def test_deprecated_visitor_for + visitor_klass = Class.new(Arel::Visitors::ToSql) + Arel::Visitors::VISITORS['fuuu'] = visitor_klass + pool = stub(:spec => stub(:config => { :adapter => 'fuuu' })) + visitor = assert_deprecated { + ActiveRecord::ConnectionAdapters::AbstractAdapter.visitor_for(pool) + } + assert visitor.is_a?(visitor_klass) + end end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 12101c1683..0a9340aea1 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -144,25 +144,6 @@ class BasicsTest < ActiveRecord::TestCase end end - def test_use_table_engine_for_quoting_where - relation = Topic.where(Topic.arel_table[:id].eq(1)) - engine = relation.table.engine - - fakepool = Class.new(Struct.new(:spec)) { - def with_connection; yield self; end - def connection_pool; self; end - def table_exists?(name); false; end - def quote_table_name(*args); raise "lol quote_table_name"; end - } - - relation.table.engine = fakepool.new(engine.connection_pool.spec) - - error = assert_raises(RuntimeError) { relation.to_a } - assert_match('lol', error.message) - ensure - relation.table.engine = engine - end - def test_preserving_time_objects assert_kind_of( Time, Topic.find(1).bonus_time, diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb index f92f4e62c5..8a0f453127 100644 --- a/activerecord/test/cases/connection_pool_test.rb +++ b/activerecord/test/cases/connection_pool_test.rb @@ -135,6 +135,10 @@ module ActiveRecord pool.with_connection end end + + def test_pool_sets_connection_visitor + assert @pool.connection.visitor.is_a?(Arel::Visitors::ToSql) + end end end end diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index a0cb5dbdc5..0ab4f30363 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -14,7 +14,7 @@ class MethodScopingTest < ActiveRecord::TestCase def test_set_conditions Developer.send(:with_scope, :find => { :conditions => 'just a test...' }) do - assert_match '(just a test...)', Developer.scoped.arel.to_sql + assert_match '(just a test...)', Developer.scoped.to_sql end end @@ -274,7 +274,7 @@ class NestedScopingTest < ActiveRecord::TestCase Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do Developer.send(:with_scope, :find => { :limit => 10 }) do devs = Developer.scoped - assert_match '(salary = 80000)', devs.arel.to_sql + assert_match '(salary = 80000)', devs.to_sql assert_equal 10, devs.taken end end @@ -308,7 +308,7 @@ class NestedScopingTest < ActiveRecord::TestCase Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do Developer.send(:with_scope, :find => { :conditions => 'salary = 80000' }) do devs = Developer.scoped - assert_match "(name = 'David') AND (salary = 80000)", devs.arel.to_sql + assert_match "(name = 'David') AND (salary = 80000)", devs.to_sql assert_equal(1, Developer.count) end Developer.send(:with_scope, :find => { :conditions => "name = 'Maiha'" }) do @@ -321,7 +321,7 @@ class NestedScopingTest < ActiveRecord::TestCase Developer.send(:with_scope, :find => { :conditions => 'salary = 80000', :limit => 10 }) do Developer.send(:with_scope, :find => { :conditions => "name = 'David'" }) do devs = Developer.scoped - assert_match "(salary = 80000) AND (name = 'David')", devs.arel.to_sql + assert_match "(salary = 80000) AND (name = 'David')", devs.to_sql assert_equal 10, devs.taken end end diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb index f2d177d834..673aff403f 100644 --- a/activerecord/test/cases/relation_scoping_test.rb +++ b/activerecord/test/cases/relation_scoping_test.rb @@ -170,7 +170,7 @@ class NestedRelationScopingTest < ActiveRecord::TestCase Developer.where('salary = 80000').scoping do Developer.limit(10).scoping do devs = Developer.scoped - assert_match '(salary = 80000)', devs.arel.to_sql + assert_match '(salary = 80000)', devs.to_sql assert_equal 10, devs.taken end end -- cgit v1.2.3 From 9062b75bb7dab38977805c1de35944079a56499a Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 8 Aug 2011 14:41:23 +0100 Subject: Fully marshal AR::Base objects. Fixes #2431. --- activerecord/test/cases/base_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 0a9340aea1..c78d887ed7 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1813,6 +1813,19 @@ class BasicsTest < ActiveRecord::TestCase assert_equal expected.attributes, actual.attributes end + def test_marshal_new_record_round_trip + post = Marshal.load(Marshal.dump(Post.new)) + assert post.new_record?, "should be a new record" + end + + def test_marshalling_with_associations + post = Post.new + post.comments.build + post = Marshal.load(Marshal.dump(post)) + + assert_equal 1, post.comments.length + end + def test_attribute_names assert_equal ["id", "type", "ruby_type", "firm_id", "firm_name", "name", "client_of", "rating", "account_id"], Company.attribute_names -- cgit v1.2.3 From d3c15a1d31d77e44b142c96cb55b654f3552a89d Mon Sep 17 00:00:00 2001 From: Myron Marston Date: Fri, 12 Aug 2011 19:58:37 -0700 Subject: Allow ActiveRecord observers to be disabled. We have to use Observer#update rather than Observer#send since the enabled state is checked in #update before forwarding the method call on. --- activerecord/test/cases/lifecycle_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 643e949087..75e5dfa49b 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -231,6 +231,18 @@ class LifecycleTest < ActiveRecord::TestCase assert_not_nil observer.topic_ids.last end + test "able to disable observers" do + observer = DeveloperObserver.instance # activate + observer.calls.clear + + ActiveRecord::Base.observers.disable DeveloperObserver do + Developer.create! :name => 'Ancestor', :salary => 100000 + SpecialDeveloper.create! :name => 'Descendent', :salary => 100000 + end + + assert_equal [], observer.calls + end + def test_observer_is_called_once observer = DeveloperObserver.instance # activate observer.calls.clear -- cgit v1.2.3 From 34689c40a03c9921b5c43ac1e120a9885edded73 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sat, 13 Aug 2011 13:54:54 +0100 Subject: Work around for lolruby bug. (Read on for explanation.) We were experiencing CI test failures, for example: * 3-1-stable: http://travis-ci.org/#!/rails/rails/builds/79473/L407 * master: http://travis-ci.org/#!/rails/rails/builds/79507/L80 These failures only happened on 1.8.7-p352, and we were only able to reproduce on the Travis CI VM worker. We even tried creating a new 32 bit Ubuntu VM and running the tests on that, and it all worked fine. After some epic trial and error, we discovered that replacing the following: fuu = Marshal.load(Marshal.dump(fuu)) with: marshalled = Marshal.dump(fuu) fuu = Marshal.load(marshalled) seemed to prevent the failure. We have NO IDEA why this is. If anyone has some great insight to contribute then that is welcome. Otherwise, hopefully this will just help us get the CI green again. Many thanks to @joshk for help with sorting this out. --- activerecord/test/cases/associations/extension_test.rb | 8 ++++++-- activerecord/test/cases/base_test.rb | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index 24830a661a..490fc5177e 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -39,7 +39,9 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase david = developers(:david) assert_equal projects(:action_controller), david.projects.find_most_recent - david = Marshal.load(Marshal.dump(david)) + marshalled = Marshal.dump(david) + david = Marshal.load(marshalled) + assert_equal projects(:action_controller), david.projects.find_most_recent end @@ -47,7 +49,9 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase david = developers(:david) assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent - david = Marshal.load(Marshal.dump(david)) + marshalled = Marshal.dump(david) + david = Marshal.load(marshalled) + assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index c78d887ed7..b8ebabfe70 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1808,20 +1808,25 @@ class BasicsTest < ActiveRecord::TestCase def test_marshal_round_trip expected = posts(:welcome) - actual = Marshal.load(Marshal.dump(expected)) + marshalled = Marshal.dump(expected) + actual = Marshal.load(marshalled) assert_equal expected.attributes, actual.attributes end def test_marshal_new_record_round_trip - post = Marshal.load(Marshal.dump(Post.new)) + marshalled = Marshal.dump(Post.new) + post = Marshal.load(marshalled) + assert post.new_record?, "should be a new record" end def test_marshalling_with_associations post = Post.new post.comments.build - post = Marshal.load(Marshal.dump(post)) + + marshalled = Marshal.dump(post) + post = Marshal.load(marshalled) assert_equal 1, post.comments.length end -- cgit v1.2.3 From 24f902b1bcfa5dca4bfc7f2b978a4b0dece73894 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sat, 13 Aug 2011 16:37:44 +0100 Subject: Fix default scope thread safety. Thanks @thedarkone for reporting. --- activerecord/test/cases/relation_scoping_test.rb | 18 ++++++++++++++++++ activerecord/test/models/developer.rb | 9 +++++++++ 2 files changed, 27 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb index 673aff403f..1e2093273e 100644 --- a/activerecord/test/cases/relation_scoping_test.rb +++ b/activerecord/test/cases/relation_scoping_test.rb @@ -524,4 +524,22 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal 1, DeveloperWithIncludes.where(:audit_logs => { :message => 'foo' }).count end + + def test_default_scope_is_threadsafe + if in_memory_db? + skip "in memory db can't share a db between threads" + end + + threads = [] + assert_not_equal 1, ThreadsafeDeveloper.unscoped.count + + threads << Thread.new do + Thread.current[:long_default_scope] = true + assert_equal 1, ThreadsafeDeveloper.all.count + end + threads << Thread.new do + assert_equal 1, ThreadsafeDeveloper.all.count + end + threads.each(&:join) + end end diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index f182a7fa97..4dc9fff9fd 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -227,3 +227,12 @@ class EagerDeveloperWithCallableDefaultScope < ActiveRecord::Base default_scope OpenStruct.new(:call => includes(:projects)) end + +class ThreadsafeDeveloper < ActiveRecord::Base + self.table_name = 'developers' + + def self.default_scope + sleep 0.05 if Thread.current[:long_default_scope] + limit(1) + end +end -- cgit v1.2.3 From 57423d815b3747aa382cd3859a15bffa538525ad Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 15 Aug 2011 16:00:35 +0100 Subject: Ensure empty has_many :through association preloaded via joins is marked as loaded. Fixes #2054. --- .../test/cases/associations/has_many_through_associations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test') 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 0b1ba31ac2..5f2328ff95 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -813,4 +813,12 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert !c.save end end + + def test_preloading_empty_through_association_via_joins + person = Person.create!(:first_name => "Gaga") + person = Person.where(:id => person.id).where('readers.id = 1 or 1=1').includes(:posts).to_a.first + + assert person.posts.loaded?, 'person.posts should be loaded' + assert_equal [], person.posts + end end -- cgit v1.2.3 From 128d006242dae07edc65ad03e0e045adac0bbbf3 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 9 Aug 2011 00:12:53 +0100 Subject: Support updates with joins. Fixes #522. --- activerecord/test/cases/relations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 821da91f0a..7bd9c44651 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -965,4 +965,12 @@ class RelationTest < ActiveRecord::TestCase def test_ordering_with_extra_spaces assert_equal authors(:david), Author.order('id DESC , name DESC').last end + + def test_update_all_with_joins + comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id) + count = comments.count + + assert_equal count, comments.update_all(:post_id => posts(:thinking).id) + assert_equal posts(:thinking), comments(:greetings).post + end end -- cgit v1.2.3 From 43b99f290a8070196919a68999db87873257b7b8 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 10 Aug 2011 00:03:49 +0100 Subject: Support for multi-table updates with limits, offsets and orders --- activerecord/test/cases/relations_test.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 7bd9c44651..97abd67385 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -973,4 +973,34 @@ class RelationTest < ActiveRecord::TestCase assert_equal count, comments.update_all(:post_id => posts(:thinking).id) assert_equal posts(:thinking), comments(:greetings).post end + + def test_update_all_with_joins_and_limit + comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id).limit(1) + assert_equal 1, comments.update_all(:post_id => posts(:thinking).id) + end + + def test_update_all_with_joins_and_limit_and_order + comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id).order('comments.id').limit(1) + assert_equal 1, comments.update_all(:post_id => posts(:thinking).id) + assert_equal posts(:thinking), comments(:greetings).post + assert_equal posts(:welcome), comments(:more_greetings).post + end + + def test_update_all_with_joins_and_offset + all_comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id) + count = all_comments.count + comments = all_comments.offset(1) + + assert_equal count - 1, comments.update_all(:post_id => posts(:thinking).id) + end + + def test_update_all_with_joins_and_offset_and_order + all_comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id).order('posts.id') + count = all_comments.count + comments = all_comments.offset(1) + + assert_equal count - 1, comments.update_all(:post_id => posts(:thinking).id) + assert_equal posts(:thinking), comments(:more_greetings).post + assert_equal posts(:welcome), comments(:greetings).post + end end -- cgit v1.2.3 From 21e2eeacff4e9c37be374c4ed67ad3b016c854a2 Mon Sep 17 00:00:00 2001 From: Hendy Tanata Date: Tue, 16 Aug 2011 12:39:13 +0800 Subject: Remove unused require. --- activerecord/test/cases/attribute_methods_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index dbf5a1ba76..1e38298b87 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -8,7 +8,6 @@ require 'models/computer' require 'models/topic' require 'models/company' require 'models/category' -require 'models/reply' require 'models/contact' require 'models/keyboard' -- cgit v1.2.3 From c6e57467a65a3ec998f06676154105f9617c22a6 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 16 Aug 2011 09:24:49 -0300 Subject: Revert "Merge pull request #2543 from htanata/unused_require_in_ar_test" This reverts commit 87152f2604e73b218df90befda576f0acfed0bbf, reversing changes made to 0d3615f04c79f6e90d8ab33fdfc920b8faac9cb8. --- activerecord/test/cases/attribute_methods_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 1e38298b87..dbf5a1ba76 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -8,6 +8,7 @@ require 'models/computer' require 'models/topic' require 'models/company' require 'models/category' +require 'models/reply' require 'models/contact' require 'models/keyboard' -- cgit v1.2.3 From 8a39f411dc3c806422785b1f4d5c7c9d58e4bf85 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Aug 2011 15:17:17 -0700 Subject: prevent sql injection attacks by escaping quotes in column names --- activerecord/test/cases/base_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index b8ebabfe70..fe46c00b47 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -67,6 +67,23 @@ end class BasicsTest < ActiveRecord::TestCase fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts + def test_column_names_are_escaped + conn = ActiveRecord::Base.connection + classname = conn.class.name[/[^:]*$/] + badchar = { + 'SQLite3Adapter' => '"', + 'MysqlAdapter' => '`', + 'Mysql2Adapter' => '`', + 'PostgreSQLAdapter' => '"', + 'OracleAdapter' => '"', + }.fetch(classname) { + raise "need a bad char for #{classname}" + } + + quoted = conn.quote_column_name "foo#{badchar}bar" + assert_equal("#{badchar}foo#{badchar * 2}bar#{badchar}", quoted) + end + def test_columns_should_obey_set_primary_key pk = Subscriber.columns.find { |x| x.name == 'nick' } assert pk.primary, 'nick should be primary key' -- cgit v1.2.3 From e2ae015952c5bdcca044504b5be1c13a518eeaac Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Sat, 20 Aug 2011 13:00:26 -0400 Subject: Fix assumption of primary key name in PredicateBuilder subquery. --- activerecord/test/cases/relations_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 97abd67385..711b07b113 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -540,6 +540,16 @@ class RelationTest < ActiveRecord::TestCase } end + def test_find_all_using_where_with_relation_and_alternate_primary_key + cool_first = minivans(:cool_first) + # switching the lines below would succeed in current rails + # assert_queries(2) { + assert_queries(1) { + relation = Minivan.where(:minivan_id => Minivan.where(:name => cool_first.name)) + assert_equal [cool_first], relation.all + } + end + def test_find_all_using_where_with_relation_with_joins david = authors(:david) assert_queries(1) { -- cgit v1.2.3 From f74f5f7f00bd591fbca0cbc2b847acb0ec420256 Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Sat, 20 Aug 2011 13:16:39 -0400 Subject: Fix PredicateBuilder clobbering select_values in subquery. --- activerecord/test/cases/relations_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 711b07b113..615551a279 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -550,6 +550,19 @@ class RelationTest < ActiveRecord::TestCase } end + def test_find_all_using_where_with_relation_does_not_alter_select_values + david = authors(:david) + + subquery = Author.where(:id => david.id) + + assert_queries(1) { + relation = Author.where(:id => subquery) + assert_equal [david], relation.all + } + + assert_equal 0, subquery.select_values.size + end + def test_find_all_using_where_with_relation_with_joins david = authors(:david) assert_queries(1) { -- cgit v1.2.3 From 0a0da9d554490f01fe57d69fe8d98f29b02be3e5 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 24 Aug 2011 21:09:41 +0900 Subject: do not compute table names for abstract classes --- activerecord/test/cases/base_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index fe46c00b47..bee183cc67 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1625,6 +1625,10 @@ class BasicsTest < ActiveRecord::TestCase assert !LooseDescendant.abstract_class? end + def test_abstract_class_table_name + assert_nil AbstractCompany.table_name + end + def test_base_class assert_equal LoosePerson, LoosePerson.base_class assert_equal LooseDescendant, LooseDescendant.base_class -- cgit v1.2.3 From 42a7979cf1810c75343b18007858b81718b90678 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 25 Aug 2011 16:07:44 -0700 Subject: Force binary data inserted for a string data type to utf-8 and log an error. Strings tagged as binary will be stored in sqlite3 as blobs. It is an error to insert binary data to a string column, so an error is emitted in the log file. People are highly encouraged to track down the source of the binary strings and make sure that the encoding is set correctly before inserting to the database. --- .../test/cases/adapters/sqlite3/sqlite3_adapter_test.rb | 16 ++++++++++++++++ 1 file changed, 16 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 6ff04e3eb3..2b598220ee 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -1,5 +1,6 @@ # encoding: utf-8 require "cases/helper" +require 'models/owner' module ActiveRecord module ConnectionAdapters @@ -19,6 +20,21 @@ module ActiveRecord eosql end + def test_column_types + return skip('only test encoding on 1.9') unless "<3".encoding_aware? + + owner = Owner.create!(:name => "hello".encode('ascii-8bit')) + owner.reload + select = Owner.columns.map { |c| "typeof(#{c.name})" }.join ', ' + result = Owner.connection.exec_query <<-esql + SELECT #{select} + FROM #{Owner.table_name} + WHERE #{Owner.primary_key} = #{owner.id} + esql + + assert(!result.rows.first.include?("blob"), "should not store blobs") + end + def test_exec_insert column = @conn.columns('items').find { |col| col.name == 'number' } vals = [[column, 10]] -- cgit v1.2.3 From b4ff82a79177757509cefa2b103ae56d84b84f6d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 28 Aug 2011 14:15:51 -0700 Subject: clear and disable query cache when an exception is raised from called middleware --- activerecord/test/cases/query_cache_test.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index ad17f6f83a..fd5e69935e 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -13,6 +13,32 @@ class QueryCacheTest < ActiveRecord::TestCase ActiveRecord::Base.connection.disable_query_cache! end + def test_exceptional_middleware_clears_and_disables_cache_on_error + assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off' + + mw = ActiveRecord::QueryCache.new lambda { |env| + Task.find 1 + Task.find 1 + assert_equal 1, ActiveRecord::Base.connection.query_cache.length + raise "lol borked" + } + assert_raises(RuntimeError) { mw.call({}) } + + assert_equal 0, ActiveRecord::Base.connection.query_cache.length + assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off' + end + + def test_exceptional_middleware_leaves_enabled_cache_alone + ActiveRecord::Base.connection.enable_query_cache! + + mw = ActiveRecord::QueryCache.new lambda { |env| + raise "lol borked" + } + assert_raises(RuntimeError) { mw.call({}) } + + assert ActiveRecord::Base.connection.query_cache_enabled, 'cache off' + end + def test_middleware_delegates called = false mw = ActiveRecord::QueryCache.new lambda { |env| @@ -213,4 +239,4 @@ class QueryCacheBodyProxyTest < ActiveRecord::TestCase assert_equal proxy.to_path, "/path" end -end \ No newline at end of file +end -- cgit v1.2.3 From d63688d9e69d2440a35ab89c9b21866272a74fe3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 28 Aug 2011 16:31:04 -0700 Subject: fixing typo --- activerecord/test/cases/query_cache_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index fd5e69935e..e3ad0cad90 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -36,7 +36,7 @@ class QueryCacheTest < ActiveRecord::TestCase } assert_raises(RuntimeError) { mw.call({}) } - assert ActiveRecord::Base.connection.query_cache_enabled, 'cache off' + assert ActiveRecord::Base.connection.query_cache_enabled, 'cache on' end def test_middleware_delegates -- cgit v1.2.3 From 5766539342426e956980bf6f54ef99600cbfc33e Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 25 Aug 2011 22:28:46 +0100 Subject: Create an AbstractMysqlAdapter to abstract the common code between MysqlAdapter and Mysql2Adapter. --- .../cases/adapters/mysql/active_schema_test.rb | 8 +++--- activerecord/test/cases/column_definition_test.rb | 32 +++++++++++----------- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapters/mysql/active_schema_test.rb b/activerecord/test/cases/adapters/mysql/active_schema_test.rb index 509baacaef..94fc3564df 100644 --- a/activerecord/test/cases/adapters/mysql/active_schema_test.rb +++ b/activerecord/test/cases/adapters/mysql/active_schema_test.rb @@ -2,7 +2,7 @@ require "cases/helper" class ActiveSchemaTest < ActiveRecord::TestCase def setup - ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do + ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.class_eval do alias_method :execute_without_stub, :execute remove_method :execute def execute(sql, name = nil) return sql end @@ -10,7 +10,7 @@ class ActiveSchemaTest < ActiveRecord::TestCase end def teardown - ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do + ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.class_eval do remove_method :execute alias_method :execute, :execute_without_stub end @@ -99,7 +99,7 @@ class ActiveSchemaTest < ActiveRecord::TestCase private def with_real_execute #we need to actually modify some data, so we make execute point to the original method - ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do + ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.class_eval do alias_method :execute_with_stub, :execute remove_method :execute alias_method :execute, :execute_without_stub @@ -107,7 +107,7 @@ class ActiveSchemaTest < ActiveRecord::TestCase yield ensure #before finishing, we restore the alias to the mock-up method - ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do + ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.class_eval do remove_method :execute alias_method :execute, :execute_with_stub end diff --git a/activerecord/test/cases/column_definition_test.rb b/activerecord/test/cases/column_definition_test.rb index d1dddd4c2c..14884e42af 100644 --- a/activerecord/test/cases/column_definition_test.rb +++ b/activerecord/test/cases/column_definition_test.rb @@ -58,68 +58,68 @@ module ActiveRecord if current_adapter?(:MysqlAdapter) def test_should_set_default_for_mysql_binary_data_types - binary_column = MysqlColumn.new("title", "a", "binary(1)") + binary_column = MysqlAdapter::Column.new("title", "a", "binary(1)") assert_equal "a", binary_column.default - varbinary_column = MysqlColumn.new("title", "a", "varbinary(1)") + varbinary_column = MysqlAdapter::Column.new("title", "a", "varbinary(1)") assert_equal "a", varbinary_column.default end def test_should_not_set_default_for_blob_and_text_data_types assert_raise ArgumentError do - MysqlColumn.new("title", "a", "blob") + MysqlAdapter::Column.new("title", "a", "blob") end assert_raise ArgumentError do - MysqlColumn.new("title", "Hello", "text") + MysqlAdapter::Column.new("title", "Hello", "text") end - text_column = MysqlColumn.new("title", nil, "text") + text_column = MysqlAdapter::Column.new("title", nil, "text") assert_equal nil, text_column.default - not_null_text_column = MysqlColumn.new("title", nil, "text", false) + not_null_text_column = MysqlAdapter::Column.new("title", nil, "text", false) assert_equal "", not_null_text_column.default end def test_has_default_should_return_false_for_blog_and_test_data_types - blob_column = MysqlColumn.new("title", nil, "blob") + blob_column = MysqlAdapter::Column.new("title", nil, "blob") assert !blob_column.has_default? - text_column = MysqlColumn.new("title", nil, "text") + text_column = MysqlAdapter::Column.new("title", nil, "text") assert !text_column.has_default? end end if current_adapter?(:Mysql2Adapter) def test_should_set_default_for_mysql_binary_data_types - binary_column = Mysql2Column.new("title", "a", "binary(1)") + binary_column = Mysql2Adapter::Column.new("title", "a", "binary(1)") assert_equal "a", binary_column.default - varbinary_column = Mysql2Column.new("title", "a", "varbinary(1)") + varbinary_column = Mysql2Adapter::Column.new("title", "a", "varbinary(1)") assert_equal "a", varbinary_column.default end def test_should_not_set_default_for_blob_and_text_data_types assert_raise ArgumentError do - Mysql2Column.new("title", "a", "blob") + Mysql2Adapter::Column.new("title", "a", "blob") end assert_raise ArgumentError do - Mysql2Column.new("title", "Hello", "text") + Mysql2Adapter::Column.new("title", "Hello", "text") end - text_column = Mysql2Column.new("title", nil, "text") + text_column = Mysql2Adapter::Column.new("title", nil, "text") assert_equal nil, text_column.default - not_null_text_column = Mysql2Column.new("title", nil, "text", false) + not_null_text_column = Mysql2Adapter::Column.new("title", nil, "text", false) assert_equal "", not_null_text_column.default end def test_has_default_should_return_false_for_blog_and_test_data_types - blob_column = Mysql2Column.new("title", nil, "blob") + blob_column = Mysql2Adapter::Column.new("title", nil, "blob") assert !blob_column.has_default? - text_column = Mysql2Column.new("title", nil, "text") + text_column = Mysql2Adapter::Column.new("title", nil, "text") assert !text_column.has_default? end end -- cgit v1.2.3 From 735d985b0162976e7e900cf36d4cbb0d657fb5e9 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 29 Aug 2011 15:01:17 +0100 Subject: The join_nodes must be passed to the JoinDependency initializer and therefore counted by the alias tracker. This is because the association_joins are aliased on initialization and then the tables are cached, so it is no use to alias the join_nodes later. Fixes #2556. --- .../test/cases/associations/has_many_through_associations_test.rb | 4 ++++ activerecord/test/models/toy.rb | 2 ++ 2 files changed, 6 insertions(+) (limited to 'activerecord/test') 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 5f2328ff95..b703c96ec1 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -821,4 +821,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert person.posts.loaded?, 'person.posts should be loaded' assert_equal [], person.posts end + + def test_explicitly_joining_join_table + assert_equal owners(:blackbeard).toys, owners(:blackbeard).toys.with_pet + end end diff --git a/activerecord/test/models/toy.rb b/activerecord/test/models/toy.rb index 79a88db0da..6c45e99671 100644 --- a/activerecord/test/models/toy.rb +++ b/activerecord/test/models/toy.rb @@ -1,4 +1,6 @@ class Toy < ActiveRecord::Base set_primary_key :toy_id belongs_to :pet + + scope :with_pet, joins(:pet) end -- cgit v1.2.3 From bd4bd3f50a7e3a8efd8f24612765a7f16e520748 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 30 Aug 2011 09:56:42 -0700 Subject: Merge pull request #2750 from rsim/fix_test_column_names_are_escaped_for_oracle Fix test column names are escaped for oracle --- activerecord/test/cases/base_test.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index bee183cc67..1e647b5970 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -81,7 +81,13 @@ class BasicsTest < ActiveRecord::TestCase } quoted = conn.quote_column_name "foo#{badchar}bar" - assert_equal("#{badchar}foo#{badchar * 2}bar#{badchar}", quoted) + if current_adapter?(:OracleAdapter) + # Oracle does not allow double quotes in table and column names at all + # therefore quoting removes them + assert_equal("#{badchar}foobar#{badchar}", quoted) + else + assert_equal("#{badchar}foo#{badchar * 2}bar#{badchar}", quoted) + end end def test_columns_should_obey_set_primary_key -- cgit v1.2.3 From 60d8e4ac28726d04d029b25cff82181a9b0ab5c3 Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Tue, 30 Aug 2011 20:37:16 +0300 Subject: Ensure correct ordering of results in test_update_all_with_joins_and_offset_and_order Last two asserts in this test assume that all_comments are ordered by posts.id and then by comments.id therefore additional ordering is added. Without it test was failing on Oracle which returned results in different order. --- 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 615551a279..da96afd718 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1018,7 +1018,7 @@ class RelationTest < ActiveRecord::TestCase end def test_update_all_with_joins_and_offset_and_order - all_comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id).order('posts.id') + all_comments = Comment.joins(:post).where('posts.id' => posts(:welcome).id).order('posts.id', 'comments.id') count = all_comments.count comments = all_comments.offset(1) -- cgit v1.2.3