diff options
Diffstat (limited to 'spec/services/users/update_todo_count_cache_service_spec.rb')
-rw-r--r-- | spec/services/users/update_todo_count_cache_service_spec.rb | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/spec/services/users/update_todo_count_cache_service_spec.rb b/spec/services/users/update_todo_count_cache_service_spec.rb index 3e3618b1291..3d96af928df 100644 --- a/spec/services/users/update_todo_count_cache_service_spec.rb +++ b/spec/services/users/update_todo_count_cache_service_spec.rb @@ -14,13 +14,21 @@ RSpec.describe Users::UpdateTodoCountCacheService do let_it_be(:todo5) { create(:todo, user: user2, state: :pending) } let_it_be(:todo6) { create(:todo, user: user2, state: :pending) } + def execute_all + described_class.new([user1.id, user2.id]).execute + end + + def execute_single + described_class.new([user1.id]).execute + end + it 'updates the todos_counts for users', :use_clean_rails_memory_store_caching do Rails.cache.write(['users', user1.id, 'todos_done_count'], 0) Rails.cache.write(['users', user1.id, 'todos_pending_count'], 0) Rails.cache.write(['users', user2.id, 'todos_done_count'], 0) Rails.cache.write(['users', user2.id, 'todos_pending_count'], 0) - expect { described_class.new([user1, user2]).execute } + expect { execute_all } .to change(user1, :todos_done_count).from(0).to(2) .and change(user1, :todos_pending_count).from(0).to(1) .and change(user2, :todos_done_count).from(0).to(1) @@ -28,7 +36,7 @@ RSpec.describe Users::UpdateTodoCountCacheService do Todo.delete_all - expect { described_class.new([user1, user2]).execute } + expect { execute_all } .to change(user1, :todos_done_count).from(2).to(0) .and change(user1, :todos_pending_count).from(1).to(0) .and change(user2, :todos_done_count).from(1).to(0) @@ -36,26 +44,24 @@ RSpec.describe Users::UpdateTodoCountCacheService do end it 'avoids N+1 queries' do - control_count = ActiveRecord::QueryRecorder.new { described_class.new([user1]).execute }.count + control_count = ActiveRecord::QueryRecorder.new { execute_single }.count - expect { described_class.new([user1, user2]).execute }.not_to exceed_query_limit(control_count) + expect { execute_all }.not_to exceed_query_limit(control_count) end it 'executes one query per batch of users' do stub_const("#{described_class}::QUERY_BATCH_SIZE", 1) - expect(ActiveRecord::QueryRecorder.new { described_class.new([user1]).execute }.count).to eq(1) - expect(ActiveRecord::QueryRecorder.new { described_class.new([user1, user2]).execute }.count).to eq(2) + expect(ActiveRecord::QueryRecorder.new { execute_single }.count).to eq(1) + expect(ActiveRecord::QueryRecorder.new { execute_all }.count).to eq(2) end - it 'sets the cache expire time to the users count_cache_validity_period' do - allow(user1).to receive(:count_cache_validity_period).and_return(1.minute) - allow(user2).to receive(:count_cache_validity_period).and_return(1.hour) - - expect(Rails.cache).to receive(:write).with(['users', user1.id, anything], anything, expires_in: 1.minute).twice - expect(Rails.cache).to receive(:write).with(['users', user2.id, anything], anything, expires_in: 1.hour).twice + it 'sets the correct cache expire time' do + expect(Rails.cache).to receive(:write) + .with(['users', user1.id, anything], anything, expires_in: User::COUNT_CACHE_VALIDITY_PERIOD) + .twice - described_class.new([user1, user2]).execute + execute_single end end end |