Commit 35984ff4f0f9a50db37262dcd9c0f8b809610f2b

Authored by Antonio Terceiro
1 parent 4e597889

Speeding up some more tests

app/models/category.rb
@@ -42,11 +42,6 @@ class Category < ActiveRecord::Base @@ -42,11 +42,6 @@ class Category < ActiveRecord::Base
42 self.articles.most_commented(limit) 42 self.articles.most_commented(limit)
43 end 43 end
44 44
45 - def total_items  
46 - # FIXME this can be SLOW (??)  
47 - articles.count + comments.count + enterprises.count + people.count + communities.count + products.count  
48 - end  
49 -  
50 def display_in_menu? 45 def display_in_menu?
51 display_in_menu 46 display_in_menu
52 end 47 end
test/unit/category_test.rb
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../test_helper' @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../test_helper'
4 class CategoryTest < Test::Unit::TestCase 4 class CategoryTest < Test::Unit::TestCase
5 5
6 def setup 6 def setup
7 - @env = Environment.create!(:name => 'Enviroment for testing') 7 + @env = fast_create(Environment)
8 end 8 end
9 9
10 def test_mandatory_field_name 10 def test_mandatory_field_name
@@ -22,13 +22,13 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -22,13 +22,13 @@ class CategoryTest &lt; Test::Unit::TestCase
22 end 22 end
23 23
24 def test_relationship_with_environment 24 def test_relationship_with_environment
25 - c = Category.create!(:name => 'product category for testing', :environment_id => @env.id) 25 + c = build(Category, :environment_id => @env.id)
26 assert_equal @env, c.environment 26 assert_equal @env, c.environment
27 end 27 end
28 28
29 def test_relation_with_parent 29 def test_relation_with_parent
30 - parent_category = Category.create!(:name => 'parent category for testing', :environment_id => @env.id)  
31 - c = Category.create!(:name => 'product category for testing', :environment_id => @env.id, :parent_id => parent_category.id) 30 + parent_category = fast_create(Category)
  31 + c = build(Category, :parent_id => parent_category.id)
32 assert_equal parent_category, c.parent 32 assert_equal parent_category, c.parent
33 end 33 end
34 34
@@ -98,8 +98,8 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -98,8 +98,8 @@ class CategoryTest &lt; Test::Unit::TestCase
98 end 98 end
99 99
100 def test_top_level_for 100 def test_top_level_for
101 - cat = Category.create(:name => 'Category for testing', :environment_id => @env.id)  
102 - sub_cat = Category.create(:name => 'SubCategory for testing', :environment_id => @env.id, :parent_id => cat.id) 101 + cat = fast_create(Category, :environment_id => @env.id)
  102 + sub_cat = fast_create(Category, :environment_id => @env.id, :parent_id => cat.id)
103 103
104 roots = Category.top_level_for(@env) 104 roots = Category.top_level_for(@env)
105 105
@@ -107,7 +107,7 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -107,7 +107,7 @@ class CategoryTest &lt; Test::Unit::TestCase
107 end 107 end
108 108
109 def test_slug 109 def test_slug
110 - c = Category.create(:name => 'Category name') 110 + c = Category.new(:name => 'Category name')
111 assert_equal 'category-name', c.slug 111 assert_equal 'category-name', c.slug
112 end 112 end
113 113
@@ -137,41 +137,29 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -137,41 +137,29 @@ class CategoryTest &lt; Test::Unit::TestCase
137 end 137 end
138 138
139 def test_should_refuse_to_duplicate_slug_under_the_same_parent 139 def test_should_refuse_to_duplicate_slug_under_the_same_parent
140 - c1 = Category.create!(:name => 'test category', :environment_id => @env.id) 140 + c1 = create(Category, :name => 'test category', :environment_id => @env.id)
141 c2 = Category.new(:name => 'Test: Category', :environment_id => @env.id) 141 c2 = Category.new(:name => 'Test: Category', :environment_id => @env.id)
142 142
143 assert !c2.valid? 143 assert !c2.valid?
144 assert c2.errors.invalid?(:slug) 144 assert c2.errors.invalid?(:slug)
145 -  
146 end 145 end
147 146
148 should 'be able to duplicated slug in different scope' do 147 should 'be able to duplicated slug in different scope' do
149 - @env.categories.destroy_all 148 + root1 = fast_create(Category, :name => 'root category 1', :environment_id => @env.id)
  149 + root2 = fast_create(Category, :name => 'root category 2', :environment_id => @env.id)
  150 + child1 = fast_create(Category, :name => 'test category', :environment_id => @env.id, :parent_id => root1.id)
