Commit ff86a2e8 authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'remove-duplicate-json-schema-matcher' into 'master'

Remove duplicate JSON schema matcher

See merge request gitlab-org/gitlab!50727
parents b5395017 34b49aec
......@@ -842,6 +842,41 @@ Example:
expect(response).to have_gitlab_http_status(:ok)
```
#### `match_schema` and `match_response_schema`
The `match_schema` matcher allows validating that the subject matches a
[JSON schema](https://json-schema.org/). The item inside `expect` can be
a JSON string or a JSON-compatible data structure.
`match_response_schema` is a convenience matcher for using with a
response object. from a [request
spec](testing_levels.md#integration-tests).
Examples:
```ruby
# Matches against spec/fixtures/api/schemas/prometheus/additional_metrics_query_result.json
expect(data).to match_schema('prometheus/additional_metrics_query_result')
# Matches against ee/spec/fixtures/api/schemas/board.json
expect(data).to match_schema('board', dir: 'ee')
# Matches against a schema made up of Ruby data structures
expect(data).to match_schema(Atlassian::Schemata.build_info)
```
#### `be_valid_json`
`be_valid_json` allows validating that a string parses as JSON and gives
a non-empty result. To combine it with the schema matching above, use
`and`:
```ruby
expect(json_string).to be_valid_json
expect(json_string).to be_valid_json.and match_schema(schema)
```
### Testing query performance
Testing query performance allows us to:
......
......@@ -114,7 +114,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
end
let(:body) do
matcher = be_valid_json.according_to_schema(schema)
matcher = be_valid_json.and match_schema(schema)
->(text) { matcher.matches?(text) }
end
......@@ -164,7 +164,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
end
let(:body) do
matcher = be_valid_json.according_to_schema(build_info_payload_schema)
matcher = be_valid_json.and match_schema(build_info_payload_schema)
->(text) { matcher.matches?(text) }
end
......
......@@ -23,7 +23,7 @@ RSpec.describe Atlassian::JiraConnect::Serializers::BuildEntity do
end
it 'is invalid, since it has no issue keys' do
expect(subject.to_json).not_to be_valid_json.according_to_schema(Atlassian::Schemata.build_info)
expect(subject.to_json).not_to match_schema(Atlassian::Schemata.build_info)
end
end
end
......@@ -43,7 +43,7 @@ RSpec.describe Atlassian::JiraConnect::Serializers::BuildEntity do
describe '#to_json' do
it 'is valid according to the build info schema' do
expect(subject.to_json).to be_valid_json.according_to_schema(Atlassian::Schemata.build_info)
expect(subject.to_json).to be_valid_json.and match_schema(Atlassian::Schemata.build_info)
end
end
end
......
......@@ -23,7 +23,7 @@ RSpec.describe Atlassian::JiraConnect::Serializers::DeploymentEntity do
end
it 'is invalid, since it has no issue keys' do
expect(subject.to_json).not_to be_valid_json.according_to_schema(Atlassian::Schemata.deployment_info)
expect(subject.to_json).not_to match_schema(Atlassian::Schemata.deployment_info)
end
end
end
......@@ -86,7 +86,7 @@ RSpec.describe Atlassian::JiraConnect::Serializers::DeploymentEntity do
describe '#to_json' do
it 'is valid according to the deployment info schema' do
expect(subject.to_json).to be_valid_json.according_to_schema(Atlassian::Schemata.deployment_info)
expect(subject.to_json).to be_valid_json.and match_schema(Atlassian::Schemata.deployment_info)
end
end
end
......
# frozen_string_literal: true
RSpec::Matchers.define :be_valid_json do
def according_to_schema(schema)
@schema = schema
self
end
match do |actual|
data = Gitlab::Json.parse(actual)
if @schema.present?
@validation_errors = JSON::Validator.fully_validate(@schema, data)
@validation_errors.empty?
else
data.present?
end
Gitlab::Json.parse(actual).present?
rescue JSON::ParserError => e
@error = e
false
......@@ -23,8 +11,6 @@ RSpec::Matchers.define :be_valid_json do
def failure_message
if @error
"Parse failed with error: #{@error}"
elsif @validation_errors.present?
"Validation failed because #{@validation_errors.join(', and ')}"
else
"Parsing did not return any data"
end
......
......@@ -2,6 +2,8 @@
module SchemaPath
def self.expand(schema, dir = nil)
return schema unless schema.is_a?(String)
if Gitlab.ee? && dir.nil?
ee_path = expand(schema, 'ee')
......
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