2020-05-19 00:07:58 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe PerformanceMonitoring::PrometheusMetric do
|
2020-05-19 00:07:58 +00:00
|
|
|
let(:json_content) do
|
|
|
|
{
|
|
|
|
"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 PrometheusMetric object' do
|
|
|
|
expect(subject).to be_a PerformanceMonitoring::PrometheusMetric
|
|
|
|
expect(subject.id).to eq(json_content['id'])
|
|
|
|
expect(subject.unit).to eq(json_content['unit'])
|
|
|
|
expect(subject.label).to eq(json_content['label'])
|
|
|
|
expect(subject.query_range).to eq(json_content['query_range'])
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
2020-06-05 18:08:19 +00:00
|
|
|
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
|
|
|
|
|
2020-05-19 00:07:58 +00:00
|
|
|
context 'when unit is missing' do
|
|
|
|
before do
|
|
|
|
json_content['unit'] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.from_json(json_content) }
|
|
|
|
|
|
|
|
it { expect { subject }.to raise_error(ActiveModel::ValidationError) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when query and query_range is missing' do
|
|
|
|
before do
|
|
|
|
json_content['query_range'] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.from_json(json_content) }
|
|
|
|
|
|
|
|
it { expect { subject }.to raise_error(ActiveModel::ValidationError) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when query_range is missing but query is available' do
|
|
|
|
before do
|
|
|
|
json_content['query_range'] = nil
|
|
|
|
json_content['query'] = 'http_requests_total'
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.from_json(json_content) }
|
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|