Welcome to mirror list, hosted at ThFree Co, Russian Federation.

schema_matcher.rb « matchers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2f32b604640d4efe609b681b46979bb90e6eeb4 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

module SchemaPath
  @schema_cache = {}

  def self.expand(schema, dir = nil)
    return schema unless schema.is_a?(String)

    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
  end

  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
end

RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
  match do |response|
    @schema_path = Pathname.new(SchemaPath.expand(schema, dir))
    validator = SchemaPath.validator(@schema_path)

    @data = Gitlab::Json.parse(response.body)

    @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")
  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)
    validator = SchemaPath.validator(schema_path)

    validator.valid?(data.stringify_keys)
  end
end

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)

    if data.is_a?(String)
      validator.valid?(Gitlab::Json.parse(data))
    else
      validator.valid?(data)
    end
  end
end