summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/migrations/observers/total_database_size_change_spec.rb
blob: e689759c5746829b9547d231a0a26846f60cac2b (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::Database::Migrations::Observers::TotalDatabaseSizeChange do
  subject { described_class.new(observation, double('unused path')) }

  let(:observation) { Gitlab::Database::Migrations::Observation.new }
  let(:connection) { ActiveRecord::Base.connection }
  let(:query) { 'select pg_database_size(current_database())' }

  it 'records the size change' do
    expect(connection).to receive(:execute).with(query).once.and_return([{ 'pg_database_size' => 1024 }])
    expect(connection).to receive(:execute).with(query).once.and_return([{ 'pg_database_size' => 256 }])

    subject.before
    subject.after
    subject.record

    expect(observation.total_database_size_change).to eq(256 - 1024)
  end

  context 'out of order calls' do
    before do
      allow(connection).to receive(:execute).with(query).and_return([{ 'pg_database_size' => 1024 }])
    end

    it 'does not record anything if before size is unknown' do
      subject.after

      expect { subject.record }.not_to change { observation.total_database_size_change }
    end

    it 'does not record anything if after size is unknown' do
      subject.before

      expect { subject.record }.not_to change { observation.total_database_size_change }
    end
  end
end