3 require 'models/comment'
4 require 'models/author'
5 require 'models/category'
6 require 'models/categorization'
8 class InnerJoinAssociationTest
< ActiveRecord
::TestCase
9 fixtures
:authors, :posts, :comments, :categories, :categories_posts, :categorizations
11 def test_construct_finder_sql_creates_inner_joins
12 sql
= Author
.send(:construct_finder_sql, :joins => :posts)
13 assert_match
/INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql
16 def test_construct_finder_sql_cascades_inner_joins
17 sql
= Author
.send(:construct_finder_sql, :joins => {:posts => :comments})
18 assert_match
/INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql
19 assert_match
/INNER JOIN .?comments.? ON .?comments.?.post_id = posts.id/, sql
22 def test_construct_finder_sql_inner_joins_through_associations
23 sql
= Author
.send(:construct_finder_sql, :joins => :categorized_posts)
24 assert_match
/INNER JOIN .?categorizations.?.*INNER JOIN .?posts.?/, sql
27 def test_construct_finder_sql_applies_association_conditions
28 sql
= Author
.send(:construct_finder_sql, :joins => :categories_like_general, :conditions => "TERMINATING_MARKER")
29 assert_match
/INNER JOIN .?categories.? ON.*AND.*.?General.?.*TERMINATING_MARKER/, sql
32 def test_construct_finder_sql_unpacks_nested_joins
33 sql
= Author
.send(:construct_finder_sql, :joins => {:posts => [[:comments]]})
34 assert_no_match
/inner join.*inner join.*inner join/i
, sql
, "only two join clauses should be present"
35 assert_match
/INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql
36 assert_match
/INNER JOIN .?comments.? ON .?comments.?.post_id = .?posts.?.id/, sql
39 def test_construct_finder_sql_ignores_empty_joins_hash
40 sql
= Author
.send(:construct_finder_sql, :joins => {})
41 assert_no_match
/JOIN/i
, sql
44 def test_construct_finder_sql_ignores_empty_joins_array
45 sql
= Author
.send(:construct_finder_sql, :joins => [])
46 assert_no_match
/JOIN/i
, sql
49 def test_find_with_implicit_inner_joins_honors_readonly_without_select
50 authors
= Author
.find(:all, :joins => :posts)
51 assert
!authors
.empty
?, "expected authors to be non-empty"
52 assert authors
.all
? {|a
| a
.readonly
? }, "expected all authors to be readonly"
55 def test_find_with_implicit_inner_joins_honors_readonly_with_select
56 authors
= Author
.find(:all, :select => 'authors.*', :joins => :posts)
57 assert
!authors
.empty
?, "expected authors to be non-empty"
58 assert authors
.all
? {|a
| !a
.readonly
? }, "expected no authors to be readonly"
61 def test_find_with_implicit_inner_joins_honors_readonly_false
62 authors
= Author
.find(:all, :joins => :posts, :readonly => false)
63 assert
!authors
.empty
?, "expected authors to be non-empty"
64 assert authors
.all
? {|a
| !a
.readonly
? }, "expected no authors to be readonly"
67 def test_find_with_implicit_inner_joins_does_not_set_associations
68 authors
= Author
.find(:all, :select => 'authors.*', :joins => :posts)
69 assert
!authors
.empty
?, "expected authors to be non-empty"
70 assert authors
.all
? {|a
| !a
.send(:instance_variable_names).include?("@posts")}, "expected no authors to have the @posts association loaded"
73 def test_count_honors_implicit_inner_joins
74 real_count
= Author
.find(:all).sum
{|a
| a
.posts
.count
}
75 assert_equal real_count
, Author
.count(:joins => :posts), "plain inner join count should match the number of referenced posts records"
78 def test_calculate_honors_implicit_inner_joins
79 real_count
= Author
.find(:all).sum
{|a
| a
.posts
.count
}
80 assert_equal real_count
, Author
.calculate(:count, 'authors.id', :joins => :posts), "plain inner join count should match the number of referenced posts records"
83 def test_calculate_honors_implicit_inner_joins_and_distinct_and_conditions
84 real_count
= Author
.find(:all).select
{|a
| a
.posts
.any
? {|p
| p
.title
=~
/^Welcome/} }.length
85 authors_with_welcoming_post_titles
= Author
.calculate(:count, 'authors.id', :joins => :posts, :distinct => true, :conditions => "posts.title like 'Welcome%'")
86 assert_equal real_count
, authors_with_welcoming_post_titles
, "inner join and conditions should have only returned authors posting titles starting with 'Welcome'"