Commit fb950c9a authored by lauraMon's avatar lauraMon

Adds spec for N+1

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