150 151
151 - root1 = Category.create!(:name => 'root category 1', :environment_id => @env.id)  
152 - root2 = Category.create!(:name => 'root category 2', :environment_id => @env.id) 152 + child2 = Category.new(:name => 'test category', :environment_id => @env.id, :parent => root2)
  153 + assert child2.valid?
153 154
154 - assert_nothing_raised ActiveRecord::RecordInvalid do  
155 - Category.create!(:name => 'test category', :environment_id => @env.id, :parent => root1)  
156 - Category.create!(:name => 'test category', :environment_id => @env.id, :parent => root2)  
157 - end  
158 - end  
159 -  
160 - should 'be able to duplicated slug in different scope without parent' do  
161 - @env.categories.destroy_all  
162 -  
163 - root1 = Category.create!(:name => 'root category 1', :environment_id => @env.id)  
164 -  
165 - assert_nothing_raised ActiveRecord::RecordInvalid do  
166 - Category.create!(:name => 'test category', :environment_id => @env.id, :parent => root1)  
167 - Category.create!(:name => 'test category', :environment_id => @env.id, :parent => nil)  
168 - end 155 + newroot = Category.new(:name => 'test category', :environment_id => @env.id, :parent => nil)
  156 + assert newroot.valid?
169 end 157 end
170 158
171 def test_renaming_a_category_should_change_path_of_children 159 def test_renaming_a_category_should_change_path_of_children
172 - c1 = Category.create!(:name => 'parent', :environment_id => @env.id)  
173 - c2 = Category.create!(:name => 'child', :environment_id => @env.id, :parent_id => c1.id)  
174 - c3 = Category.create!(:name => 'grandchild', :environment_id => @env.id, :parent_id => c2.id) 160 + c1 = create(Category, :name => 'parent', :environment_id => @env.id)
  161 + c2 = create(Category, :name => 'child', :environment_id => @env.id, :parent_id => c1.id)
  162 + c3 = create(Category, :name => 'grandchild', :environment_id => @env.id, :parent_id => c2.id)
175 163
176 c1.name = 'parent new name' 164 c1.name = 'parent new name'
177 c1.save! 165 c1.save!
@@ -185,7 +173,6 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -185,7 +173,6 @@ class CategoryTest &lt; Test::Unit::TestCase
185 should "limit the possibile display colors" do 173 should "limit the possibile display colors" do
186 c = Category.new(:name => 'test category', :environment_id => @env.id) 174 c = Category.new(:name => 'test category', :environment_id => @env.id)
187 175
188 -  
189 c.display_color = 10 176 c.display_color = 10
190 c.valid? 177 c.valid?
191 assert c.errors.invalid?(:display_color) 178 assert c.errors.invalid?(:display_color)
@@ -200,10 +187,7 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -200,10 +187,7 @@ class CategoryTest &lt; Test::Unit::TestCase
200 end 187 end
201 188
202 should 'avoid duplicated display colors' do 189 should 'avoid duplicated display colors' do
203 -  
204 - @env.categories.destroy_all  
205 -  
206 - c1 = Category.create!(:name => 'test category', :environment_id => @env.id, :display_color => 1) 190 + c1 = fast_create(Category, :name => 'test category', :environment_id => @env.id, :display_color => 1)
207 191
208 c = Category.new(:name => 'lalala', :environment_id => @env.id) 192 c = Category.new(:name => 'lalala', :environment_id => @env.id)
209 c.display_color = 1 193 c.display_color = 1
@@ -217,9 +201,9 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -217,9 +201,9 @@ class CategoryTest &lt; Test::Unit::TestCase
217 end 201 end
218 202
219 should 'be able to get top ancestor' do 203 should 'be able to get top ancestor' do
220 - c1 = Category.create!(:name => 'test category', :environment_id => @env.id)  
221 - c2 = Category.create!(:name => 'test category', :environment_id => @env.id, :parent_id => c1.id)  
222 - c3 = Category.create!(:name => 'test category', :environment_id => @env.id, :parent_id => c2.id) 204 + c1 = fast_create(Category, :name => 'test category', :environment_id => @env.id)
  205 + c2 = fast_create(Category, :name => 'test category', :environment_id => @env.id, :parent_id => c1.id)
  206 + c3 = fast_create(Category, :name => 'test category', :environment_id => @env.id, :parent_id => c2.id)
