summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/event_store/event_spec.rb
blob: 97f6870a5ece7a1d7cf5a9dce94b16db2693175a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::EventStore::Event 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
      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
    end
  end
end