-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test/schema coverage #297
Test/schema coverage #297
Changes from all commits
165f33c
f8b71c9
508c7be
4d93744
e6d0647
9e60f00
718222c
63c036d
0712eed
1681c87
5778b2c
c0ccbb1
32600b2
9c9f330
8043cc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,10 @@ def build_schema_validator(request) | |
end | ||
|
||
def operation_object(request) | ||
return nil unless includes_request?(request) | ||
|
||
path = request.path | ||
path = path.gsub(@prefix_regexp, '') if prefix_request?(request) | ||
path = path.gsub(@prefix_regexp, '') if @prefix_regexp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice 👍 👍 👍 👍 |
||
|
||
request_method = request.request_method.downcase | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# frozen_string_literal: true | ||
|
||
module Committee | ||
module Test | ||
class SchemaCoverage | ||
attr_reader :schema | ||
|
||
class << self | ||
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 | ||
|
||
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 | ||
end | ||
|
||
def initialize(schema) | ||
raise 'Unsupported schema' unless schema.is_a?(Committee::Drivers::OpenAPI3::Schema) | ||
|
||
@schema = schema | ||
@covered = {} | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
def report_flatten | ||
self.class.flatten_report(report) | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: OpenAPI3 Coverage Test | ||
description: A Sample file for coverage test | ||
servers: | ||
- url: https://github.com/interagent/committee/ | ||
paths: | ||
/threads/{id}: | ||
parameters: | ||
- name: id | ||
in: path | ||
required: true | ||
schema: | ||
type: string | ||
get: | ||
description: get a thread | ||
responses: | ||
'200': | ||
description: success | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
/posts: | ||
get: | ||
description: get a post | ||
responses: | ||
'200': | ||
description: success | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
'404': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OpenAPI 3 support So please implement and test it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done via 0712eed |
||
description: post not found | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
default: | ||
description: unknown request | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
post: | ||
description: create a new post | ||
responses: | ||
'200': | ||
description: success | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
/likes: | ||
post: | ||
description: like a post | ||
responses: | ||
'200': | ||
description: success | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
delete: | ||
description: unlike a post | ||
responses: | ||
'200': | ||
description: success | ||
content: | ||
application/json: | ||
schema: | ||
type: object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nice!!!!!!!!!!!!!!!!! 👍 👍 👍 👍 👍 👍 👍 👍 👍