summaryrefslogtreecommitdiff
path: root/spec/lib/api/entities/ml/mlflow/run_spec.rb
blob: b8d38093681e4e4be93a52f05e9c648d1650657a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::Entities::Ml::Mlflow::Run do
  let_it_be(:candidate) { create(:ml_candidates, :with_metrics_and_params) }

  subject { described_class.new(candidate).as_json }

  it 'has run key' do
    expect(subject).to have_key(:run)
  end

  it 'has the id' do
    expect(subject.dig(:run, :info, :run_id)).to eq(candidate.iid.to_s)
  end

  it 'presents the metrics' do
    expect(subject.dig(:run, :data, :metrics).size).to eq(candidate.metrics.size)
  end

  it 'presents metrics correctly' do
    presented_metric = subject.dig(:run, :data, :metrics)[0]
    metric = candidate.metrics[0]

    expect(presented_metric[:key]).to eq(metric.name)
    expect(presented_metric[:value]).to eq(metric.value)
    expect(presented_metric[:timestamp]).to eq(metric.tracked_at)
    expect(presented_metric[:step]).to eq(metric.step)
  end

  it 'presents the params' do
    expect(subject.dig(:run, :data, :params).size).to eq(candidate.params.size)
  end

  it 'presents params correctly' do
    presented_param = subject.dig(:run, :data, :params)[0]
    param = candidate.params[0]

    expect(presented_param[:key]).to eq(param.name)
    expect(presented_param[:value]).to eq(param.value)
  end

  context 'when candidate has no metrics' do
    before do
      allow(candidate).to receive(:metrics).and_return([])
    end

    it 'returns empty data' do
      expect(subject.dig(:run, :data, :metrics)).to be_empty
    end
  end

  context 'when candidate has no params' do
    before do
      allow(candidate).to receive(:params).and_return([])
    end

    it 'data is empty' do
      expect(subject.dig(:run, :data, :params)).to be_empty
    end
  end
end