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

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

require 'spec_helper'

RSpec.describe NestedAttributesDuplicatesValidator do
  let(:validator) { described_class.new(attributes: [attribute], **options) }

  describe '#validate_each' do
    let(:project) { build(:project) }
    let(:record) { project }
    let(:attribute) { :variables }
    let(:value) { project.variables }

    subject { validator.validate_each(record, attribute, value) }

    context 'with no scope' do
      let(:options) { {} }
      let(:variables) { build_list(:ci_variable, 2, project: project) }

      before do
        project.variables << variables
      end

      it 'does not have any errors' do
        subject

        expect(project.errors.empty?).to be true
      end

      context 'with duplicates' do
        before do
          project.variables.build(key: variables.first.key, value: 'dummy_value')
        end

        it 'has a duplicate key error' do
          subject

          expect(project.errors).to have_key(:variables)
        end
      end
    end

    context 'with a scope attribute' do
      let(:options) { { scope: :environment_scope } }
      let(:first_variable) { build(:ci_variable, key: 'test_key', environment_scope: '*', project: project) }
      let(:second_variable) { build(:ci_variable, key: 'test_key', environment_scope: 'prod', project: project) }

      before do
        project.variables << first_variable
        project.variables << second_variable
      end

      it 'does not have any errors' do
        subject

        expect(project.errors.empty?).to be true
      end

      context 'with duplicates' do
        before do
          project.variables.build(key: second_variable.key, value: 'dummy_value', environment_scope: second_variable.environment_scope)
        end

        it 'has a duplicate key error' do
          subject

          expect(project.errors).to have_key(:variables)
        end
      end
    end

    context 'with a child attribute' do
      let(:release) { build(:release) }
      let(:first_link) { build(:release_link, name: 'test1', url: 'https://www.google1.com', release: release) }
      let(:second_link) { build(:release_link, name: 'test2', url: 'https://www.google2.com', release: release) }
      let(:record) { release }
      let(:attribute) { :links }
      let(:value) { release.links }
      let(:options) { { scope: :release, child_attributes: %i[name url] } }

      before do
        release.links << first_link
        release.links << second_link
      end

      it 'does not have any errors' do
        subject

        expect(release.errors.empty?).to be true
      end

      context 'when name is duplicated' do
        let(:second_link) { build(:release_link, name: 'test1', release: release) }

        it 'has a duplicate error' do
          subject

          expect(release.errors).to have_key(attribute)
        end
      end

      context 'when url is duplicated' do
        let(:second_link) { build(:release_link, url: 'https://www.google1.com', release: release) }

        it 'has a duplicate error' do
          subject

          expect(release.errors).to have_key(attribute)
        end
      end
    end
  end
end