summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/ci/jobs_spec.rb
blob: 618705e5f94a1375b68c4acc8e96e1bf3d2f2c5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe 'Query.project.pipeline.stages.groups.jobs' do
  include GraphqlHelpers

  let(:project) { create(:project, :repository, :public) }
  let(:user) { create(:user) }
  let(:pipeline) do
    pipeline = create(:ci_pipeline, project: project, user: user)
    stage = create(:ci_stage_entity, pipeline: pipeline, name: 'first')
    create(:commit_status, stage_id: stage.id, pipeline: pipeline, name: 'my test job')

    pipeline
  end

  def first(field)
    [field.pluralize, 'nodes', 0]
  end

  let(:jobs_graphql_data) { graphql_data.dig(*%w[project pipeline], *first('stage'), *first('group'), 'jobs', 'nodes') }

  let(:query) do
    %(
      query {
        project(fullPath: "#{project.full_path}") {
          pipeline(iid: "#{pipeline.iid}") {
            stages {
              nodes {
                name
                groups {
                  nodes {
                    name
                    jobs {
                      nodes {
                        name
                        pipeline {
                          id
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    )
  end

  it 'returns the jobs of a pipeline stage' do
    post_graphql(query, current_user: user)

    expect(jobs_graphql_data).to contain_exactly(a_hash_including('name' => 'my test job'))
  end

  context 'when fetching jobs from the pipeline' do
    it 'avoids N+1 queries', :aggregate_failures do
      control_count = ActiveRecord::QueryRecorder.new do
        post_graphql(query, current_user: user)
      end

      build_stage = create(:ci_stage_entity, name: 'build', pipeline: pipeline)
      test_stage = create(:ci_stage_entity, name: 'test', pipeline: pipeline)
      create(:commit_status, pipeline: pipeline, stage_id: build_stage.id, name: 'docker 1 2')
      create(:commit_status, pipeline: pipeline, stage_id: build_stage.id, name: 'docker 2 2')
      create(:commit_status, pipeline: pipeline, stage_id: test_stage.id, name: 'rspec 1 2')
      create(:commit_status, pipeline: pipeline, stage_id: test_stage.id, name: 'rspec 2 2')

      expect do
        post_graphql(query, current_user: user)
      end.not_to exceed_query_limit(control_count)

      expect(response).to have_gitlab_http_status(:ok)

      build_stage = graphql_data.dig('project', 'pipeline', 'stages', 'nodes').find do |stage|
        stage['name'] == 'build'
      end
      test_stage = graphql_data.dig('project', 'pipeline', 'stages', 'nodes').find do |stage|
        stage['name'] == 'test'
      end
      docker_group = build_stage.dig('groups', 'nodes').first
      rspec_group = test_stage.dig('groups', 'nodes').first

      expect(docker_group['name']).to eq('docker')
      expect(rspec_group['name']).to eq('rspec')

      docker_jobs = docker_group.dig('jobs', 'nodes')
      rspec_jobs = rspec_group.dig('jobs', 'nodes')

      expect(docker_jobs).to eq([
        {
          'name' => 'docker 1 2',
          'pipeline' => { 'id' => pipeline.to_global_id.to_s }
        },
        {
          'name' => 'docker 2 2',
          'pipeline' => { 'id' => pipeline.to_global_id.to_s }
        }
      ])

      expect(rspec_jobs).to eq([
        {
          'name' => 'rspec 1 2',
          'pipeline' => { 'id' => pipeline.to_global_id.to_s }
        },
        {
          'name' => 'rspec 2 2',
          'pipeline' => { 'id' => pipeline.to_global_id.to_s }
        }
      ])
    end
  end
end