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 @@
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(:group) { create(:group) }
let_it_be(:namespace) { create_default(:namespace) }
let(:project) { create(:project, :public, :repository, :wiki_repo, name: 'awesome project', group: group) }
shared_examples 'response is correct' do |schema:, size: 1|
it { expect(response).to have_gitlab_http_status(:ok) }
it { expect(response).to match_response_schema(schema) }
it { expect(response).to include_limited_pagination_headers }
it { expect(json_response.size).to eq(size) }
it 'responds correctly' do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema(schema)
expect(response).to include_limited_pagination_headers
expect(json_response.size).to eq(size)
end
end
shared_examples 'pagination' do |scope:, search: '*'|
it 'returns a different result for each page' do
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
get api(endpoint, user), params: { scope: scope, search: search, page: 2, per_page: 1 }
second = Gitlab::Json.parse(response.body).first
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 }
expect(Gitlab::Json.parse(response.body).count).to eq(2)
......@@ -39,19 +38,15 @@ RSpec.describe API::Search do
end
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' }
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' }
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' }
expect(response).to have_gitlab_http_status(:bad_request)
......@@ -84,11 +79,13 @@ RSpec.describe API::Search do
project.wiki.index_wiki_blobs
ensure_elasticsearch_index!
get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' }
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'
end
......@@ -100,12 +97,12 @@ RSpec.describe API::Search do
end
context 'for commits scope' do
before do
get api(endpoint, user), params: { scope: 'commits', search: 'folder' }
it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details', size: 2 do
before do
get api(endpoint, user), params: { scope: 'commits', search: 'folder' }
end
end
it_behaves_like 'response is correct', schema: 'public_api/v4/commits_details', size: 2
it_behaves_like 'pagination', scope: 'commits'
it 'avoids N+1 queries' do
......@@ -124,12 +121,12 @@ RSpec.describe API::Search do
end
context 'for blobs scope' do
before do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' }
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs' do
before do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' }
end
end
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs'
it_behaves_like 'pagination', scope: 'blobs'
context 'filters' do
......@@ -402,12 +399,12 @@ RSpec.describe API::Search do
end
context 'for blobs scope' do
before do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' }
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs', size: 2 do
before do
get api(endpoint, user), params: { scope: 'blobs', search: 'monitors' }
end
end
it_behaves_like 'response is correct', schema: 'public_api/v4/blobs', size: 2
context 'filters' do
it 'by filename' do
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