PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/rails/activerecord/test/associations/eager_test.rb

http://monkeycharger.googlecode.com/
Ruby | 393 lines | 376 code | 12 blank | 5 comment | 7 complexity | 9a596d43aff8af4ea43b0a9aa458cca7 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'fixtures/post'
  3. require 'fixtures/comment'
  4. require 'fixtures/author'
  5. require 'fixtures/category'
  6. require 'fixtures/company'
  7. require 'fixtures/person'
  8. require 'fixtures/reader'
  9. class EagerAssociationTest < Test::Unit::TestCase
  10. fixtures :posts, :comments, :authors, :categories, :categories_posts,
  11. :companies, :accounts, :tags, :people, :readers
  12. def test_loading_with_one_association
  13. posts = Post.find(:all, :include => :comments)
  14. post = posts.find { |p| p.id == 1 }
  15. assert_equal 2, post.comments.size
  16. assert post.comments.include?(comments(:greetings))
  17. post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
  18. assert_equal 2, post.comments.size
  19. assert post.comments.include?(comments(:greetings))
  20. end
  21. def test_loading_conditions_with_or
  22. posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
  23. assert_nil posts.detect { |p| p.author_id != authors(:david).id },
  24. "expected to find only david's posts"
  25. end
  26. def test_with_ordering
  27. list = Post.find(:all, :include => :comments, :order => "posts.id DESC")
  28. [:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
  29. :authorless, :thinking, :welcome
  30. ].each_with_index do |post, index|
  31. assert_equal posts(post), list[index]
  32. end
  33. end
  34. def test_loading_with_multiple_associations
  35. posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
  36. assert_equal 2, posts.first.comments.size
  37. assert_equal 2, posts.first.categories.size
  38. assert posts.first.comments.include?(comments(:greetings))
  39. end
  40. def test_loading_from_an_association
  41. posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
  42. assert_equal 2, posts.first.comments.size
  43. end
  44. def test_loading_with_no_associations
  45. assert_nil Post.find(posts(:authorless).id, :include => :author).author
  46. end
  47. def test_eager_association_loading_with_belongs_to
  48. comments = Comment.find(:all, :include => :post)
  49. assert_equal 10, comments.length
  50. titles = comments.map { |c| c.post.title }
  51. assert titles.include?(posts(:welcome).title)
  52. assert titles.include?(posts(:sti_post_and_comments).title)
  53. end
  54. def test_eager_association_loading_with_belongs_to_and_limit
  55. comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
  56. assert_equal 5, comments.length
  57. assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
  58. end
  59. def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
  60. comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :order => 'comments.id')
  61. assert_equal 3, comments.length
  62. assert_equal [5,6,7], comments.collect { |c| c.id }
  63. end
  64. def test_eager_association_loading_with_belongs_to_and_limit_and_offset
  65. comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
  66. assert_equal 3, comments.length
  67. assert_equal [3,5,6], comments.collect { |c| c.id }
  68. end
  69. def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
  70. comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id')
  71. assert_equal 3, comments.length
  72. assert_equal [6,7,8], comments.collect { |c| c.id }
  73. end
  74. def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
  75. comments = Comment.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id')
  76. assert_equal 3, comments.length
  77. assert_equal [6,7,8], comments.collect { |c| c.id }
  78. end
  79. def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
  80. posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
  81. assert_equal 1, posts.length
  82. assert_equal [1], posts.collect { |p| p.id }
  83. end
  84. def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
  85. posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
  86. assert_equal 1, posts.length
  87. assert_equal [2], posts.collect { |p| p.id }
  88. end
  89. def test_eager_with_has_many_through
  90. posts_with_comments = people(:michael).posts.find(:all, :include => :comments)
  91. posts_with_author = people(:michael).posts.find(:all, :include => :author )
  92. posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ])
  93. assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
  94. assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
  95. assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
  96. end
  97. def test_eager_with_has_many_through_an_sti_join_model
  98. author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
  99. assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
  100. end
  101. def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
  102. author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
  103. assert_equal [], author.special_nonexistant_post_comments
  104. end
  105. def test_eager_with_has_many_through_join_model_with_conditions
  106. assert_equal Author.find(:first, :include => :hello_post_comments,
  107. :order => 'authors.id').hello_post_comments.sort_by(&:id),
  108. Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
  109. end
  110. def test_eager_with_has_many_and_limit
  111. posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
  112. assert_equal 2, posts.size
  113. assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
  114. end
  115. def test_eager_with_has_many_and_limit_and_conditions
  116. posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
  117. assert_equal 2, posts.size
  118. assert_equal [4,5], posts.collect { |p| p.id }
  119. end
  120. def test_eager_with_has_many_and_limit_and_conditions_array
  121. posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
  122. assert_equal 2, posts.size
  123. assert_equal [4,5], posts.collect { |p| p.id }
  124. end
  125. def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
  126. posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
  127. assert_equal 2, posts.size
  128. count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
  129. assert_equal count, posts.size
  130. end
  131. def test_eager_with_has_many_and_limit_ond_high_offset
  132. posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
  133. assert_equal 0, posts.size
  134. end
  135. def test_count_eager_with_has_many_and_limit_ond_high_offset
  136. posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
  137. assert_equal 0, posts
  138. end
  139. def test_eager_with_has_many_and_limit_with_no_results
  140. posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
  141. assert_equal 0, posts.size
  142. end
  143. def test_eager_with_has_and_belongs_to_many_and_limit
  144. posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
  145. assert_equal 3, posts.size
  146. assert_equal 2, posts[0].categories.size
  147. assert_equal 1, posts[1].categories.size
  148. assert_equal 0, posts[2].categories.size
  149. assert posts[0].categories.include?(categories(:technology))
  150. assert posts[1].categories.include?(categories(:general))
  151. end
  152. def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
  153. posts = authors(:david).posts.find(:all,
  154. :include => :comments,
  155. :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
  156. :limit => 2
  157. )
  158. assert_equal 2, posts.size
  159. count = Post.count(
  160. :include => [ :comments, :author ],
  161. :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
  162. :limit => 2
  163. )
  164. assert_equal count, posts.size
  165. end
  166. def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
  167. posts = nil
  168. Post.with_scope(:find => {
  169. :include => :comments,
  170. :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
  171. }) do
  172. posts = authors(:david).posts.find(:all, :limit => 2)
  173. assert_equal 2, posts.size
  174. end
  175. Post.with_scope(:find => {
  176. :include => [ :comments, :author ],
  177. :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
  178. }) do
  179. count = Post.count(:limit => 2)
  180. assert_equal count, posts.size
  181. end
  182. end
  183. def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
  184. Post.with_scope(:find => { :conditions => "1=1" }) do
  185. posts = authors(:david).posts.find(:all,
  186. :include => :comments,
  187. :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
  188. :limit => 2
  189. )
  190. assert_equal 2, posts.size
  191. count = Post.count(
  192. :include => [ :comments, :author ],
  193. :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
  194. :limit => 2
  195. )
  196. assert_equal count, posts.size
  197. end
  198. end
  199. def test_eager_association_loading_with_habtm
  200. posts = Post.find(:all, :include => :categories, :order => "posts.id")
  201. assert_equal 2, posts[0].categories.size
  202. assert_equal 1, posts[1].categories.size
  203. assert_equal 0, posts[2].categories.size
  204. assert posts[0].categories.include?(categories(:technology))
  205. assert posts[1].categories.include?(categories(:general))
  206. end
  207. def test_eager_with_inheritance
  208. posts = SpecialPost.find(:all, :include => [ :comments ])
  209. end
  210. def test_eager_has_one_with_association_inheritance
  211. post = Post.find(4, :include => [ :very_special_comment ])
  212. assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
  213. end
  214. def test_eager_has_many_with_association_inheritance
  215. post = Post.find(4, :include => [ :special_comments ])
  216. post.special_comments.each do |special_comment|
  217. assert_equal "SpecialComment", special_comment.class.to_s
  218. end
  219. end
  220. def test_eager_habtm_with_association_inheritance
  221. post = Post.find(6, :include => [ :special_categories ])
  222. assert_equal 1, post.special_categories.size
  223. post.special_categories.each do |special_category|
  224. assert_equal "SpecialCategory", special_category.class.to_s
  225. end
  226. end
  227. def test_eager_with_has_one_dependent_does_not_destroy_dependent
  228. assert_not_nil companies(:first_firm).account
  229. f = Firm.find(:first, :include => :account,
  230. :conditions => ["companies.name = ?", "37signals"])
  231. assert_not_nil f.account
  232. assert_equal companies(:first_firm, :reload).account, f.account
  233. end
  234. def test_eager_with_invalid_association_reference
  235. assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
  236. post = Post.find(6, :include=> :monkeys )
  237. }
  238. assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
  239. post = Post.find(6, :include=>[ :monkeys ])
  240. }
  241. assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
  242. post = Post.find(6, :include=>[ 'monkeys' ])
  243. }
  244. assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
  245. post = Post.find(6, :include=>[ :monkeys, :elephants ])
  246. }
  247. end
  248. def find_all_ordered(className, include=nil)
  249. className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
  250. end
  251. def test_limited_eager_with_order
  252. assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
  253. 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)
  254. end
  255. def test_limited_eager_with_multiple_order_columns
  256. 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)
  257. 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)
  258. end
  259. def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
  260. # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
  261. def assert_equal_after_sort(item1, item2, item3 = nil)
  262. assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
  263. assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
  264. end
  265. # Test regular association, association with conditions, association with
  266. # STI, and association with conditions assured not to be true
  267. post_types = [:posts, :other_posts, :special_posts]
  268. # test both has_many and has_and_belongs_to_many
  269. [Author, Category].each do |className|
  270. d1 = find_all_ordered(className)
  271. # test including all post types at once
  272. d2 = find_all_ordered(className, post_types)
  273. d1.each_index do |i|
  274. assert_equal(d1[i], d2[i])
  275. assert_equal_after_sort(d1[i].posts, d2[i].posts)
  276. post_types[1..-1].each do |post_type|
  277. # test including post_types together
  278. d3 = find_all_ordered(className, [:posts, post_type])
  279. assert_equal(d1[i], d3[i])
  280. assert_equal_after_sort(d1[i].posts, d3[i].posts)
  281. assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
  282. end
  283. end
  284. end
  285. end
  286. def test_eager_with_multiple_associations_with_same_table_has_one
  287. d1 = find_all_ordered(Firm)
  288. d2 = find_all_ordered(Firm, :account)
  289. d1.each_index do |i|
  290. assert_equal(d1[i], d2[i])
  291. assert_equal(d1[i].account, d2[i].account)
  292. end
  293. end
  294. def test_eager_with_multiple_associations_with_same_table_belongs_to
  295. firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
  296. d1 = find_all_ordered(Client)
  297. d2 = find_all_ordered(Client, firm_types)
  298. d1.each_index do |i|
  299. assert_equal(d1[i], d2[i])
  300. firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
  301. end
  302. end
  303. def test_eager_with_valid_association_as_string_not_symbol
  304. assert_nothing_raised { Post.find(:all, :include => 'comments') }
  305. end
  306. def test_preconfigured_includes_with_belongs_to
  307. author = posts(:welcome).author_with_posts
  308. assert_equal 5, author.posts.size
  309. end
  310. def test_preconfigured_includes_with_has_one
  311. comment = posts(:sti_comments).very_special_comment_with_post
  312. assert_equal posts(:sti_comments), comment.post
  313. end
  314. def test_preconfigured_includes_with_has_many
  315. posts = authors(:david).posts_with_comments
  316. one = posts.detect { |p| p.id == 1 }
  317. assert_equal 5, posts.size
  318. assert_equal 2, one.comments.size
  319. end
  320. def test_preconfigured_includes_with_habtm
  321. posts = authors(:david).posts_with_categories
  322. one = posts.detect { |p| p.id == 1 }
  323. assert_equal 5, posts.size
  324. assert_equal 2, one.categories.size
  325. end
  326. def test_preconfigured_includes_with_has_many_and_habtm
  327. posts = authors(:david).posts_with_comments_and_categories
  328. one = posts.detect { |p| p.id == 1 }
  329. assert_equal 5, posts.size
  330. assert_equal 2, one.comments.size
  331. assert_equal 2, one.categories.size
  332. end
  333. def test_count_with_include
  334. if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
  335. assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
  336. else
  337. assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
  338. end
  339. end
  340. end