Commit 1bf9914a authored by Scott Hampton's avatar Scott Hampton

Refactor resolver and spec

Refactor the resolver to be more clear about
what each code section is doing.

Include authorization for reading builds in resolver.

Refactor a few areas in the spec file to make
things a little more simple.
parent 1e8aec77
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
module Resolvers module Resolvers
module Ci module Ci
class TestSuiteResolver < BaseResolver class TestSuiteResolver < BaseResolver
include Gitlab::Graphql::Authorize::AuthorizeResource
authorize :read_build
type ::Types::Ci::TestSuiteType, null: true type ::Types::Ci::TestSuiteType, null: true
alias_method :pipeline, :object alias_method :pipeline, :object
...@@ -13,17 +16,23 @@ module Resolvers ...@@ -13,17 +16,23 @@ module Resolvers
def resolve(build_ids:) def resolve(build_ids:)
builds = pipeline.latest_builds.id_in(build_ids).presence builds = pipeline.latest_builds.id_in(build_ids).presence
return if builds.nil? || builds.empty? return unless builds
TestSuiteSerializer
.new(project: pipeline.project, current_user: @current_user)
.represent(load_test_suite_data(builds), details: true)
end
private
def load_test_suite_data(builds)
suite = builds.sum do |build| suite = builds.sum do |build|
build.collect_test_reports!(Gitlab::Ci::Reports::TestReports.new) build.collect_test_reports!(Gitlab::Ci::Reports::TestReports.new)
end end
Gitlab::Ci::Reports::TestFailureHistory.new(suite.failed.values, pipeline.project).load! Gitlab::Ci::Reports::TestFailureHistory.new(suite.failed.values, pipeline.project).load!
TestSuiteSerializer suite
.new(project: pipeline.project, current_user: @current_user)
.represent(suite, details: true)
end end
end end
end end
......
...@@ -9,6 +9,8 @@ RSpec.describe Resolvers::Ci::TestSuiteResolver do ...@@ -9,6 +9,8 @@ RSpec.describe Resolvers::Ci::TestSuiteResolver do
let(:project) { create(:project, :public, :repository) } let(:project) { create(:project, :public, :repository) }
describe '#resolve' do describe '#resolve' do
subject(:test_suite) { resolve(described_class, obj: pipeline, args: { build_ids: build_ids }) }
context 'when pipeline has builds with test reports' do context 'when pipeline has builds with test reports' do
let(:main_pipeline) { create(:ci_pipeline, :with_test_reports_with_three_failures, project: project) } let(:main_pipeline) { create(:ci_pipeline, :with_test_reports_with_three_failures, project: project) }
let(:pipeline) { create(:ci_pipeline, :with_test_reports_with_three_failures, project: project, ref: 'new-feature') } let(:pipeline) { create(:ci_pipeline, :with_test_reports_with_three_failures, project: project, ref: 'new-feature') }
...@@ -25,8 +27,6 @@ RSpec.describe Resolvers::Ci::TestSuiteResolver do ...@@ -25,8 +27,6 @@ RSpec.describe Resolvers::Ci::TestSuiteResolver do
end end
it 'renders test suite data' do it 'renders test suite data' do
test_suite = resolve(described_class, obj: pipeline, args: { build_ids: build_ids })
expect(test_suite[:name]).to eq('test') expect(test_suite[:name]).to eq('test')
# Each test failure in this pipeline has a matching failure in the default branch # Each test failure in this pipeline has a matching failure in the default branch
...@@ -42,11 +42,10 @@ RSpec.describe Resolvers::Ci::TestSuiteResolver do ...@@ -42,11 +42,10 @@ RSpec.describe Resolvers::Ci::TestSuiteResolver do
context 'when pipeline has no builds that matches the given build_ids' do context 'when pipeline has no builds that matches the given build_ids' do
let(:pipeline) { create(:ci_empty_pipeline) } let(:pipeline) { create(:ci_empty_pipeline) }
let(:suite_name) { 'test' } let(:suite_name) { 'test' }
let(:build_ids) { [non_existing_record_id] }
it 'returns nil' do it 'returns nil' do
test_suite = resolve(described_class, obj: pipeline, args: { build_ids: ['1'] }) expect(test_suite).to be_nil
expect(test_suite).to eq(nil)
end end
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