Skip to content

Commit

Permalink
Merge branch 'oracle_enhanced'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Aug 7, 2009
2 parents 010a0c9 + 5f0c425 commit 06afa48
Show file tree
Hide file tree
Showing 23 changed files with 335 additions and 113 deletions.
25 changes: 11 additions & 14 deletions activerecord/test/cases/adapter_test.rb
Expand Up @@ -112,23 +112,14 @@ def test_reset_table_with_non_integer_pk

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

def test_uniqueness_violations_are_translated_to_specific_exception
Expand All @@ -141,7 +132,13 @@ def test_uniqueness_violations_are_translated_to_specific_exception
def test_foreign_key_violations_are_translated_to_specific_exception
unless @connection.adapter_name == 'SQLite'
assert_raises(ActiveRecord::InvalidForeignKey) do
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
if @connection.prefetch_primary_key?
id_value = @connection.next_sequence_value(@connection.default_sequence_name("fk_test_has_fk", "id"))
@connection.execute "INSERT INTO fk_test_has_fk (id, fk_id) VALUES (#{id_value},0)"
else
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
end
end
end
end
Expand Down
Expand Up @@ -293,15 +293,17 @@ def test_assignment_before_child_saved_with_primary_key

def test_new_record_with_foreign_key_but_no_object
c = Client.new("firm_id" => 1)
assert_equal Firm.find(:first), c.firm_with_basic_id
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
end

def test_forgetting_the_load_when_foreign_key_enters_late
c = Client.new
assert_nil c.firm_with_basic_id

c.firm_id = 1
assert_equal Firm.find(:first), c.firm_with_basic_id
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
end

def test_field_name_same_as_foreign_key
Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -813,7 +813,12 @@ def test_preload_has_many_using_primary_key

def test_include_has_many_using_primary_key
expected = Firm.find(1).clients_using_primary_key.sort_by &:name
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
# Oracle adapter truncates alias to 30 characters
if current_adapter?(:OracleAdapter)
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies'[0,30]+'.name'
else
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
end
assert_no_queries do
assert_equal expected, firm.clients_using_primary_key
end
Expand Down
Expand Up @@ -284,12 +284,14 @@ def test_create_by_new_record
end

def test_creation_respects_hash_condition
post = categories(:general).post_with_conditions.build(:body => '')
# in Oracle '' is saved as null therefore need to save ' ' in not null column
post = categories(:general).post_with_conditions.build(:body => ' ')

assert post.save
assert_equal 'Yet Another Testing Title', post.title

another_post = categories(:general).post_with_conditions.create(:body => '')
# in Oracle '' is saved as null therefore need to save ' ' in not null column
another_post = categories(:general).post_with_conditions.create(:body => ' ')

assert !another_post.new_record?
assert_equal 'Yet Another Testing Title', another_post.title
Expand Down
80 changes: 42 additions & 38 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -24,28 +24,29 @@ def force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.each {|f| }
end

# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
def test_counting_with_counter_sql
assert_equal 2, Firm.find(:first).clients.count
assert_equal 2, Firm.find(:first, :order => "id").clients.count
end

def test_counting
assert_equal 2, Firm.find(:first).plain_clients.count
assert_equal 2, Firm.find(:first, :order => "id").plain_clients.count
end

def test_counting_with_empty_hash_conditions
assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => {})
assert_equal 2, Firm.find(:first, :order => "id").plain_clients.count(:conditions => {})
end

def test_counting_with_single_conditions
assert_equal 1, Firm.find(:first).plain_clients.count(:conditions => ['name=?', "Microsoft"])
assert_equal 1, Firm.find(:first, :order => "id").plain_clients.count(:conditions => ['name=?', "Microsoft"])
end

def test_counting_with_single_hash
assert_equal 1, Firm.find(:first).plain_clients.count(:conditions => {:name => "Microsoft"})
assert_equal 1, Firm.find(:first, :order => "id").plain_clients.count(:conditions => {:name => "Microsoft"})
end

def test_counting_with_column_name_and_hash
assert_equal 2, Firm.find(:first).plain_clients.count(:name)
assert_equal 2, Firm.find(:first, :order => "id").plain_clients.count(:name)
end

