PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/benburkert/cruisecontrolrb
Ruby | 497 lines | 408 code | 86 blank | 3 comment | 6 complexity | 8670c962b0745dbfde2564c81fb81c1e MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'abstract_unit'
  2. require 'fixtures/tag'
  3. require 'fixtures/tagging'
  4. require 'fixtures/post'
  5. require 'fixtures/comment'
  6. require 'fixtures/author'
  7. require 'fixtures/category'
  8. require 'fixtures/categorization'
  9. require 'fixtures/vertex'
  10. require 'fixtures/edge'
  11. class AssociationsJoinModelTest < Test::Unit::TestCase
  12. self.use_transactional_fixtures = false
  13. fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices
  14. def test_has_many
  15. assert authors(:david).categories.include?(categories(:general))
  16. end
  17. def test_has_many_inherited
  18. assert authors(:mary).categories.include?(categories(:sti_test))
  19. end
  20. def test_inherited_has_many
  21. assert categories(:sti_test).authors.include?(authors(:mary))
  22. end
  23. def test_has_many_uniq_through_join_model
  24. assert_equal 2, authors(:mary).categorized_posts.size
  25. assert_equal 1, authors(:mary).unique_categorized_posts.size
  26. end
  27. def test_polymorphic_has_many
  28. assert posts(:welcome).taggings.include?(taggings(:welcome_general))
  29. end
  30. def test_polymorphic_has_one
  31. assert_equal taggings(:welcome_general), posts(:welcome).tagging
  32. end
  33. def test_polymorphic_belongs_to
  34. assert_equal posts(:welcome), posts(:welcome).taggings.first.taggable
  35. end
  36. def test_polymorphic_has_many_going_through_join_model
  37. assert_equal tags(:general), tag = posts(:welcome).tags.first
  38. assert_no_queries do
  39. tag.tagging
  40. end
  41. end
  42. def test_count_polymorphic_has_many
  43. assert_equal 1, posts(:welcome).taggings.count
  44. assert_equal 1, posts(:welcome).tags.count
  45. end
  46. def test_polymorphic_has_many_going_through_join_model_with_find
  47. assert_equal tags(:general), tag = posts(:welcome).tags.find(:first)
  48. assert_no_queries do
  49. tag.tagging
  50. end
  51. end
  52. def test_polymorphic_has_many_going_through_join_model_with_include_on_source_reflection
  53. assert_equal tags(:general), tag = posts(:welcome).funky_tags.first
  54. assert_no_queries do
  55. tag.tagging
  56. end
  57. end
  58. def test_polymorphic_has_many_going_through_join_model_with_include_on_source_reflection_with_find
  59. assert_equal tags(:general), tag = posts(:welcome).funky_tags.find(:first)
  60. assert_no_queries do
  61. tag.tagging
  62. end
  63. end
  64. def test_polymorphic_has_many_going_through_join_model_with_disabled_include
  65. assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
  66. assert_queries 1 do
  67. tag.tagging
  68. end
  69. end
  70. def test_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins
  71. assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
  72. tag.author_id
  73. end
  74. def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key
  75. assert_equal tags(:misc), taggings(:welcome_general).super_tag
  76. assert_equal tags(:misc), posts(:welcome).super_tags.first
  77. end
  78. def test_polymorphic_has_many_create_model_with_inheritance_and_custom_base_class
  79. post = SubStiPost.create :title => 'SubStiPost', :body => 'SubStiPost body'
  80. assert_instance_of SubStiPost, post
  81. tagging = tags(:misc).taggings.create(:taggable => post)
  82. assert_equal "SubStiPost", tagging.taggable_type
  83. end
  84. def test_polymorphic_has_many_going_through_join_model_with_inheritance
  85. assert_equal tags(:general), posts(:thinking).tags.first
  86. end
  87. def test_polymorphic_has_many_going_through_join_model_with_inheritance_with_custom_class_name
  88. assert_equal tags(:general), posts(:thinking).funky_tags.first
  89. end
  90. def test_polymorphic_has_many_create_model_with_inheritance
  91. post = posts(:thinking)
  92. assert_instance_of SpecialPost, post
  93. tagging = tags(:misc).taggings.create(:taggable => post)
  94. assert_equal "Post", tagging.taggable_type
  95. end
  96. def test_polymorphic_has_one_create_model_with_inheritance
  97. tagging = tags(:misc).create_tagging(:taggable => posts(:thinking))
  98. assert_equal "Post", tagging.taggable_type
  99. end
  100. def test_set_polymorphic_has_many
  101. tagging = tags(:misc).taggings.create
  102. posts(:thinking).taggings << tagging
  103. assert_equal "Post", tagging.taggable_type
  104. end
  105. def test_set_polymorphic_has_one
  106. tagging = tags(:misc).taggings.create
  107. posts(:thinking).tagging = tagging
  108. assert_equal "Post", tagging.taggable_type
  109. end
  110. def test_create_polymorphic_has_many_with_scope
  111. old_count = posts(:welcome).taggings.count
  112. tagging = posts(:welcome).taggings.create(:tag => tags(:misc))
  113. assert_equal "Post", tagging.taggable_type
  114. assert_equal old_count+1, posts(:welcome).taggings.count
  115. end
  116. def test_create_bang_polymorphic_with_has_many_scope
  117. old_count = posts(:welcome).taggings.count
  118. tagging = posts(:welcome).taggings.create!(:tag => tags(:misc))
  119. assert_equal "Post", tagging.taggable_type
  120. assert_equal old_count+1, posts(:welcome).taggings.count
  121. end
  122. def test_create_polymorphic_has_one_with_scope
  123. old_count = Tagging.count
  124. tagging = posts(:welcome).tagging.create(:tag => tags(:misc))
  125. assert_equal "Post", tagging.taggable_type
  126. assert_equal old_count+1, Tagging.count
  127. end
  128. def test_delete_polymorphic_has_many_with_delete_all
  129. assert_equal 1, posts(:welcome).taggings.count
  130. posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyDeleteAll'
  131. post = find_post_with_dependency(1, :has_many, :taggings, :delete_all)
  132. old_count = Tagging.count
  133. post.destroy
  134. assert_equal old_count-1, Tagging.count
  135. assert_equal 0, posts(:welcome).taggings.count
  136. end
  137. def test_delete_polymorphic_has_many_with_destroy
  138. assert_equal 1, posts(:welcome).taggings.count
  139. posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyDestroy'
  140. post = find_post_with_dependency(1, :has_many, :taggings, :destroy)
  141. old_count = Tagging.count
  142. post.destroy
  143. assert_equal old_count-1, Tagging.count
  144. assert_equal 0, posts(:welcome).taggings.count
  145. end
  146. def test_delete_polymorphic_has_many_with_nullify
  147. assert_equal 1, posts(:welcome).taggings.count
  148. posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyNullify'
  149. post = find_post_with_dependency(1, :has_many, :taggings, :nullify)
  150. old_count = Tagging.count
  151. post.destroy
  152. assert_equal old_count, Tagging.count
  153. assert_equal 0, posts(:welcome).taggings.count
  154. end
  155. def test_delete_polymorphic_has_one_with_destroy
  156. assert posts(:welcome).tagging
  157. posts(:welcome).tagging.update_attribute :taggable_type, 'PostWithHasOneDestroy'
  158. post = find_post_with_dependency(1, :has_one, :tagging, :destroy)
  159. old_count = Tagging.count
  160. post.destroy
  161. assert_equal old_count-1, Tagging.count
  162. assert_nil posts(:welcome).tagging(true)
  163. end
  164. def test_delete_polymorphic_has_one_with_nullify
  165. assert posts(:welcome).tagging
  166. posts(:welcome).tagging.update_attribute :taggable_type, 'PostWithHasOneNullify'
  167. post = find_post_with_dependency(1, :has_one, :tagging, :nullify)
  168. old_count = Tagging.count
  169. post.destroy
  170. assert_equal old_count, Tagging.count
  171. assert_nil posts(:welcome).tagging(true)
  172. end
  173. def test_has_many_with_piggyback
  174. assert_equal "2", categories(:sti_test).authors.first.post_id.to_s
  175. end
  176. def test_include_has_many_through
  177. posts = Post.find(:all, :order => 'posts.id')
  178. posts_with_authors = Post.find(:all, :include => :authors, :order => 'posts.id')
  179. assert_equal posts.length, posts_with_authors.length
  180. posts.length.times do |i|
  181. assert_equal posts[i].authors.length, assert_no_queries { posts_with_authors[i].authors.length }
  182. end
  183. end
  184. def test_include_polymorphic_has_one
  185. post = Post.find_by_id(posts(:welcome).id, :include => :tagging)
  186. tagging = taggings(:welcome_general)
  187. assert_no_queries do
  188. assert_equal tagging, post.tagging
  189. end
  190. end
  191. def test_include_polymorphic_has_many_through
  192. posts = Post.find(:all, :order => 'posts.id')
  193. posts_with_tags = Post.find(:all, :include => :tags, :order => 'posts.id')
  194. assert_equal posts.length, posts_with_tags.length
  195. posts.length.times do |i|
  196. assert_equal posts[i].tags.length, assert_no_queries { posts_with_tags[i].tags.length }
  197. end
  198. end
  199. def test_include_polymorphic_has_many
  200. posts = Post.find(:all, :order => 'posts.id')
  201. posts_with_taggings = Post.find(:all, :include => :taggings, :order => 'posts.id')
  202. assert_equal posts.length, posts_with_taggings.length
  203. posts.length.times do |i|
  204. assert_equal posts[i].taggings.length, assert_no_queries { posts_with_taggings[i].taggings.length }
  205. end
  206. end
  207. def test_has_many_find_all
  208. assert_equal [categories(:general)], authors(:david).categories.find(:all)
  209. end
  210. def test_has_many_find_first
  211. assert_equal categories(:general), authors(:david).categories.find(:first)
  212. end
  213. def test_has_many_with_hash_conditions
  214. assert_equal categories(:general), authors(:david).categories_like_general.find(:first)
  215. end
  216. def test_has_many_find_conditions
  217. assert_equal categories(:general), authors(:david).categories.find(:first, :conditions => "categories.name = 'General'")
  218. assert_equal nil, authors(:david).categories.find(:first, :conditions => "categories.name = 'Technology'")
  219. end
  220. def test_has_many_class_methods_called_by_method_missing
  221. assert_equal categories(:general), authors(:david).categories.find_all_by_name('General').first
  222. # assert_equal nil, authors(:david).categories.find_by_name('Technology')
  223. end
  224. def test_has_many_going_through_join_model_with_custom_foreign_key
  225. assert_equal [], posts(:thinking).authors
  226. assert_equal [authors(:mary)], posts(:authorless).authors
  227. end
  228. def test_belongs_to_polymorphic_with_counter_cache
  229. assert_equal 0, posts(:welcome)[:taggings_count]
  230. tagging = posts(:welcome).taggings.create(:tag => tags(:general))
  231. assert_equal 1, posts(:welcome, :reload)[:taggings_count]
  232. tagging.destroy
  233. assert posts(:welcome, :reload)[:taggings_count].zero?
  234. end
  235. def test_unavailable_through_reflection
  236. assert_raises (ActiveRecord::HasManyThroughAssociationNotFoundError) { authors(:david).nothings }
  237. end
  238. def test_has_many_through_join_model_with_conditions
  239. assert_equal [], posts(:welcome).invalid_taggings
  240. assert_equal [], posts(:welcome).invalid_tags
  241. end
  242. def test_has_many_polymorphic
  243. assert_raises ActiveRecord::HasManyThroughAssociationPolymorphicError do
  244. assert_equal [posts(:welcome), posts(:thinking)], tags(:general).taggables
  245. end
  246. assert_raises ActiveRecord::EagerLoadPolymorphicError do
  247. assert_equal [posts(:welcome), posts(:thinking)], tags(:general).taggings.find(:all, :include => :taggable)
  248. end
  249. end
  250. def test_has_many_polymorphic_with_source_type
  251. assert_equal [posts(:welcome), posts(:thinking)], tags(:general).tagged_posts
  252. end
  253. def test_eager_has_many_polymorphic_with_source_type
  254. tag_with_include = Tag.find(tags(:general).id, :include => :tagged_posts)
  255. desired = [posts(:welcome), posts(:thinking)]
  256. assert_no_queries do
  257. assert_equal desired, tag_with_include.tagged_posts
  258. end
  259. end
  260. def test_has_many_through_has_many_find_all
  261. assert_equal comments(:greetings), authors(:david).comments.find(:all, :order => 'comments.id').first
  262. end
  263. def test_has_many_through_has_many_find_all_with_custom_class
  264. assert_equal comments(:greetings), authors(:david).funky_comments.find(:all, :order => 'comments.id').first
  265. end
  266. def test_has_many_through_has_many_find_first
  267. assert_equal comments(:greetings), authors(:david).comments.find(:first, :order => 'comments.id')
  268. end
  269. def test_has_many_through_has_many_find_conditions
  270. options = { :conditions => "comments.#{QUOTED_TYPE}='SpecialComment'", :order => 'comments.id' }
  271. assert_equal comments(:does_it_hurt), authors(:david).comments.find(:first, options)
  272. end
  273. def test_has_many_through_has_many_find_by_id
  274. assert_equal comments(:more_greetings), authors(:david).comments.find(2)
  275. end
  276. def test_has_many_through_polymorphic_has_one
  277. assert_raise(ActiveRecord::HasManyThroughSourceAssociationMacroError) { authors(:david).tagging }
  278. end
  279. def test_has_many_through_polymorphic_has_many
  280. assert_equal [taggings(:welcome_general), taggings(:thinking_general)], authors(:david).taggings.uniq.sort_by { |t| t.id }
  281. end
  282. def test_include_has_many_through_polymorphic_has_many
  283. author = Author.find_by_id(authors(:david).id, :include => :taggings)
  284. expected_taggings = [taggings(:welcome_general), taggings(:thinking_general)]
  285. assert_no_queries do
  286. assert_equal expected_taggings, author.taggings.uniq.sort_by { |t| t.id }
  287. end
  288. end
  289. def test_has_many_through_has_many_through
  290. assert_raise(ActiveRecord::HasManyThroughSourceAssociationMacroError) { authors(:david).tags }
  291. end
  292. def test_has_many_through_habtm
  293. assert_raise(ActiveRecord::HasManyThroughSourceAssociationMacroError) { authors(:david).post_categories }
  294. end
  295. def test_eager_load_has_many_through_has_many
  296. author = Author.find :first, :conditions => ['name = ?', 'David'], :include => :comments, :order => 'comments.id'
  297. SpecialComment.new; VerySpecialComment.new
  298. assert_no_queries do
  299. assert_equal [1,2,3,5,6,7,8,9,10], author.comments.collect(&:id)
  300. end
  301. end
  302. def test_eager_load_has_many_through_has_many_with_conditions
  303. post = Post.find(:first, :include => :invalid_tags)
  304. assert_no_queries do
  305. post.invalid_tags
  306. end
  307. end
  308. def test_eager_belongs_to_and_has_one_not_singularized
  309. assert_nothing_raised do
  310. Author.find(:first, :include => :author_address)
  311. AuthorAddress.find(:first, :include => :author)
  312. end
  313. end
  314. def test_self_referential_has_many_through
  315. assert_equal [authors(:mary)], authors(:david).favorite_authors
  316. assert_equal [], authors(:mary).favorite_authors
  317. end
  318. def test_add_to_self_referential_has_many_through
  319. new_author = Author.create(:name => "Bob")
  320. authors(:david).author_favorites.create :favorite_author => new_author
  321. assert_equal new_author, authors(:david).reload.favorite_authors.first
  322. end
  323. def test_has_many_through_uses_correct_attributes
  324. assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"]
  325. end
  326. def test_raise_error_when_adding_new_record_to_has_many_through
  327. assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags << tags(:general).clone }
  328. assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).clone.tags << tags(:general) }
  329. assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags.build }
  330. end
  331. def test_create_associate_when_adding_to_has_many_through
  332. count = posts(:thinking).tags.count
  333. push = Tag.create!(:name => 'pushme')
  334. post_thinking = posts(:thinking)
  335. assert_nothing_raised { post_thinking.tags << push }
  336. assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
  337. message = "Expected a Tag in tags collection, got #{wrong.class}.")
  338. assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
  339. message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
  340. assert_equal(count + 1, post_thinking.tags.size)
  341. assert_equal(count + 1, post_thinking.tags(true).size)
  342. assert_nothing_raised { post_thinking.tags.create!(:name => 'foo') }
  343. assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
  344. message = "Expected a Tag in tags collection, got #{wrong.class}.")
  345. assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
  346. message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
  347. assert_equal(count + 2, post_thinking.tags.size)
  348. assert_equal(count + 2, post_thinking.tags(true).size)
  349. assert_nothing_raised { post_thinking.tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) }
  350. assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
  351. message = "Expected a Tag in tags collection, got #{wrong.class}.")
  352. assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
  353. message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
  354. assert_equal(count + 4, post_thinking.tags.size)
  355. assert_equal(count + 4, post_thinking.tags(true).size)
  356. # Raises if the wrong reflection name is used to set the Edge belongs_to
  357. assert_nothing_raised { vertices(:vertex_1).sinks << vertices(:vertex_5) }
  358. end
  359. def test_adding_junk_to_has_many_through_should_raise_type_mismatch
  360. assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags << "Uhh what now?" }
  361. end
  362. def test_adding_to_has_many_through_should_return_self
  363. tags = posts(:thinking).tags
  364. assert_equal tags, posts(:thinking).tags.push(tags(:general))
  365. end
  366. def test_delete_associate_when_deleting_from_has_many_through
  367. count = posts(:thinking).tags.count
  368. tags_before = posts(:thinking).tags
  369. tag = Tag.create!(:name => 'doomed')
  370. post_thinking = posts(:thinking)
  371. post_thinking.tags << tag
  372. assert_equal(count + 1, post_thinking.tags(true).size)
  373. assert_nothing_raised { post_thinking.tags.delete(tag) }
  374. assert_equal(count, post_thinking.tags.size)
  375. assert_equal(count, post_thinking.tags(true).size)
  376. assert_equal(tags_before.sort, post_thinking.tags.sort)
  377. end
  378. def test_delete_associate_when_deleting_from_has_many_through_with_multiple_tags
  379. count = posts(:thinking).tags.count
  380. tags_before = posts(:thinking).tags
  381. doomed = Tag.create!(:name => 'doomed')
  382. doomed2 = Tag.create!(:name => 'doomed2')
  383. quaked = Tag.create!(:name => 'quaked')
  384. post_thinking = posts(:thinking)
  385. post_thinking.tags << doomed << doomed2
  386. assert_equal(count + 2, post_thinking.tags(true).size)
  387. assert_nothing_raised { post_thinking.tags.delete(doomed, doomed2, quaked) }
  388. assert_equal(count, post_thinking.tags.size)
  389. assert_equal(count, post_thinking.tags(true).size)
  390. assert_equal(tags_before.sort, post_thinking.tags.sort)
  391. end
  392. def test_deleting_junk_from_has_many_through_should_raise_type_mismatch
  393. assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags.delete("Uhh what now?") }
  394. end
  395. def test_has_many_through_sum_uses_calculations
  396. assert_nothing_raised { authors(:david).comments.sum(:post_id) }
  397. end
  398. def test_has_many_through_has_many_with_sti
  399. assert_equal [comments(:does_it_hurt)], authors(:david).special_post_comments
  400. end
  401. private
  402. # create dynamic Post models to allow different dependency options
  403. def find_post_with_dependency(post_id, association, association_name, dependency)
  404. class_name = "PostWith#{association.to_s.classify}#{dependency.to_s.classify}"
  405. Post.find(post_id).update_attribute :type, class_name
  406. klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
  407. klass.set_table_name 'posts'
  408. klass.send(association, association_name, :as => :taggable, :dependent => dependency)
  409. klass.find(post_id)
  410. end
  411. end