summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models/concerns/analytics/cycle_analytics/stage_event_model_examples.rb
blob: f928fb1eb43a5369cfb478e5d1d91223bb4d0aa9 (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
# frozen_string_literal: true

RSpec.shared_examples 'StageEventModel' do
  describe '.upsert_data' do
    let(:time) { Time.parse(Time.current.to_s(:db)) } # truncating the timestamp so we can compare it with the timestamp loaded from the DB
    let(:input_data) do
      [
        {
          stage_event_hash_id: 1,
          issuable_id: 2,
          group_id: 3,
          project_id: 4,
          author_id: 5,
          milestone_id: 6,
          start_event_timestamp: time,
          end_event_timestamp: time
        },
        {
          stage_event_hash_id: 7,
          issuable_id: 8,
          group_id: 10,
          project_id: 11,
          author_id: 12,
          milestone_id: 13,
          start_event_timestamp: time,
          end_event_timestamp: time
        }
      ]
    end

    let(:column_order) do
      [
        :stage_event_hash_id,
        described_class.issuable_id_column,
        :group_id,
        :project_id,
        :milestone_id,
        :author_id,
        :start_event_timestamp,
        :end_event_timestamp
      ]
    end

    subject(:upsert_data) { described_class.upsert_data(input_data) }

    it 'inserts the data' do
      upsert_data

      expect(described_class.count).to eq(input_data.count)
    end

    it 'does not produce duplicate rows' do
      2.times { upsert_data }

      expect(described_class.count).to eq(input_data.count)
    end

    it 'inserts the data correctly' do
      upsert_data

      output_data = described_class.all.map do |record|
        column_order.map { |column| record[column] }
      end.sort

      expect(input_data.map(&:values).sort).to eq(output_data)
    end
  end
end