summaryrefslogtreecommitdiff
path: root/spec/services/projects/prometheus/alerts/create_service_spec.rb
blob: c0bc9336558afefbb87b1fcf1a31f2b700466a53 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::Prometheus::Alerts::CreateService do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user) }

  let(:service) { described_class.new(project, user, params) }

  subject { service.execute }

  describe '#execute' do
    context 'with params' do
      let_it_be(:environment) { create(:environment, project: project) }

      let_it_be(:metric) do
        create(:prometheus_metric, project: project)
      end

      let(:params) do
        {
          environment_id: environment.id,
          prometheus_metric_id: metric.id,
          operator: '<',
          threshold: 1.0
        }
      end

      it 'creates an alert' do
        expect(subject).to be_persisted

        expect(subject).to have_attributes(
          project: project,
          environment: environment,
          prometheus_metric: metric,
          operator: 'lt',
          threshold: 1.0
        )
      end
    end

    context 'without params' do
      let(:params) { {} }

      it 'fails to create' do
        expect(subject).to be_new_record
        expect(subject).to be_invalid
      end
    end
  end
end