Skip to content

Commit

Permalink
Added tests for HABTM associations with counter_sql
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1102 state:committed]
  • Loading branch information
tekin authored and NZKoz committed Nov 7, 2008
1 parent 26978e3 commit 32a5cfc
Showing 1 changed file with 23 additions and 0 deletions.
Expand Up @@ -68,6 +68,16 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base
:foreign_key => "developer_id"
end

class DeveloperWithCounterSQL < ActiveRecord::Base
set_table_name 'developers'
has_and_belongs_to_many :projects,
:class_name => "DeveloperWithCounterSQL",
:join_table => "developers_projects",
:association_foreign_key => "project_id",
:foreign_key => "developer_id",
:counter_sql => 'SELECT COUNT(*) AS count_all FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE developers_projects.developer_id =#{id}'
end

class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
:parrots, :pirates, :treasures, :price_estimates, :tags, :taggings
Expand Down Expand Up @@ -739,6 +749,19 @@ def test_counting_on_habtm_association_and_not_array
assert_nothing_raised { david.projects.count(:all, :conditions => '1=1') }
end

def test_count
david = Developer.find(1)
assert_equal 2, david.projects.count
end

def test_count_with_counter_sql
developer = DeveloperWithCounterSQL.create(:name => 'tekin')
developer.project_ids = [projects(:active_record).id]
developer.save
developer.reload
assert_equal 1, developer.projects.count
end

uses_mocha 'mocking Post.transaction' do
def test_association_proxy_transaction_method_starts_transaction_in_association_class
Post.expects(:transaction)
Expand Down

0 comments on commit 32a5cfc

Please sign in to comment.