summaryrefslogtreecommitdiff
path: root/spec/finders
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-09-26 11:22:52 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 22:49:42 +0200
commitac0b104ae4968adaed7b94db76c0ac86badb6d6b (patch)
treebe0737e1d9971911bedc48bb05d1fe84e46ac93a /spec/finders
parentcd85c22faa7092edabf252fa157125ea571ed054 (diff)
downloadgitlab-ce-ac0b104ae4968adaed7b94db76c0ac86badb6d6b.tar.gz
Minimize the number of queries by preloading counts and ancestors
By preloading the count of members, projects and subgroups of a group, we don't need to query them later. We also preload the entire hierarchy for a search result and include the counts so we don't need to query for them again
Diffstat (limited to 'spec/finders')
-rw-r--r--spec/finders/group_descendants_finder_spec.rb38
1 files changed, 16 insertions, 22 deletions
diff --git a/spec/finders/group_descendants_finder_spec.rb b/spec/finders/group_descendants_finder_spec.rb
index 77401ba09a2..09a773aaf68 100644
--- a/spec/finders/group_descendants_finder_spec.rb
+++ b/spec/finders/group_descendants_finder_spec.rb
@@ -46,6 +46,18 @@ describe GroupDescendantsFinder do
expect(finder.execute).to contain_exactly(subgroup, project)
end
+ it 'includes the preloaded counts for groups' do
+ create(:group, parent: subgroup)
+ create(:project, namespace: subgroup)
+ subgroup.add_developer(create(:user))
+
+ found_group = finder.execute.detect { |child| child.is_a?(Group) }
+
+ expect(found_group.preloaded_project_count).to eq(1)
+ expect(found_group.preloaded_subgroup_count).to eq(1)
+ expect(found_group.preloaded_member_count).to eq(1)
+ end
+
context 'with a filter' do
let(:params) { { filter: 'test' } }
@@ -57,16 +69,16 @@ describe GroupDescendantsFinder do
end
context 'with matching children' do
- it 'includes a group that has a subgroup matching the query' do
+ it 'includes a group that has a subgroup matching the query and its parent' do
matching_subgroup = create(:group, name: 'testgroup', parent: subgroup)
- expect(finder.execute).to contain_exactly(matching_subgroup)
+ expect(finder.execute).to contain_exactly(subgroup, matching_subgroup)
end
- it 'includes a group that has a project matching the query' do
+ it 'includes the parent of a matching project' do
matching_project = create(:project, namespace: subgroup, name: 'Testproject')
- expect(finder.execute).to contain_exactly(matching_project)
+ expect(finder.execute).to contain_exactly(subgroup, matching_project)
end
it 'does not include the parent itself' do
@@ -77,23 +89,5 @@ describe GroupDescendantsFinder do
end
end
end
-
- describe '#total_count' do
- it 'counts the array children were already loaded' do
- finder.instance_variable_set(:@children, [build(:project)])
-
- expect(finder).not_to receive(:subgroups)
- expect(finder).not_to receive(:projects)
-
- expect(finder.total_count).to eq(1)
- end
-
- it 'performs a count without loading children when they are not loaded yet' do
- expect(finder).to receive(:subgroups).and_call_original
- expect(finder).to receive(:projects).and_call_original
-
- expect(finder.total_count).to eq(2)
- end
- end
end
end