From eec90bab2815642d1771c9f9c37bd5a0bcd6fc9d Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Fri, 7 Aug 2009 17:18:45 +0300 Subject: create DateTime value with local offset as later it is compared to Time value with local offset (otherwise test is failing for oracle_enhanced JDBC adapter) --- activerecord/test/cases/date_time_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/date_time_test.rb b/activerecord/test/cases/date_time_test.rb index 36e1caa0b6..a8b4b7a096 100644 --- a/activerecord/test/cases/date_time_test.rb +++ b/activerecord/test/cases/date_time_test.rb @@ -5,7 +5,9 @@ require 'models/task' class DateTimeTest < ActiveRecord::TestCase def test_saves_both_date_and_time time_values = [1807, 2, 10, 15, 30, 45] - now = DateTime.civil(*time_values) + # create DateTime value with local time zone offset + local_offset = Rational(Time.local_time(*time_values).utc_offset, 86400) + now = DateTime.civil(*(time_values + [local_offset])) task = Task.new task.starting = now -- cgit v1.2.3 From 8136230cca4ff7550f9fbc786a4327bd520d27cb Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Mon, 10 Aug 2009 17:25:53 +0300 Subject: Explicitly set Topic model last_read attribute as Date value when Oracle enhanced adapter is used (otherwise some tests are failing which assume that this attribute will have Date value) --- activerecord/test/cases/base_test.rb | 7 ++++--- activerecord/test/models/topic.rb | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 1d883f7ea8..36c572b5e7 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -430,14 +430,14 @@ class BasicsTest < ActiveRecord::TestCase end def test_preserving_date_objects - if current_adapter?(:SybaseAdapter, :OracleAdapter) + if current_adapter?(:SybaseAdapter) # Sybase ctlib does not (yet?) support the date type; use datetime instead. - # Oracle treats all dates/times as Time. assert_kind_of( Time, Topic.find(1).last_read, "The last_read attribute should be of the Time class" ) else + # Oracle enhanced adapter allows to define Date attributes in model class (see topic.rb) assert_kind_of( Date, Topic.find(1).last_read, "The last_read attribute should be of the Date class" @@ -2125,10 +2125,11 @@ class BasicsTest < ActiveRecord::TestCase assert_equal "integer", xml.elements["//parent-id"].attributes['type'] assert_equal "true", xml.elements["//parent-id"].attributes['nil'] - if current_adapter?(:SybaseAdapter, :OracleAdapter) + if current_adapter?(:SybaseAdapter) assert_equal last_read_in_current_timezone, xml.elements["//last-read"].text assert_equal "datetime" , xml.elements["//last-read"].attributes['type'] else + # Oracle enhanced adapter allows to define Date attributes in model class (see topic.rb) assert_equal "2004-04-15", xml.elements["//last-read"].text assert_equal "date" , xml.elements["//last-read"].attributes['type'] end diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 91fc7c9416..617f01b47d 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -43,6 +43,12 @@ class Topic < ActiveRecord::Base before_create :default_written_on before_destroy :destroy_children + # Explicitly define as :date column so that returned Oracle DATE values would be typecasted to Date and not Time. + # Some tests depend on assumption that this attribute will have Date values. + if current_adapter?(:OracleEnhancedAdapter) + set_date_columns :last_read + end + def parent Topic.find(parent_id) end -- cgit v1.2.3 From 6356a48faa07908e7eb1759b0a849adc9e0a1d97 Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Mon, 10 Aug 2009 17:32:38 +0300 Subject: Reduced size of table name prefix and suffix in migrations test as in Oracle identifier name cannot be larger than 30 characters --- activerecord/test/cases/migration_test.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index b5fa258f7b..5313274f3e 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -30,13 +30,14 @@ if ActiveRecord::Base.connection.supports_migrations? conn = ActiveRecord::Base.connection conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) - ActiveRecord::Base.table_name_prefix = 'foo_' - ActiveRecord::Base.table_name_suffix = '_bar' + # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters + ActiveRecord::Base.table_name_prefix = 'p_' + ActiveRecord::Base.table_name_suffix = '_s' conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) conn.initialize_schema_migrations_table - assert_equal "foo_unique_schema_migrations_bar", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name] + assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name] ensure ActiveRecord::Base.table_name_prefix = "" ActiveRecord::Base.table_name_suffix = "" -- cgit v1.2.3 From f12f3776806d5c6f003533b5bddb96070f0f720d Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Tue, 11 Aug 2009 18:29:32 +0300 Subject: Oracle enhanced adapter now supports shortening of default generated index names, some additional tests now pass --- activerecord/test/cases/migration_test.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 5313274f3e..a0237d57a7 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -84,13 +84,17 @@ if ActiveRecord::Base.connection.supports_migrations? # Orcl nds shrt indx nms. Sybs 2. # OpenBase does not have named indexes. You must specify a single column name - unless current_adapter?(:OracleAdapter, :SybaseAdapter, :OpenBaseAdapter) + unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter) assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.remove_index("people", :column => ["last_name", "first_name"]) } - assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } - assert_nothing_raised { Person.connection.remove_index("people", :name => "index_people_on_last_name_and_first_name") } - assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } - assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") } + # Oracle adapter cannot have specified index name larger than 30 characters + # Oracle adapter is shortening index name when just column list is given + unless current_adapter?(:OracleAdapter) + assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } + assert_nothing_raised { Person.connection.remove_index("people", :name => "index_people_on_last_name_and_first_name") } + assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } + assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") } + end assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) } assert_nothing_raised { Person.connection.add_index("people", ["last_name"], :length => 10) } @@ -737,13 +741,7 @@ if ActiveRecord::Base.connection.supports_migrations? table.column :hat_size, :integer table.column :hat_style, :string, :limit => 100 end - # Oracle index names should be 30 or less characters - if current_adapter?(:OracleAdapter) - ActiveRecord::Base.connection.add_index "hats", ["hat_style", "hat_size"], :unique => true, - :name => 'index_hats_on_hat_style_size' - else - ActiveRecord::Base.connection.add_index "hats", ["hat_style", "hat_size"], :unique => true - end + ActiveRecord::Base.connection.add_index "hats", ["hat_style", "hat_size"], :unique => true assert_nothing_raised { Person.connection.remove_column("hats", "hat_size") } ensure -- cgit v1.2.3 From 9bf7b6334fae71821c2056ae2e6e8461ec3f73be Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Fri, 18 Sep 2009 18:56:55 +0300 Subject: do order by id when finding first fixture to ensure that it is correct one (as otherwise was failing under JRuby and oracle_enhanced adapter) --- activerecord/test/cases/associations/has_many_associations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (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 8e5bc56008..45c7498013 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -511,7 +511,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_deleting_updates_counter_cache - topic = Topic.first + topic = Topic.first(:order => "id ASC") assert_equal topic.replies.to_a.size, topic.replies_count topic.replies.delete(topic.replies.first) -- cgit v1.2.3 From 464b7f3ddcf5e9b23741862bf8c7a49f61792de3 Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Fri, 23 Oct 2009 18:20:03 +0300 Subject: test fixes for Oracle enhanced adapter: latest oracle_enhanced adapter does automatic shortening of index names ignore select from all_triggers table in assert_queries --- activerecord/test/cases/migration_test.rb | 4 ++-- activerecord/test/connections/native_oracle/connection.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index a0237d57a7..1edec66c25 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1374,8 +1374,8 @@ if ActiveRecord::Base.connection.supports_migrations? return unless current_adapter? :OracleAdapter # table name is 29 chars, the standard sequence name will - # be 33 chars and fail - assert_raise(ActiveRecord::StatementInvalid) do + # be 33 chars and should be shortened + assert_nothing_raised do begin Person.connection.create_table :table_with_name_thats_just_ok do |t| t.column :foo, :string, :null => false diff --git a/activerecord/test/connections/native_oracle/connection.rb b/activerecord/test/connections/native_oracle/connection.rb index c8183dc0fb..a8ff462dd7 100644 --- a/activerecord/test/connections/native_oracle/connection.rb +++ b/activerecord/test/connections/native_oracle/connection.rb @@ -53,7 +53,7 @@ Course.establish_connection 'arunit2' # for assert_queries test helper ActiveRecord::Base.connection.class.class_eval do - IGNORED_SELECT_SQL = [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^\s*select .* from all_tab_columns/im] + IGNORED_SELECT_SQL = [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^\s*select .* from (all_tab_columns|all_triggers)/im] def select_with_query_record(sql, name = nil, return_column_names = false) $queries_executed ||= [] -- cgit v1.2.3 From a82e06709179791e6b0a3bca37113f144dcefbb8 Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Mon, 15 Feb 2010 17:57:45 +0200 Subject: assert log output match in case insensitive mode to avoid failure when quoted table name is in uppercase (when using Oracle) --- activerecord/test/cases/log_subscriber_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index 6ba84fa57b..1f544b4211 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -27,7 +27,7 @@ class LogSubscriberTest < ActiveSupport::TestCase wait assert_equal 1, @logger.logged(:debug).size assert_match(/Developer Load/, @logger.logged(:debug).last) - assert_match(/SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last) + assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last) end def test_cached_queries @@ -38,6 +38,6 @@ class LogSubscriberTest < ActiveSupport::TestCase wait assert_equal 2, @logger.logged(:debug).size assert_match(/CACHE/, @logger.logged(:debug).last) - assert_match(/SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last) + assert_match(/SELECT .*?FROM .?developers.?/i, @logger.logged(:debug).last) end end -- cgit v1.2.3 From c6d6b5016631ae0d2c6f09bb289fb9b54dca9a0a Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Sun, 7 Mar 2010 16:46:59 +0200 Subject: ignore selects from data dictionary views when running tests on Oracle --- activerecord/test/connections/native_oracle/connection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/connections/native_oracle/connection.rb b/activerecord/test/connections/native_oracle/connection.rb index a8ff462dd7..bb4040058f 100644 --- a/activerecord/test/connections/native_oracle/connection.rb +++ b/activerecord/test/connections/native_oracle/connection.rb @@ -53,7 +53,7 @@ Course.establish_connection 'arunit2' # for assert_queries test helper ActiveRecord::Base.connection.class.class_eval do - IGNORED_SELECT_SQL = [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^\s*select .* from (all_tab_columns|all_triggers)/im] + IGNORED_SELECT_SQL = [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO/, /^\s*select .* from ((all|user)_tab_columns|(all|user)_triggers|(all|user)_constraints)/im] def select_with_query_record(sql, name = nil, return_column_names = false) $queries_executed ||= [] -- cgit v1.2.3 From 05ef038bb955d4a0c9cbda50bf7ff7eb259bdf59 Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Sun, 16 May 2010 19:55:21 +0300 Subject: Fixed adapter tests not to assert LIMIT and OFFSET in SQL strings Fixed adapter test cases that were failing in oracle because the asserts were looking for the presence of offset and limit which are not available in oracle. Changed the tests to check that the sql injection is not present in the output so that the tests are database adapter agnostic. --- activerecord/test/cases/adapter_test.rb | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 9b28766405..0152b7be2a 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -145,22 +145,13 @@ class AdapterTest < ActiveRecord::TestCase def test_add_limit_offset_should_sanitize_sql_injection_for_limit_without_comas sql_inject = "1 select * from schema" - assert_equal " LIMIT 1", @connection.add_limit_offset!("", :limit => sql_inject) - if current_adapter?(:MysqlAdapter) - assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit => sql_inject, :offset => 7) - else - assert_equal " LIMIT 1 OFFSET 7", @connection.add_limit_offset!("", :limit => sql_inject, :offset => 7) - end + assert_no_match /schema/, @connection.add_limit_offset!("", :limit=>sql_inject) + assert_no_match /schema/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7) end def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas sql_inject = "1, 7 procedure help()" - if current_adapter?(:MysqlAdapter) - assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit => sql_inject) - assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit => '1 ; DROP TABLE USERS', :offset => 7) - else - assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit => sql_inject) - assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit => sql_inject, :offset => 7) - end + assert_no_match /procedure/, @connection.add_limit_offset!("", :limit=>sql_inject) + assert_no_match /procedure/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7) end end -- cgit v1.2.3 From b0fdd290f4b186e6da9c550336ee610970f2e8a3 Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Sun, 16 May 2010 23:40:22 +0300 Subject: fix test_belongs_to_with_primary_key_joins_on_correct_column test on Oracle --- activerecord/test/cases/associations/belongs_to_associations_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 9258c987ef..fb1e6e7e70 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -37,6 +37,10 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase if current_adapter?(:MysqlAdapter) assert_no_match(/`firm_with_primary_keys_companies`\.`id`/, sql) assert_match(/`firm_with_primary_keys_companies`\.`name`/, sql) + elsif current_adapter?(:OracleAdapter) + # on Oracle aliases are truncated to 30 characters and are quoted in uppercase + assert_no_match(/"firm_with_primary_keys_compani"\."id"/i, sql) + assert_match(/"firm_with_primary_keys_compani"\."name"/i, sql) else assert_no_match(/"firm_with_primary_keys_companies"\."id"/, sql) assert_match(/"firm_with_primary_keys_companies"\."name"/, sql) -- cgit v1.2.3 From acef8feafa8a44271eb28685e180f8c28b7e4a0f Mon Sep 17 00:00:00 2001 From: Raimonds Simanovskis Date: Fri, 4 Jun 2010 22:58:55 +0300 Subject: compare sorted arrays in relations_test Oracle does not guarantee that SELECT will return records ordered by primary key --- 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 4097c5119e..43519db976 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -114,7 +114,7 @@ class RelationTest < ActiveRecord::TestCase def test_select_with_block even_ids = Developer.scoped.select {|d| d.id % 2 == 0 }.map(&:id) - assert_equal [2, 4, 6, 8, 10], even_ids + assert_equal [2, 4, 6, 8, 10], even_ids.sort end def test_finding_with_hash_conditions_on_joined_table -- cgit v1.2.3 From a04060fb6fe006b1dbc224263dd6c39525733c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 8 Jun 2010 01:02:45 +0200 Subject: Really make include_root_in_json default to true [#3770 state:resolved] --- activerecord/test/cases/json_serialization_test.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb index a3145d2c04..c275557da8 100644 --- a/activerecord/test/cases/json_serialization_test.rb +++ b/activerecord/test/cases/json_serialization_test.rb @@ -8,7 +8,7 @@ require 'models/comment' class JsonSerializationTest < ActiveRecord::TestCase class NamespacedContact < Contact - column :name, :string + column :name, :string end def setup @@ -23,16 +23,12 @@ class JsonSerializationTest < ActiveRecord::TestCase end def test_should_demodulize_root_in_json - NamespacedContact.include_root_in_json = true @contact = NamespacedContact.new :name => 'whatever' json = @contact.to_json assert_match %r{^\{"namespaced_contact":\{}, json - ensure - NamespacedContact.include_root_in_json = false end def test_should_include_root_in_json - Contact.include_root_in_json = true json = @contact.to_json assert_match %r{^\{"contact":\{}, json @@ -41,8 +37,6 @@ class JsonSerializationTest < ActiveRecord::TestCase assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) assert_match %r{"awesome":true}, json assert_match %r{"preferences":\{"shows":"anime"\}}, json - ensure - Contact.include_root_in_json = false end def test_should_encode_all_encodable_attributes @@ -170,15 +164,19 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase end def test_should_allow_only_option_for_list_of_authors + ActiveRecord::Base.include_root_in_json = false authors = [@david, @mary] - assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name) + ensure + ActiveRecord::Base.include_root_in_json = true end def test_should_allow_except_option_for_list_of_authors + ActiveRecord::Base.include_root_in_json = false authors = [@david, @mary] - assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id]) + ensure + ActiveRecord::Base.include_root_in_json = true end def test_should_allow_includes_for_list_of_authors @@ -201,7 +199,6 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase 1 => @david, 2 => @mary } - - assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name]) + assert_equal %({"1":{"author":{"name":"David"}}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name]) end end -- cgit v1.2.3 From 94ed39d146e898f15317477c8ca61cbb4d3b97d0 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 7 Jun 2010 20:56:26 -0300 Subject: This test shouldn't rely on the order of the data returned by select Signed-off-by: Jeremy Kemper --- 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 4097c5119e..43519db976 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -114,7 +114,7 @@ class RelationTest < ActiveRecord::TestCase def test_select_with_block even_ids = Developer.scoped.select {|d| d.id % 2 == 0 }.map(&:id) - assert_equal [2, 4, 6, 8, 10], even_ids + assert_equal [2, 4, 6, 8, 10], even_ids.sort end def test_finding_with_hash_conditions_on_joined_table -- cgit v1.2.3 From a4eaa1fd39f93eff975bfd8a5cc4c7dfc3f18aa7 Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Sat, 22 May 2010 15:31:06 -0400 Subject: Fix multiple self-referencing eager loads failing to join multiple times [#4679 state:committed] Signed-off-by: Jeremy Kemper --- .../test/cases/associations/cascaded_eager_loading_test.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index fe558f9d3b..7a4a33d177 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -7,9 +7,10 @@ require 'models/categorization' require 'models/company' require 'models/topic' require 'models/reply' +require 'models/person' class CascadedEagerLoadingTest < ActiveRecord::TestCase - fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations + fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations, :people def test_eager_association_loading_with_cascaded_two_levels authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id") @@ -38,6 +39,13 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase assert_equal 9, assert_no_queries { authors[0].comments.size } end + def test_eager_association_loading_grafts_stashed_associations_to_correct_parent + assert_nothing_raised do + Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').all + end + assert_equal people(:michael), Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').first + end + def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id") assert_equal 2, authors.size -- cgit v1.2.3 From 1b2941cba1165b0721f57524645fe378bee2a950 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 8 Jun 2010 14:40:20 -0400 Subject: Temporarily revert "Update after_commit and after_rollback docs and tests to use new style API with an :on options instead of on_* suffix." and "Add after_commit and after_rollback callbacks to ActiveRecord that are called after transactions either commit or rollback on all records saved or destroyed in the transaction." This reverts commits d2a49e4b1f30c5997e169110eed94a55aee53f56 and da840d13da865331297d5287391231b1ed39721b. [#2991] Conflicts: activerecord/CHANGELOG activerecord/lib/active_record/transactions.rb activerecord/test/cases/transaction_callbacks_test.rb --- .../test/cases/transaction_callbacks_test.rb | 240 --------------------- activerecord/test/cases/transactions_test.rb | 33 --- 2 files changed, 273 deletions(-) delete mode 100644 activerecord/test/cases/transaction_callbacks_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb deleted file mode 100644 index ebc16653cb..0000000000 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ /dev/null @@ -1,240 +0,0 @@ -require "cases/helper" -require 'models/topic' -require 'models/reply' - -class TransactionCallbacksTest < ActiveRecord::TestCase - self.use_transactional_fixtures = false - fixtures :topics - - class TopicWithCallbacks < ActiveRecord::Base - set_table_name :topics - - after_commit{|record| record.send(:do_after_commit, nil)} - after_commit(:on => :create){|record| record.send(:do_after_commit, :create)} - after_commit(:on => :update){|record| record.send(:do_after_commit, :update)} - after_commit(:on => :destroy){|record| record.send(:do_after_commit, :destroy)} - after_rollback{|record| record.send(:do_after_rollback, nil)} - after_rollback(:on => :create){|record| record.send(:do_after_rollback, :create)} - after_rollback(:on => :update){|record| record.send(:do_after_rollback, :update)} - after_rollback(:on => :destroy){|record| record.send(:do_after_rollback, :destroy)} - - def history - @history ||= [] - end - - def after_commit_block(on = nil, &block) - @after_commit ||= {} - @after_commit[on] ||= [] - @after_commit[on] << block - end - - def after_rollback_block(on = nil, &block) - @after_rollback ||= {} - @after_rollback[on] ||= [] - @after_rollback[on] << block - end - - def do_after_commit(on) - blocks = @after_commit[on] if defined?(@after_commit) - blocks.each{|b| b.call(self)} if blocks - end - - def do_after_rollback(on) - blocks = @after_rollback[on] if defined?(@after_rollback) - blocks.each{|b| b.call(self)} if blocks - end - end - - def setup - @first, @second = TopicWithCallbacks.find(1, 3).sort_by { |t| t.id } - end - - def test_call_after_commit_after_transaction_commits - @first.after_commit_block{|r| r.history << :after_commit} - @first.after_rollback_block{|r| r.history << :after_rollback} - - @first.save! - assert_equal [:after_commit], @first.history - end - - def test_only_call_after_commit_on_update_after_transaction_commits_for_existing_record - @first.after_commit_block(:create){|r| r.history << :commit_on_create} - @first.after_commit_block(:update){|r| r.history << :commit_on_update} - @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy} - @first.after_rollback_block(:create){|r| r.history << :rollback_on_create} - @first.after_rollback_block(:update){|r| r.history << :rollback_on_update} - @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy} - - @first.save! - assert_equal [:commit_on_update], @first.history - end - - def test_only_call_after_commit_on_destroy_after_transaction_commits_for_destroyed_record - @first.after_commit_block(:create){|r| r.history << :commit_on_create} - @first.after_commit_block(:update){|r| r.history << :commit_on_update} - @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy} - @first.after_rollback_block(:create){|r| r.history << :rollback_on_create} - @first.after_rollback_block(:update){|r| r.history << :rollback_on_update} - @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy} - - @first.destroy - assert_equal [:commit_on_destroy], @first.history - end - - def test_only_call_after_commit_on_create_after_transaction_commits_for_new_record - @new_record = TopicWithCallbacks.new(:title => "New topic", :written_on => Date.today) - @new_record.after_commit_block(:create){|r| r.history << :commit_on_create} - @new_record.after_commit_block(:update){|r| r.history << :commit_on_update} - @new_record.after_commit_block(:destroy){|r| r.history << :commit_on_destroy} - @new_record.after_rollback_block(:create){|r| r.history << :rollback_on_create} - @new_record.after_rollback_block(:update){|r| r.history << :rollback_on_update} - @new_record.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy} - - @new_record.save! - assert_equal [:commit_on_create], @new_record.history - end - - def test_call_after_rollback_after_transaction_rollsback - @first.after_commit_block{|r| r.history << :after_commit} - @first.after_rollback_block{|r| r.history << :after_rollback} - - Topic.transaction do - @first.save! - raise ActiveRecord::Rollback - end - - assert_equal [:after_rollback], @first.history - end - - def test_only_call_after_rollback_on_update_after_transaction_rollsback_for_existing_record - @first.after_commit_block(:create){|r| r.history << :commit_on_create} - @first.after_commit_block(:update){|r| r.history << :commit_on_update} - @first.after_commit_block(:destroy){|r| r.history << :commit_on_destroy} - @first.after_rollback_block(:create){|r| r.history << :rollback_on_create} - @first.after_rollback_block(:update){|r| r.history << :rollback_on_update} - @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy} - - Topic.transaction do - @first.save! - raise ActiveRecord::Rollback - end - - assert_equal [:rollback_on_update], @first.history - end - - def test_only_call_after_rollback_on_destroy_after_transaction_rollsback_for_destroyed_record - @first.after_commit_block(:create){|r| r.history << :commit_on_create} - @first.after_commit_block(:update){|r| r.history << :commit_on_update} - @first.after_commit_block(:destroy){|r| r.history << :commit_on_update} - @first.after_rollback_block(:create){|r| r.history << :rollback_on_create} - @first.after_rollback_block(:update){|r| r.history << :rollback_on_update} - @first.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy} - - Topic.transaction do - @first.destroy - raise ActiveRecord::Rollback - end - - assert_equal [:rollback_on_destroy], @first.history - end - - def test_only_call_after_rollback_on_create_after_transaction_rollsback_for_new_record - @new_record = TopicWithCallbacks.new(:title => "New topic", :written_on => Date.today) - @new_record.after_commit_block(:create){|r| r.history << :commit_on_create} - @new_record.after_commit_block(:update){|r| r.history << :commit_on_update} - @new_record.after_commit_block(:destroy){|r| r.history << :commit_on_destroy} - @new_record.after_rollback_block(:create){|r| r.history << :rollback_on_create} - @new_record.after_rollback_block(:update){|r| r.history << :rollback_on_update} - @new_record.after_rollback_block(:destroy){|r| r.history << :rollback_on_destroy} - - Topic.transaction do - @new_record.save! - raise ActiveRecord::Rollback - end - - assert_equal [:rollback_on_create], @new_record.history - end - - def test_call_after_rollback_when_commit_fails - @first.connection.class.send(:alias_method, :real_method_commit_db_transaction, :commit_db_transaction) - begin - @first.connection.class.class_eval do - def commit_db_transaction; raise "boom!"; end - end - - @first.after_commit_block{|r| r.history << :after_commit} - @first.after_rollback_block{|r| r.history << :after_rollback} - - assert !@first.save rescue nil - assert_equal [:after_rollback], @first.history - ensure - @first.connection.class.send(:remove_method, :commit_db_transaction) - @first.connection.class.send(:alias_method, :commit_db_transaction, :real_method_commit_db_transaction) - end - end - - def test_only_call_after_rollback_on_records_rolled_back_to_a_savepoint - def @first.rollbacks(i=0); @rollbacks ||= 0; @rollbacks += i if i; end - def @first.commits(i=0); @commits ||= 0; @commits += i if i; end - @first.after_rollback_block{|r| r.rollbacks(1)} - @first.after_commit_block{|r| r.commits(1)} - - def @second.rollbacks(i=0); @rollbacks ||= 0; @rollbacks += i if i; end - def @second.commits(i=0); @commits ||= 0; @commits += i if i; end - @second.after_rollback_block{|r| r.rollbacks(1)} - @second.after_commit_block{|r| r.commits(1)} - - Topic.transaction do - @first.save! - Topic.transaction(:requires_new => true) do - @second.save! - raise ActiveRecord::Rollback - end - end - - assert_equal 1, @first.commits - assert_equal 0, @first.rollbacks - assert_equal 0, @second.commits - assert_equal 1, @second.rollbacks - end - - def test_only_call_after_rollback_on_records_rolled_back_to_a_savepoint_when_release_savepoint_fails - def @first.rollbacks(i=0); @rollbacks ||= 0; @rollbacks += i if i; end - def @first.commits(i=0); @commits ||= 0; @commits += i if i; end - - @first.after_rollback_block{|r| r.rollbacks(1)} - @first.after_commit_block{|r| r.commits(1)} - - Topic.transaction do - @first.save - Topic.transaction(:requires_new => true) do - @first.save! - raise ActiveRecord::Rollback - end - Topic.transaction(:requires_new => true) do - @first.save! - raise ActiveRecord::Rollback - end - end - - assert_equal 1, @first.commits - assert_equal 2, @first.rollbacks - end - - def test_after_transaction_callbacks_should_not_raise_errors - def @first.last_after_transaction_error=(e); @last_transaction_error = e; end - def @first.last_after_transaction_error; @last_transaction_error; end - @first.after_commit_block{|r| r.last_after_transaction_error = :commit; raise "fail!";} - @first.after_rollback_block{|r| r.last_after_transaction_error = :rollback; raise "fail!";} - - @first.save! - assert_equal :commit, @first.last_after_transaction_error - - Topic.transaction do - @first.save! - raise ActiveRecord::Rollback - end - - assert_equal :rollback, @first.last_after_transaction_error - end -end diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 958a4e4f94..00f3b527d7 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -320,33 +320,6 @@ class TransactionTest < ActiveRecord::TestCase end end - def test_restore_active_record_state_for_all_records_in_a_transaction - topic_1 = Topic.new(:title => 'test_1') - topic_2 = Topic.new(:title => 'test_2') - Topic.transaction do - assert topic_1.save - assert topic_2.save - @first.save - @second.destroy - assert_equal false, topic_1.new_record? - assert_not_nil topic_1.id - assert_equal false, topic_2.new_record? - assert_not_nil topic_2.id - assert_equal false, @first.new_record? - assert_not_nil @first.id - assert_equal true, @second.destroyed? - raise ActiveRecord::Rollback - end - - assert_equal true, topic_1.new_record? - assert_nil topic_1.id - assert_equal true, topic_2.new_record? - assert_nil topic_2.id - assert_equal false, @first.new_record? - assert_not_nil @first.id - assert_equal false, @second.destroyed? - end - if current_adapter?(:PostgreSQLAdapter) && defined?(PGconn::PQTRANS_IDLE) def test_outside_transaction_works assert Topic.connection.outside_transaction? @@ -409,12 +382,6 @@ class TransactionTest < ActiveRecord::TestCase end private - def define_callback_method(callback_method) - define_method(callback_method) do - self.history << [callback_method, :method] - end - end - def add_exception_raising_after_save_callback_to_topic Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1 remove_method(:after_save_for_transaction) -- cgit v1.2.3 From ab2877cbe89e266ee986fc12e603abd93ac017ad Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Tue, 8 Jun 2010 15:07:51 -0400 Subject: Add parentheses to avoid warnings when running AR tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/test/cases/adapter_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 0152b7be2a..fc08c2178a 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -145,13 +145,13 @@ class AdapterTest < ActiveRecord::TestCase def test_add_limit_offset_should_sanitize_sql_injection_for_limit_without_comas sql_inject = "1 select * from schema" - assert_no_match /schema/, @connection.add_limit_offset!("", :limit=>sql_inject) - assert_no_match /schema/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7) + assert_no_match(/schema/, @connection.add_limit_offset!("", :limit=>sql_inject)) + assert_no_match(/schema/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)) end def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas sql_inject = "1, 7 procedure help()" - assert_no_match /procedure/, @connection.add_limit_offset!("", :limit=>sql_inject) - assert_no_match /procedure/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7) + assert_no_match(/procedure/, @connection.add_limit_offset!("", :limit=>sql_inject)) + assert_no_match(/procedure/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)) end end -- cgit v1.2.3