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

job_annotation_spec.rb « ci « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f94494bc91d3ec83ef8856f06ea37a5df5386736 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::JobAnnotation, feature_category: :build_artifacts do
  let_it_be_with_reload(:job) { create(:ci_build, :success) }

  describe 'validations' do
    subject { create(:ci_job_annotation, job: job) }

    it { is_expected.to belong_to(:job).class_name('Ci::Build').inverse_of(:job_annotations) }
    it { is_expected.to validate_presence_of(:name) }
    it { is_expected.to validate_length_of(:name).is_at_most(255) }
    it { is_expected.to validate_uniqueness_of(:name).scoped_to([:job_id, :partition_id]) }
  end

  describe '.create' do
    context 'when JSON data is valid' do
      subject do
        job.job_annotations.create!(
          name: 'external',
          data: [{ external_link: { label: 'Example', url: 'https://example.com/' } }]
        )
      end

      it 'creates the object' do
        expect(subject).to be_a(described_class)
        expect(subject.data).to contain_exactly(a_hash_including('external_link' =>
          a_hash_including('label' => 'Example', 'url' => 'https://example.com/')))
      end
    end

    context 'when JSON data is invalid' do
      subject { job.job_annotations.create!(name: 'external', data: [{ invalid: 'invalid' }]) }

      it 'throws an error' do
        expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end

    context 'when there are more than 1000 JSON entries' do
      subject { job.job_annotations.create!(data: [{ external_link: { label: 'Example', url: 'https://example.com/' } }] * 1001) }

      it 'throws an error' do
        expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end
  end

  describe 'partitioning' do
    context 'with job' do
      before do
        job.partition_id = 123
      end

      let(:annotation) { build(:ci_job_annotation, job: job) }

      it 'copies the partition_id from job' do
        expect { annotation.valid? }.to change { annotation.partition_id }.to(123)
      end

      context 'when it is already set' do
        let(:annotation) { build(:ci_job_annotation, job: job, partition_id: 125) }

        it 'does not change the partition_id value' do
          expect { annotation.valid? }.not_to change { annotation.partition_id }
        end
      end
    end

    context 'without job' do
      let(:annotation) { build(:ci_job_annotation, job: nil) }

      it { is_expected.to validate_presence_of(:partition_id) }

      it 'does not change the partition_id value' do
        expect { annotation.valid? }.not_to change { annotation.partition_id }
      end
    end
  end
end