summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/cycle_analytics/event_shared_examples.rb
blob: bce889b454d025d1d3a694402b63f421be3a50a1 (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
# frozen_string_literal: true

RSpec.shared_examples_for 'value stream analytics event' do
  let(:params) { {} }
  let(:instance) { described_class.new(params) }
  let(:expected_hash_code) { Digest::SHA256.hexdigest(instance.class.identifier.to_s) }

  it { expect(described_class.name).to be_a_kind_of(String) }
  it { expect(described_class.identifier).to be_a_kind_of(Symbol) }
  it { expect(instance.object_type.ancestors).to include(ApplicationRecord) }
  it { expect(instance).to respond_to(:timestamp_projection) }
  it { expect(instance).to respond_to(:html_description) }
  it { expect(instance.column_list).to be_a_kind_of(Array) }

  describe '#apply_query_customization' do
    it 'expects an ActiveRecord::Relation object as argument and returns a modified version of it' do
      input_query = instance.object_type.all

      output_query = instance.apply_query_customization(input_query)
      expect(output_query).to be_a_kind_of(ActiveRecord::Relation)
    end
  end

  describe '#hash_code' do
    it 'returns a hash that uniquely identifies an event' do
      expect(instance.hash_code).to eq(expected_hash_code)
    end

    it 'does not differ when the same object is built with the same params' do
      another_instance_with_same_params = described_class.new(params)

      expect(another_instance_with_same_params.hash_code).to eq(instance.hash_code)
    end
  end
end

RSpec.shared_examples_for 'LEFT JOIN-able value stream analytics event' do
  let(:params) { {} }
  let(:instance) { described_class.new(params) }
  let(:record_with_data) { nil }
  let(:record_without_data) { nil }
  let(:scope) { instance.object_type.all }

  let(:records) do
    scope_with_left_join = instance.include_in(scope)
    scope_with_left_join.select(scope.model.arel_table[:id], instance.timestamp_projection.as('timestamp_column_data')).to_a
  end

  it 'can use the event as LEFT JOIN' do
    expected_record_count = record_without_data.nil? ? 1 : 2

    expect(records.count).to eq(expected_record_count)
  end

  context 'when looking at the record with data' do
    subject(:record) { records.to_a.find { |r| r.id == record_with_data.id } }

    it 'contains the timestamp expression' do
      expect(record.timestamp_column_data).not_to eq(nil)
    end
  end

  context 'when looking at the record without data' do
    subject(:record) { records.to_a.find { |r| r.id == record_without_data.id } }

    it 'returns nil for the timestamp expression' do
      expect(record.timestamp_column_data).to eq(nil) if record_without_data
    end
  end
end