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

candidate_details_presenter_spec.rb « ml « presenters « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ecf80b683eca3b02e9a56679612f24f1c0d0a27 (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
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::Ml::CandidateDetailsPresenter, feature_category: :mlops do
  let_it_be(:user) { create(:user, :with_avatar) } # rubocop:disable RSpec/FactoryBot/AvoidCreate
  let_it_be(:project) { create(:project, :private, creator: user) } # rubocop:disable RSpec/FactoryBot/AvoidCreate
  let_it_be(:experiment) { create(:ml_experiments, user: user, project: project) } # rubocop:disable RSpec/FactoryBot/AvoidCreate
  let_it_be(:candidate) do
    create(:ml_candidates, :with_artifact, experiment: experiment, user: user, project: project) # rubocop:disable RSpec/FactoryBot/AvoidCreate
  end

  let_it_be(:metrics) do
    [
      build_stubbed(:ml_candidate_metrics, name: 'metric1', value: 0.1, candidate: candidate),
      build_stubbed(:ml_candidate_metrics, name: 'metric2', value: 0.2, candidate: candidate),
      build_stubbed(:ml_candidate_metrics, name: 'metric3', value: 0.3, candidate: candidate)
    ]
  end

  let_it_be(:params) do
    [
      build_stubbed(:ml_candidate_params, name: 'param1', value: 'p1', candidate: candidate),
      build_stubbed(:ml_candidate_params, name: 'param2', value: 'p2', candidate: candidate)
    ]
  end

  let(:include_ci_job) { true }

  subject { Gitlab::Json.parse(described_class.new(candidate, include_ci_job).present)['candidate'] }

  before do
    allow(candidate).to receive(:latest_metrics).and_return(metrics)
    allow(candidate).to receive(:params).and_return(params)
  end

  describe '#execute' do
    context 'when candidate has metrics, params and artifacts' do
      it 'generates the correct params' do
        expect(subject['params']).to include(
          hash_including('name' => 'param1', 'value' => 'p1'),
          hash_including('name' => 'param2', 'value' => 'p2')
        )
      end

      it 'generates the correct metrics' do
        expect(subject['metrics']).to include(
          hash_including('name' => 'metric1', 'value' => 0.1),
          hash_including('name' => 'metric2', 'value' => 0.2),
          hash_including('name' => 'metric3', 'value' => 0.3)
        )
      end

      it 'generates the correct info' do
        expected_info = {
          'iid' => candidate.iid,
          'eid' => candidate.eid,
          'path_to_artifact' => "/#{project.full_path}/-/packages/#{candidate.artifact.id}",
          'experiment_name' => candidate.experiment.name,
          'path_to_experiment' => "/#{project.full_path}/-/ml/experiments/#{experiment.iid}",
          'status' => 'running',
          'path' => "/#{project.full_path}/-/ml/candidates/#{candidate.iid}"
        }

        expect(subject['info']).to include(expected_info)
      end
    end

    context 'when candidate has job' do
      let_it_be(:pipeline) { build_stubbed(:ci_pipeline, project: project, user: user) }
      let_it_be(:build) { candidate.ci_build = build_stubbed(:ci_build, pipeline: pipeline, user: user) }

      let(:can_read_build) { true }

      it 'generates the correct ci' do
        expected_info = {
          'path' => "/#{project.full_path}/-/jobs/#{build.id}",
          'name' => 'test',
          'user' => {
            'path' => "/#{pipeline.user.username}",
            'name' => pipeline.user.name,
            'username' => pipeline.user.username,
            'avatar' => user.avatar_url
          }
        }

        expect(subject.dig('info', 'ci_job')).to include(expected_info)
      end

      context 'when build user is nil' do
        it 'does not include build user info' do
          expected_info = {
            'path' => "/#{project.full_path}/-/jobs/#{build.id}",
            'name' => 'test'
          }

          allow(build).to receive(:user).and_return(nil)

          expect(subject.dig('info', 'ci_job')).to eq(expected_info)
        end
      end

      context 'and job is from MR' do
        let_it_be(:mr) { pipeline.merge_request = build_stubbed(:merge_request, source_project: project) }

        it 'generates the correct ci' do
          expected_info = {
            'path' => "/#{project.full_path}/-/merge_requests/#{mr.iid}",
            'iid' => mr.iid,
            'title' => mr.title
          }

          expect(subject.dig('info', 'ci_job', 'merge_request')).to include(expected_info)
        end
      end

      context 'when ci job is not to be added' do
        let(:include_ci_job) { false }

        it 'ci_job is nil' do
          expect(subject.dig('info', 'ci_job')).to be_nil
        end
      end
    end
  end
end