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

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

require 'spec_helper'

RSpec.describe ShaAttribute do
  let(:model) do
    Class.new(ActiveRecord::Base) do
      include ShaAttribute

      self.table_name = 'merge_requests'
    end
  end

  let(:binary_column) { :merge_ref_sha }
  let(:text_column) { :target_branch }

  describe '.sha_attribute' do
    it 'defines a SHA attribute with Gitlab::Database::ShaAttribute type' do
      expect(model).to receive(:attribute)
        .with(binary_column, an_instance_of(Gitlab::Database::ShaAttribute))
        .and_call_original

      model.sha_attribute(binary_column)
    end
  end

  describe '.sha256_attribute' do
    it 'defines a SHA256 attribute with Gitlab::Database::ShaAttribute type' do
      expect(model).to receive(:attribute)
        .with(binary_column, an_instance_of(Gitlab::Database::Sha256Attribute))
        .and_call_original

      model.sha256_attribute(binary_column)
    end
  end

  describe '.load_schema!' do
    # load_schema! is not a documented class method, so use a documented method
    # that we know will call load_schema!
    def load_schema!
      expect(model).to receive(:load_schema!).and_call_original

      model.new
    end

    using RSpec::Parameterized::TableSyntax

    where(:column_name, :environment, :expected_error) do
      ref(:binary_column)    | 'development' | :no_error
      ref(:binary_column)    | 'production'  | :no_error
      ref(:text_column)      | 'development' | :sha_mismatch_error
      ref(:text_column)      | 'production'  | :no_error
      :__non_existent_column | 'development' | :no_error
      :__non_existent_column | 'production'  | :no_error
    end

    let(:sha_mismatch_error) do
      [
        described_class::ShaAttributeTypeMismatchError,
        /#{column_name}.* should be a :binary column/
      ]
    end

    with_them do
      before do
        stub_rails_env(environment)
      end

      context 'with sha_attribute' do
        before do
          model.sha_attribute(column_name)
        end

        it 'validates column type' do
          case expected_error
          when :no_error
            expect { load_schema! }.not_to raise_error
          when :sha_mismatch_error
            expect { load_schema! }.to raise_error(
              described_class::ShaAttributeTypeMismatchError,
              /sha_attribute.*#{column_name}.* should be a :binary column/
            )
          end
        end
      end

      context 'with sha256_attribute' do
        before do
          model.sha256_attribute(column_name)
        end

        it 'validates column type' do
          case expected_error
          when :no_error
            expect { load_schema! }.not_to raise_error
          when :sha_mismatch_error
            expect { load_schema! }.to raise_error(
              described_class::Sha256AttributeTypeMismatchError,
              /sha256_attribute.*#{column_name}.* should be a :binary column/
            )
          end
        end
      end
    end
  end
end