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

require 'spec_helper'

describe PerformanceMonitoring::PrometheusPanelGroup do
  let(:json_content) do
    {
      "group" => "Group Title",
      "panels" => [{
        "type" => "area-chart",
        "title" => "Chart Title",
        "y_label" => "Y-Axis",
        "metrics" => [{
          "id" => "metric_of_ages",
          "unit" => "count",
          "label" => "Metric of Ages",
          "query_range" => "http_requests_total"
        }]
      }]
    }
  end

  describe '.from_json' do
    subject { described_class.from_json(json_content) }

    it 'creates a PrometheusPanelGroup object' do
      expect(subject).to be_a PerformanceMonitoring::PrometheusPanelGroup
      expect(subject.group).to eq(json_content['group'])
      expect(subject.panels).to all(be_a PerformanceMonitoring::PrometheusPanel)
    end

    describe 'validations' do
      context 'json_content is not a hash' do
        let(:json_content) { nil }

        subject { described_class.from_json(json_content) }

        it { expect { subject }.to raise_error(ActiveModel::ValidationError) }
      end

      context 'when group is missing' do
        before do
          json_content.delete('group')
        end

        subject { described_class.from_json(json_content) }

        it { expect { subject }.to raise_error(ActiveModel::ValidationError) }
      end

      context 'when panels are missing' do
        before do
          json_content['panels'] = []
        end

        subject { described_class.from_json(json_content) }

        it { expect { subject }.to raise_error(ActiveModel::ValidationError) }
      end
    end
  end
end