Commit 946f3833 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'issue_323481' into 'master'

Preload correct latest test report for requirements

See merge request gitlab-org/gitlab!66091
parents 11e58268 d7f46366
...@@ -37,8 +37,8 @@ module Resolvers ...@@ -37,8 +37,8 @@ module Resolvers
def preloads def preloads
{ {
last_test_report_manually_created: [:test_reports], last_test_report_manually_created: [:recent_test_reports],
last_test_report_state: [:test_reports, { test_reports: [:build] }] last_test_report_state: [:recent_test_reports, { recent_test_reports: [:build] }]
} }
end end
......
...@@ -29,6 +29,7 @@ module RequirementsManagement ...@@ -29,6 +29,7 @@ module RequirementsManagement
validates :issue_id, uniqueness: true, allow_nil: true validates :issue_id, uniqueness: true, allow_nil: true
has_many :test_reports, inverse_of: :requirement has_many :test_reports, inverse_of: :requirement
has_many :recent_test_reports, -> { order(created_at: :desc) }, class_name: 'TestReport', inverse_of: :requirement
has_internal_id :iid, scope: :project has_internal_id :iid, scope: :project
...@@ -90,7 +91,7 @@ module RequirementsManagement ...@@ -90,7 +91,7 @@ module RequirementsManagement
end end
def latest_report def latest_report
test_reports.last recent_test_reports.first
end end
def last_test_report_state def last_test_report_state
......
...@@ -42,6 +42,18 @@ RSpec.describe Resolvers::RequirementsManagement::RequirementsResolver do ...@@ -42,6 +42,18 @@ RSpec.describe Resolvers::RequirementsManagement::RequirementsResolver do
expect(resolve_requirements(iids: [requirement1.iid, requirement3.iid])).to contain_exactly(requirement1, requirement3) expect(resolve_requirements(iids: [requirement1.iid, requirement3.iid])).to contain_exactly(requirement1, requirement3)
end end
it 'preloads correct latest test report' do
requirement_2_latest_report = create(:test_report, requirement: requirement2, created_at: 1.hour.ago)
create(:test_report, requirement: requirement1, created_at: 2.hours.ago)
create(:test_report, requirement: requirement2, created_at: 4.hours.ago)
requirement_3_latest_report = create(:test_report, requirement: requirement3, created_at: 3.hours.ago)
requirements = resolve_requirements(sort: 'created_desc').to_a
expect(requirements[0].latest_report).to eq(requirement_2_latest_report)
expect(requirements[1].latest_report).to eq(requirement_3_latest_report)
end
context 'when filtering by last test report state' do context 'when filtering by last test report state' do
before do before do
create(:test_report, state: :failed) create(:test_report, state: :failed)
......
...@@ -11,6 +11,8 @@ RSpec.describe RequirementsManagement::Requirement do ...@@ -11,6 +11,8 @@ RSpec.describe RequirementsManagement::Requirement do
it { is_expected.to belong_to(:author).class_name('User') } it { is_expected.to belong_to(:author).class_name('User') }
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
it { is_expected.to have_many(:test_reports) }
it { is_expected.to have_many(:recent_test_reports).order(created_at: :desc) }
it_behaves_like 'a model with a requirement issue association' it_behaves_like 'a model with a requirement issue association'
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