def test_counting_with_association_limit
Expand All @@ -55,12 +56,12 @@ def test_counting_with_association_limit
end

def test_finding
assert_equal 2, Firm.find(:first).clients.length
assert_equal 2, Firm.find(:first, :order => "id").clients.length
end

def test_find_with_blank_conditions
[[], {}, nil, ""].each do |blank|
assert_equal 2, Firm.find(:first).clients.find(:all, :conditions => blank).size
assert_equal 2, Firm.find(:first, :order => "id").clients.find(:all, :conditions => blank).size
end
end

Expand Down Expand Up @@ -115,52 +116,53 @@ def test_cant_save_has_many_readonly_association
end

def test_triple_equality
assert !(Array === Firm.find(:first).clients)
assert Firm.find(:first).clients === Array
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
assert !(Array === Firm.find(:first, :order => "id").clients)
assert Firm.find(:first, :order => "id").clients === Array
end

def test_finding_default_orders
assert_equal "Summit", Firm.find(:first).clients.first.name
assert_equal "Summit", Firm.find(:first, :order => "id").clients.first.name
end

def test_finding_with_different_class_name_and_order
assert_equal "Microsoft", Firm.find(:first).clients_sorted_desc.first.name
assert_equal "Microsoft", Firm.find(:first, :order => "id").clients_sorted_desc.first.name
end

def test_finding_with_foreign_key
assert_equal "Microsoft", Firm.find(:first).clients_of_firm.first.name
assert_equal "Microsoft", Firm.find(:first, :order => "id").clients_of_firm.first.name
end

def test_finding_with_condition
assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name
assert_equal "Microsoft", Firm.find(:first, :order => "id").clients_like_ms.first.name
end

def test_finding_with_condition_hash
assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name
assert_equal "Microsoft", Firm.find(:first, :order => "id").clients_like_ms_with_hash_conditions.first.name
end

def test_finding_using_primary_key
assert_equal "Summit", Firm.find(:first).clients_using_primary_key.first.name
assert_equal "Summit", Firm.find(:first, :order => "id").clients_using_primary_key.first.name
end

def test_finding_using_sql
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
first_client = firm.clients_using_sql.first
assert_not_nil first_client
assert_equal "Microsoft", first_client.name
assert_equal 1, firm.clients_using_sql.size
assert_equal 1, Firm.find(:first).clients_using_sql.size
assert_equal 1, Firm.find(:first, :order => "id").clients_using_sql.size
end

def test_counting_using_sql
assert_equal 1, Firm.find(:first).clients_using_counter_sql.size
assert Firm.find(:first).clients_using_counter_sql.any?
assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size
assert !Firm.find(:first).clients_using_zero_counter_sql.any?
assert_equal 1, Firm.find(:first, :order => "id").clients_using_counter_sql.size
assert Firm.find(:first, :order => "id").clients_using_counter_sql.any?
assert_equal 0, Firm.find(:first, :order => "id").clients_using_zero_counter_sql.size
assert !Firm.find(:first, :order => "id").clients_using_zero_counter_sql.any?
end

def test_counting_non_existant_items_using_sql
assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size
assert_equal 0, Firm.find(:first, :order => "id").no_clients_using_counter_sql.size
end

def test_counting_using_finder_sql
Expand All @@ -183,7 +185,7 @@ def test_belongs_to_sanity
end

def test_find_ids
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")

assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }

Expand All @@ -203,7 +205,7 @@ def test_find_ids
end

def test_find_string_ids_when_using_finder_sql
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")

client = firm.clients_using_finder_sql.find("2")
assert_kind_of Client, client
Expand All @@ -219,7 +221,7 @@ def test_find_string_ids_when_using_finder_sql
end

def test_find_all
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
end
Expand Down Expand Up @@ -264,24 +266,25 @@ def test_find_in_batches
end

def test_find_all_sanitized
firm = Firm.find(:first)
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
firm = Firm.find(:first, :order => "id")
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
end

