Commit 9f7261cd52cd058191f150fa39205324d10725ed
1 parent
36701ca8
Exists in
staging
and in
42 other branches
rails3: fix organization tests
Also moving profile extension to make count use distinct by default to ActiveRecord::Calculations module.
Showing
3 changed files
with
16 additions
and
18 deletions
Show diff stats
app/models/profile.rb
| @@ -98,15 +98,6 @@ class Profile < ActiveRecord::Base | @@ -98,15 +98,6 @@ class Profile < ActiveRecord::Base | ||
| 98 | members.count | 98 | members.count |
| 99 | end | 99 | end |
| 100 | 100 | ||
| 101 | - class << self | ||
| 102 | - def count_with_distinct(*args) | ||
| 103 | - options = args.last || {} | ||
| 104 | - count_without_distinct(:id, {:distinct => true}.merge(options)) | ||
| 105 | - end | ||
| 106 | - alias_method_chain :count, :distinct | ||
| 107 | - end | ||
| 108 | - | ||
| 109 | - | ||
| 110 | def members_by_role(role) | 101 | def members_by_role(role) |
| 111 | Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', role.id]) | 102 | Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', role.id]) |
| 112 | end | 103 | end |
lib/noosfero/core_ext/active_record.rb
| @@ -27,3 +27,10 @@ class ActiveRecord::Base | @@ -27,3 +27,10 @@ class ActiveRecord::Base | ||
| 27 | end | 27 | end |
| 28 | 28 | ||
| 29 | end | 29 | end |
| 30 | + | ||
| 31 | +ActiveRecord::Calculations.module_eval do | ||
| 32 | + def count_with_default_distinct(column_name=:id, options={}) | ||
| 33 | + count_without_default_distinct(column_name, {:distinct => true}.merge(options)) | ||
| 34 | + end | ||
| 35 | + alias_method_chain :count, :default_distinct | ||
| 36 | +end |
test/unit/organization_test.rb
| @@ -21,7 +21,7 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -21,7 +21,7 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 21 | :requestor => requestor, | 21 | :requestor => requestor, |
| 22 | :target => org, | 22 | :target => org, |
| 23 | } | 23 | } |
| 24 | - CreateEnterprise.create!(data) | 24 | + create(CreateEnterprise, data) |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | 27 | ||
| @@ -97,7 +97,7 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -97,7 +97,7 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 97 | end | 97 | end |
| 98 | 98 | ||
| 99 | should 'list contact_email plus admin emails as "notification emails"' do | 99 | should 'list contact_email plus admin emails as "notification emails"' do |
| 100 | - o = Organization.new(:contact_email => 'org@email.com') | 100 | + o = build(Organization, :contact_email => 'org@email.com') |
| 101 | admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') | 101 | admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') |
| 102 | admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') | 102 | admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') |
| 103 | o.stubs(:admins).returns([admin1, admin2]) | 103 | o.stubs(:admins).returns([admin1, admin2]) |
| @@ -106,7 +106,7 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -106,7 +106,7 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 106 | end | 106 | end |
| 107 | 107 | ||
| 108 | should 'list only admins if contact_email is nil' do | 108 | should 'list only admins if contact_email is nil' do |
| 109 | - o = Organization.new(:contact_email => nil) | 109 | + o = build(Organization, :contact_email => nil) |
| 110 | admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') | 110 | admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') |
| 111 | admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') | 111 | admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') |
| 112 | o.stubs(:admins).returns([admin1, admin2]) | 112 | o.stubs(:admins).returns([admin1, admin2]) |
| @@ -115,7 +115,7 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -115,7 +115,7 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 115 | end | 115 | end |
| 116 | 116 | ||
| 117 | should 'list only admins if contact_email is a blank string' do | 117 | should 'list only admins if contact_email is a blank string' do |
| 118 | - o = Organization.new(:contact_email => '') | 118 | + o = build(Organization, :contact_email => '') |
| 119 | admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') | 119 | admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') |
| 120 | admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') | 120 | admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') |
| 121 | o.stubs(:admins).returns([admin1, admin2]) | 121 | o.stubs(:admins).returns([admin1, admin2]) |
| @@ -124,13 +124,13 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -124,13 +124,13 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 124 | end | 124 | end |
| 125 | 125 | ||
| 126 | should 'return empty array if contact_email is a blank string and it has no admin' do | 126 | should 'return empty array if contact_email is a blank string and it has no admin' do |
| 127 | - o = Organization.new(:contact_email => '', :environment => Environment.default) | 127 | + o = build(Organization, :contact_email => '', :environment => Environment.default) |
| 128 | assert_equal [], o.notification_emails | 128 | assert_equal [], o.notification_emails |
| 129 | end | 129 | end |
| 130 | 130 | ||
| 131 | should 'list pending enterprise validations' do | 131 | should 'list pending enterprise validations' do |
| 132 | org = Organization.new | 132 | org = Organization.new |
| 133 | - assert_kind_of Array, org.pending_validations | 133 | + assert_kind_of ActiveRecord::Relation, org.pending_validations |
| 134 | end | 134 | end |
| 135 | 135 | ||
| 136 | should 'be able to find a pending validation by its code' do | 136 | should 'be able to find a pending validation by its code' do |
| @@ -148,7 +148,7 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -148,7 +148,7 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 148 | 148 | ||
| 149 | should 'be able to find already processed validations' do | 149 | should 'be able to find already processed validations' do |
| 150 | org = Organization.new | 150 | org = Organization.new |
| 151 | - assert_kind_of Array, org.processed_validations | 151 | + assert_kind_of ActiveRecord::Relation, org.processed_validations |
| 152 | end | 152 | end |
| 153 | 153 | ||
| 154 | should 'be able to find an already processed validation by its code' do | 154 | should 'be able to find an already processed validation by its code' do |
| @@ -160,7 +160,7 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -160,7 +160,7 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 160 | end | 160 | end |
| 161 | 161 | ||
| 162 | should 'have boxes and blocks upon creation' do | 162 | should 'have boxes and blocks upon creation' do |
| 163 | - profile = Organization.create!(:name => 'test org', :identifier => 'testorg') | 163 | + profile = create(Organization, :name => 'test org', :identifier => 'testorg') |
| 164 | 164 | ||
| 165 | assert profile.boxes.size > 0 | 165 | assert profile.boxes.size > 0 |
| 166 | assert profile.blocks.size > 0 | 166 | assert profile.blocks.size > 0 |
| @@ -391,7 +391,7 @@ class OrganizationTest < ActiveSupport::TestCase | @@ -391,7 +391,7 @@ class OrganizationTest < ActiveSupport::TestCase | ||
| 391 | end | 391 | end |
| 392 | 392 | ||
| 393 | should 'validates format of cnpj' do | 393 | should 'validates format of cnpj' do |
| 394 | - organization = Organization.new(:cnpj => '239-234.234') | 394 | + organization = build(Organization, :cnpj => '239-234.234') |
| 395 | organization.valid? | 395 | organization.valid? |
| 396 | assert organization.errors[:cnpj.to_s].present? | 396 | assert organization.errors[:cnpj.to_s].present? |
| 397 | 397 |