Commit fb950c9a authored by lauraMon's avatar lauraMon

Adds spec for N+1

parent 21608c77
......@@ -52,9 +52,13 @@ module Types
# rubocop: disable CodeReuse/ActiveRecord
def jobs_for_pipeline(pipeline, stage_ids, include_needs)
results = pipeline.latest_statuses.where(stage_id: stage_ids)
results = results.preload(:project)
results = results.preload(:needs) if include_needs
builds_results = pipeline.latest_builds.where(stage_id: stage_ids).preload(:job_artifacts, :project)
bridges_results = pipeline.bridges.where(stage_id: stage_ids).preload(:project)
builds_results = builds_results.preload(:needs) if include_needs
bridges_results = bridges_results.preload(:needs) if include_needs
commit_status_results = pipeline.latest_statuses.where(stage_id: stage_ids)
results = builds_results | bridges_results | commit_status_results
results.group_by(&:stage_id)
end
......
......@@ -117,14 +117,19 @@ RSpec.describe 'Query.project.pipeline' do
)
end
it 'avoids N+1 queries' do
control_count = ActiveRecord::QueryRecorder.new do
post_graphql(query, current_user: user, variables: first_n.with(1))
it 'does not generate N+1 queries', :request_store, :use_sql_query_cache do
post_graphql(query, current_user: user)
control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
post_graphql(query, current_user: user)
end
create(:ci_build, name: 'test-a', pipeline: pipeline)
create(:ci_build, name: 'test-b', pipeline: pipeline)
expect do
post_graphql(query, current_user: user, variables: first_n.with(3))
end.not_to exceed_query_limit(control_count)
post_graphql(query, current_user: user)
end.not_to exceed_all_query_limit(control)
end
end
end
......@@ -137,11 +142,19 @@ RSpec.describe 'Query.project.pipeline' do
query {
project(fullPath: "#{project.full_path}") {
pipeline(iid: "#{pipeline.iid}") {
jobs {
stages {
nodes {
artifacts {
groups{
nodes {
downloadPath
jobs {
nodes {
artifacts {
nodes {
downloadPath
}
}
}
}
}
}
}
......@@ -158,7 +171,7 @@ RSpec.describe 'Query.project.pipeline' do
post_graphql(query, current_user: user)
job_data = graphql_data.dig('project', 'pipeline', 'jobs', 'nodes').first
job_data = graphql_data_at(:project, :pipeline, :stages, :nodes, :groups, :nodes, :jobs, :nodes).first
expect(job_data.dig('artifacts', 'nodes').count).to be(2)
end
end
......@@ -169,7 +182,7 @@ RSpec.describe 'Query.project.pipeline' do
post_graphql(query, current_user: user)
job_data = graphql_data.dig('project', 'pipeline', 'jobs', 'nodes').first
job_data = graphql_data_at(:project, :pipeline, :stages, :nodes, :groups, :nodes, :jobs, :nodes).first
expect(job_data['artifacts']).to be_nil
end
end
......
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