Commit 092f06a7 authored by Piotr Skorupa's avatar Piotr Skorupa

Add validations to DevOpsReport::Metric

This commit:

- Adds presence and numericality validations to DevOpsReport::Metric
model to not rely on database constraints alone when handling record
creation.

- Moves the `METRICS` constant to the model to have a single source of
truth for these metric attributes.
parent 918b92eb
......@@ -6,6 +6,20 @@ module DevOpsReport
self.table_name = 'conversational_development_index_metrics'
METRICS = %w[leader_issues instance_issues percentage_issues leader_notes instance_notes
percentage_notes leader_milestones instance_milestones percentage_milestones
leader_boards instance_boards percentage_boards leader_merge_requests
instance_merge_requests percentage_merge_requests leader_ci_pipelines
instance_ci_pipelines percentage_ci_pipelines leader_environments instance_environments
percentage_environments leader_deployments instance_deployments percentage_deployments
leader_projects_prometheus_active instance_projects_prometheus_active
percentage_projects_prometheus_active leader_service_desk_issues instance_service_desk_issues
percentage_service_desk_issues].freeze
METRICS.each do |metric_name|
validates metric_name, presence: true, numericality: { greater_than_or_equal_to: 0 }
end
def instance_score(feature)
self["instance_#{feature}"]
end
......
......@@ -2,16 +2,6 @@
module ServicePing
class DevopsReportService
METRICS = %w[leader_issues instance_issues percentage_issues leader_notes instance_notes
percentage_notes leader_milestones instance_milestones percentage_milestones
leader_boards instance_boards percentage_boards leader_merge_requests
instance_merge_requests percentage_merge_requests leader_ci_pipelines
instance_ci_pipelines percentage_ci_pipelines leader_environments instance_environments
percentage_environments leader_deployments instance_deployments percentage_deployments
leader_projects_prometheus_active instance_projects_prometheus_active
percentage_projects_prometheus_active leader_service_desk_issues instance_service_desk_issues
percentage_service_desk_issues].freeze
def initialize(data)
@data = data
end
......@@ -22,7 +12,7 @@ module ServicePing
return unless metrics.except('usage_data_id').present?
DevOpsReport::Metric.create!(
metrics.slice(*METRICS)
metrics.slice(*DevOpsReport::Metric::METRICS)
)
end
end
......
......@@ -5,6 +5,13 @@ require 'spec_helper'
RSpec.describe DevOpsReport::Metric do
let(:conv_dev_index) { create(:dev_ops_report_metric) }
describe 'validations' do
DevOpsReport::Metric::METRICS.each do |metric_name|
it { is_expected.to validate_presence_of(metric_name) }
it { is_expected.to validate_numericality_of(metric_name).is_greater_than_or_equal_to(0) }
end
end
describe '#percentage_score' do
it 'returns stored percentage score' do
expect(conv_dev_index.percentage_score('issues')).to eq(13.331)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment