PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/acts_as_taggable_on_steroids/test/acts_as_taggable_test.rb

https://bitbucket.org/josselin_beaumont/music-spaces
Ruby | 333 lines | 253 code | 74 blank | 6 comment | 0 complexity | 4d99363786906efbe4feb0051bb19b51 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, JSON, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. require File.dirname(__FILE__) + '/abstract_unit'
  2. class ActsAsTaggableOnSteroidsTest < Test::Unit::TestCase
  3. fixtures :tags, :taggings, :posts, :users, :photos, :subscriptions, :magazines
  4. def test_find_tagged_with
  5. assert_equivalent [posts(:jonathan_sky), posts(:sam_flowers)], Post.find_tagged_with('"Very good"')
  6. assert_equal Post.find_tagged_with('"Very good"'), Post.find_tagged_with(['Very good'])
  7. assert_equal Post.find_tagged_with('"Very good"'), Post.find_tagged_with([tags(:good)])
  8. assert_equivalent [photos(:jonathan_dog), photos(:sam_flower), photos(:sam_sky)], Photo.find_tagged_with('Nature')
  9. assert_equal Photo.find_tagged_with('Nature'), Photo.find_tagged_with(['Nature'])
  10. assert_equal Photo.find_tagged_with('Nature'), Photo.find_tagged_with([tags(:nature)])
  11. assert_equivalent [photos(:jonathan_bad_cat), photos(:jonathan_dog), photos(:jonathan_questioning_dog)], Photo.find_tagged_with('"Crazy animal" Bad')
  12. assert_equal Photo.find_tagged_with('"Crazy animal" Bad'), Photo.find_tagged_with(['Crazy animal', 'Bad'])
  13. assert_equal Photo.find_tagged_with('"Crazy animal" Bad'), Photo.find_tagged_with([tags(:animal), tags(:bad)])
  14. end
  15. def test_find_tagged_with_nothing
  16. assert_equal [], Post.find_tagged_with("")
  17. assert_equal [], Post.find_tagged_with([])
  18. end
  19. def test_find_tagged_with_nonexistant_tags
  20. assert_equal [], Post.find_tagged_with('ABCDEFG')
  21. assert_equal [], Photo.find_tagged_with(['HIJKLM'])
  22. assert_equal [], Photo.find_tagged_with([Tag.new(:name => 'unsaved tag')])
  23. end
  24. def test_find_tagged_with_match_all
  25. assert_equivalent [photos(:jonathan_dog)], Photo.find_tagged_with('Crazy animal, "Nature"', :match_all => true)
  26. end
  27. def test_find_tagged_with_match_all_and_include
  28. assert_equivalent [posts(:jonathan_sky), posts(:sam_flowers)], Post.find_tagged_with(['Very good', 'Nature'], :match_all => true, :include => :tags)
  29. end
  30. def test_find_tagged_with_conditions
  31. assert_equal [], Post.find_tagged_with('"Very good", Nature', :conditions => '1=0')
  32. end
  33. def test_find_tagged_with_exclusions
  34. assert_equivalent [photos(:jonathan_questioning_dog), photos(:jonathan_bad_cat)], Photo.find_tagged_with("Nature", :exclude => true)
  35. assert_equivalent [posts(:jonathan_grass), posts(:jonathan_rain), posts(:jonathan_cloudy), posts(:jonathan_still_cloudy)], Post.find_tagged_with("'Very good', Bad", :exclude => true)
  36. end
  37. def test_find_options_for_find_tagged_with_no_tags_returns_empty_hash
  38. assert_equal Hash.new, Post.find_options_for_find_tagged_with("")
  39. assert_equal Hash.new, Post.find_options_for_find_tagged_with([nil])
  40. end
  41. def test_find_options_for_find_tagged_with_leaves_arguments_unchanged
  42. original_tags = photos(:jonathan_questioning_dog).tags.dup
  43. Photo.find_options_for_find_tagged_with(photos(:jonathan_questioning_dog).tags)
  44. assert_equal original_tags, photos(:jonathan_questioning_dog).tags
  45. end
  46. def test_find_options_for_find_tagged_with_respects_custom_table_name
  47. Tagging.table_name = "categorisations"
  48. Tag.table_name = "categories"
  49. options = Photo.find_options_for_find_tagged_with("Hello")
  50. assert_no_match(/ taggings /, options[:joins])
  51. assert_no_match(/ tags /, options[:joins])
  52. assert_match(/ categorisations /, options[:joins])
  53. assert_match(/ categories /, options[:joins])
  54. ensure
  55. Tagging.table_name = "taggings"
  56. Tag.table_name = "tags"
  57. end
  58. def test_include_tags_on_find_tagged_with
  59. assert_nothing_raised do
  60. Photo.find_tagged_with('Nature', :include => :tags)
  61. Photo.find_tagged_with("Nature", :include => { :taggings => :tag })
  62. end
  63. end
  64. def test_basic_tag_counts_on_class
  65. assert_tag_counts Post.tag_counts, :good => 2, :nature => 7, :question => 1, :bad => 1
  66. assert_tag_counts Photo.tag_counts, :good => 1, :nature => 3, :question => 1, :bad => 1, :animal => 3
  67. end
  68. def test_tag_counts_on_class_with_date_conditions
  69. assert_tag_counts Post.tag_counts(:start_at => Date.new(2006, 8, 4)), :good => 1, :nature => 5, :question => 1, :bad => 1
  70. assert_tag_counts Post.tag_counts(:end_at => Date.new(2006, 8, 6)), :good => 1, :nature => 4, :question => 1
  71. assert_tag_counts Post.tag_counts(:start_at => Date.new(2006, 8, 5), :end_at => Date.new(2006, 8, 10)), :good => 1, :nature => 4, :bad => 1
  72. assert_tag_counts Photo.tag_counts(:start_at => Date.new(2006, 8, 12), :end_at => Date.new(2006, 8, 19)), :good => 1, :nature => 2, :bad => 1, :question => 1, :animal => 3
  73. end
  74. def test_tag_counts_on_class_with_frequencies
  75. assert_tag_counts Photo.tag_counts(:at_least => 2), :nature => 3, :animal => 3
  76. assert_tag_counts Photo.tag_counts(:at_most => 2), :good => 1, :question => 1, :bad => 1
  77. end
  78. def test_tag_counts_with_limit
  79. assert_equal 2, Photo.tag_counts(:limit => 2).size
  80. assert_equal 1, Post.tag_counts(:at_least => 4, :limit => 2).size
  81. end
  82. def test_tag_counts_with_limit_and_order
  83. assert_equal [tags(:nature), tags(:good)], Post.tag_counts(:order => 'count desc', :limit => 2)
  84. end
  85. def test_tag_counts_on_association
  86. assert_tag_counts users(:jonathan).posts.tag_counts, :good => 1, :nature => 5, :question => 1
  87. assert_tag_counts users(:sam).posts.tag_counts, :good => 1, :nature => 2, :bad => 1
  88. assert_tag_counts users(:jonathan).photos.tag_counts, :animal => 3, :nature => 1, :question => 1, :bad => 1
  89. assert_tag_counts users(:sam).photos.tag_counts, :nature => 2, :good => 1
  90. end
  91. def test_tag_counts_on_association_with_options
  92. assert_equal [], users(:jonathan).posts.tag_counts(:conditions => '1=0')
  93. assert_tag_counts users(:jonathan).posts.tag_counts(:at_most => 2), :good => 1, :question => 1
  94. end
  95. def test_tag_counts_on_has_many_through
  96. assert_tag_counts users(:jonathan).magazines.tag_counts, :good => 1
  97. end
  98. def test_tag_counts_respects_custom_table_names
  99. Tagging.table_name = "categorisations"
  100. Tag.table_name = "categories"
  101. options = Photo.find_options_for_tag_counts(:start_at => 2.weeks.ago, :end_at => Date.today)
  102. sql = options.values.join(' ')
  103. assert_no_match /taggings/, sql
  104. assert_no_match /tags/, sql
  105. assert_match /categorisations/, sql
  106. assert_match /categories/, sql
  107. ensure
  108. Tagging.table_name = "taggings"
  109. Tag.table_name = "tags"
  110. end
  111. def test_tag_list_reader
  112. assert_equivalent ["Very good", "Nature"], posts(:jonathan_sky).tag_list
  113. assert_equivalent ["Bad", "Crazy animal"], photos(:jonathan_bad_cat).tag_list
  114. end
  115. def test_reassign_tag_list
  116. assert_equivalent ["Nature", "Question"], posts(:jonathan_rain).tag_list
  117. posts(:jonathan_rain).taggings.reload
  118. # Only an update of the posts table should be executed
  119. assert_queries 1 do
  120. posts(:jonathan_rain).update_attributes!(:tag_list => posts(:jonathan_rain).tag_list.to_s)
  121. end
  122. assert_equivalent ["Nature", "Question"], posts(:jonathan_rain).tag_list
  123. end
  124. def test_new_tags
  125. assert_equivalent ["Very good", "Nature"], posts(:jonathan_sky).tag_list
  126. posts(:jonathan_sky).update_attributes!(:tag_list => "#{posts(:jonathan_sky).tag_list}, One, Two")
  127. assert_equivalent ["Very good", "Nature", "One", "Two"], posts(:jonathan_sky).tag_list
  128. end
  129. def test_remove_tag
  130. assert_equivalent ["Very good", "Nature"], posts(:jonathan_sky).tag_list
  131. posts(:jonathan_sky).update_attributes!(:tag_list => "Nature")
  132. assert_equivalent ["Nature"], posts(:jonathan_sky).tag_list
  133. end
  134. def test_change_case_of_tags
  135. original_tag_names = photos(:jonathan_questioning_dog).tag_list
  136. photos(:jonathan_questioning_dog).update_attributes!(:tag_list => photos(:jonathan_questioning_dog).tag_list.to_s.upcase)
  137. # The new tag list is not uppercase becuase the AR finders are not case-sensitive
  138. # and find the old tags when re-tagging with the uppercase tags.
  139. assert_equivalent original_tag_names, photos(:jonathan_questioning_dog).reload.tag_list
  140. end
  141. def test_remove_and_add_tag
  142. assert_equivalent ["Very good", "Nature"], posts(:jonathan_sky).tag_list
  143. posts(:jonathan_sky).update_attributes!(:tag_list => "Nature, Beautiful")
  144. assert_equivalent ["Nature", "Beautiful"], posts(:jonathan_sky).tag_list
  145. end
  146. def test_tags_not_saved_if_validation_fails
  147. assert_equivalent ["Very good", "Nature"], posts(:jonathan_sky).tag_list
  148. assert !posts(:jonathan_sky).update_attributes(:tag_list => "One, Two", :text => "")
  149. assert_equivalent ["Very good", "Nature"], Post.find(posts(:jonathan_sky).id).tag_list
  150. end
  151. def test_tag_list_accessors_on_new_record
  152. p = Post.new(:text => 'Test')
  153. assert p.tag_list.blank?
  154. p.tag_list = "One, Two"
  155. assert_equal "One, Two", p.tag_list.to_s
  156. end
  157. def test_clear_tag_list_with_nil
  158. p = photos(:jonathan_questioning_dog)
  159. assert !p.tag_list.blank?
  160. assert p.update_attributes(:tag_list => nil)
  161. assert p.tag_list.blank?
  162. assert p.reload.tag_list.blank?
  163. end
  164. def test_clear_tag_list_with_string
  165. p = photos(:jonathan_questioning_dog)
  166. assert !p.tag_list.blank?
  167. assert p.update_attributes(:tag_list => ' ')
  168. assert p.tag_list.blank?
  169. assert p.reload.tag_list.blank?
  170. end
  171. def test_tag_list_reset_on_reload
  172. p = photos(:jonathan_questioning_dog)
  173. assert !p.tag_list.blank?
  174. p.tag_list = nil
  175. assert p.tag_list.blank?
  176. assert !p.reload.tag_list.blank?
  177. end
  178. def test_instance_tag_counts
  179. assert_tag_counts posts(:jonathan_sky).tag_counts, :good => 4, :nature => 10
  180. end
  181. def test_tag_list_populated_when_cache_nil
  182. assert_nil posts(:jonathan_sky).cached_tag_list
  183. posts(:jonathan_sky).save!
  184. assert_equal posts(:jonathan_sky).tag_list.to_s, posts(:jonathan_sky).cached_tag_list
  185. end
  186. def test_cached_tag_list_used
  187. posts(:jonathan_sky).save!
  188. posts(:jonathan_sky).reload
  189. assert_no_queries do
  190. assert_equivalent ["Very good", "Nature"], posts(:jonathan_sky).tag_list
  191. end
  192. end
  193. def test_cached_tag_list_not_used
  194. # Load fixture and column information
  195. posts(:jonathan_sky).taggings(:reload)
  196. assert_queries 1 do
  197. # Tags association will be loaded
  198. posts(:jonathan_sky).tag_list
  199. end
  200. end
  201. def test_cached_tag_list_updated
  202. assert_nil posts(:jonathan_sky).cached_tag_list
  203. posts(:jonathan_sky).save!
  204. assert_equivalent ["Very good", "Nature"], TagList.from(posts(:jonathan_sky).cached_tag_list)
  205. posts(:jonathan_sky).update_attributes!(:tag_list => "None")
  206. assert_equal 'None', posts(:jonathan_sky).cached_tag_list
  207. assert_equal 'None', posts(:jonathan_sky).reload.cached_tag_list
  208. end
  209. def test_clearing_cached_tag_list
  210. # Generate the cached tag list
  211. posts(:jonathan_sky).save!
  212. posts(:jonathan_sky).update_attributes!(:tag_list => "")
  213. assert_equal "", posts(:jonathan_sky).cached_tag_list
  214. end
  215. def test_find_tagged_with_using_sti
  216. special_post = SpecialPost.create!(:text => "Test", :tag_list => "Random")
  217. assert_equal [special_post], SpecialPost.find_tagged_with("Random")
  218. assert Post.find_tagged_with("Random").include?(special_post)
  219. end
  220. def test_tag_counts_using_sti
  221. SpecialPost.create!(:text => "Test", :tag_list => "Nature")
  222. assert_tag_counts SpecialPost.tag_counts, :nature => 1
  223. end
  224. def test_case_insensitivity
  225. assert_difference "Tag.count", 1 do
  226. Post.create!(:text => "Test", :tag_list => "one")
  227. Post.create!(:text => "Test", :tag_list => "One")
  228. end
  229. assert_equal Post.find_tagged_with("Nature"), Post.find_tagged_with("nature")
  230. end
  231. def test_tag_not_destroyed_when_unused
  232. posts(:jonathan_sky).tag_list.add("Random")
  233. posts(:jonathan_sky).save!
  234. assert_no_difference 'Tag.count' do
  235. posts(:jonathan_sky).tag_list.remove("Random")
  236. posts(:jonathan_sky).save!
  237. end
  238. end
  239. def test_tag_destroyed_when_unused
  240. Tag.destroy_unused = true
  241. posts(:jonathan_sky).tag_list.add("Random")
  242. posts(:jonathan_sky).save!
  243. assert_difference 'Tag.count', -1 do
  244. posts(:jonathan_sky).tag_list.remove("Random")
  245. posts(:jonathan_sky).save!
  246. end
  247. ensure
  248. Tag.destroy_unused = false
  249. end
  250. end
  251. class ActsAsTaggableOnSteroidsFormTest < Test::Unit::TestCase
  252. fixtures :tags, :taggings, :posts, :users, :photos
  253. include ActionView::Helpers::FormHelper
  254. def test_tag_list_contents
  255. fields_for :post, posts(:jonathan_sky) do |f|
  256. assert_match /Very good, Nature/, f.text_field(:tag_list)
  257. end
  258. end
  259. end