PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

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