diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-09-20 15:18:04 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-10-08 15:19:12 +0200 |
commit | 4c1dc31051fb741bbd6daff4b5c1dcb166d85eeb (patch) | |
tree | f47f5d5bec9e2f13d8904d60341e32d7c57ebedd /spec | |
parent | c616327c1221dc91318a35769e01ab6932d497ea (diff) | |
download | gitlab-ce-4c1dc31051fb741bbd6daff4b5c1dcb166d85eeb.tar.gz |
Clean up ActiveRecord code in TodosFinder
This refactors the TodosFinder finder according to the new code reuse
rules, as enforced by the CodeReuse cops. I also changed some of the
methods to use regular if statements, instead of assignments and/or
early returns. This results in a more natural flow when reading the
code, and it makes it harder to accidentally return the wrong result.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/finders/todos_finder_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 23 | ||||
-rw-r--r-- | spec/models/todo_spec.rb | 69 |
3 files changed, 92 insertions, 1 deletions
diff --git a/spec/finders/todos_finder_spec.rb b/spec/finders/todos_finder_spec.rb index 7f7cfb2cb98..64289224933 100644 --- a/spec/finders/todos_finder_spec.rb +++ b/spec/finders/todos_finder_spec.rb @@ -105,7 +105,6 @@ describe TodosFinder do todos = finder.new(user, { sort: 'priority' }).execute - puts todos.to_sql expect(todos).to eq([todo_3, todo_5, todo_4, todo_2, todo_1]) end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 31c69e5bd2c..3fecddefff2 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -4073,6 +4073,29 @@ describe Project do end end + describe '.find_without_deleted' do + it 'returns nil if the project is about to be removed' do + project = create(:project, pending_delete: true) + + expect(described_class.find_without_deleted(project.id)).to be_nil + end + + it 'returns a project when it is not about to be removed' do + project = create(:project) + + expect(described_class.find_without_deleted(project.id)).to eq(project) + end + end + + describe '.for_group' do + it 'returns the projects for a given group' do + group = create(:group) + project = create(:project, namespace: group) + + expect(described_class.for_group(group)).to eq([project]) + end + end + def rugged_config rugged_repo(project.repository).config end diff --git a/spec/models/todo_spec.rb b/spec/models/todo_spec.rb index f29abcf536e..f1a9d81da08 100644 --- a/spec/models/todo_spec.rb +++ b/spec/models/todo_spec.rb @@ -173,4 +173,73 @@ describe Todo do expect(subject).not_to be_self_assigned end end + + describe '.for_action' do + it 'returns the todos for a given action' do + create(:todo, action: Todo::MENTIONED) + + todo = create(:todo, action: Todo::ASSIGNED) + + expect(described_class.for_action(Todo::ASSIGNED)).to eq([todo]) + end + end + + describe '.for_author' do + it 'returns the todos for a given author' do + user1 = create(:user) + user2 = create(:user) + todo = create(:todo, author: user1) + + create(:todo, author: user2) + + expect(described_class.for_author(user1)).to eq([todo]) + end + end + + describe '.for_project' do + it 'returns the todos for a given project' do + project1 = create(:project) + project2 = create(:project) + todo = create(:todo, project: project1) + + create(:todo, project: project2) + + expect(described_class.for_project(project1)).to eq([todo]) + end + end + + describe '.for_group' do + it 'returns the todos for a given group' do + group1 = create(:group) + group2 = create(:group) + todo = create(:todo, group: group1) + + create(:todo, group: group2) + + expect(described_class.for_group(group1)).to eq([todo]) + end + end + + describe '.for_type' do + it 'returns the todos for a given target type' do + todo = create(:todo, target: create(:issue)) + + create(:todo, target: create(:merge_request)) + + expect(described_class.for_type(Issue)).to eq([todo]) + end + end + + describe '.for_group_and_descendants' do + it 'returns the todos for a group and its descendants' do + parent_group = create(:group) + child_group = create(:group, parent: parent_group) + + todo1 = create(:todo, group: parent_group) + todo2 = create(:todo, group: child_group) + + expect(described_class.for_group_and_descendants(parent_group)) + .to include(todo1, todo2) + end + end end |