json_matchers 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +11 -0
- data/.ruby-version +1 -1
- data/.thoughtbot.rubocop.yml +13 -3
- data/.travis.yml +1 -1
- data/CONTRIBUTING.md +3 -2
- data/Gemfile +1 -1
- data/NEWS.md +8 -0
- data/README.md +61 -44
- data/Rakefile +6 -1
- data/json_matchers.gemspec +3 -0
- data/lib/json_matchers.rb +0 -2
- data/lib/json_matchers/assertion.rb +68 -0
- data/lib/json_matchers/matcher.rb +4 -4
- data/lib/json_matchers/minitest/assertions.rb +26 -0
- data/lib/json_matchers/payload.rb +1 -1
- data/lib/json_matchers/rspec.rb +19 -79
- data/lib/json_matchers/validator.rb +2 -3
- data/lib/json_matchers/version.rb +1 -1
- data/spec/factories.rb +88 -0
- data/spec/json_matchers/match_json_schema_spec.rb +160 -174
- data/spec/spec_helper.rb +1 -1
- data/spec/support/factory_bot.rb +9 -0
- data/spec/support/fake_response.rb +9 -0
- data/spec/support/fake_schema.rb +9 -0
- data/spec/support/file_helpers.rb +0 -26
- data/test/json_matchers/minitest/assertions_test.rb +177 -0
- data/test/support/factory_bot.rb +5 -0
- data/test/support/json_matchers/test_case.rb +18 -0
- data/test/test_helper.rb +8 -0
- metadata +63 -3
data/spec/spec_helper.rb
CHANGED
@@ -1,32 +1,6 @@
|
|
1
1
|
require "fileutils"
|
2
|
-
require "json"
|
3
2
|
|
4
3
|
module FileHelpers
|
5
|
-
FakeResponse = Struct.new(:body)
|
6
|
-
|
7
|
-
def create_schema(name, json)
|
8
|
-
path = File.join(JsonMatchers.schema_root, "#{name}.json")
|
9
|
-
|
10
|
-
File.open(path, "w") do |file|
|
11
|
-
case json
|
12
|
-
when NilClass, String
|
13
|
-
file.write(json.to_s)
|
14
|
-
else
|
15
|
-
file.write(JSON.generate(json))
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def response_for(json)
|
21
|
-
response_body = case json
|
22
|
-
when String, NilClass
|
23
|
-
json.to_s
|
24
|
-
else
|
25
|
-
json.to_json
|
26
|
-
end
|
27
|
-
FakeResponse.new(response_body)
|
28
|
-
end
|
29
|
-
|
30
4
|
def setup_fixtures(*pathnames)
|
31
5
|
JSON::Validator.clear_cache
|
32
6
|
original_schema_root = JsonMatchers.schema_root
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require_relative "../../test_helper"
|
2
|
+
require "active_support/core_ext/string"
|
3
|
+
|
4
|
+
class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
|
5
|
+
test "fails with an invalid JSON schema" do
|
6
|
+
schema = create(:schema, :invalid)
|
7
|
+
|
8
|
+
json = build(:response)
|
9
|
+
|
10
|
+
assert_raises JsonMatchers::InvalidSchemaError do
|
11
|
+
assert_matches_json_schema(json, schema)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
test "does not fail with an empty JSON body" do
|
16
|
+
schema = create(:schema, {})
|
17
|
+
|
18
|
+
json = build(:response, {})
|
19
|
+
|
20
|
+
assert_matches_json_schema(json, schema)
|
21
|
+
end
|
22
|
+
|
23
|
+
test "fails when the body contains a property with the wrong type" do
|
24
|
+
schema = create(:schema, :object)
|
25
|
+
|
26
|
+
json = build(:response, :invalid_object)
|
27
|
+
|
28
|
+
refute_matches_json_schema(json, schema)
|
29
|
+
end
|
30
|
+
|
31
|
+
test "fails when the body is missing a required property" do
|
32
|
+
schema = create(:schema, :object)
|
33
|
+
|
34
|
+
json = build(:response, {})
|
35
|
+
|
36
|
+
refute_matches_json_schema(json, schema)
|
37
|
+
end
|
38
|
+
|
39
|
+
test "when passed a Hash, validates that the schema matches" do
|
40
|
+
schema = create(:schema, :object)
|
41
|
+
|
42
|
+
json = build(:response, :object)
|
43
|
+
json_as_hash = json.to_h
|
44
|
+
|
45
|
+
assert_matches_json_schema(json_as_hash, schema)
|
46
|
+
end
|
47
|
+
|
48
|
+
test "when passed a Hash, fails with message when negated" do
|
49
|
+
schema = create(:schema, :object)
|
50
|
+
|
51
|
+
json = build(:response, :invalid_object)
|
52
|
+
json_as_hash = json.to_h
|
53
|
+
|
54
|
+
assert_raises_error_containing(schema) do
|
55
|
+
assert_matches_json_schema(json_as_hash, schema)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
test "when passed a Array, validates a root-level Array in the JSON" do
|
60
|
+
schema = create(:schema, :array_of, :objects)
|
61
|
+
|
62
|
+
json = build(:response, :object)
|
63
|
+
json_as_array = [json.to_h]
|
64
|
+
|
65
|
+
assert_matches_json_schema(json_as_array, schema)
|
66
|
+
end
|
67
|
+
|
68
|
+
test "when passed a Array, refutes a root-level Array in the JSON" do
|
69
|
+
schema = create(:schema, :array_of, :objects)
|
70
|
+
|
71
|
+
json = build(:response, :invalid_object)
|
72
|
+
json_as_array = [json.to_h]
|
73
|
+
|
74
|
+
refute_matches_json_schema(json_as_array, schema)
|
75
|
+
end
|
76
|
+
|
77
|
+
test "when passed a Array, fails with message when negated" do
|
78
|
+
schema = create(:schema, :array_of, :object)
|
79
|
+
|
80
|
+
json = build(:response, :invalid_object)
|
81
|
+
json_as_array = [json.to_h]
|
82
|
+
|
83
|
+
assert_raises_error_containing(schema) do
|
84
|
+
assert_matches_json_schema(json_as_array, schema)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
test "when JSON is a string, validates that the schema matches" do
|
89
|
+
schema = create(:schema, :object)
|
90
|
+
|
91
|
+
json = build(:response, :object)
|
92
|
+
json_as_string = json.to_json
|
93
|
+
|
94
|
+
assert_matches_json_schema(json_as_string, schema)
|
95
|
+
end
|
96
|
+
|
97
|
+
test "when JSON is a string, fails with message when negated" do
|
98
|
+
schema = create(:schema, :object)
|
99
|
+
|
100
|
+
json = build(:response, :invalid_object)
|
101
|
+
json_as_string = json.to_json
|
102
|
+
|
103
|
+
assert_raises_error_containing(schema) do
|
104
|
+
assert_matches_json_schema(json_as_string, schema)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
test "the failure message contains the body" do
|
109
|
+
schema = create(:schema, :object)
|
110
|
+
|
111
|
+
json = build(:response, :invalid_object)
|
112
|
+
|
113
|
+
assert_raises_error_containing(json) do
|
114
|
+
assert_matches_json_schema(json, schema)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
test "the failure message contains the schema" do
|
119
|
+
schema = create(:schema, :object)
|
120
|
+
|
121
|
+
json = build(:response, :invalid_object)
|
122
|
+
|
123
|
+
assert_raises_error_containing(schema) do
|
124
|
+
assert_matches_json_schema(json, schema)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
test "the failure message when negated, contains the body" do
|
129
|
+
schema = create(:schema, :object)
|
130
|
+
|
131
|
+
json = build(:response, :object)
|
132
|
+
|
133
|
+
assert_raises_error_containing(json) do
|
134
|
+
refute_matches_json_schema(json, schema)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
test "the failure message when negated, contains the schema" do
|
139
|
+
schema = create(:schema, :object)
|
140
|
+
|
141
|
+
json = build(:response, :object)
|
142
|
+
|
143
|
+
assert_raises_error_containing(schema) do
|
144
|
+
refute_matches_json_schema(json, schema)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
test "asserts valid JSON against a schema that uses $ref" do
|
149
|
+
schema = create(:schema, :referencing_objects)
|
150
|
+
|
151
|
+
json = build(:response, :object)
|
152
|
+
json_as_array = [json.to_h]
|
153
|
+
|
154
|
+
assert_matches_json_schema(json_as_array, schema)
|
155
|
+
end
|
156
|
+
|
157
|
+
test "refutes valid JSON against a schema that uses $ref" do
|
158
|
+
schema = create(:schema, :referencing_objects)
|
159
|
+
|
160
|
+
json = build(:response, :invalid_object)
|
161
|
+
json_as_array = [json.to_h]
|
162
|
+
|
163
|
+
refute_matches_json_schema(json_as_array, schema)
|
164
|
+
end
|
165
|
+
|
166
|
+
def assert_raises_error_containing(schema_or_body)
|
167
|
+
raised_error = assert_raises(Minitest::Assertion) do
|
168
|
+
yield
|
169
|
+
end
|
170
|
+
|
171
|
+
sanitized_message = raised_error.message.squish
|
172
|
+
json = JSON.pretty_generate(schema_or_body.to_h)
|
173
|
+
error_message = json.squish
|
174
|
+
|
175
|
+
assert_includes sanitized_message, error_message
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "active_support/testing/declarative"
|
2
|
+
require_relative "../../../spec/support/file_helpers"
|
3
|
+
|
4
|
+
module JsonMatchers
|
5
|
+
class TestCase < ::Minitest::Test
|
6
|
+
extend ActiveSupport::Testing::Declarative
|
7
|
+
|
8
|
+
include FileHelpers
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@original_schema_root = setup_fixtures("test", "fixtures", "schemas")
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
teardown_fixtures(@original_schema_root)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Doyle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -80,6 +80,48 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: factory_bot
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.8'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '4.8'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activesupport
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
83
125
|
description: Validate your Rails JSON API's JSON
|
84
126
|
email:
|
85
127
|
- sean.p.doyle24@gmail.com
|
@@ -102,18 +144,28 @@ files:
|
|
102
144
|
- Rakefile
|
103
145
|
- json_matchers.gemspec
|
104
146
|
- lib/json_matchers.rb
|
147
|
+
- lib/json_matchers/assertion.rb
|
105
148
|
- lib/json_matchers/configuration.rb
|
106
149
|
- lib/json_matchers/errors.rb
|
107
150
|
- lib/json_matchers/matcher.rb
|
151
|
+
- lib/json_matchers/minitest/assertions.rb
|
108
152
|
- lib/json_matchers/payload.rb
|
109
153
|
- lib/json_matchers/rspec.rb
|
110
154
|
- lib/json_matchers/validator.rb
|
111
155
|
- lib/json_matchers/version.rb
|
156
|
+
- spec/factories.rb
|
112
157
|
- spec/json_matchers/configuration_spec.rb
|
113
158
|
- spec/json_matchers/match_json_schema_spec.rb
|
114
159
|
- spec/spec_helper.rb
|
115
160
|
- spec/support/configuration.rb
|
161
|
+
- spec/support/factory_bot.rb
|
162
|
+
- spec/support/fake_response.rb
|
163
|
+
- spec/support/fake_schema.rb
|
116
164
|
- spec/support/file_helpers.rb
|
165
|
+
- test/json_matchers/minitest/assertions_test.rb
|
166
|
+
- test/support/factory_bot.rb
|
167
|
+
- test/support/json_matchers/test_case.rb
|
168
|
+
- test/test_helper.rb
|
117
169
|
homepage: https://github.com/thoughtbot/json_matchers
|
118
170
|
licenses:
|
119
171
|
- MIT
|
@@ -134,13 +186,21 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
186
|
version: '0'
|
135
187
|
requirements: []
|
136
188
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.
|
189
|
+
rubygems_version: 2.7.3
|
138
190
|
signing_key:
|
139
191
|
specification_version: 4
|
140
192
|
summary: Validate your Rails JSON API's JSON
|
141
193
|
test_files:
|
194
|
+
- spec/factories.rb
|
142
195
|
- spec/json_matchers/configuration_spec.rb
|
143
196
|
- spec/json_matchers/match_json_schema_spec.rb
|
144
197
|
- spec/spec_helper.rb
|
145
198
|
- spec/support/configuration.rb
|
199
|
+
- spec/support/factory_bot.rb
|
200
|
+
- spec/support/fake_response.rb
|
201
|
+
- spec/support/fake_schema.rb
|
146
202
|
- spec/support/file_helpers.rb
|
203
|
+
- test/json_matchers/minitest/assertions_test.rb
|
204
|
+
- test/support/factory_bot.rb
|
205
|
+
- test/support/json_matchers/test_case.rb
|
206
|
+
- test/test_helper.rb
|