Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pipeline_schedules_spec.rb « ci « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b8ba09a95c5ce80a48a48133f6b55b5884be3b8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Query.project.pipelineSchedules' do
  include GraphqlHelpers

  let_it_be(:project) { create(:project, :repository, :public) }
  let_it_be(:user) { create(:user) }
  let_it_be(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: user) }

  let(:pipeline_schedule_graphql_data) { graphql_data_at(:project, :pipeline_schedules, :nodes, 0) }

  let(:params) { {} }

  let(:fields) do
    <<~QUERY
      nodes {
        id
        description
        active
        nextRunAt
        realNextRun
        lastPipeline {
             id
        }
        refForDisplay
        refPath
        forTag
        cron
        cronTimezone
      }
    QUERY
  end

  let(:query) do
    %(
      query {
        project(fullPath: "#{project.full_path}") {
          pipelineSchedules {
            #{fields}
          }
        }
      }
    )
  end

  describe 'computed graphql fields' do
    before do
      pipeline_schedule.pipelines << build(:ci_pipeline, project: project)

      post_graphql(query, current_user: user)
    end

    it_behaves_like 'a working graphql query'

    it 'returns calculated fields for a pipeline schedule' do
      ref_for_display = pipeline_schedule_graphql_data['refForDisplay']

      expect(ref_for_display).to eq('master')
      expect(pipeline_schedule_graphql_data['refPath']).to eq("/#{project.full_path}/-/commits/#{ref_for_display}")
      expect(pipeline_schedule_graphql_data['forTag']).to be(false)
    end
  end

  it 'avoids N+1 queries' do
    create_pipeline_schedules(1)

    control = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: user) }

    create_pipeline_schedules(3)

    action = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: user) }

    expect(action).not_to exceed_query_limit(control)
  end

  def create_pipeline_schedules(count)
    create_list(:ci_pipeline_schedule, count, project: project)
      .each do |pipeline_schedule|
      create(:user).tap do |user|
        project.add_developer(user)
        pipeline_schedule.update!(owner: user)
      end
      pipeline_schedule.pipelines << build(:ci_pipeline, project: project)
    end
  end
end