File: experiment_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (180 lines) | stat: -rw-r--r-- 5,315 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ml::Experiment, feature_category: :mlops do
  let_it_be(:exp) { create(:ml_experiments) }
  let_it_be(:exp2) { create(:ml_experiments, project: exp.project) }
  let_it_be(:model) { create(:ml_models, project: exp.project) }
  let_it_be(:model_experiment) { model.default_experiment }

  let(:iid) { exp.iid }
  let(:exp_name) { exp.name }

  describe 'associations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:user) }
    it { is_expected.to have_many(:candidates) }
    it { is_expected.to have_many(:metadata) }
    it { is_expected.to belong_to(:model).class_name('Ml::Model') }
  end

  describe '#destroy' do
    it 'allow experiment without model to be destroyed' do
      experiment = create(:ml_experiments, project: exp.project)

      expect { experiment.destroy! }.to change { Ml::Experiment.count }.by(-1)
    end

    it 'throws error when destroying experiment with model' do
      experiment = create(:ml_models, project: exp.project).default_experiment

      expect { experiment.destroy! }.to raise_error(ActiveRecord::ActiveRecordError)
      expect(experiment.errors.full_messages).to include('Cannot delete an experiment associated to a model')
    end
  end

  describe '.package_name' do
    it { expect(exp.package_name).to eq("ml_experiment_#{exp.iid}") }

    context 'when model belongs to package' do
      it 'is the model name' do
        expect(model_experiment.package_name).to eq(model.name)
      end
    end
  end

  describe '.for_model?' do
    it 'is false if it is not the default experiment for a model' do
      expect(exp.for_model?).to be(false)
    end

    it 'is true if it is not the default experiment for a model' do
      expect(model_experiment.for_model?).to be(true)
    end
  end

  describe '.by_project' do
    subject { described_class.by_project(exp.project) }

    it { is_expected.to match_array([exp, exp2, model_experiment]) }
  end

  describe '.including_project' do
    subject { described_class.including_project }

    it 'loads latest version' do
      expect(subject.first.association_cached?(:project)).to be(true)
    end
  end

  describe '#by_project_id_and_iid' do
    subject { described_class.by_project_id_and_iid(exp.project_id, iid) }

    context 'if exists' do
      it { is_expected.to eq(exp) }
    end

    context 'if does not exist' do
      let(:iid) { non_existing_record_id }

      it { is_expected.to be(nil) }
    end
  end

  describe '#by_project_id_and_name' do
    subject { described_class.by_project_id_and_name(exp.project_id, exp_name) }

    context 'if exists' do
      it { is_expected.to eq(exp) }
    end

    context 'if does not exist' do
      let(:exp_name) { 'hello' }

      it { is_expected.to be_nil }
    end
  end

  describe '.find_or_create' do
    let(:name) { exp.name }
    let(:project) { exp.project }

    subject(:find_or_create) { described_class.find_or_create(project, name, exp.user) }

    context 'when experiments exists' do
      it 'fetches existing experiment', :aggregate_failures do
        expect { find_or_create }.not_to change { Ml::Experiment.count }

        expect(find_or_create).to eq(exp)
      end
    end

    context 'when experiments does not exist' do
      let(:name) { 'a new experiment' }

      it 'creates the experiment', :aggregate_failures do
        expect { find_or_create }.to change { Ml::Experiment.count }.by(1)

        expect(find_or_create.name).to eq(name)
        expect(find_or_create.user).to eq(exp.user)
        expect(find_or_create.project).to eq(project)
      end
    end

    context 'when experiment name exists but project is different' do
      let(:project) { create(:project) }

      it 'creates a model', :aggregate_failures do
        expect { find_or_create }.to change { Ml::Experiment.count }.by(1)

        expect(find_or_create.name).to eq(name)
        expect(find_or_create.user).to eq(exp.user)
        expect(find_or_create.project).to eq(project)
      end
    end
  end

  describe '#with_candidate_count' do
    let_it_be(:exp3) do
      create(:ml_experiments, project: exp.project).tap do |e|
        create_list(:ml_candidates, 3, experiment: e, user: nil)
        create(:ml_candidates, experiment: exp2, user: nil)
      end
    end

    subject { described_class.with_candidate_count.to_h { |e| [e.id, e.candidate_count] } }

    it 'fetches the candidate count', :aggregate_failures do
      expect(subject[exp.id]).to eq(0)
      expect(subject[exp2.id]).to eq(1)
      expect(subject[exp3.id]).to eq(3)
    end
  end

  describe '#package_for_experiment?' do
    using RSpec::Parameterized::TableSyntax

    subject { described_class.package_for_experiment?(package_name) }

    where(:package_name, :id) do
      'ml_experiment_1234' | true
      'ml_experiment_1234abc' | false
      'ml_experiment_abc' | false
      'ml_experiment_' | false
      'blah' | false
    end

    with_them do
      it { is_expected.to be(id) }
    end
  end

  describe "#exclude_experiments_for_models" do
    subject { described_class.by_project(exp.project).exclude_experiments_for_models }

    it 'excludes experiments that belongs to a model' do
      is_expected.to match_array([exp, exp2])
    end
  end
end