debian-mirror-gitlab/spec/models/experiment_subject_spec.rb

73 lines
2.3 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ExperimentSubject, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:experiment) }
it { is_expected.to belong_to(:user) }
2021-09-04 01:27:46 +05:30
it { is_expected.to belong_to(:namespace) }
2021-02-22 17:27:13 +05:30
it { is_expected.to belong_to(:project) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:experiment) }
describe 'must_have_one_subject_present' do
2021-09-04 01:27:46 +05:30
let(:experiment_subject) { build(:experiment_subject, user: nil, namespace: nil, project: nil) }
let(:error_message) { 'Must have exactly one of User, Namespace, or Project.' }
2021-02-22 17:27:13 +05:30
it 'fails when no subject is present' do
expect(experiment_subject).not_to be_valid
expect(experiment_subject.errors[:base]).to include(error_message)
end
it 'passes when user subject is present' do
experiment_subject.user = build(:user)
expect(experiment_subject).to be_valid
end
2021-09-04 01:27:46 +05:30
it 'passes when namespace subject is present' do
experiment_subject.namespace = build(:group)
2021-02-22 17:27:13 +05:30
expect(experiment_subject).to be_valid
end
it 'passes when project subject is present' do
experiment_subject.project = build(:project)
expect(experiment_subject).to be_valid
end
it 'fails when more than one subject is present', :aggregate_failures do
# two subjects
experiment_subject.user = build(:user)
2021-09-04 01:27:46 +05:30
experiment_subject.namespace = build(:group)
2021-02-22 17:27:13 +05:30
expect(experiment_subject).not_to be_valid
expect(experiment_subject.errors[:base]).to include(error_message)
# three subjects
experiment_subject.project = build(:project)
expect(experiment_subject).not_to be_valid
expect(experiment_subject.errors[:base]).to include(error_message)
end
end
end
2021-09-04 01:27:46 +05:30
describe '.valid_subject?' do
subject(:valid_subject?) { described_class.valid_subject?(subject_class.new) }
context 'when passing a Group, Namespace, User or Project' do
[Group, Namespace, User, Project].each do |subject_class|
let(:subject_class) { subject_class }
it { is_expected.to be(true) }
end
end
context 'when passing another object' do
let(:subject_class) { Issue }
it { is_expected.to be(false) }
end
end
2021-02-22 17:27:13 +05:30
end