From 8bef50d2a64f3cfeefdc33bd63180dfa194f1648 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 19 Mar 2014 16:33:17 +0000 Subject: [PATCH] search-optimizations: fix broken tests --- app/models/profile.rb | 4 ++-- test/functional/invite_controller_test.rb | 3 ++- test/functional/profile_controller_test.rb | 33 ++++++++++++++++++++++----------- test/functional/search_controller_test.rb | 6 +++--- test/unit/action_tracker_ext_test.rb | 4 ++++ test/unit/activities_counter_cache_job_test.rb | 2 ++ test/unit/article_test.rb | 6 +++--- test/unit/organization_mailing_test.rb | 3 +++ test/unit/person_test.rb | 28 ++++++++++++++-------------- test/unit/role_assignment_ext_test.rb | 1 + test/unit/uploaded_file_test.rb | 7 ++++--- 11 files changed, 60 insertions(+), 37 deletions(-) diff --git a/app/models/profile.rb b/app/models/profile.rb index 1366523..56f54bc 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -608,10 +608,10 @@ private :generate_url, :url_options # Adds a person as member of this Profile. def add_member(person) if self.has_members? - if self.closed? && members_count > 0 + if self.closed? && members.count > 0 AddMember.create!(:person => person, :organization => self) unless self.already_request_membership?(person) else - self.affiliate(person, Profile::Roles.admin(environment.id)) if members_count == 0 + self.affiliate(person, Profile::Roles.admin(environment.id)) if members.count == 0 self.affiliate(person, Profile::Roles.member(environment.id)) end else diff --git a/test/functional/invite_controller_test.rb b/test/functional/invite_controller_test.rb index 41104bd..ed2b5e0 100644 --- a/test/functional/invite_controller_test.rb +++ b/test/functional/invite_controller_test.rb @@ -230,7 +230,8 @@ class InviteControllerTest < ActionController::TestCase contact_list = ContactList.create post :select_friends, :profile => profile.identifier, :manual_import_addresses => "#{friend.name} <#{friend.email}>", :import_from => "manual", :mail_template => "click: ", :contact_list => contact_list.id - assert_equal 'pt', Delayed::Job.first.payload_object.locale + job = Delayed::Job.where("handler LIKE '%InvitationJob%'").first + assert_equal 'pt', job.payload_object.locale end private diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 25ca6b3..e025e66 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -766,23 +766,28 @@ class ProfileControllerTest < ActionController::TestCase end should 'see all the activities in the current profile network' do - p1= Person.first + p1= fast_create(Person) p2= fast_create(Person) assert !p1.is_a_friend?(p2) + p3= fast_create(Person) p3.add_friend(p1) assert p3.is_a_friend?(p1) - ActionTracker::Record.destroy_all + + ActionTracker::Record.delete_all + + UserStampSweeper.any_instance.stubs(:current_user).returns(p1) Scrap.create!(defaults_for_scrap(:sender => p1, :receiver => p1)) a1 = ActionTracker::Record.last + UserStampSweeper.any_instance.stubs(:current_user).returns(p2) Scrap.create!(defaults_for_scrap(:sender => p2, :receiver => p3)) a2 = ActionTracker::Record.last + UserStampSweeper.any_instance.stubs(:current_user).returns(p3) Scrap.create!(defaults_for_scrap(:sender => p3, :receiver => p1)) a3 = ActionTracker::Record.last - @controller.stubs(:logged_in?).returns(true) user = mock() user.stubs(:person).returns(p3) @@ -792,24 +797,29 @@ class ProfileControllerTest < ActionController::TestCase process_delayed_job_queue get :index, :profile => p1.identifier - assert_not_nil assigns(:network_activities) - assert_equal [], [a1,a3] - assigns(:network_activities) - assert_equal assigns(:network_activities) - [a1, a3], [] + + assert_equivalent [a1,a3].map(&:id), assigns(:network_activities).map(&:id) end should 'the network activity be visible only to profile followers' do - p1= Person.first + p1= fast_create(Person) p2= fast_create(Person) assert !p1.is_a_friend?(p2) + p3= fast_create(Person) p3.add_friend(p1) assert p3.is_a_friend?(p1) - ActionTracker::Record.destroy_all + + ActionTracker::Record.delete_all + + UserStampSweeper.any_instance.stubs(:current_user).returns(p1) Scrap.create!(defaults_for_scrap(:sender => p1, :receiver => p1)) a1 = ActionTracker::Record.last + UserStampSweeper.any_instance.stubs(:current_user).returns(p2) Scrap.create!(defaults_for_scrap(:sender => p2, :receiver => p3)) a2 = ActionTracker::Record.last + UserStampSweeper.any_instance.stubs(:current_user).returns(p3) Scrap.create!(defaults_for_scrap(:sender => p3, :receiver => p1)) a3 = ActionTracker::Record.last @@ -819,8 +829,9 @@ class ProfileControllerTest < ActionController::TestCase user.stubs(:person).returns(p2) user.stubs(:login).returns('some') @controller.stubs(:current_user).returns(user) + get :index, :profile => p1.identifier - assert_equal [], assigns(:network_activities) + assert assigns(:network_activities).blank? user = mock() user.stubs(:person).returns(p3) @@ -828,9 +839,9 @@ class ProfileControllerTest < ActionController::TestCase @controller.stubs(:current_user).returns(user) Person.any_instance.stubs(:follows?).returns(true) process_delayed_job_queue + get :index, :profile => p3.identifier - assert_equal [], [a1,a3] - assigns(:network_activities) - assert_equal assigns(:network_activities) - [a1, a3], [] + assert_equivalent [a1,a3], assigns(:network_activities) end should 'the network activity be paginated' do diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 137be0e..b630f84 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -549,9 +549,9 @@ class SearchControllerTest < ActionController::TestCase c2 = create(Community, :name => 'Testing community 2') c3 = create(Community, :name => 'Testing community 3') ActionTracker::Record.delete_all - fast_create(ActionTracker::Record, :target_id => c1, :user_type => 'Profile', :user_id => person, :created_at => Time.now) - fast_create(ActionTracker::Record, :target_id => c2, :user_type => 'Profile', :user_id => person, :created_at => Time.now) - fast_create(ActionTracker::Record, :target_id => c2, :user_type => 'Profile', :user_id => person, :created_at => Time.now) + ActionTracker::Record.create!(:target => c1, :user => person, :created_at => Time.now, :verb => 'leave_scrap') + ActionTracker::Record.create!(:target => c2, :user => person, :created_at => Time.now, :verb => 'leave_scrap') + ActionTracker::Record.create!(:target => c2, :user => person, :created_at => Time.now, :verb => 'leave_scrap') get :communities, :filter => 'more_active' assert_equal [c2,c1,c3] , assigns(:searches)[:communities][:results] end diff --git a/test/unit/action_tracker_ext_test.rb b/test/unit/action_tracker_ext_test.rb index b7ae1d8..019e2ef 100644 --- a/test/unit/action_tracker_ext_test.rb +++ b/test/unit/action_tracker_ext_test.rb @@ -12,6 +12,7 @@ class ActionTrackerExtTest < ActiveSupport::TestCase should 'decrease person activities_count on activity removal' do person = fast_create(Person) record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) + person.reload assert_difference person, :activities_count, -1 do record.destroy person.reload @@ -23,6 +24,7 @@ class ActionTrackerExtTest < ActiveSupport::TestCase record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => fast_create(Profile) record.created_at = record.created_at - ActionTracker::Record::RECENT_DELAY.days - 1.day record.save! + person.reload assert_no_difference person, :activities_count do record.destroy person.reload @@ -42,6 +44,7 @@ class ActionTrackerExtTest < ActiveSupport::TestCase person = fast_create(Person) organization = fast_create(Organization) record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization + organization.reload assert_difference organization, :activities_count, -1 do record.destroy organization.reload @@ -52,6 +55,7 @@ class ActionTrackerExtTest < ActiveSupport::TestCase person = fast_create(Person) organization = fast_create(Organization) record = ActionTracker::Record.create! :verb => :leave_scrap, :user => person, :target => organization, :created_at => (ActionTracker::Record::RECENT_DELAY + 1).days.ago + organization.reload assert_no_difference organization, :activities_count do record.destroy organization.reload diff --git a/test/unit/activities_counter_cache_job_test.rb b/test/unit/activities_counter_cache_job_test.rb index 0a8de19..9f1881d 100644 --- a/test/unit/activities_counter_cache_job_test.rb +++ b/test/unit/activities_counter_cache_job_test.rb @@ -6,6 +6,7 @@ class ActivitiesCounterCacheJobTest < ActiveSupport::TestCase person = create_user('person').person ActionTracker::Record.create!(:user => person, :verb => 'create_article') ActionTracker::Record.create!(:user => person, :verb => 'create_article') + person.reload assert_equal 2, person.activities_count person.activities_count = 0 @@ -21,6 +22,7 @@ class ActivitiesCounterCacheJobTest < ActiveSupport::TestCase organization = Organization.create!(:name => 'Organization1', :identifier => 'organization1') ActionTracker::Record.create!(:user => person, :verb => 'create_article', :target => organization) ActionTracker::Record.create!(:user => person, :verb => 'create_article', :target => organization) + organization.reload assert_equal 2, organization.activities_count organization.activities_count = 0 diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 8eb8246..cfa2931 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1625,10 +1625,10 @@ class ArticleTest < ActiveSupport::TestCase should 'not allow all community members to edit by default' do community = fast_create(Community) - admin = create_user('community-admin').person - member = create_user.person - + admin = fast_create(Person) + member = fast_create(Person) community.add_admin(admin) + community.reload community.add_member(member) a = Article.new(:profile => community) diff --git a/test/unit/organization_mailing_test.rb b/test/unit/organization_mailing_test.rb index 33ad4b0..07d50e6 100644 --- a/test/unit/organization_mailing_test.rb +++ b/test/unit/organization_mailing_test.rb @@ -62,18 +62,21 @@ class OrganizationMailingTest < ActiveSupport::TestCase should 'deliver mailing to each member after create' do mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + community.reload process_delayed_job_queue assert_equal 2, ActionMailer::Base.deliveries.count end should 'deliver mailing when there are many mailings created' do 50.times { OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) } + community.reload process_delayed_job_queue assert_equal 50*community.members_count, ActionMailer::Base.deliveries.count end should 'create mailing sent to each recipient after delivering mailing' do mailing = OrganizationMailing.create(:source => community, :subject => 'Hello', :body => 'We have some news', :person => person) + community.reload assert_difference MailingSent, :count, 2 do process_delayed_job_queue end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index fa24557..18a9202 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -407,11 +407,11 @@ class PersonTest < ActiveSupport::TestCase admin = fast_create(Person) community.add_member(admin) member = fast_create(Person) + community.reload community.add_member(member) community.tasks << Task.new - - assert_not_includes Person.with_pending_tasks, person + assert_not_includes Person.with_pending_tasks, member end should 'person has organization pending tasks' do @@ -641,17 +641,16 @@ class PersonTest < ActiveSupport::TestCase end should 'find more popular people' do + extend CacheCounterHelper + Person.delete_all p1 = fast_create(Person) p2 = fast_create(Person) p3 = fast_create(Person) - p1.friends_count = 1 - p2.friends_count = 2 - p3.friends_count = 3 - p1.save! - p2.save! - p3.save! + update_cache_counter(:friends_count, p1, 1) + update_cache_counter(:friends_count, p2, 2) + update_cache_counter(:friends_count, p3, 3) assert_order [p3, p2, p1], Person.more_popular end @@ -893,6 +892,7 @@ class PersonTest < ActiveSupport::TestCase p1 = fast_create(Person) p2 = fast_create(Person) p3 = fast_create(Person) + p4 = fast_create(Person) community.add_member(p1) assert p1.is_member_of?(community) @@ -1136,12 +1136,12 @@ class PersonTest < ActiveSupport::TestCase p3 = fast_create(Person) ActionTracker::Record.destroy_all - fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p1) - fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2) - fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2) - fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p3) - fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p3) - fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p3) + ActionTracker::Record.create!(:user => p1, :verb => 'leave_scrap') + ActionTracker::Record.create!(:user => p2, :verb => 'leave_scrap') + ActionTracker::Record.create!(:user => p2, :verb => 'leave_scrap') + ActionTracker::Record.create!(:user => p3, :verb => 'leave_scrap') + ActionTracker::Record.create!(:user => p3, :verb => 'leave_scrap') + ActionTracker::Record.create!(:user => p3, :verb => 'leave_scrap') assert_order [p3,p2,p1] , Person.more_active end diff --git a/test/unit/role_assignment_ext_test.rb b/test/unit/role_assignment_ext_test.rb index c4a182c..d329747 100644 --- a/test/unit/role_assignment_ext_test.rb +++ b/test/unit/role_assignment_ext_test.rb @@ -20,6 +20,7 @@ class RoleAssignmentExtTest < ActiveSupport::TestCase organization = Organization.create!(:name => 'Organization', :identifier => 'organization') RoleAssignment.create!(:accessor => member, :resource => organization, :role => role1) RoleAssignment.create!(:accessor => member, :resource => organization, :role => role2) + organization.reload assert_difference organization, :members_count, -1 do organization.role_assignments.destroy_all organization.reload diff --git a/test/unit/uploaded_file_test.rb b/test/unit/uploaded_file_test.rb index 13a836f..6b27a90 100644 --- a/test/unit/uploaded_file_test.rb +++ b/test/unit/uploaded_file_test.rb @@ -327,12 +327,13 @@ class UploadedFileTest < ActiveSupport::TestCase should 'group trackers activity of image\'s upload' do gallery = fast_create(Gallery, :profile_id => profile.id) - + count = ActionTracker::Record.find_all_by_verb('upload_image').count image1 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => gallery, :profile => profile) - assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count + count += 1 + assert_equal count, ActionTracker::Record.find_all_by_verb('upload_image').count image2 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/other-pic.jpg', 'image/jpg'), :parent => gallery, :profile => profile) - assert_equal 1, ActionTracker::Record.find_all_by_verb('upload_image').count + assert_equal count, ActionTracker::Record.find_all_by_verb('upload_image').count end { -- libgit2 0.21.2