summaryrefslogtreecommitdiff
path: root/spec/models
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-10-02 18:21:18 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 22:49:42 +0200
commit167fd71348d145c6fee953004bf77ceebf6efb1e (patch)
tree8dcead204c15ce39758e7dc9dd55c84940c54917 /spec/models
parentef043063f9d6f9f9482707d78214709b09620a78 (diff)
downloadgitlab-ce-167fd71348d145c6fee953004bf77ceebf6efb1e.tar.gz
Always preload all elements in a hierarchy to avoid extra queries.
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/concerns/group_descendant_spec.rb21
1 files changed, 19 insertions, 2 deletions
diff --git a/spec/models/concerns/group_descendant_spec.rb b/spec/models/concerns/group_descendant_spec.rb
index f3a0c342d35..c17c8f2abec 100644
--- a/spec/models/concerns/group_descendant_spec.rb
+++ b/spec/models/concerns/group_descendant_spec.rb
@@ -7,6 +7,23 @@ describe GroupDescendant, :nested_groups do
context 'for a group' do
describe '#hierarchy' do
+ it 'only queries once for the ancestors' do
+ # make sure the subsub_group does not have anything cached
+ test_group = create(:group, parent: subsub_group).reload
+
+ query_count = ActiveRecord::QueryRecorder.new { test_group.hierarchy }.count
+
+ expect(query_count).to eq(1)
+ end
+
+ it 'only queries once for the ancestors when a top is given' do
+ test_group = create(:group, parent: subsub_group).reload
+
+ query_count = ActiveRecord::QueryRecorder.new { test_group.hierarchy(subgroup) }.count
+
+ expect(query_count).to eq(1)
+ end
+
it 'builds a hierarchy for a group' do
expected_hierarchy = { parent => { subgroup => subsub_group } }
@@ -20,7 +37,7 @@ describe GroupDescendant, :nested_groups do
end
it 'raises an error if specifying a base that is not part of the tree' do
- expect { subsub_group.hierarchy(double) }.to raise_error('specified base is not part of the tree')
+ expect { subsub_group.hierarchy(build_stubbed(:group)) }.to raise_error('specified top is not part of the tree')
end
end
@@ -77,7 +94,7 @@ describe GroupDescendant, :nested_groups do
end
it 'raises an error if specifying a base that is not part of the tree' do
- expect { project.hierarchy(double) }.to raise_error('specified base is not part of the tree')
+ expect { project.hierarchy(build_stubbed(:group)) }.to raise_error('specified top is not part of the tree')
end
end