Commit dbe0ca0c authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'id-speed-up-search-api-spec' into 'master'

Speed up ee/spec/requests/api/search_spec.rb

See merge request gitlab-org/gitlab!42695
parents 519b7790 a13b872b
...@@ -2,36 +2,35 @@ ...@@ -2,36 +2,35 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe API::Search do RSpec.describe API::Search, factory_default: :keep do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
let_it_be(:namespace) { create_default(:namespace) }
let(:project) { create(:project, :public, :repository, :wiki_repo, name: 'awesome project', group: group) } let(:project) { create(:project, :public, :repository, :wiki_repo, name: 'awesome project', group: group) }
shared_examples 'response is correct' do |schema:, size: 1| shared_examples 'response is correct' do |schema:, size: 1|
it { expect(response).to have_gitlab_http_status(:ok) } it 'responds correctly' do
it { expect(response).to match_response_schema(schema) } expect(response).to have_gitlab_http_status(:ok)
it { expect(response).to include_limited_pagination_headers } expect(response).to match_response_schema(schema)
it { expect(json_response.size).to eq(size) } expect(response).to include_limited_pagination_headers
expect(json_response.size).to eq(size)
end
end end
shared_examples 'pagination' do |scope:, search: '*'| shared_examples 'pagination' do |scope:, search: '*'|
it 'returns a different result for each page' do it 'returns a different result for each page' do
get api(endpoint, user), params: { scope: scope, search: search, page: 1, per_page: 1 } get api(endpoint, user), params: { scope: scope, search: search, page: 1, per_page: 1 }
expect(json_response.count).to eq(1)
first = json_response.first first = json_response.first
get api(endpoint, user), params: { scope: scope, search: search, page: 2, per_page: 1 } get api(endpoint, user), params: { scope: scope, search: search, page: 2, per_page: 1 }
second = Gitlab::Json.parse(response.body).first second = Gitlab::Json.parse(response.body).first
expect(first).not_to eq(second) expect(first).not_to eq(second)
end
it 'returns 1 result when per_page is 1' do
get api(endpoint, user), params: { scope: scope, search: search, per_page: 1 }
expect(json_response.count).to eq(1)
end
it 'returns 2 results when per_page is 2' do
get api(endpoint, user), params: { scope: scope, search: search, per_page: 2 } get api(endpoint, user), params: { scope: scope, search: search, per_page: 2 }
expect(Gitlab::Json.parse(response.body).count).to eq(2) expect(Gitlab::Json.parse(response.body).count).to eq(2)
...@@ -39,19 +38,15 @@ RSpec.describe API::Search do ...@@ -39,19 +38,15 @@ RSpec.describe API::Search do
end end
shared_examples 'elasticsearch disabled' do shared_examples 'elasticsearch disabled' do
it 'returns 400 error for wiki_blobs scope' do it 'returns 400 error for wiki_blobs, blobs and commits scope' do
get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' } get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' }
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
end
it 'returns 400 error for blobs scope' do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' } get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' }
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
end
it 'returns 400 error for commits scope' do
get api(endpoint, user), params: { scope: 'commits', search: 'folder' } get api(endpoint, user), params: { scope: 'commits', search: 'folder' }
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
...@@ -84,11 +79,13 @@ RSpec.describe API::Search do ...@@ -84,11 +79,13 @@ RSpec.describe API::Search do
project.wiki.index_wiki_blobs project.wiki.index_wiki_blobs
ensure_elasticsearch_index! ensure_elasticsearch_index!
get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' }
end end
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs' it_behaves_like 'response is correct', schema: 'public_api/v4/blobs' do
before do
get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' }
end
end
it_behaves_like 'pagination', scope: 'wiki_blobs' it_behaves_like 'pagination', scope: 'wiki_blobs'
end end
...@@ -100,12 +97,12 @@ RSpec.describe API::Search do ...@@ -100,12 +97,12 @@ RSpec.describe API::Search do
end end
context 'for commits scope' do context 'for commits scope' do
before do it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details', size: 2 do
get api(endpoint, user), params: { scope: 'commits', search: 'folder' } before do
get api(endpoint, user), params: { scope: 'commits', search: 'folder' }
end
end end
it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details', size: 2
it_behaves_like 'pagination', scope: 'commits' it_behaves_like 'pagination', scope: 'commits'
it 'avoids N+1 queries' do it 'avoids N+1 queries' do
...@@ -124,12 +121,12 @@ RSpec.describe API::Search do ...@@ -124,12 +121,12 @@ RSpec.describe API::Search do
end end
context 'for blobs scope' do context 'for blobs scope' do
before do it_behaves_like 'response is correct', schema: 'public_api/v4/blobs' do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' } before do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' }
end
end end
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs'
it_behaves_like 'pagination', scope: 'blobs' it_behaves_like 'pagination', scope: 'blobs'
context 'filters' do context 'filters' do
...@@ -402,12 +399,12 @@ RSpec.describe API::Search do ...@@ -402,12 +399,12 @@ RSpec.describe API::Search do
end end
context 'for blobs scope' do context 'for blobs scope' do
before do it_behaves_like 'response is correct', schema: 'public_api/v4/blobs', size: 2 do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' } before do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' }
end
end end
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs', size: 2
context 'filters' do context 'filters' do
it 'by filename' do it 'by filename' do
get api(endpoint, user), params: { scope: 'blobs', search: 'mon filename:PROCESS.md' } get api(endpoint, user), params: { scope: 'blobs', search: 'mon filename:PROCESS.md' }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment