Commit fc50b619beb6424d42cb92fa20d945d82e0e2208

Authored by Antonio Terceiro
1 parent 904c1eff

Speeding up EnvironmentTest

app/models/environment.rb
@@ -22,11 +22,12 @@ class Environment < ActiveRecord::Base @@ -22,11 +22,12 @@ class Environment < ActiveRecord::Base
22 end 22 end
23 end 23 end
24 24
25 - after_create do |e| 25 + after_create :create_roles
  26 + def create_roles
26 Role.create!( 27 Role.create!(
27 :key => 'environment_administrator', 28 :key => 'environment_administrator',
28 :name => N_('Environment Administrator'), 29 :name => N_('Environment Administrator'),
29 - :environment => e, 30 + :environment => self,
30 :permissions => [ 31 :permissions => [
31 'view_environment_admin_panel', 32 'view_environment_admin_panel',
32 'edit_environment_features', 33 'edit_environment_features',
@@ -49,7 +50,7 @@ class Environment < ActiveRecord::Base @@ -49,7 +50,7 @@ class Environment < ActiveRecord::Base
49 Role.create!( 50 Role.create!(
50 :key => 'profile_admin', 51 :key => 'profile_admin',
51 :name => N_('Profile Administrator'), 52 :name => N_('Profile Administrator'),
52 - :environment => e, 53 + :environment => self,
53 :permissions => [ 54 :permissions => [
54 'edit_profile', 55 'edit_profile',
55 'destroy_profile', 56 'destroy_profile',
@@ -64,7 +65,7 @@ class Environment < ActiveRecord::Base @@ -64,7 +65,7 @@ class Environment < ActiveRecord::Base
64 Role.create!( 65 Role.create!(
65 :key => "profile_member", 66 :key => "profile_member",
66 :name => N_('Member'), 67 :name => N_('Member'),
67 - :environment => e, 68 + :environment => self,
68 :permissions => [ 69 :permissions => [
69 'edit_profile', 70 'edit_profile',
70 'post_content', 71 'post_content',
@@ -75,7 +76,7 @@ class Environment < ActiveRecord::Base @@ -75,7 +76,7 @@ class Environment < ActiveRecord::Base
75 Role.create!( 76 Role.create!(
76 :key => 'profile_moderator', 77 :key => 'profile_moderator',
77 :name => N_('Moderator'), 78 :name => N_('Moderator'),
78 - :environment => e, 79 + :environment => self,
79 :permissions => [ 80 :permissions => [
80 'manage_memberships', 81 'manage_memberships',
81 'edit_profile_design', 82 'edit_profile_design',
test/unit/environment_test.rb
@@ -95,7 +95,7 @@ class EnvironmentTest < Test::Unit::TestCase @@ -95,7 +95,7 @@ class EnvironmentTest < Test::Unit::TestCase
95 end 95 end
96 96
97 def test_should_list_top_level_categories 97 def test_should_list_top_level_categories
98 - env = Environment.create!(:name => 'a test environment') 98 + env = fast_create(Environment)
99 cat1 = Category.create!(:name => 'first category', :environment_id => env.id) 99 cat1 = Category.create!(:name => 'first category', :environment_id => env.id)
100 cat2 = Category.create!(:name => 'second category', :environment_id => env.id) 100 cat2 = Category.create!(:name => 'second category', :environment_id => env.id)
101 subcat = Category.create!(:name => 'child category', :environment_id => env.id, :parent_id => cat2.id) 101 subcat = Category.create!(:name => 'child category', :environment_id => env.id, :parent_id => cat2.id)
@@ -108,7 +108,7 @@ class EnvironmentTest < Test::Unit::TestCase @@ -108,7 +108,7 @@ class EnvironmentTest < Test::Unit::TestCase
108 end 108 end
109 109
110 def test_should_list_all_categories 110 def test_should_list_all_categories
111 - env = Environment.create!(:name => 'a test environment') 111 + env = fast_create(Environment)
112 cat1 = Category.create!(:name => 'first category', :environment_id => env.id) 112 cat1 = Category.create!(:name => 'first category', :environment_id => env.id)
113 cat2 = Category.create!(:name => 'second category', :environment_id => env.id) 113 cat2 = Category.create!(:name => 'second category', :environment_id => env.id)
114 subcat = Category.create!(:name => 'child category', :environment_id => env.id, :parent_id => cat2.id) 114 subcat = Category.create!(:name => 'child category', :environment_id => env.id, :parent_id => cat2.id)
@@ -121,7 +121,7 @@ class EnvironmentTest < Test::Unit::TestCase @@ -121,7 +121,7 @@ class EnvironmentTest < Test::Unit::TestCase
121 end 121 end
122 122
123 should 'list displayable categories' do 123 should 'list displayable categories' do
124 - env = Environment.create!(:name => 'a test environment') 124 + env = fast_create(Environment)
125 cat1 = env.categories.create(:name => 'category one', :display_color => 1) 125 cat1 = env.categories.create(:name => 'category one', :display_color => 1)
126 assert ! cat1.new_record? 126 assert ! cat1.new_record?
127 127
@@ -138,7 +138,7 @@ class EnvironmentTest < Test::Unit::TestCase @@ -138,7 +138,7 @@ class EnvironmentTest < Test::Unit::TestCase
138 end 138 end
139 139
140 should 'have regions' do 140 should 'have regions' do
141 - env = Environment.create!(:name => 'a test environment') 141 + env = fast_create(Environment)
142 assert_kind_of Array, env.regions 142 assert_kind_of Array, env.regions
143 assert_raise ActiveRecord::AssociationTypeMismatch do 143 assert_raise ActiveRecord::AssociationTypeMismatch do
144 env.regions << 1 144 env.regions << 1
@@ -162,37 +162,38 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -162,37 +162,38 @@ class EnvironmentTest &lt; Test::Unit::TestCase
162 end 162 end
163 163
164 should 'provide a default hostname' do 164 should 'provide a default hostname' do
165 - env = Environment.create!(:name => 'test environment') 165 + env = fast_create(Environment)
166 env.domains << Domain.create(:name => 'example.com', :is_default => true) 166 env.domains << Domain.create(:name => 'example.com', :is_default => true)
167 assert_equal 'example.com', env.default_hostname 167 assert_equal 'example.com', env.default_hostname
168 end 168 end
169 169
170 should 'default to localhost as hostname' do 170 should 'default to localhost as hostname' do
171 - env = Environment.create!(:name => 'test environment') 171 + env = Environment.new
172 assert_equal 'localhost', env.default_hostname 172 assert_equal 'localhost', env.default_hostname
173 end 173 end
174 174
175 should 'add www when told to force www' do 175 should 'add www when told to force www' do
176 - env = Environment.create!(:name => 'test environment', :force_www => true) 176 + env = fast_create(Environment); env.force_www = true; env.save!
  177 +
177 env.domains << Domain.create(:name => 'example.com', :is_default => true) 178 env.domains << Domain.create(:name => 'example.com', :is_default => true)
178 assert_equal 'www.example.com', env.default_hostname 179 assert_equal 'www.example.com', env.default_hostname
179 end 180 end
180 181
181 should 'not add www when requesting domain for email address' do 182 should 'not add www when requesting domain for email address' do
182 - env = Environment.create!(:name => 'test environment', :force_www => true) 183 + env = fast_create(Environment)
183 env.domains << Domain.create(:name => 'example.com', :is_default => true) 184 env.domains << Domain.create(:name => 'example.com', :is_default => true)
184 assert_equal 'example.com', env.default_hostname(true) 185 assert_equal 'example.com', env.default_hostname(true)
185 end 186 end
186 187
187 should 'use default domain when there is more than one' do 188 should 'use default domain when there is more than one' do
188 - env = Environment.create!(:name => 'test environment') 189 + env = fast_create(Environment)
189 env.domains << Domain.create(:name => 'example.com', :is_default => false) 190 env.domains << Domain.create(:name => 'example.com', :is_default => false)
190 env.domains << Domain.create(:name => 'default.com', :is_default => true) 191 env.domains << Domain.create(:name => 'default.com', :is_default => true)
191 assert_equal 'default.com', env.default_hostname 192 assert_equal 'default.com', env.default_hostname
192 end 193 end
193 194
194 should 'use first domain when there is no default' do 195 should 'use first domain when there is no default' do
195 - env = Environment.create!(:name => 'test environment') 196 + env = fast_create(Environment)
196 env.domains << Domain.create(:name => 'domain1.com', :is_default => false) 197 env.domains << Domain.create(:name => 'domain1.com', :is_default => false)
197 env.domains << Domain.create(:name => 'domain2.com', :is_default => false) 198 env.domains << Domain.create(:name => 'domain2.com', :is_default => false)
198 assert_equal 'domain1.com', env.default_hostname 199 assert_equal 'domain1.com', env.default_hostname
@@ -255,6 +256,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -255,6 +256,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase
255 end 256 end
256 257
257 should 'remove boxes and blocks when removing environment' do 258 should 'remove boxes and blocks when removing environment' do
  259 + Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive
258 env = Environment.create!(:name => 'test environment') 260 env = Environment.create!(:name => 'test environment')
259 261
260 env_boxes = env.boxes.size 262 env_boxes = env.boxes.size
@@ -272,18 +274,20 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -272,18 +274,20 @@ class EnvironmentTest &lt; Test::Unit::TestCase
272 end 274 end
273 275
274 should 'have boxes and blocks upon creation' do 276 should 'have boxes and blocks upon creation' do
  277 + Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive
275 environment = Environment.create!(:name => 'a test environment') 278 environment = Environment.create!(:name => 'a test environment')
276 assert environment.boxes.size > 0 279 assert environment.boxes.size > 0
277 assert environment.blocks.size > 0 280 assert environment.blocks.size > 0
278 end 281 end
279 282
280 should 'have at least one MainBlock upon creation' do 283 should 'have at least one MainBlock upon creation' do
  284 + Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive
281 environment = Environment.create!(:name => 'a test environment') 285 environment = Environment.create!(:name => 'a test environment')
282 assert(environment.blocks.any? { |block| block.kind_of? MainBlock }) 286 assert(environment.blocks.any? { |block| block.kind_of? MainBlock })
283 end 287 end
284 288
285 should 'provide recent_documents' do 289 should 'provide recent_documents' do
286 - environment = Environment.create(:name => 'a test environment') 290 + environment = fast_create(Environment)
287 291
288 p1 = environment.profiles.build(:identifier => 'testprofile1', :name => 'test profile 1'); p1.save! 292 p1 = environment.profiles.build(:identifier => 'testprofile1', :name => 'test profile 1'); p1.save!
289 p2 = environment.profiles.build(:identifier => 'testprofile2', :name => 'test profile 2'); p2.save! 293 p2 = environment.profiles.build(:identifier => 'testprofile2', :name => 'test profile 2'); p2.save!
@@ -327,7 +331,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -327,7 +331,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase
327 end 331 end
328 332
329 should 'be able to add admins easily' do 333 should 'be able to add admins easily' do
330 - env = Environment.create!(:name => 'test') 334 + Environment.any_instance.stubs(:create_templates) # avoid creating templates, it's expensive
  335 + env = Environment.create!(:name => 'bli')
331 user = create_user('testuser').person 336 user = create_user('testuser').person
332 env.add_admin(user) 337 env.add_admin(user)
333 env.reload 338 env.reload
@@ -337,7 +342,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -337,7 +342,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase
337 342
338 should 'have products through enterprises' do 343 should 'have products through enterprises' do
339 env = Environment.default 344 env = Environment.default
340 - e1 = Enterprise.create!(:name => 'test_ent1', :identifier => 'test_ent1') 345 + e1 = fast_create(Enterprise)
341 p1 = e1.products.create!(:name => 'test_prod1') 346 p1 = e1.products.create!(:name => 'test_prod1')
342 347
343 assert_includes env.products, p1 348 assert_includes env.products, p1
@@ -345,24 +350,24 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -345,24 +350,24 @@ class EnvironmentTest &lt; Test::Unit::TestCase
345 350
346 should 'not have person through communities' do 351 should 'not have person through communities' do
347 env = Environment.default 352 env = Environment.default
348 - com = Community.create!(:identifier => 'community_1', :name => 'Community one')  
349 - person = create_user('test_user').person 353 + com = fast_create(Community)
  354 + person = fast_create(Person)
350 assert_includes env.communities, com 355 assert_includes env.communities, com
351 assert_not_includes env.communities, person 356 assert_not_includes env.communities, person
352 end 357 end
353 358
354 should 'not have person through enterprises' do 359 should 'not have person through enterprises' do
355 env = Environment.default 360 env = Environment.default
356 - ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')  
357 - person = create_user('test_user').person 361 + ent = fast_create(Enterprise)
  362 + person = fast_create(Person)
358 assert_includes env.enterprises, ent 363 assert_includes env.enterprises, ent
359 assert_not_includes env.enterprises, person 364 assert_not_includes env.enterprises, person
360 end 365 end
361 366
362 should 'not have enterprises through people' do 367 should 'not have enterprises through people' do
363 env = Environment.default 368 env = Environment.default
364 - person = create_user('test_user').person  
365 - ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') 369 + person = fast_create(Person)
  370 + ent = fast_create(Enterprise)
366 assert_includes env.people, person 371 assert_includes env.people, person
367 assert_not_includes env.people, ent 372 assert_not_includes env.people, ent
368 end 373 end
@@ -396,7 +401,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -396,7 +401,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase
396 end 401 end
397 402
398 should 'find by contents from articles' do 403 should 'find by contents from articles' do
399 - environment = Environment.create(:name => 'a test environment') 404 + environment = fast_create(Environment)
400 assert_nothing_raised do 405 assert_nothing_raised do
401 environment.articles.find_by_contents('') 406 environment.articles.find_by_contents('')
402 # FIXME 407 # FIXME
@@ -449,42 +454,31 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -449,42 +454,31 @@ class EnvironmentTest &lt; Test::Unit::TestCase
449 e = Environment.create!(:name => 'test_env') 454 e = Environment.create!(:name => 'test_env')
450 e.reload 455 e.reload
451 456
  457 + # the templates must be created
452 assert_kind_of Enterprise, e.enterprise_template 458 assert_kind_of Enterprise, e.enterprise_template
453 assert_kind_of Community, e.community_template 459 assert_kind_of Community, e.community_template
454 assert_kind_of Person, e.person_template 460 assert_kind_of Person, e.person_template
455 - end  
456 -  
457 - should 'have private templates' do  
458 - e = Environment.create!(:name => 'test_env')  
459 - e.reload  
460 461
  462 + # the templates must be private
461 assert !e.enterprise_template.public? 463 assert !e.enterprise_template.public?
462 assert !e.community_template.public? 464 assert !e.community_template.public?
463 assert !e.person_template.public? 465 assert !e.person_template.public?
464 end 466 end
465 467
466 - should 'set a template in community_template' do  
467 - e = Environment.create!(:name => 'test_env')  
468 - template = Community.create!(:name => 'Community template 2', :identifier => e.name.to_slug + 'community_template_2', :environment => e, :public_profile => false)  
469 - e.community_template = template 468 + should 'set templates' do
  469 + e = fast_create(Environment)
470 470
471 - assert_equal template, e.community_template  
472 - end  
473 -  
474 - should 'set a template in person_template' do  
475 - e = Environment.create!(:name => 'test_env')  
476 - template = create_user('person_template_2').person  
477 - e.person_template = template  
478 -  
479 - assert_equal template, e.person_template  
480 - end 471 + comm = fast_create(Community)
  472 + e.community_template = comm
  473 + assert_equal comm, e.community_template
481 474
482 - should 'set a template in enterprise_template' do  
483 - e = Environment.create!(:name => 'test_env')  
484 - template = Enterprise.create!(:name => 'Enterprise template 2', :identifier => e.name.to_slug + 'enterprise_template', :environment => e, :public_profile => false)  
485 - e.enterprise_template = template 475 + person = fast_create(Person)
  476 + e.person_template = person
  477 + assert_equal person, e.person_template
486 478
487 - assert_equal template, e.enterprise_template 479 + enterprise = fast_create(Enterprise)
  480 + e.enterprise_template = enterprise
  481 + assert_equal enterprise, e.enterprise_template
488 end 482 end
489 483
490 should 'not enable ssl by default' do 484 should 'not enable ssl by default' do
@@ -580,7 +574,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -580,7 +574,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase
580 end 574 end
581 575
582 should 'provide a default invitation message' do 576 should 'provide a default invitation message' do
583 - env = Environment.create!(:name => 'test environment') 577 + env = Environment.new
584 message = [ 578 message = [
585 'Hello <friend>,', 579 'Hello <friend>,',
586 "<user> is inviting you to participate on #{env.name}.", 580 "<user> is inviting you to participate on #{env.name}.",
@@ -713,9 +707,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -713,9 +707,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase
713 707
714 should 'have a portal community' do 708 should 'have a portal community' do
715 e = Environment.default 709 e = Environment.default
716 - c = Community.create!(:name => 'portal community') 710 + c = fast_create(Community)
717 711
718 - assert_respond_to e, :portal_community  
719 e.portal_community = c; e.save! 712 e.portal_community = c; e.save!
720 e.reload 713 e.reload
721 714
@@ -725,8 +718,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -725,8 +718,7 @@ class EnvironmentTest &lt; Test::Unit::TestCase
725 should 'have a set of portal folders' do 718 should 'have a set of portal folders' do
726 e = Environment.default 719 e = Environment.default
727 720
728 - assert_respond_to e, :portal_folders  
729 - c = e.portal_community = Community.create!(:name => 'portal community') 721 + c = e.portal_community = fast_create(Community)
730 news_folder = Folder.create!(:name => 'news folder', :profile => c) 722 news_folder = Folder.create!(:name => 'news folder', :profile => c)
731 723
732 e.portal_folders = [news_folder] 724 e.portal_folders = [news_folder]
@@ -751,18 +743,18 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -751,18 +743,18 @@ class EnvironmentTest &lt; Test::Unit::TestCase
751 end 743 end
752 744
753 should 'have roles with names independent of other environments' do 745 should 'have roles with names independent of other environments' do
754 - e1 = Environment.create!(:name => 'a test environment') 746 + e1 = fast_create(Environment)
755 role1 = Role.create!(:name => 'test_role', :environment => e1) 747 role1 = Role.create!(:name => 'test_role', :environment => e1)
756 - e2 = Environment.create!(:name => 'another test environment') 748 + e2 = fast_create(Environment)
757 role2 = Role.new(:name => 'test_role', :environment => e2) 749 role2 = Role.new(:name => 'test_role', :environment => e2)
758 750
759 assert_valid role2 751 assert_valid role2
760 end 752 end
761 753
762 should 'have roles with keys independent of other environments' do 754 should 'have roles with keys independent of other environments' do
763 - e1 = Environment.create!(:name => 'a test environment') 755 + e1 = fast_create(Environment)
764 role1 = Role.create!(:name => 'test_role', :environment => e1, :key => 'a_member') 756 role1 = Role.create!(:name => 'test_role', :environment => e1, :key => 'a_member')
765 - e2 = Environment.create!(:name => 'another test environment') 757 + e2 = fast_create(Environment)
766 role2 = Role.new(:name => 'test_role', :environment => e2, :key => 'a_member') 758 role2 = Role.new(:name => 'test_role', :environment => e2, :key => 'a_member')
767 759
768 assert_valid role2 760 assert_valid role2
@@ -806,16 +798,16 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -806,16 +798,16 @@ class EnvironmentTest &lt; Test::Unit::TestCase
806 end 798 end
807 799
808 should 'list tags with their counts' do 800 should 'list tags with their counts' do
809 - user = create_user('testinguser').person  
810 - user.articles.build(:name => 'article 1', :tag_list => 'first-tag').save!  
811 - user.articles.build(:name => 'article 2', :tag_list => 'first-tag, second-tag').save!  
812 - user.articles.build(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag').save! 801 + person = fast_create(Person)
  802 + person.articles.build(:name => 'article 1', :tag_list => 'first-tag').save!
  803 + person.articles.build(:name => 'article 2', :tag_list => 'first-tag, second-tag').save!
  804 + person.articles.build(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag').save!
813 805
814 assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, Environment.default.tag_counts) 806 assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, Environment.default.tag_counts)
815 end 807 end
816 808
817 should 'not list tags count from other environment' do 809 should 'not list tags count from other environment' do
818 - e = Environment.create!(:name => 'test_env') 810 + e = fast_create(Environment)
819 user = create_user('testinguser', :environment => e).person 811 user = create_user('testinguser', :environment => e).person
820 user.articles.build(:name => 'article 1', :tag_list => 'first-tag').save! 812 user.articles.build(:name => 'article 1', :tag_list => 'first-tag').save!
821 813