summaryrefslogtreecommitdiff
path: root/spec/models/shard_spec.rb
blob: a9d11f4290c32bd7a5ab7c1ccd4a95076030ab39 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Shard do
  describe '.populate!' do
    it 'creates shards based on the config file' do
      expect(described_class.all).to be_empty

      stub_storage_settings(foo: {}, bar: {}, baz: {})

      described_class.populate!

      expect(described_class.all.map(&:name)).to match_array(%w[default foo bar baz])
    end
  end

  describe '.by_name' do
    let(:default_shard) { described_class.find_by(name: 'default') }

    before do
      described_class.populate!
    end

    it 'returns an existing shard' do
      expect(described_class.by_name('default')).to eq(default_shard)
    end

    it 'creates a new shard' do
      result = described_class.by_name('foo')

      expect(result).not_to eq(default_shard)
      expect(result.name).to eq('foo')
    end

    it 'retries if creation races' do
      expect(described_class)
        .to receive(:find_or_create_by)
        .with(name: 'default')
        .and_raise(ActiveRecord::RecordNotUnique, 'fail')
        .once

      expect(described_class)
        .to receive(:find_or_create_by)
        .with(name: 'default')
        .and_call_original

      expect(described_class.by_name('default')).to eq(default_shard)
    end
  end
end