Commit d7f46366 authored by Felipe Artur's avatar Felipe Artur

Preload correct latest test report for requirements

Preload correct latest test report for requirementes
on GraphQL look ahead.

Changelog: fixed
parent 7946d895
......@@ -37,8 +37,8 @@ module Resolvers
def preloads
{
last_test_report_manually_created: [:test_reports],
last_test_report_state: [:test_reports, { test_reports: [:build] }]
last_test_report_manually_created: [:recent_test_reports],
last_test_report_state: [:recent_test_reports, { recent_test_reports: [:build] }]
}
end
......
......@@ -29,6 +29,7 @@ module RequirementsManagement
validates :issue_id, uniqueness: true, allow_nil: true
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
......@@ -90,7 +91,7 @@ module RequirementsManagement
end
def latest_report
test_reports.last
recent_test_reports.first
end
def last_test_report_state
......
......@@ -42,6 +42,18 @@ RSpec.describe Resolvers::RequirementsManagement::RequirementsResolver do
expect(resolve_requirements(iids: [requirement1.iid, requirement3.iid])).to contain_exactly(requirement1, requirement3)
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
before do
create(:test_report, state: :failed)
......
......@@ -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(: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'
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