db2 2.5.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGES +181 -0
- data/LICENSE +18 -0
- data/MANIFEST +14 -0
- data/ParameterizedQueries README +39 -0
- data/README +282 -0
- data/ext/Makefile.nt32 +181 -0
- data/ext/extconf.rb +66 -0
- data/ext/ibm_db.c +11166 -0
- data/ext/ruby_ibm_db.h +236 -0
- data/ext/ruby_ibm_db_cli.c +738 -0
- data/ext/ruby_ibm_db_cli.h +431 -0
- data/init.rb +42 -0
- data/lib/IBM_DB.rb +2 -0
- data/lib/active_record/connection_adapters/ibm_db_adapter.rb +2558 -0
- data/lib/active_record/connection_adapters/ibm_db_pstmt.rb +1965 -0
- data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -0
- data/test/cases/adapter_test.rb +202 -0
- data/test/cases/associations/belongs_to_associations_test.rb +486 -0
- data/test/cases/associations/cascaded_eager_loading_test.rb +183 -0
- data/test/cases/associations/eager_test.rb +862 -0
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +917 -0
- data/test/cases/associations/has_many_through_associations_test.rb +461 -0
- data/test/cases/associations/join_model_test.rb +793 -0
- data/test/cases/attribute_methods_test.rb +621 -0
- data/test/cases/base_test.rb +1486 -0
- data/test/cases/calculations_test.rb +362 -0
- data/test/cases/finder_test.rb +1088 -0
- data/test/cases/fixtures_test.rb +684 -0
- data/test/cases/migration_test.rb +2014 -0
- data/test/cases/schema_dumper_test.rb +232 -0
- data/test/cases/validations/uniqueness_validation_test.rb +283 -0
- data/test/connections/native_ibm_db/connection.rb +42 -0
- data/test/ibm_db_test.rb +25 -0
- data/test/models/warehouse_thing.rb +5 -0
- data/test/schema/i5/ibm_db_specific_schema.rb +135 -0
- data/test/schema/ids/ibm_db_specific_schema.rb +138 -0
- data/test/schema/luw/ibm_db_specific_schema.rb +135 -0
- data/test/schema/schema.rb +647 -0
- data/test/schema/zOS/ibm_db_specific_schema.rb +206 -0
- metadata +105 -0
@@ -0,0 +1,183 @@
|
|
1
|
+
require "cases/helper"
|
2
|
+
require 'models/post'
|
3
|
+
require 'models/comment'
|
4
|
+
require 'models/author'
|
5
|
+
require 'models/categorization'
|
6
|
+
require 'models/category'
|
7
|
+
require 'models/company'
|
8
|
+
require 'models/topic'
|
9
|
+
require 'models/reply'
|
10
|
+
require 'models/person'
|
11
|
+
|
12
|
+
class CascadedEagerLoadingTest < ActiveRecord::TestCase
|
13
|
+
fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments,
|
14
|
+
:categorizations, :people, :categories
|
15
|
+
|
16
|
+
def test_eager_association_loading_with_cascaded_two_levels
|
17
|
+
authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
|
18
|
+
assert_equal 2, authors.size
|
19
|
+
assert_equal 5, authors[0].posts.size
|
20
|
+
assert_equal 1, authors[1].posts.size
|
21
|
+
assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_eager_association_loading_with_cascaded_two_levels_and_one_level
|
25
|
+
authors = Author.find(:all, :include=>[{:posts=>:comments}, :categorizations], :order=>"authors.id")
|
26
|
+
assert_equal 2, authors.size
|
27
|
+
assert_equal 5, authors[0].posts.size
|
28
|
+
assert_equal 1, authors[1].posts.size
|
29
|
+
assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
|
30
|
+
assert_equal 1, authors[0].categorizations.size
|
31
|
+
assert_equal 2, authors[1].categorizations.size
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_eager_association_loading_with_hmt_does_not_table_name_collide_when_joining_associations
|
35
|
+
assert_nothing_raised do
|
36
|
+
Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).all
|
37
|
+
end
|
38
|
+
authors = Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).all
|
39
|
+
assert_equal 1, assert_no_queries { authors.size }
|
40
|
+
assert_equal 9, assert_no_queries { authors[0].comments.size }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
|
44
|
+
assert_nothing_raised do
|
45
|
+
Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').all
|
46
|
+
end
|
47
|
+
assert_equal people(:michael), Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').first
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_cascaded_eager_association_loading_with_join_for_count
|
51
|
+
categories = Category.joins(:categorizations).includes([{:posts=>:comments}, :authors])
|
52
|
+
|
53
|
+
assert_nothing_raised do
|
54
|
+
assert_equal 2, categories.count
|
55
|
+
assert_equal 2, categories.all.uniq.size # Must uniq since instantiating with inner joins will get dupes
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_cascaded_eager_association_loading_with_duplicated_includes
|
60
|
+
categories = Category.includes(:categorizations).includes(:categorizations => :author).where("categorizations.id is not null")
|
61
|
+
assert_nothing_raised do
|
62
|
+
assert_equal 2, categories.count
|
63
|
+
assert_equal 2, categories.all.size
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_cascaded_eager_association_loading_with_twice_includes_edge_cases
|
68
|
+
categories = Category.includes(:categorizations => :author).includes(:categorizations => :post).where("posts.id is not null")
|
69
|
+
assert_nothing_raised do
|
70
|
+
assert_equal 2, categories.count
|
71
|
+
assert_equal 2, categories.all.size
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_eager_association_loading_with_join_for_count
|
76
|
+
authors = Author.joins(:special_posts).includes([:posts, :categorizations])
|
77
|
+
|
78
|
+
assert_nothing_raised { authors.count }
|
79
|
+
assert_queries(3) { authors.all }
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
|
83
|
+
authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
|
84
|
+
assert_equal 2, authors.size
|
85
|
+
assert_equal 5, authors[0].posts.size
|
86
|
+
assert_equal 1, authors[1].posts.size
|
87
|
+
assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
|
91
|
+
authors = Author.find(:all, :include=>{:posts=>[:comments, :author]}, :order=>"authors.id")
|
92
|
+
assert_equal 2, authors.size
|
93
|
+
assert_equal 5, authors[0].posts.size
|
94
|
+
assert_equal authors(:david).name, authors[0].name
|
95
|
+
assert_equal [authors(:david).name], authors[0].posts.collect{|post| post.author.name}.uniq
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_eager_association_loading_with_cascaded_two_levels_with_condition
|
99
|
+
authors = Author.find(:all, :include=>{:posts=>:comments}, :conditions=>"authors.id=1", :order=>"authors.id")
|
100
|
+
assert_equal 1, authors.size
|
101
|
+
assert_equal 5, authors[0].posts.size
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
|
105
|
+
firms = Firm.find(:all, :include=>{:account=>{:firm=>:account}}, :order=>"companies.id")
|
106
|
+
assert_equal 2, firms.size
|
107
|
+
assert_equal firms.first.account, firms.first.account.firm.account
|
108
|
+
assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
|
109
|
+
assert_equal companies(:first_firm).account.firm.account, assert_no_queries { firms.first.account.firm.account }
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_eager_association_loading_with_has_many_sti
|
113
|
+
topics = Topic.find(:all, :include => :replies, :order => 'topics.id')
|
114
|
+
first, second, = topics(:first).replies.size, topics(:second).replies.size
|
115
|
+
assert_no_queries do
|
116
|
+
assert_equal first, topics[0].replies.size
|
117
|
+
assert_equal second, topics[1].replies.size
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_eager_association_loading_with_has_many_sti_and_subclasses
|
122
|
+
silly = SillyReply.new(:title => "gaga", :content => "boo-boo", :parent_id => 1)
|
123
|
+
silly.parent_id = 1
|
124
|
+
assert silly.save
|
125
|
+
|
126
|
+
topics = Topic.find(:all, :include => :replies, :order => 'topics.id, replies_topics.id')
|
127
|
+
assert_no_queries do
|
128
|
+
assert_equal 2, topics[0].replies.size
|
129
|
+
assert_equal 0, topics[1].replies.size
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_eager_association_loading_with_belongs_to_sti
|
134
|
+
replies = Reply.find(:all, :include => :topic, :order => 'topics.id')
|
135
|
+
assert replies.include?(topics(:second))
|
136
|
+
assert !replies.include?(topics(:first))
|
137
|
+
assert_equal topics(:first), assert_no_queries { replies.first.topic }
|
138
|
+
end
|
139
|
+
|
140
|
+
unless current_adapter?(:IBM_DBAdapter)
|
141
|
+
def test_eager_association_loading_with_multiple_stis_and_order
|
142
|
+
author = Author.find(:first, :include => { :posts => [ :special_comments , :very_special_comment ] }, :order => 'authors.name, comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
|
143
|
+
assert_equal authors(:david), author
|
144
|
+
assert_no_queries do
|
145
|
+
author.posts.first.special_comments
|
146
|
+
author.posts.first.very_special_comment
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_eager_association_loading_of_stis_with_multiple_references
|
152
|
+
authors = Author.find(:all, :include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
|
153
|
+
assert_equal [authors(:david)], authors
|
154
|
+
assert_no_queries do
|
155
|
+
authors.first.posts.first.special_comments.first.post.special_comments
|
156
|
+
authors.first.posts.first.special_comments.first.post.very_special_comment
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_eager_association_loading_where_first_level_returns_nil
|
161
|
+
authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
|
162
|
+
assert_equal [authors(:mary), authors(:david)], authors
|
163
|
+
assert_no_queries do
|
164
|
+
authors[1].post_about_thinking.comments.first
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
require 'models/vertex'
|
170
|
+
require 'models/edge'
|
171
|
+
class CascadedEagerLoadingTest < ActiveRecord::TestCase
|
172
|
+
fixtures :edges, :vertices
|
173
|
+
|
174
|
+
def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
|
175
|
+
source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id')
|
176
|
+
assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
|
180
|
+
sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC')
|
181
|
+
assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,862 @@
|
|
1
|
+
require "cases/helper"
|
2
|
+
require 'models/post'
|
3
|
+
require 'models/tagging'
|
4
|
+
require 'models/tag'
|
5
|
+
require 'models/comment'
|
6
|
+
require 'models/author'
|
7
|
+
require 'models/category'
|
8
|
+
require 'models/company'
|
9
|
+
require 'models/person'
|
10
|
+
require 'models/reader'
|
11
|
+
require 'models/owner'
|
12
|
+
require 'models/pet'
|
13
|
+
require 'models/reference'
|
14
|
+
require 'models/job'
|
15
|
+
require 'models/subscriber'
|
16
|
+
require 'models/subscription'
|
17
|
+
require 'models/book'
|
18
|
+
require 'models/developer'
|
19
|
+
require 'models/project'
|
20
|
+
|
21
|
+
class EagerAssociationTest < ActiveRecord::TestCase
|
22
|
+
fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
|
23
|
+
:companies, :accounts, :tags, :taggings, :people, :readers,
|
24
|
+
:owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
|
25
|
+
:developers, :projects, :developers_projects
|
26
|
+
|
27
|
+
def test_loading_with_one_association
|
28
|
+
posts = Post.find(:all, :include => :comments)
|
29
|
+
post = posts.find { |p| p.id == 1 }
|
30
|
+
assert_equal 2, post.comments.size
|
31
|
+
assert post.comments.include?(comments(:greetings))
|
32
|
+
|
33
|
+
post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
|
34
|
+
assert_equal 2, post.comments.size
|
35
|
+
assert post.comments.include?(comments(:greetings))
|
36
|
+
|
37
|
+
posts = Post.find(:all, :include => :last_comment)
|
38
|
+
post = posts.find { |p| p.id == 1 }
|
39
|
+
assert_equal Post.find(1).last_comment, post.last_comment
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_loading_with_one_association_with_non_preload
|
43
|
+
posts = Post.find(:all, :include => :last_comment, :order => 'comments.id DESC')
|
44
|
+
post = posts.find { |p| p.id == 1 }
|
45
|
+
assert_equal Post.find(1).last_comment, post.last_comment
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_loading_conditions_with_or
|
49
|
+
posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
|
50
|
+
assert_nil posts.detect { |p| p.author_id != authors(:david).id },
|
51
|
+
"expected to find only david's posts"
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_with_ordering
|
55
|
+
list = Post.find(:all, :include => :comments, :order => "posts.id DESC")
|
56
|
+
[:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
|
57
|
+
:authorless, :thinking, :welcome
|
58
|
+
].each_with_index do |post, index|
|
59
|
+
assert_equal posts(post), list[index]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_with_two_tables_in_from_without_getting_double_quoted
|
64
|
+
posts = Post.select("posts.*").from("authors, posts").eager_load(:comments).where("posts.author_id = authors.id").order("posts.id").to_a
|
65
|
+
assert_equal 2, posts.first.comments.size
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_loading_with_multiple_associations
|
69
|
+
posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
|
70
|
+
assert_equal 2, posts.first.comments.size
|
71
|
+
assert_equal 2, posts.first.categories.size
|
72
|
+
assert posts.first.comments.include?(comments(:greetings))
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_duplicate_middle_objects
|
76
|
+
comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author]
|
77
|
+
assert_no_queries do
|
78
|
+
comments.each {|comment| comment.post.author.name}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_including_duplicate_objects_from_belongs_to
|
83
|
+
popular_post = Post.create!(:title => 'foo', :body => "I like cars!")
|
84
|
+
comment = popular_post.comments.create!(:body => "lol")
|
85
|
+
popular_post.readers.create!(:person => people(:michael))
|
86
|
+
popular_post.readers.create!(:person => people(:david))
|
87
|
+
|
88
|
+
readers = Reader.find(:all, :conditions => ["post_id = ?", popular_post.id],
|
89
|
+
:include => {:post => :comments})
|
90
|
+
readers.each do |reader|
|
91
|
+
assert_equal [comment], reader.post.comments
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_including_duplicate_objects_from_has_many
|
96
|
+
car_post = Post.create!(:title => 'foo', :body => "I like cars!")
|
97
|
+
car_post.categories << categories(:general)
|
98
|
+
car_post.categories << categories(:technology)
|
99
|
+
|
100
|
+
comment = car_post.comments.create!(:body => "hmm")
|
101
|
+
categories = Category.find(:all, :conditions => ["posts.id=?", car_post.id],
|
102
|
+
:include => {:posts => :comments})
|
103
|
+
categories.each do |category|
|
104
|
+
assert_equal [comment], category.posts[0].comments
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
|
109
|
+
author_id = authors(:david).id
|
110
|
+
author = assert_queries(3) { Author.find(author_id, :include => {:posts_with_comments => :comments}) } # find the author, then find the posts, then find the comments
|
111
|
+
author.posts_with_comments.each do |post_with_comments|
|
112
|
+
assert_equal post_with_comments.comments.length, post_with_comments.comments.count
|
113
|
+
assert_nil post_with_comments.comments.uniq!
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_only_once
|
118
|
+
author = authors(:david)
|
119
|
+
post = author.post_about_thinking_with_last_comment
|
120
|
+
last_comment = post.last_comment
|
121
|
+
author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
|
122
|
+
assert_no_queries do
|
123
|
+
assert_equal post, author.post_about_thinking_with_last_comment
|
124
|
+
assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
|
129
|
+
post = posts(:welcome)
|
130
|
+
author = post.author
|
131
|
+
author_address = author.author_address
|
132
|
+
post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
|
133
|
+
assert_no_queries do
|
134
|
+
assert_equal author, post.author_with_address
|
135
|
+
assert_equal author_address, post.author_with_address.author_address
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
|
140
|
+
post = posts(:welcome)
|
141
|
+
post.update_attributes!(:author => nil)
|
142
|
+
post = assert_queries(1) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the author or address
|
143
|
+
assert_no_queries do
|
144
|
+
assert_equal nil, post.author_with_address
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_loading_from_an_association
|
149
|
+
posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
|
150
|
+
assert_equal 2, posts.first.comments.size
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_loading_from_an_association_that_has_a_hash_of_conditions
|
154
|
+
assert_nothing_raised do
|
155
|
+
Author.find(:all, :include => :hello_posts_with_hash_conditions)
|
156
|
+
end
|
157
|
+
assert !Author.find(authors(:david).id, :include => :hello_posts_with_hash_conditions).hello_posts.empty?
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_loading_with_no_associations
|
161
|
+
assert_nil Post.find(posts(:authorless).id, :include => :author).author
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_nested_loading_with_no_associations
|
165
|
+
assert_nothing_raised do
|
166
|
+
Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_eager_association_loading_with_belongs_to_and_foreign_keys
|
171
|
+
pets = Pet.find(:all, :include => :owner)
|
172
|
+
assert_equal 3, pets.length
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_eager_association_loading_with_belongs_to
|
176
|
+
comments = Comment.find(:all, :include => :post)
|
177
|
+
assert_equal 10, comments.length
|
178
|
+
titles = comments.map { |c| c.post.title }
|
179
|
+
assert titles.include?(posts(:welcome).title)
|
180
|
+
assert titles.include?(posts(:sti_post_and_comments).title)
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_eager_association_loading_with_belongs_to_and_limit
|
184
|
+
comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
|
185
|
+
assert_equal 5, comments.length
|
186
|
+
assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
|
190
|
+
comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :order => 'comments.id')
|
191
|
+
assert_equal 3, comments.length
|
192
|
+
assert_equal [5,6,7], comments.collect { |c| c.id }
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_eager_association_loading_with_belongs_to_and_limit_and_offset
|
196
|
+
comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
|
197
|
+
assert_equal 3, comments.length
|
198
|
+
assert_equal [3,5,6], comments.collect { |c| c.id }
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
|
202
|
+
comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id')
|
203
|
+
assert_equal 3, comments.length
|
204
|
+
assert_equal [6,7,8], comments.collect { |c| c.id }
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
|
208
|
+
comments = Comment.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id')
|
209
|
+
assert_equal 3, comments.length
|
210
|
+
assert_equal [6,7,8], comments.collect { |c| c.id }
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
|
214
|
+
assert_nothing_raised do
|
215
|
+
Comment.find(:all, :include => :post, :conditions => ['posts.id = ?',4])
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_eager_association_loading_with_belongs_to_and_conditions_hash
|
220
|
+
comments = []
|
221
|
+
assert_nothing_raised do
|
222
|
+
comments = Comment.find(:all, :include => :post, :conditions => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id')
|
223
|
+
end
|
224
|
+
assert_equal 3, comments.length
|
225
|
+
assert_equal [5,6,7], comments.collect { |c| c.id }
|
226
|
+
assert_no_queries do
|
227
|
+
comments.first.post
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
|
232
|
+
quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
|
233
|
+
assert_nothing_raised do
|
234
|
+
Comment.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
|
239
|
+
assert_nothing_raised do
|
240
|
+
Comment.find(:all, :include => :post, :order => 'posts.id')
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
|
245
|
+
quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
|
246
|
+
assert_nothing_raised do
|
247
|
+
Comment.find(:all, :include => :post, :order => quoted_posts_id)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
|
252
|
+
posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
|
253
|
+
assert_equal 1, posts.length
|
254
|
+
assert_equal [1], posts.collect { |p| p.id }
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
|
258
|
+
posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
|
259
|
+
assert_equal 1, posts.length
|
260
|
+
assert_equal [2], posts.collect { |p| p.id }
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
|
264
|
+
author_favorite = AuthorFavorite.find(:first, :include => :favorite_author)
|
265
|
+
assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
|
266
|
+
end
|
267
|
+
|
268
|
+
def test_eager_load_belongs_to_quotes_table_and_column_names
|
269
|
+
job = Job.find jobs(:unicyclist).id, :include => :ideal_reference
|
270
|
+
references(:michael_unicyclist)
|
271
|
+
assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_eager_load_has_one_quotes_table_and_column_names
|
275
|
+
michael = Person.find(people(:michael), :include => :favourite_reference)
|
276
|
+
references(:michael_unicyclist)
|
277
|
+
assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_eager_load_has_many_quotes_table_and_column_names
|
281
|
+
michael = Person.find(people(:michael), :include => :references)
|
282
|
+
references(:michael_magician,:michael_unicyclist)
|
283
|
+
assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_eager_load_has_many_through_quotes_table_and_column_names
|
287
|
+
michael = Person.find(people(:michael), :include => :jobs)
|
288
|
+
jobs(:magician, :unicyclist)
|
289
|
+
assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_eager_load_has_many_with_string_keys
|
293
|
+
subscriptions = subscriptions(:webster_awdr, :webster_rfr)
|
294
|
+
subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions)
|
295
|
+
assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_eager_load_has_many_through_with_string_keys
|
299
|
+
books = books(:awdr, :rfr)
|
300
|
+
subscriber = Subscriber.find(subscribers(:second).id, :include => :books)
|
301
|
+
assert_equal books, subscriber.books.sort_by(&:id)
|
302
|
+
end
|
303
|
+
|
304
|
+
def test_eager_load_belongs_to_with_string_keys
|
305
|
+
subscriber = subscribers(:second)
|
306
|
+
subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber)
|
307
|
+
assert_equal subscriber, subscription.subscriber
|
308
|
+
end
|
309
|
+
|
310
|
+
unless current_adapter?(:IBM_DBAdapter)
|
311
|
+
def test_eager_association_loading_with_explicit_join
|
312
|
+
posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id')
|
313
|
+
assert_equal 1, posts.length
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_eager_with_has_many_through
|
318
|
+
posts_with_comments = people(:michael).posts.find(:all, :include => :comments, :order => 'posts.id')
|
319
|
+
posts_with_author = people(:michael).posts.find(:all, :include => :author, :order => 'posts.id')
|
320
|
+
posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ], :order => 'posts.id')
|
321
|
+
assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
|
322
|
+
assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
|
323
|
+
assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_eager_with_has_many_through_a_belongs_to_association
|
327
|
+
author = authors(:mary)
|
328
|
+
post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
|
329
|
+
author.author_favorites.create(:favorite_author_id => 1)
|
330
|
+
author.author_favorites.create(:favorite_author_id => 2)
|
331
|
+
posts_with_author_favorites = author.posts.find(:all, :include => :author_favorites)
|
332
|
+
assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_eager_with_has_many_through_an_sti_join_model
|
336
|
+
author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
|
337
|
+
assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
|
341
|
+
author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
|
342
|
+
assert_equal [], author.special_nonexistant_post_comments
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_eager_with_has_many_through_join_model_with_conditions
|
346
|
+
assert_equal Author.find(:first, :include => :hello_post_comments,
|
347
|
+
:order => 'authors.id').hello_post_comments.sort_by(&:id),
|
348
|
+
Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
|
349
|
+
end
|
350
|
+
|
351
|
+
def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
|
352
|
+
assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_eager_with_has_many_through_join_model_with_include
|
356
|
+
author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
|
357
|
+
assert_no_queries do
|
358
|
+
author_comments.first.post.title
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
def test_eager_with_has_many_through_association_with_order
|
363
|
+
author_comments = Author.find(authors(:david).id).comments_desc
|
364
|
+
eager_author_comments = Author.find(authors(:david).id, :include => :comments_desc).comments_desc
|
365
|
+
assert_equal eager_author_comments, author_comments
|
366
|
+
end
|
367
|
+
|
368
|
+
def test_eager_with_has_many_through_with_conditions_join_model_with_include
|
369
|
+
post_tags = Post.find(posts(:welcome).id).misc_tags
|
370
|
+
eager_post_tags = Post.find(1, :include => :misc_tags).misc_tags
|
371
|
+
assert_equal post_tags, eager_post_tags
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_eager_with_has_many_and_limit
|
375
|
+
posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
|
376
|
+
assert_equal 2, posts.size
|
377
|
+
assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
|
378
|
+
end
|
379
|
+
|
380
|
+
def test_eager_with_has_many_and_limit_and_conditions
|
381
|
+
if current_adapter?(:OpenBaseAdapter)
|
382
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id")
|
383
|
+
else
|
384
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
|
385
|
+
end
|
386
|
+
assert_equal 2, posts.size
|
387
|
+
assert_equal [4,5], posts.collect { |p| p.id }
|
388
|
+
end
|
389
|
+
|
390
|
+
def test_eager_with_has_many_and_limit_and_conditions_array
|
391
|
+
if current_adapter?(:OpenBaseAdapter)
|
392
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id")
|
393
|
+
else
|
394
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
|
395
|
+
end
|
396
|
+
assert_equal 2, posts.size
|
397
|
+
assert_equal [4,5], posts.collect { |p| p.id }
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
|
401
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
|
402
|
+
assert_equal 2, posts.size
|
403
|
+
|
404
|
+
count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
|
405
|
+
assert_equal count, posts.size
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_eager_with_has_many_and_limit_and_high_offset
|
409
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
|
410
|
+
assert_equal 0, posts.size
|
411
|
+
end
|
412
|
+
|
413
|
+
def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
|
414
|
+
assert_queries(1) do
|
415
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
|
416
|
+
:conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ])
|
417
|
+
assert_equal 0, posts.size
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
|
422
|
+
assert_queries(1) do
|
423
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
|
424
|
+
:conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' })
|
425
|
+
assert_equal 0, posts.size
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
def test_count_eager_with_has_many_and_limit_and_high_offset
|
430
|
+
posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
|
431
|
+
assert_equal 0, posts
|
432
|
+
end
|
433
|
+
|
434
|
+
def test_eager_with_has_many_and_limit_with_no_results
|
435
|
+
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
|
436
|
+
assert_equal 0, posts.size
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
|
440
|
+
author = authors(:david)
|
441
|
+
author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
|
442
|
+
assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
|
443
|
+
end
|
444
|
+
|
445
|
+
def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
|
446
|
+
person = people(:michael)
|
447
|
+
person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
|
448
|
+
assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
|
449
|
+
end
|
450
|
+
|
451
|
+
def test_eager_with_has_and_belongs_to_many_and_limit
|
452
|
+
posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
|
453
|
+
assert_equal 3, posts.size
|
454
|
+
assert_equal 2, posts[0].categories.size
|
455
|
+
assert_equal 1, posts[1].categories.size
|
456
|
+
assert_equal 0, posts[2].categories.size
|
457
|
+
assert posts[0].categories.include?(categories(:technology))
|
458
|
+
assert posts[1].categories.include?(categories(:general))
|
459
|
+
end
|
460
|
+
|
461
|
+
def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
|
462
|
+
posts = authors(:david).posts.find(:all,
|
463
|
+
:include => :comments,
|
464
|
+
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
|
465
|
+
:limit => 2
|
466
|
+
)
|
467
|
+
assert_equal 2, posts.size
|
468
|
+
|
469
|
+
count = Post.count(
|
470
|
+
:include => [ :comments, :author ],
|
471
|
+
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
|
472
|
+
:limit => 2
|
473
|
+
)
|
474
|
+
assert_equal count, posts.size
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
|
478
|
+
posts = nil
|
479
|
+
Post.send(:with_scope, :find => {
|
480
|
+
:include => :comments,
|
481
|
+
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
|
482
|
+
}) do
|
483
|
+
posts = authors(:david).posts.find(:all, :limit => 2)
|
484
|
+
assert_equal 2, posts.size
|
485
|
+
end
|
486
|
+
|
487
|
+
Post.send(:with_scope, :find => {
|
488
|
+
:include => [ :comments, :author ],
|
489
|
+
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
|
490
|
+
}) do
|
491
|
+
count = Post.count(:limit => 2)
|
492
|
+
assert_equal count, posts.size
|
493
|
+
end
|
494
|
+
end
|
495
|
+
|
496
|
+
def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
|
497
|
+
Post.send(:with_scope, :find => { :conditions => "1=1" }) do
|
498
|
+
posts = authors(:david).posts.find(:all,
|
499
|
+
:include => :comments,
|
500
|
+
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
|
501
|
+
:limit => 2
|
502
|
+
)
|
503
|
+
assert_equal 2, posts.size
|
504
|
+
|
505
|
+
count = Post.count(
|
506
|
+
:include => [ :comments, :author ],
|
507
|
+
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
|
508
|
+
:limit => 2
|
509
|
+
)
|
510
|
+
assert_equal count, posts.size
|
511
|
+
end
|
512
|
+
end
|
513
|
+
|
514
|
+
def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
|
515
|
+
posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
|
516
|
+
posts_with_scoped_order = Post.send(:with_scope, :find => {:order => 'posts.id DESC'}) do
|
517
|
+
Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
|
518
|
+
end
|
519
|
+
assert_equal posts_with_explicit_order, posts_with_scoped_order
|
520
|
+
end
|
521
|
+
|
522
|
+
def test_eager_association_loading_with_habtm
|
523
|
+
posts = Post.find(:all, :include => :categories, :order => "posts.id")
|
524
|
+
assert_equal 2, posts[0].categories.size
|
525
|
+
assert_equal 1, posts[1].categories.size
|
526
|
+
assert_equal 0, posts[2].categories.size
|
527
|
+
assert posts[0].categories.include?(categories(:technology))
|
528
|
+
assert posts[1].categories.include?(categories(:general))
|
529
|
+
end
|
530
|
+
|
531
|
+
def test_eager_with_inheritance
|
532
|
+
posts = SpecialPost.find(:all, :include => [ :comments ])
|
533
|
+
end
|
534
|
+
|
535
|
+
def test_eager_has_one_with_association_inheritance
|
536
|
+
post = Post.find(4, :include => [ :very_special_comment ])
|
537
|
+
assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
|
538
|
+
end
|
539
|
+
|
540
|
+
def test_eager_has_many_with_association_inheritance
|
541
|
+
post = Post.find(4, :include => [ :special_comments ])
|
542
|
+
post.special_comments.each do |special_comment|
|
543
|
+
assert_equal "SpecialComment", special_comment.class.to_s
|
544
|
+
end
|
545
|
+
end
|
546
|
+
|
547
|
+
def test_eager_habtm_with_association_inheritance
|
548
|
+
post = Post.find(6, :include => [ :special_categories ])
|
549
|
+
assert_equal 1, post.special_categories.size
|
550
|
+
post.special_categories.each do |special_category|
|
551
|
+
assert_equal "SpecialCategory", special_category.class.to_s
|
552
|
+
end
|
553
|
+
end
|
554
|
+
|
555
|
+
def test_eager_with_has_one_dependent_does_not_destroy_dependent
|
556
|
+
assert_not_nil companies(:first_firm).account
|
557
|
+
f = Firm.find(:first, :include => :account,
|
558
|
+
:conditions => ["companies.name = ?", "37signals"])
|
559
|
+
assert_not_nil f.account
|
560
|
+
assert_equal companies(:first_firm, :reload).account, f.account
|
561
|
+
end
|
562
|
+
|
563
|
+
def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
|
564
|
+
author = authors(:david)
|
565
|
+
posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
|
566
|
+
assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
|
567
|
+
assert_equal posts_with_no_comments, author.posts_with_no_comments
|
568
|
+
end
|
569
|
+
|
570
|
+
def test_eager_with_invalid_association_reference
|
571
|
+
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
|
572
|
+
post = Post.find(6, :include=> :monkeys )
|
573
|
+
}
|
574
|
+
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
|
575
|
+
post = Post.find(6, :include=>[ :monkeys ])
|
576
|
+
}
|
577
|
+
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
|
578
|
+
post = Post.find(6, :include=>[ 'monkeys' ])
|
579
|
+
}
|
580
|
+
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
|
581
|
+
post = Post.find(6, :include=>[ :monkeys, :elephants ])
|
582
|
+
}
|
583
|
+
end
|
584
|
+
|
585
|
+
def find_all_ordered(className, include=nil)
|
586
|
+
className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
|
587
|
+
end
|
588
|
+
|
589
|
+
unless current_adapter?(:IBM_DBAdapter)
|
590
|
+
def test_limited_eager_with_order
|
591
|
+
assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
|
592
|
+
assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC', :limit => 2, :offset => 1)
|
593
|
+
end
|
594
|
+
|
595
|
+
def test_limited_eager_with_multiple_order_columns
|
596
|
+
assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title), posts.id', :limit => 2, :offset => 1)
|
597
|
+
assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC, posts.id', :limit => 2, :offset => 1)
|
598
|
+
end
|
599
|
+
end
|
600
|
+
|
601
|
+
def test_limited_eager_with_numeric_in_association
|
602
|
+
assert_equal people(:david, :susan), Person.find(:all, :include => [:readers, :primary_contact, :number1_fan], :conditions => "number1_fans_people.first_name like 'M%'", :order => 'people.id', :limit => 2, :offset => 0)
|
603
|
+
end
|
604
|
+
|
605
|
+
def test_preload_with_interpolation
|
606
|
+
assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
|
607
|
+
end
|
608
|
+
|
609
|
+
def test_polymorphic_type_condition
|
610
|
+
post = Post.find(posts(:thinking).id, :include => :taggings)
|
611
|
+
assert post.taggings.include?(taggings(:thinking_general))
|
612
|
+
post = SpecialPost.find(posts(:thinking).id, :include => :taggings)
|
613
|
+
assert post.taggings.include?(taggings(:thinking_general))
|
614
|
+
end
|
615
|
+
|
616
|
+
def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
|
617
|
+
# Eager includes of has many and habtm associations aren't necessarily sorted in the same way
|
618
|
+
def assert_equal_after_sort(item1, item2, item3 = nil)
|
619
|
+
assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
|
620
|
+
assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
|
621
|
+
end
|
622
|
+
# Test regular association, association with conditions, association with
|
623
|
+
# STI, and association with conditions assured not to be true
|
624
|
+
post_types = [:posts, :other_posts, :special_posts]
|
625
|
+
# test both has_many and has_and_belongs_to_many
|
626
|
+
[Author, Category].each do |className|
|
627
|
+
d1 = find_all_ordered(className)
|
628
|
+
# test including all post types at once
|
629
|
+
d2 = find_all_ordered(className, post_types)
|
630
|
+
d1.each_index do |i|
|
631
|
+
assert_equal(d1[i], d2[i])
|
632
|
+
assert_equal_after_sort(d1[i].posts, d2[i].posts)
|
633
|
+
post_types[1..-1].each do |post_type|
|
634
|
+
# test including post_types together
|
635
|
+
d3 = find_all_ordered(className, [:posts, post_type])
|
636
|
+
assert_equal(d1[i], d3[i])
|
637
|
+
assert_equal_after_sort(d1[i].posts, d3[i].posts)
|
638
|
+
assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
|
639
|
+
end
|
640
|
+
end
|
641
|
+
end
|
642
|
+
end
|
643
|
+
|
644
|
+
def test_eager_with_multiple_associations_with_same_table_has_one
|
645
|
+
d1 = find_all_ordered(Firm)
|
646
|
+
d2 = find_all_ordered(Firm, :account)
|
647
|
+
d1.each_index do |i|
|
648
|
+
assert_equal(d1[i], d2[i])
|
649
|
+
assert_equal(d1[i].account, d2[i].account)
|
650
|
+
end
|
651
|
+
end
|
652
|
+
|
653
|
+
def test_eager_with_multiple_associations_with_same_table_belongs_to
|
654
|
+
firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
|
655
|
+
d1 = find_all_ordered(Client)
|
656
|
+
d2 = find_all_ordered(Client, firm_types)
|
657
|
+
d1.each_index do |i|
|
658
|
+
assert_equal(d1[i], d2[i])
|
659
|
+
firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
|
660
|
+
end
|
661
|
+
end
|
662
|
+
def test_eager_with_valid_association_as_string_not_symbol
|
663
|
+
assert_nothing_raised { Post.find(:all, :include => 'comments') }
|
664
|
+
end
|
665
|
+
|
666
|
+
def test_eager_with_floating_point_numbers
|
667
|
+
assert_queries(2) do
|
668
|
+
# Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
|
669
|
+
Comment.find :all, :conditions => "123.456 = 123.456", :include => :post
|
670
|
+
end
|
671
|
+
end
|
672
|
+
|
673
|
+
def test_preconfigured_includes_with_belongs_to
|
674
|
+
author = posts(:welcome).author_with_posts
|
675
|
+
assert_no_queries {assert_equal 5, author.posts.size}
|
676
|
+
end
|
677
|
+
|
678
|
+
def test_preconfigured_includes_with_has_one
|
679
|
+
comment = posts(:sti_comments).very_special_comment_with_post
|
680
|
+
assert_no_queries {assert_equal posts(:sti_comments), comment.post}
|
681
|
+
end
|
682
|
+
|
683
|
+
def test_preconfigured_includes_with_has_many
|
684
|
+
posts = authors(:david).posts_with_comments
|
685
|
+
one = posts.detect { |p| p.id == 1 }
|
686
|
+
assert_no_queries do
|
687
|
+
assert_equal 5, posts.size
|
688
|
+
assert_equal 2, one.comments.size
|
689
|
+
end
|
690
|
+
end
|
691
|
+
|
692
|
+
def test_preconfigured_includes_with_habtm
|
693
|
+
posts = authors(:david).posts_with_categories
|
694
|
+
one = posts.detect { |p| p.id == 1 }
|
695
|
+
assert_no_queries do
|
696
|
+
assert_equal 5, posts.size
|
697
|
+
assert_equal 2, one.categories.size
|
698
|
+
end
|
699
|
+
end
|
700
|
+
|
701
|
+
def test_preconfigured_includes_with_has_many_and_habtm
|
702
|
+
posts = authors(:david).posts_with_comments_and_categories
|
703
|
+
one = posts.detect { |p| p.id == 1 }
|
704
|
+
assert_no_queries do
|
705
|
+
assert_equal 5, posts.size
|
706
|
+
assert_equal 2, one.comments.size
|
707
|
+
assert_equal 2, one.categories.size
|
708
|
+
end
|
709
|
+
end
|
710
|
+
|
711
|
+
def test_count_with_include
|
712
|
+
if current_adapter?(:SybaseAdapter)
|
713
|
+
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
|
714
|
+
elsif current_adapter?(:OpenBaseAdapter)
|
715
|
+
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
|
716
|
+
else
|
717
|
+
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
|
718
|
+
end
|
719
|
+
end
|
720
|
+
|
721
|
+
def test_load_with_sti_sharing_association
|
722
|
+
assert_queries(2) do #should not do 1 query per subclass
|
723
|
+
Comment.find :all, :include => :post
|
724
|
+
end
|
725
|
+
end
|
726
|
+
|
727
|
+
def test_conditions_on_join_table_with_include_and_limit
|
728
|
+
assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size
|
729
|
+
end
|
730
|
+
|
731
|
+
unless current_adapter?(:IBM_DBAdapter) #refer db2 ? SQL0214N
|
732
|
+
def test_order_on_join_table_with_include_and_limit
|
733
|
+
assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
|
734
|
+
end
|
735
|
+
end
|
736
|
+
|
737
|
+
def test_eager_loading_with_order_on_joined_table_preloads
|
738
|
+
posts = assert_queries(2) do
|
739
|
+
Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
|
740
|
+
end
|
741
|
+
assert_equal posts(:eager_other), posts[0]
|
742
|
+
assert_equal authors(:mary), assert_no_queries { posts[0].author}
|
743
|
+
end
|
744
|
+
|
745
|
+
def test_eager_loading_with_conditions_on_joined_table_preloads
|
746
|
+
posts = assert_queries(2) do
|
747
|
+
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
|
748
|
+
end
|
749
|
+
assert_equal [posts(:welcome)], posts
|
750
|
+
assert_equal authors(:david), assert_no_queries { posts[0].author}
|
751
|
+
|
752
|
+
posts = assert_queries(2) do
|
753
|
+
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
|
754
|
+
end
|
755
|
+
assert_equal [posts(:welcome)], posts
|
756
|
+
assert_equal authors(:david), assert_no_queries { posts[0].author}
|
757
|
+
|
758
|
+
posts = assert_queries(2) do
|
759
|
+
Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
|
760
|
+
end
|
761
|
+
assert_equal posts(:welcome, :thinking), posts
|
762
|
+
|
763
|
+
posts = assert_queries(2) do
|
764
|
+
Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
|
765
|
+
end
|
766
|
+
assert_equal posts(:welcome, :thinking), posts
|
767
|
+
|
768
|
+
end
|
769
|
+
|
770
|
+
def test_eager_loading_with_conditions_on_string_joined_table_preloads
|
771
|
+
posts = assert_queries(2) do
|
772
|
+
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
|
773
|
+
end
|
774
|
+
assert_equal [posts(:welcome)], posts
|
775
|
+
assert_equal authors(:david), assert_no_queries { posts[0].author}
|
776
|
+
|
777
|
+
posts = assert_queries(2) do
|
778
|
+
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
|
779
|
+
end
|
780
|
+
assert_equal [posts(:welcome)], posts
|
781
|
+
assert_equal authors(:david), assert_no_queries { posts[0].author}
|
782
|
+
|
783
|
+
end
|
784
|
+
|
785
|
+
def test_eager_loading_with_select_on_joined_table_preloads
|
786
|
+
posts = assert_queries(2) do
|
787
|
+
Post.find(:all, :select => 'posts.*, authors.name as author_name', :include => :comments, :joins => :author, :order => 'posts.id')
|
788
|
+
end
|
789
|
+
assert_equal 'David', posts[0].author_name
|
790
|
+
assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
|
791
|
+
end
|
792
|
+
|
793
|
+
def test_eager_loading_with_conditions_on_join_model_preloads
|
794
|
+
authors = assert_queries(2) do
|
795
|
+
Author.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'")
|
796
|
+
end
|
797
|
+
assert_equal authors(:david), authors[0]
|
798
|
+
assert_equal author_addresses(:david_address), authors[0].author_address
|
799
|
+
end
|
800
|
+
|
801
|
+
def test_preload_belongs_to_uses_exclusive_scope
|
802
|
+
people = Person.males.find(:all, :include => :primary_contact)
|
803
|
+
assert_not_equal people.length, 0
|
804
|
+
people.each do |person|
|
805
|
+
assert_no_queries {assert_not_nil person.primary_contact}
|
806
|
+
assert_equal Person.find(person.id).primary_contact, person.primary_contact
|
807
|
+
end
|
808
|
+
end
|
809
|
+
|
810
|
+
def test_preload_has_many_uses_exclusive_scope
|
811
|
+
people = Person.males.find :all, :include => :agents
|
812
|
+
people.each do |person|
|
813
|
+
assert_equal Person.find(person.id).agents, person.agents
|
814
|
+
end
|
815
|
+
end
|
816
|
+
|
817
|
+
def test_preload_has_many_using_primary_key
|
818
|
+
expected = Firm.find(:first).clients_using_primary_key.to_a
|
819
|
+
firm = Firm.find :first, :include => :clients_using_primary_key
|
820
|
+
assert_no_queries do
|
821
|
+
assert_equal expected, firm.clients_using_primary_key
|
822
|
+
end
|
823
|
+
end
|
824
|
+
|
825
|
+
def test_include_has_many_using_primary_key
|
826
|
+
expected = Firm.find(1).clients_using_primary_key.sort_by(&:name)
|
827
|
+
# Oracle adapter truncates alias to 30 characters
|
828
|
+
if current_adapter?(:OracleAdapter)
|
829
|
+
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies'[0,30]+'.name'
|
830
|
+
else
|
831
|
+
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
|
832
|
+
end
|
833
|
+
assert_no_queries do
|
834
|
+
assert_equal expected, firm.clients_using_primary_key
|
835
|
+
end
|
836
|
+
end
|
837
|
+
|
838
|
+
def test_preload_has_one_using_primary_key
|
839
|
+
expected = Firm.find(:first).account_using_primary_key
|
840
|
+
firm = Firm.find :first, :include => :account_using_primary_key
|
841
|
+
assert_no_queries do
|
842
|
+
assert_equal expected, firm.account_using_primary_key
|
843
|
+
end
|
844
|
+
end
|
845
|
+
|
846
|
+
unless current_adapter?(:IBM_DBAdapter)
|
847
|
+
def test_include_has_one_using_primary_key
|
848
|
+
expected = Firm.find(1).account_using_primary_key
|
849
|
+
firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
|
850
|
+
assert_no_queries do
|
851
|
+
assert_equal expected, firm.account_using_primary_key
|
852
|
+
end
|
853
|
+
end
|
854
|
+
end
|
855
|
+
|
856
|
+
def test_preloading_empty_polymorphic_parent
|
857
|
+
t = Tagging.create!(:taggable_type => 'Post', :taggable_id => Post.maximum(:id) + 1, :tag => tags(:general))
|
858
|
+
|
859
|
+
assert_queries(2) { @tagging = Tagging.preload(:taggable).find(t.id) }
|
860
|
+
assert_no_queries { assert ! @tagging.taggable }
|
861
|
+
end
|
862
|
+
end
|