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