summaryrefslogtreecommitdiff
path: root/spec/services/todo_service_spec.rb
diff options
context:
space:
mode:
authorPaco Guzman <pacoguzmanp@gmail.com>2016-06-02 15:46:58 +0200
committerPaco Guzman <pacoguzmanp@gmail.com>2016-06-17 19:04:36 +0200
commitf6bfa46daae3a00ca6f74abb6e6eddc9eac96197 (patch)
treee80a1c3cde48387c034dbec51636b8d2c5277873 /spec/services/todo_service_spec.rb
parentfcd9f90641d5ee59cc84d8388b7cc372370ac25a (diff)
downloadgitlab-ce-f6bfa46daae3a00ca6f74abb6e6eddc9eac96197.tar.gz
Cache todo counters (pending/done)18034-cache-todo-counter
- As todos are created/updated inside the TodoService we repopulate the cache just there for both pending/done todos - Todos as mark as done from the TodosController we update cache there too - All the added methods are kept in the User class for cohesion
Diffstat (limited to 'spec/services/todo_service_spec.rb')
-rw-r--r--spec/services/todo_service_spec.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/spec/services/todo_service_spec.rb b/spec/services/todo_service_spec.rb
index a09413692d9..b4522536724 100644
--- a/spec/services/todo_service_spec.rb
+++ b/spec/services/todo_service_spec.rb
@@ -173,6 +173,48 @@ describe TodoService, services: true do
expect(first_todo.reload).to be_done
expect(second_todo.reload).to be_done
end
+
+ describe 'cached counts' do
+ it 'updates when todos change' do
+ create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
+
+ expect(john_doe.todos_done_count).to eq(0)
+ expect(john_doe.todos_pending_count).to eq(1)
+ expect(john_doe).to receive(:update_todos_count_cache).and_call_original
+
+ service.mark_pending_todos_as_done(issue, john_doe)
+
+ expect(john_doe.todos_done_count).to eq(1)
+ expect(john_doe.todos_pending_count).to eq(0)
+ end
+ end
+ end
+
+ describe '#mark_todos_as_done' do
+ it 'marks related todos for the user as done' do
+ first_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
+ second_todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
+
+ service.mark_todos_as_done([first_todo, second_todo], john_doe)
+
+ expect(first_todo.reload).to be_done
+ expect(second_todo.reload).to be_done
+ end
+
+ describe 'cached counts' do
+ it 'updates when todos change' do
+ todo = create(:todo, :assigned, user: john_doe, project: project, target: issue, author: author)
+
+ expect(john_doe.todos_done_count).to eq(0)
+ expect(john_doe.todos_pending_count).to eq(1)
+ expect(john_doe).to receive(:update_todos_count_cache).and_call_original
+
+ service.mark_todos_as_done([todo], john_doe)
+
+ expect(john_doe.todos_done_count).to eq(1)
+ expect(john_doe.todos_pending_count).to eq(0)
+ end
+ end
end
describe '#new_note' do
@@ -395,6 +437,18 @@ describe TodoService, services: true do
end
end
+ it 'updates cached counts when a todo is created' do
+ issue = create(:issue, project: project, assignee: john_doe, author: author, description: mentions)
+
+ expect(john_doe.todos_pending_count).to eq(0)
+ expect(john_doe).to receive(:update_todos_count_cache)
+
+ service.new_issue(issue, author)
+
+ expect(Todo.where(user_id: john_doe.id, state: :pending).count).to eq 1
+ expect(john_doe.todos_pending_count).to eq(1)
+ end
+
def should_create_todo(attributes = {})
attributes.reverse_merge!(
project: project,