def test_find_first
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
client2 = Client.find(2)
assert_equal firm.clients.first, firm.clients.find(:first)
assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
assert_equal firm.clients.first, firm.clients.find(:first, :order => "id")
assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'", :order => "id")
end

def test_find_first_sanitized
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
client2 = Client.find(2)
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'], :order => "id")
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }], :order => "id")
end

def test_find_in_collection
Expand Down Expand Up @@ -341,7 +344,7 @@ def test_regular_create_on_has_many_when_parent_is_new_raises

def test_create_with_bang_on_has_many_raises_when_record_not_saved
assert_raise(ActiveRecord::RecordInvalid) do
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
firm.plain_clients.create!
end
end
Expand Down Expand Up @@ -731,7 +734,8 @@ def test_dependence_for_associations_with_hash_condition
end

def test_destroy_dependent_when_deleted_from_association
firm = Firm.find(:first)
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
firm = Firm.find(:first, :order => "id")
assert_equal 2, firm.clients.size

client = firm.clients.first
Expand Down Expand Up @@ -798,7 +802,7 @@ def test_find_all_without_conditions
end

def test_replace_with_less
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
firm.clients = [companies(:first_client)]
assert firm.save, "Could not save firm"
firm.reload
Expand All @@ -812,7 +816,7 @@ def test_replace_with_less_and_dependent_nullify
end

def test_replace_with_new
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
firm.save
firm.reload
Expand Down Expand Up @@ -1104,7 +1108,7 @@ def test_respond_to_private_class_methods
end

def test_creating_using_primary_key
firm = Firm.find(:first)
firm = Firm.find(:first, :order => "id")
client = firm.clients_using_primary_key.create!(:name => 'test')
assert_equal firm.name, client.firm_name
end
Expand Down
10 changes: 7 additions & 3 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -14,7 +14,9 @@

class AssociationsJoinModelTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items, :books
fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items, :books,
# Reload edges table from fixtures as otherwise repeated test was failing
:edges

def test_has_many
assert authors(:david).categories.include?(categories(:general))
Expand Down Expand Up @@ -343,14 +345,16 @@ def test_has_many_polymorphic
end

def test_has_many_polymorphic_with_source_type
assert_equal posts(:welcome, :thinking), tags(:general).tagged_posts
# added sort by ID as otherwise Oracle select sometimes returned rows in different order
assert_equal posts(:welcome, :thinking).sort_by(&:id), tags(:general).tagged_posts.sort_by(&:id)
end

def test_eager_has_many_polymorphic_with_source_type
tag_with_include = Tag.find(tags(:general).id, :include => :tagged_posts)
desired = posts(:welcome, :thinking)
assert_no_queries do
assert_equal desired, tag_with_include.tagged_posts
# added sort by ID as otherwise test using JRuby was failing as array elements were in different order
assert_equal desired.sort_by(&:id), tag_with_include.tagged_posts.sort_by(&:id)
end
assert_equal 5, tag_with_include.taggings.length
end
Expand Down
14 changes: 12 additions & 2 deletions activerecord/test/cases/attribute_methods_test.rb
Expand Up @@ -75,13 +75,23 @@ def test_should_unserialize_attributes_for_frozen_records

def test_typecast_attribute_from_select_to_false
topic = Topic.create(:title => 'Budget')
topic = Topic.find(:first, :select => "topics.*, 1=2 as is_test")
# Oracle does not support boolean expressions in SELECT
if current_adapter?(:OracleAdapter)
topic = Topic.find(:first, :select => "topics.*, 0 as is_test")
else
topic = Topic.find(:first, :select => "topics.*, 1=2 as is_test")
end
assert !topic.is_test?
end

def test_typecast_attribute_from_select_to_true
topic = Topic.create(:title => 'Budget')
topic = Topic.find(:first, :select => "topics.*, 2=2 as is_test")
# Oracle does not support boolean expressions in SELECT
if current_adapter?(:OracleAdapter)
topic = Topic.find(:first, :select => "topics.*, 1 as is_test")
else
topic = Topic.find(:first, :select => "topics.*, 2=2 as is_test")
end
assert topic.is_test?
end

Expand Down

0 comments on commit 06afa48

Please sign in to comment.