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