summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models/concerns/ttl_expirable_shared_examples.rb
blob: a4e0d6c871e6eb1d81d708e553ba9a2ef381f433 (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.shared_examples 'ttl_expirable' do
  let_it_be(:class_symbol) { described_class.model_name.param_key.to_sym }

  it_behaves_like 'having unique enum values'

  describe 'validations' do
    it { is_expected.to validate_presence_of(:status) }
  end

  describe '.updated_before' do
    # rubocop:disable Rails/SaveBang
    let_it_be_with_reload(:item1) { create(class_symbol) }
    let_it_be(:item2) { create(class_symbol) }
    # rubocop:enable Rails/SaveBang

    before do
      item1.update_column(:updated_at, 1.month.ago)
    end

    it 'returns items with created at older than the supplied number of days' do
      expect(described_class.updated_before(10)).to contain_exactly(item1)
    end
  end

  describe '.active' do
    # rubocop:disable Rails/SaveBang
    let_it_be(:item1) { create(class_symbol) }
    let_it_be(:item2) { create(class_symbol, :expired) }
    let_it_be(:item3) { create(class_symbol, status: :error) }
    # rubocop:enable Rails/SaveBang

    it 'returns only active items' do
      expect(described_class.active).to contain_exactly(item1)
    end
  end

  describe '.lock_next_by' do
    let_it_be(:item1) { create(class_symbol, created_at: 1.month.ago, updated_at: 1.day.ago) }
    let_it_be(:item2) { create(class_symbol, created_at: 1.year.ago, updated_at: 1.year.ago) }
    let_it_be(:item3) { create(class_symbol, created_at: 2.years.ago, updated_at: 1.month.ago) }

    it 'returns the first item sorted by the argument' do
      expect(described_class.lock_next_by(:updated_at)).to contain_exactly(item2)
      expect(described_class.lock_next_by(:created_at)).to contain_exactly(item3)
    end
  end
end