debian-mirror-gitlab/spec/models/ml/candidate_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

213 lines
6 KiB
Ruby
Raw Normal View History

2022-08-27 11:52:29 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Ml::Candidate, factory_default: :keep, feature_category: :mlops do
2023-06-20 00:43:36 +05:30
let_it_be(:candidate) { create(:ml_candidates, :with_metrics_and_params, :with_artifact, name: 'candidate0') }
2023-04-23 21:23:45 +05:30
let_it_be(:candidate2) do
create(:ml_candidates, experiment: candidate.experiment, user: create(:user), name: 'candidate2')
end
2023-03-17 16:20:25 +05:30
2023-06-20 00:43:36 +05:30
let(:project) { candidate.project }
2023-03-04 22:38:38 +05:30
2022-08-27 11:52:29 +05:30
describe 'associations' do
it { is_expected.to belong_to(:experiment) }
2023-06-20 00:43:36 +05:30
it { is_expected.to belong_to(:project) }
2022-08-27 11:52:29 +05:30
it { is_expected.to belong_to(:user) }
2023-06-20 00:43:36 +05:30
it { is_expected.to belong_to(:package) }
2022-08-27 11:52:29 +05:30
it { is_expected.to have_many(:params) }
it { is_expected.to have_many(:metrics) }
2023-03-04 22:38:38 +05:30
it { is_expected.to have_many(:metadata) }
end
2023-06-20 00:43:36 +05:30
describe 'modules' do
it_behaves_like 'AtomicInternalId' do
let(:internal_id_attribute) { :internal_id }
let(:instance) { build(:ml_candidates, experiment: candidate.experiment) }
let(:scope) { :project }
let(:scope_attrs) { { project: instance.project } }
let(:usage) { :ml_candidates }
end
end
2023-03-04 22:38:38 +05:30
describe 'default values' do
2023-06-20 00:43:36 +05:30
it { expect(described_class.new.eid).to be_present }
2022-08-27 11:52:29 +05:30
end
2022-10-11 01:57:18 +05:30
2023-06-20 00:43:36 +05:30
describe '.destroy' do
let_it_be(:candidate_to_destroy) do
create(:ml_candidates, :with_metrics_and_params, :with_metadata, :with_artifact)
end
2023-01-13 00:05:48 +05:30
2023-06-20 00:43:36 +05:30
it 'destroys metrics, params and metadata, but not the artifact', :aggregate_failures do
expect { candidate_to_destroy.destroy! }
.to change { Ml::CandidateMetadata.count }.by(-2)
.and change { Ml::CandidateParam.count }.by(-2)
.and change { Ml::CandidateMetric.count }.by(-2)
.and not_change { Packages::Package.count }
end
2023-01-13 00:05:48 +05:30
end
2023-06-20 00:43:36 +05:30
describe '.artifact_root' do
subject { candidate.artifact_root }
2023-03-04 22:38:38 +05:30
2023-06-20 00:43:36 +05:30
it { is_expected.to eq("/#{candidate.package_name}/#{candidate.iid}/") }
2023-03-04 22:38:38 +05:30
end
describe '.package_version' do
subject { candidate.package_version }
2023-06-20 00:43:36 +05:30
it { is_expected.to eq(candidate.iid) }
end
describe '.eid' do
let_it_be(:eid) { SecureRandom.uuid }
let_it_be(:candidate3) do
build(:ml_candidates, :with_metrics_and_params, name: 'candidate0', eid: eid)
end
subject { candidate3.eid }
it { is_expected.to eq(eid) }
2023-03-04 22:38:38 +05:30
end
describe '.artifact' do
2023-03-17 16:20:25 +05:30
let(:tested_candidate) { candidate }
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
subject { tested_candidate.artifact }
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
context 'when has logged artifacts' do
it 'returns the package' do
expect(subject.name).to eq(tested_candidate.package_name)
2023-03-04 22:38:38 +05:30
end
end
context 'when does not have logged artifacts' do
2023-03-17 16:20:25 +05:30
let(:tested_candidate) { candidate2 }
2023-03-04 22:38:38 +05:30
it { is_expected.to be_nil }
end
2022-10-11 01:57:18 +05:30
end
2023-06-20 00:43:36 +05:30
describe '#by_project_id_and_eid' do
let(:project_id) { candidate.experiment.project_id }
let(:eid) { candidate.eid }
2023-03-17 16:20:25 +05:30
2023-06-20 00:43:36 +05:30
subject { described_class.with_project_id_and_eid(project_id, eid) }
2023-03-17 16:20:25 +05:30
2023-06-20 00:43:36 +05:30
context 'when eid exists', 'and belongs to project' do
it { is_expected.to eq(candidate) }
end
2023-03-17 16:20:25 +05:30
2023-06-20 00:43:36 +05:30
context 'when eid exists', 'and does not belong to project' do
let(:project_id) { non_existing_record_id }
it { is_expected.to be_nil }
end
context 'when eid does not exist' do
let(:eid) { 'a' }
it { is_expected.to be_nil }
2023-03-17 16:20:25 +05:30
end
end
2022-11-25 23:54:43 +05:30
describe '#by_project_id_and_iid' do
2022-10-11 01:57:18 +05:30
let(:project_id) { candidate.experiment.project_id }
let(:iid) { candidate.iid }
subject { described_class.with_project_id_and_iid(project_id, iid) }
2023-06-20 00:43:36 +05:30
context 'when internal_id exists', 'and belongs to project' do
2022-10-11 01:57:18 +05:30
it { is_expected.to eq(candidate) }
end
2023-06-20 00:43:36 +05:30
context 'when internal_id exists', 'and does not belong to project' do
2022-10-11 01:57:18 +05:30
let(:project_id) { non_existing_record_id }
it { is_expected.to be_nil }
end
2023-06-20 00:43:36 +05:30
context 'when internal_id does not exist' do
let(:iid) { non_existing_record_id }
2022-10-11 01:57:18 +05:30
it { is_expected.to be_nil }
end
end
2023-01-13 00:05:48 +05:30
describe "#latest_metrics" do
2023-04-23 21:23:45 +05:30
let_it_be(:candidate3) { create(:ml_candidates, experiment: candidate.experiment) }
let_it_be(:metric1) { create(:ml_candidate_metrics, candidate: candidate3) }
let_it_be(:metric2) { create(:ml_candidate_metrics, candidate: candidate3 ) }
let_it_be(:metric3) { create(:ml_candidate_metrics, name: metric1.name, candidate: candidate3) }
2023-01-13 00:05:48 +05:30
2023-04-23 21:23:45 +05:30
subject { candidate3.latest_metrics }
2023-01-13 00:05:48 +05:30
it 'fetches only the last metric for the name' do
expect(subject).to match_array([metric2, metric3] )
end
end
2023-03-17 16:20:25 +05:30
describe "#including_relationships" do
subject { described_class.including_relationships.find_by(id: candidate.id) }
2023-01-13 00:05:48 +05:30
it 'loads latest metrics and params', :aggregate_failures do
expect(subject.association_cached?(:latest_metrics)).to be(true)
expect(subject.association_cached?(:params)).to be(true)
2023-03-17 16:20:25 +05:30
expect(subject.association_cached?(:user)).to be(true)
2023-01-13 00:05:48 +05:30
end
end
2023-04-23 21:23:45 +05:30
describe '#by_name' do
let(:name) { candidate.name }
subject { described_class.by_name(name) }
context 'when name matches' do
it 'gets the correct candidates' do
expect(subject).to match_array([candidate])
end
end
context 'when name matches partially' do
let(:name) { 'andidate' }
it 'gets the correct candidates' do
expect(subject).to match_array([candidate, candidate2])
end
end
context 'when name does not match' do
let(:name) { non_existing_record_id.to_s }
it 'does not fetch any candidate' do
expect(subject).to match_array([])
end
end
end
describe '#order_by_metric' do
let_it_be(:auc_metrics) do
create(:ml_candidate_metrics, name: 'auc', value: 0.4, candidate: candidate)
create(:ml_candidate_metrics, name: 'auc', value: 0.8, candidate: candidate2)
end
let(:direction) { 'desc' }
subject { described_class.order_by_metric('auc', direction) }
it 'orders correctly' do
expect(subject).to eq([candidate2, candidate])
end
context 'when direction is asc' do
let(:direction) { 'asc' }
it 'orders correctly' do
expect(subject).to eq([candidate, candidate2])
end
end
end
2022-08-27 11:52:29 +05:30
end