debian-mirror-gitlab/spec/support/matchers/schema_matcher.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

107 lines
2.9 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module SchemaPath
2021-06-08 01:23:25 +05:30
@schema_cache = {}
2019-07-07 11:18:12 +05:30
def self.expand(schema, dir = nil)
2021-03-08 18:12:59 +05:30
return schema unless schema.is_a?(String)
2019-07-07 11:18:12 +05:30
if Gitlab.ee? && dir.nil?
ee_path = expand(schema, 'ee')
return ee_path if File.exist?(ee_path)
end
Rails.root.join(dir.to_s, 'spec', "fixtures/api/schemas/#{schema}.json").to_s
2017-09-10 17:25:29 +05:30
end
2021-06-08 01:23:25 +05:30
def self.validator(schema_path)
unless @schema_cache.key?(schema_path)
@schema_cache[schema_path] = JSONSchemer.schema(schema_path, ref_resolver: SchemaPath.file_ref_resolver)
end
@schema_cache[schema_path]
end
def self.file_ref_resolver
proc do |uri|
file = Rails.root.join(uri.path)
raise StandardError, "Ref file #{uri.path} must be json" unless uri.path.ends_with?('.json')
raise StandardError, "File #{file.to_path} doesn't exists" unless file.exist?
Gitlab::Json.parse(File.read(file))
end
end
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
2016-09-13 17:45:13 +05:30
match do |response|
2022-04-04 11:22:00 +05:30
@schema_path = Pathname.new(SchemaPath.expand(schema, dir))
validator = SchemaPath.validator(@schema_path)
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
@data = Gitlab::Json.parse(response.body)
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
@schema_errors = validator.validate(@data)
@schema_errors.none?
end
failure_message do |actual|
message = []
message << <<~MESSAGE
expected JSON response to match schema #{@schema_path.inspect}.
JSON input: #{Gitlab::Json.pretty_generate(@data).indent(2)}
Schema errors:
MESSAGE
@schema_errors.each do |error|
property_name, actual_value = error.values_at('data_pointer', 'data')
property_name = 'root' if property_name.empty?
message << <<~MESSAGE
Property: #{property_name}
Actual value: #{Gitlab::Json.pretty_generate(actual_value).indent(2)}
Error: #{JSONSchemer::Errors.pretty(error)}
MESSAGE
end
message.join("\n")
2017-09-10 17:25:29 +05:30
end
end
2016-09-13 17:45:13 +05:30
2021-10-27 15:23:28 +05:30
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
2021-06-08 01:23:25 +05:30
RSpec::Matchers.define :match_snowplow_schema do |schema, dir: nil, **options|
2017-09-10 17:25:29 +05:30
match do |data|
2021-06-08 01:23:25 +05:30
schema_path = Pathname.new(Rails.root.join(dir.to_s, 'spec', "fixtures/product_intelligence/#{schema}.json").to_s)
validator = SchemaPath.validator(schema_path)
2019-07-07 11:18:12 +05:30
2021-06-08 01:23:25 +05:30
validator.valid?(data.stringify_keys)
2016-09-13 17:45:13 +05:30
end
2021-06-08 01:23:25 +05:30
end
2021-03-08 18:12:59 +05:30
2021-06-08 01:23:25 +05:30
RSpec::Matchers.define :match_schema do |schema, dir: nil, **options|
match do |data|
schema = SchemaPath.expand(schema, dir)
schema = Pathname.new(schema) if schema.is_a?(String)
validator = SchemaPath.validator(schema)
2021-03-08 18:12:59 +05:30
2021-06-08 01:23:25 +05:30
if data.is_a?(String)
validator.valid?(Gitlab::Json.parse(data))
else
validator.valid?(data)
end
2021-03-08 18:12:59 +05:30
end
2016-09-13 17:45:13 +05:30
end