Skip to content

Commit

Permalink
added :order option to find :first methods and associations as otherw…
Browse files Browse the repository at this point in the history
…ise Oracle tests were failing

Oracle stores '' string as NULL
Oracle cannot have identifiers larger than 30 characters
added missing fixtures to test setup method
  • Loading branch information
rsim committed Aug 6, 2009
1 parent 963570b commit 5666a3a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 48 deletions.
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
8 changes: 6 additions & 2 deletions activerecord/test/models/company.rb
Expand Up @@ -73,12 +73,16 @@ class Firm < Company
has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false
has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account'
has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account"
# added order by id as in fixtures there are two accounts for Rails Core
# Oracle tests were failing because of that as the second fixture was selected
has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account", :order => "id"
has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete
end

class DependentFirm < Company
has_one :account, :foreign_key => "firm_id", :dependent => :nullify
# added order by id as in fixtures there are two accounts for Rails Core
# Oracle tests were failing because of that as the second fixture was selected
has_one :account, :foreign_key => "firm_id", :dependent => :nullify, :order => "id"
has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify
end

Expand Down

0 comments on commit 5666a3a

Please sign in to comment.