223 207
224 assert_equal c1, c1.top_ancestor 208 assert_equal c1, c1.top_ancestor
225 assert_equal c1, c2.top_ancestor 209 assert_equal c1, c2.top_ancestor
@@ -227,10 +211,9 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -227,10 +211,9 @@ class CategoryTest &lt; Test::Unit::TestCase
227 end 211 end
228 212
229 should 'explode path' do 213 should 'explode path' do
230 - c1 = Category.create!(:name => 'parent', :environment_id => @env.id)  
231 - c2 = Category.create!(:name => 'child', :environment_id => @env.id, :parent_id => c1.id)  
232 -  
233 - assert_equal [ 'parent', 'child'], c2.explode_path 214 + c1 = Category.new
  215 + c1.expects(:path).returns("path/to/myself")
  216 + assert_equal [ 'path', 'to', 'myself'], c1.explode_path
234 end 217 end
235 218
236 ################################################################ 219 ################################################################
@@ -301,9 +284,9 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -301,9 +284,9 @@ class CategoryTest &lt; Test::Unit::TestCase
301 284
302 should 'have enterprises' do 285 should 'have enterprises' do
303 c = @env.categories.build(:name => 'my category'); c.save! 286 c = @env.categories.build(:name => 'my category'); c.save!
304 - ent1 = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') 287 + ent1 = fast_create(Enterprise, :identifier => 'enterprise_1', :name => 'Enterprise one')
305 ent1.add_category c 288 ent1.add_category c
306 - ent2 = Enterprise.create!(:identifier => 'enterprise_2', :name => 'Enterprise one') 289 + ent2 = fast_create(Enterprise, :identifier => 'enterprise_2', :name => 'Enterprise one')
307 ent2.add_category c 290 ent2.add_category c
308 assert_includes c.enterprises, ent1 291 assert_includes c.enterprises, ent1
309 assert_includes c.enterprises, ent2 292 assert_includes c.enterprises, ent2
@@ -320,18 +303,18 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -320,18 +303,18 @@ class CategoryTest &lt; Test::Unit::TestCase
320 303
321 should 'have communities' do 304 should 'have communities' do
322 c = @env.categories.build(:name => 'my category'); c.save! 305 c = @env.categories.build(:name => 'my category'); c.save!
323 - c1 = Environment.default.communities.create!(:name => 'testcommunity_1') 306 + c1 = fast_create(Community, :name => 'testcommunity_1')
324 c1.add_category c 307 c1.add_category c
325 - c2 = Environment.default.communities.create!(:name => 'testcommunity_2') 308 + c2 = fast_create(Community, :name => 'testcommunity_2')
326 c2.add_category c 309 c2.add_category c
327 assert_equal [c1, c2], c.communities 310 assert_equal [c1, c2], c.communities
328 end 311 end
329 312
330 should 'have products through enteprises' do 313 should 'have products through enteprises' do
331 c = @env.categories.build(:name => 'my category'); c.save! 314 c = @env.categories.build(:name => 'my category'); c.save!
332 - ent1 = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') 315 + ent1 = fast_create(Enterprise, :identifier => 'enterprise_1', :name => 'Enterprise one')
333 ent1.add_category c 316 ent1.add_category c
334 - ent2 = Enterprise.create!(:identifier => 'enterprise_2', :name => 'Enterprise one') 317 + ent2 = fast_create(Enterprise, :identifier => 'enterprise_2', :name => 'Enterprise one')
335 ent2.add_category c 318 ent2.add_category c
336 prod1 = ent1.products.create!(:name => 'test_prod1') 319 prod1 = ent1.products.create!(:name => 'test_prod1')
337 prod2 = ent2.products.create!(:name => 'test_prod2') 320 prod2 = ent2.products.create!(:name => 'test_prod2')
@@ -341,7 +324,7 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -341,7 +324,7 @@ class CategoryTest &lt; Test::Unit::TestCase
341 324
342 should 'not have person through communities' do 325 should 'not have person through communities' do
343 c = @env.categories.build(:name => 'my category'); c.save! 326 c = @env.categories.build(:name => 'my category'); c.save!
344 - com = Community.create!(:identifier => 'community_1', :name => 'Community one') 327 + com = fast_create(Community, :identifier => 'community_1', :name => 'Community one')
345 com.add_category c 328 com.add_category c
346 person = create_user('test_user').person 329 person = create_user('test_user').person
347 person.add_category c 330 person.add_category c
@@ -351,7 +334,7 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -351,7 +334,7 @@ class CategoryTest &lt; Test::Unit::TestCase
351 334
352 should 'not have person through enterprises' do 335 should 'not have person through enterprises' do
353 c = @env.categories.build(:name => 'my category'); c.save! 336 c = @env.categories.build(:name => 'my category'); c.save!
354 - ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') 337 + ent = fast_create(Enterprise, :identifier => 'enterprise_1', :name => 'Enterprise one')
355 ent.add_category c 338 ent.add_category c
356 person = create_user('test_user').person 339 person = create_user('test_user').person
357 person.add_category c 340 person.add_category c
@@ -363,33 +346,12 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -363,33 +346,12 @@ class CategoryTest &lt; Test::Unit::TestCase
363 c = @env.categories.build(:name => 'my category'); c.save! 346 c = @env.categories.build(:name => 'my category'); c.save!
364 person = create_user('test_user').person 347 person = create_user('test_user').person
365 person.add_category c 348 person.add_category c
366 - ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') 349 + ent = fast_create(Enterprise, :identifier => 'enterprise_1', :name => 'Enterprise one')
367 ent.add_category c 350 ent.add_category c
368 assert_includes c.people, person 351 assert_includes c.people, person
369 assert_not_includes c.people, ent 352 assert_not_includes c.people, ent
370 end 353 end
371 354
372 - should 'report the total items in this category' do  
373 - @category = Category.create!(:name => 'my category', :environment => @env)  
374 - # in category  
375 - person1 = create_user('test1').person; person1.add_category @category; person1.save!  
376 - art1 = person1.articles.build(:name => 'an article to be counted'); art1.add_category @category; art1.save!  
377 - comment1 = art1.comments.build(:title => 'comment to be counted', :body => 'hfyfyh', :author => person1); comment1.save!  
378 - ent1 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :category_ids => [@category.id])  
379 - com1 = Community.create!(:name => 'test3', :identifier => 'test3', :category_ids => [@category.id])  
380 - prod1 = Product.create!(:name => 'test4', :enterprise => ent1)  
381 -  
382 - # not in category  
383 - person2 = create_user('test5').person  
384 - art2 = person2.articles.build(:name => 'an article not to be counted'); art2.save!  
385 - comment2 = art2.comments.build(:title => 'comment not to be counted', :body => 'hfh', :author => person2); comment2.save!  
386 - ent2 = Enterprise.create!(:name => 'test6', :identifier => 'test6')  
387 - com2 = Community.create!(:name => 'test7', :identifier => 'test7')  
388 - prod2 = Product.create!(:name => 'test8', :enterprise => ent2)  
389 -  
390 - assert_equal 6, @category.total_items  
391 - end  
392 -  
393 should 'have image' do 355 should 'have image' do
394 assert_difference Category, :count do 356 assert_difference Category, :count do
395 c = Category.create!(:name => 'test category1', :environment => Environment.default, :image_builder => { 357 c = Category.create!(:name => 'test category1', :environment => Environment.default, :image_builder => {
@@ -400,11 +362,11 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -400,11 +362,11 @@ class CategoryTest &lt; Test::Unit::TestCase
400 end 362 end
401 363
402 should 'display in menu only if have display_menu setted to true' do 364 should 'display in menu only if have display_menu setted to true' do
403 - c = Category.create!(:name => 'test category top', :environment => Environment.default, :display_in_menu => true)  
404 - c1 = Category.create!(:name => 'test category 1', :environment => Environment.default, :display_in_menu => true, :parent => c)  
405 - c11 = Category.create!(:name => 'test category 11', :environment => Environment.default, :display_in_menu => true, :parent => c1)  
406 - c2 = Category.create!(:name => 'test category 2', :environment => Environment.default, :display_in_menu => true, :parent => c)  
407 - c3 = Category.create!(:name => 'test category 3', :environment => Environment.default, :parent => c) 365 + c = fast_create(Category, :display_in_menu => true)
  366 + c1 = fast_create(Category, :display_in_menu => true, :parent_id => c.id)
  367 + c11 = fast_create(Category, :display_in_menu => true, :parent_id => c1.id)
  368 + c2 = fast_create(Category, :display_in_menu => true, :parent_id => c.id)
  369 + c3 = fast_create(Category, :parent_id => c.id)
408 370
409 assert_equivalent [c1, c11, c2], c.children_for_menu 371 assert_equivalent [c1, c11, c2], c.children_for_menu
410 end 372 end
test/unit/environment_finder_test.rb
@@ -2,12 +2,6 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39; @@ -2,12 +2,6 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39;
2 2
3 class EnvironmentFinderTest < ActiveSupport::TestCase 3 class EnvironmentFinderTest < ActiveSupport::TestCase
4 4
5 -# all_fixtures  
6 -  
7 - def setup  
8 - Profile.rebuild_index  
9 - end  
10 -  
11 should 'find articles' do 5 should 'find articles' do
12 person = create_user('teste').person 6 person = create_user('teste').person
13 art = person.articles.build(:name => 'an article to be found'); art.save! 7 art = person.articles.build(:name => 'an article to be found'); art.save!