Commit d9f337fe authored by Piotr Skorupa's avatar Piotr Skorupa

Add metric definition schema validation spec

parent 5dce47e8
{
"type": "object",
"description": "Histogram (buckets 1 to 100) of projects with at least 1 enabled integration"
"description": "Histogram (buckets 1 to 100) of projects with at least 1 enabled integration",
"propertyNames": {
"pattern": "^[1-9][0-9]?0?$"
}
......
......@@ -51,7 +51,6 @@ RSpec.describe 'Every metric definition' do
Gitlab::Usage::MetricDefinition
.definitions
.select { |k, v| v.respond_to?(:value_json_schema) }
.keys
end
# Recursively traverse nested Hash of a generated Usage Ping to return an Array of key paths
......@@ -67,7 +66,7 @@ RSpec.describe 'Every metric definition' do
end
def object_with_schema?(key_path)
metric_files_with_schema.include?(key_path)
metric_files_with_schema.keys.include?(key_path)
end
before do
......@@ -81,4 +80,14 @@ RSpec.describe 'Every metric definition' do
it 'is included in the Usage Ping hash structure' do
expect(metric_files_key_paths).to match_array(usage_ping_key_paths)
end
context 'with value json schema' do
it 'has a valid structure' do
metric_files_with_schema.each do |key_path, metric|
structure = usage_ping.dig(*key_path.split('.').map(&:to_sym))
expect(structure).to match_metric_definition_schema(metric.value_json_schema)
end
end
end
end
......@@ -6,7 +6,7 @@ module Gitlab
module Instrumentations
class CollectedDataCategoriesMetric < GenericMetric
value do
::ServicePing::PermitDataCategoriesService.new.execute
::ServicePing::PermitDataCategoriesService.new.execute.to_a
end
end
end
......
......@@ -45,6 +45,17 @@ RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
end
end
RSpec::Matchers.define :match_metric_definition_schema do |path, dir: nil, **options|
match do |data|
schema_path = Pathname.new(Rails.root.join(dir.to_s, path).to_s)
validator = SchemaPath.validator(schema_path)
data = data.stringify_keys if data.is_a? Hash
validator.valid?(data)
end
end
RSpec::Matchers.define :match_snowplow_schema do |schema, dir: nil, **options|
match do |data|
schema_path = Pathname.new(Rails.root.join(dir.to_s, 'spec', "fixtures/product_intelligence/#{schema}.json").to_s)
......
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