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

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

require 'fast_spec_helper'
require 'json_schemer'
require 'oj'

RSpec.describe Gitlab::EventStore::Event, feature_category: :shared do
  let(:event_class) { stub_const('TestEvent', Class.new(described_class)) }
  let(:event) { event_class.new(data: data) }
  let(:data) { { project_id: 123, project_path: 'org/the-project' } }

  context 'when schema is not defined' do
    it 'raises an error on initialization' do
      expect { event }.to raise_error(NotImplementedError)
    end
  end

  context 'when schema is defined' do
    before do
      event_class.class_eval do
        def schema
          {
            'required' => ['project_id'],
            'type' => 'object',
            'properties' => {
              'project_id' => { 'type' => 'integer' },
              'project_path' => { 'type' => 'string' }
            }
          }
        end
      end
    end

    describe 'schema validation' do
      context 'when data matches the schema' do
        it 'initializes the event correctly' do
          expect(event.data).to eq(data)
        end
      end

      context 'when required properties are present as well as unknown properties' do
        let(:data) { { project_id: 123, unknown_key: 'unknown_value' } }

        it 'initializes the event correctly' do
          expect(event.data).to eq(data)
        end

        it 'validates schema' do
          expect(event_class.json_schema_valid).to eq(nil)

          event

          expect(event_class.json_schema_valid).to eq(true)
        end
      end

      context 'when some properties are missing' do
        let(:data) { { project_path: 'org/the-project' } }

        it 'expects all properties to be present' do
          expect { event }.to raise_error(Gitlab::EventStore::InvalidEvent, /does not match the defined schema/)
        end
      end

      context 'when data is not a Hash' do
        let(:data) { 123 }

        it 'raises an error' do
          expect { event }.to raise_error(Gitlab::EventStore::InvalidEvent, 'Event data must be a Hash')
        end
      end

      context 'when schema is invalid' do
        before do
          event_class.class_eval do
            def schema
              {
                'required' => ['project_id'],
                'type' => 'object',
                'properties' => {
                  'project_id' => { 'type' => 'int' },
                  'project_path' => { 'type' => 'string ' }
                }
              }
            end
          end
        end

        it 'raises an error' do
          expect(event_class.json_schema_valid).to eq(nil)

          expect { event }.to raise_error(Gitlab::EventStore::InvalidEvent, 'Schema for event TestEvent is invalid')

          expect(event_class.json_schema_valid).to eq(false)
        end
      end
    end
  end
end