summaryrefslogtreecommitdiff
path: root/spec/models/plan_limits_spec.rb
blob: 1366f08862396d61cef7eec20af11ca174513cce (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
71
72
73
74
# frozen_string_literal: true

require 'spec_helper'

describe PlanLimits do
  let(:plan_limits) { create(:plan_limits) }
  let(:model) { ProjectHook }
  let(:count) { model.count }

  before do
    create(:project_hook)
  end

  context 'without plan limits configured' do
    describe '#exceeded?' do
      it 'does not exceed any relation offset' do
        expect(plan_limits.exceeded?(:project_hooks, model)).to be false
        expect(plan_limits.exceeded?(:project_hooks, count)).to be false
      end
    end
  end

  context 'with plan limits configured' do
    before do
      plan_limits.update!(project_hooks: 2)
    end

    describe '#exceeded?' do
      it 'does not exceed the relation offset' do
        expect(plan_limits.exceeded?(:project_hooks, model)).to be false
        expect(plan_limits.exceeded?(:project_hooks, count)).to be false
      end
    end

    context 'with boundary values' do
      before do
        create(:project_hook)
      end

      describe '#exceeded?' do
        it 'does exceed the relation offset' do
          expect(plan_limits.exceeded?(:project_hooks, model)).to be true
          expect(plan_limits.exceeded?(:project_hooks, count)).to be true
        end
      end
    end
  end

  context 'validates default values' do
    let(:columns_with_zero) do
      %w[
        ci_active_pipelines
        ci_pipeline_size
        ci_active_jobs
      ]
    end

    it "has positive values for enabled limits" do
      attributes = plan_limits.attributes
      attributes = attributes.except(described_class.primary_key)
      attributes = attributes.except(described_class.reflections.values.map(&:foreign_key))
      attributes = attributes.except(*columns_with_zero)

      expect(attributes).to all(include(be_positive))
    end

    it "has zero values for disabled limits" do
      attributes = plan_limits.attributes
      attributes = attributes.slice(*columns_with_zero)

      expect(attributes).to all(include(be_zero))
    end
  end
end