Class: Committee::Test::SchemaCoverage

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/test/schema_coverage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaCoverage

Returns a new instance of SchemaCoverage.



46
47
48
49
50
51
# File 'lib/committee/test/schema_coverage.rb', line 46

def initialize(schema)
  raise 'Unsupported schema' unless schema.is_a?(Committee::Drivers::OpenAPI3::Schema)

  @schema = schema
  @covered = {}
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/committee/test/schema_coverage.rb', line 6

def schema
  @schema
end

Class Method Details

.flatten_report(report) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/committee/test/schema_coverage.rb', line 25

def flatten_report(report)
  responses = []
  report.each do |path_name, path_coverage|
    path_coverage.each do |method, method_coverage|
      responses_coverage = method_coverage['responses']
      responses_coverage.each do |response_status, is_covered|
        responses << {
          path: path_name,
          method: method,
          status: response_status,
          is_covered: is_covered,
        }
      end
    end
  end
  {
    responses: responses,
  }
end

.merge_report(first, second) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/committee/test/schema_coverage.rb', line 9

def merge_report(first, second)
  report = first.dup
  second.each do |k, v|
    if v.is_a?(Hash)
      if report[k].nil?
        report[k] = v
      else
        report[k] = merge_report(report[k], v)
      end
    else
      report[k] ||= v
    end
  end
  report
end

Instance Method Details

#reportObject



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
# File 'lib/committee/test/schema_coverage.rb', line 63

def report
  report = {}

  schema.open_api.paths.path.each do |path_name, path_item|
    report[path_name] = {}
    path_item._openapi_all_child_objects.each do |object_name, object|
      next unless object.is_a?(OpenAPIParser::Schemas::Operation)

      method = object_name.split('/').last&.downcase
      next unless method

      report[path_name][method] ||= {}

      # TODO: check coverage on request params/body as well?

      report[path_name][method]['responses'] ||= {}
      object.responses.response.each do |response_status, _|
        is_covered = @covered.dig(path_name, method, 'responses', response_status) || false
        report[path_name][method]['responses'][response_status] = is_covered
      end
      if object.responses.default
        is_default_covered = (@covered.dig(path_name, method, 'responses') || {}).any? do |status, is_covered|
          is_covered && !object.responses.response.key?(status)
        end
        report[path_name][method]['responses']['default'] = is_default_covered
      end
    end
  end

  report
end

#report_flattenObject



95
96
97
# File 'lib/committee/test/schema_coverage.rb', line 95

def report_flatten
  self.class.flatten_report(report)
end

#update_response_coverage!(path, method, response_status) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/committee/test/schema_coverage.rb', line 53

def update_response_coverage!(path, method, response_status)
  method = method.to_s.downcase
  response_status = response_status.to_s

  @covered[path] ||= {}
  @covered[path][method] ||= {}
  @covered[path][method]['responses'] ||= {}
  @covered[path][method]['responses'][response_status] = true
end