summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2017-12-04 15:45:43 +0000
committerDouwe Maan <douwe@gitlab.com>2017-12-04 15:45:43 +0000
commit65b7a7a0632280c77de1dc9d6c93dfb5df0c2fc9 (patch)
treeb86d2e81c4d550d89906654d5bd378b93d5654d0 /spec
parent6a27f9b1b234470a994b0b43460bd9c8097ae12f (diff)
parent264171f72d4ef3e5dfafaf32d3d267ab279469e1 (diff)
downloadgitlab-ce-65b7a7a0632280c77de1dc9d6c93dfb5df0c2fc9.tar.gz
Merge branch 'sh-optimize-groups-api' into 'master'
Optimize API /groups/:id/projects by preloading assocations Closes #40308 See merge request gitlab-org/gitlab-ce!15475
Diffstat (limited to 'spec')
-rw-r--r--spec/models/project_spec.rb3
-rw-r--r--spec/requests/api/groups_spec.rb14
-rw-r--r--spec/services/projects/count_service_spec.rb12
3 files changed, 25 insertions, 4 deletions
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 34e160aa599..000d5b7126d 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -315,7 +315,6 @@ describe Project do
it { is_expected.to delegate_method(:empty_repo?).to(:repository) }
it { is_expected.to delegate_method(:members).to(:team).with_prefix(true) }
- it { is_expected.to delegate_method(:count).to(:forks).with_prefix(true) }
it { is_expected.to delegate_method(:name).to(:owner).with_prefix(true).with_arguments(allow_nil: true) }
end
@@ -2472,7 +2471,7 @@ describe Project do
it 'returns the number of forks' do
project = build(:project)
- allow(project.forks).to receive(:count).and_return(1)
+ expect_any_instance_of(Projects::ForksCountService).to receive(:count).and_return(1)
expect(project.forks_count).to eq(1)
end
diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb
index 04a658cd6c3..554723d6b1e 100644
--- a/spec/requests/api/groups_spec.rb
+++ b/spec/requests/api/groups_spec.rb
@@ -401,6 +401,20 @@ describe API::Groups do
expect(response).to have_gitlab_http_status(404)
end
+
+ it 'avoids N+1 queries' do
+ get api("/groups/#{group1.id}/projects", admin)
+
+ control_count = ActiveRecord::QueryRecorder.new do
+ get api("/groups/#{group1.id}/projects", admin)
+ end.count
+
+ create(:project, namespace: group1)
+
+ expect do
+ get api("/groups/#{group1.id}/projects", admin)
+ end.not_to exceed_query_limit(control_count)
+ end
end
context 'when using group path in URL' do
diff --git a/spec/services/projects/count_service_spec.rb b/spec/services/projects/count_service_spec.rb
index cc496501bad..183f6128c7b 100644
--- a/spec/services/projects/count_service_spec.rb
+++ b/spec/services/projects/count_service_spec.rb
@@ -4,9 +4,17 @@ describe Projects::CountService do
let(:project) { build(:project, id: 1) }
let(:service) { described_class.new(project) }
- describe '#relation_for_count' do
+ describe '.query' do
it 'raises NotImplementedError' do
- expect { service.relation_for_count }.to raise_error(NotImplementedError)
+ expect { described_class.query(project.id) }.to raise_error(NotImplementedError)
+ end
+ end
+
+ describe '#relation_for_count' do
+ it 'calls the class method query with the project id' do
+ expect(described_class).to receive(:query).with(project.id)
+
+ service.relation_for_count
end
end