json_test_data 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/features/conditional_properties.feature +76 -0
- data/lib/json_test_data.rb +3 -3
- data/lib/json_test_data/json_schema.rb +15 -0
- data/spec/json_test_data/json_schema_spec.rb +63 -1
- data/spec/matchers/json_schema_matchers.rb +8 -2
- data/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c5f6f0377bba24e8f816a1f8b3b327e144822d1
|
4
|
+
data.tar.gz: f53bf688aa2652360877105f3f1024d6a165df52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31f89873ed4f158663b44f77f89f8b42ef23ec177e28a938471e71bb08febe2a4dc6551ab68c4d72423f38119e8ba23428ae48c7d396ff0ec29dc2c8f9c1df6c
|
7
|
+
data.tar.gz: cf50baf1ce792786b37bce046a8de4a142587773eb24dbe93cd9e413d3c952bf799b9b9d94b31264ba23d8586eafcc52133b1393f44cf75234d6017bf809c7ce
|
data/Gemfile.lock
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
Feature: Assign values conditionally
|
2
|
+
Scenario: Multiple schemas inline
|
3
|
+
Given the following JSON schema:
|
4
|
+
"""
|
5
|
+
{
|
6
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
7
|
+
"type": "object",
|
8
|
+
"oneOf": [
|
9
|
+
{
|
10
|
+
"required": [ "foo", "bar" ],
|
11
|
+
"properties": {
|
12
|
+
"foo": { "type": "string" },
|
13
|
+
"bar": { "type": "integer" }
|
14
|
+
}
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"required": [ "baz", "qux" ],
|
18
|
+
"properties": {
|
19
|
+
"baz": { "type": "string" },
|
20
|
+
"qux": { "type": "string" }
|
21
|
+
}
|
22
|
+
}
|
23
|
+
]
|
24
|
+
}
|
25
|
+
"""
|
26
|
+
When I run the JSON data generator
|
27
|
+
Then the output should match the schema
|
28
|
+
|
29
|
+
Scenario: Multiple schemas with refs
|
30
|
+
Given the following JSON schema:
|
31
|
+
"""
|
32
|
+
{
|
33
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
34
|
+
"type": "object",
|
35
|
+
"oneOf": [
|
36
|
+
{ "$ref": "#/definitions/californiaAddress" },
|
37
|
+
{ "$ref": "#/definitions/floridaAddress"}
|
38
|
+
],
|
39
|
+
"definitions": {
|
40
|
+
"californiaAddress": {
|
41
|
+
"required": [ "city", "state" ],
|
42
|
+
"properties": {
|
43
|
+
"city": {
|
44
|
+
"type": "string",
|
45
|
+
"enum": [
|
46
|
+
"San Diego",
|
47
|
+
"Los Angeles"
|
48
|
+
]
|
49
|
+
},
|
50
|
+
"state": {
|
51
|
+
"type": "string",
|
52
|
+
"enum": [ "CA" ]
|
53
|
+
}
|
54
|
+
}
|
55
|
+
},
|
56
|
+
"floridaAddress": {
|
57
|
+
"required": [ "city", "state" ],
|
58
|
+
"properties": {
|
59
|
+
"city": {
|
60
|
+
"type": "string",
|
61
|
+
"enum": [
|
62
|
+
"Miami",
|
63
|
+
"Orlando"
|
64
|
+
]
|
65
|
+
},
|
66
|
+
"state": {
|
67
|
+
"type": "string",
|
68
|
+
"enum": [ "FL" ]
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
"""
|
75
|
+
When I run the JSON data generator
|
76
|
+
Then the output should match the schema
|
data/lib/json_test_data.rb
CHANGED
@@ -45,9 +45,24 @@ module JsonTestData
|
|
45
45
|
property.fetch(:type, nil) == "array"
|
46
46
|
end
|
47
47
|
|
48
|
+
def resolve_ref(object, ref)
|
49
|
+
ref = ref.split("/").map(&:to_sym)[1..-1]
|
50
|
+
ref.each {|key| object = object.fetch(key) }
|
51
|
+
object
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_object(obj, schema)
|
55
|
+
schema.has_key?(:"$ref") ? resolve_ref(obj, schema.fetch(:"$ref")) : schema
|
56
|
+
end
|
57
|
+
|
48
58
|
def generate_object(object)
|
49
59
|
obj = {}
|
50
60
|
|
61
|
+
if object.has_key?(:oneOf)
|
62
|
+
schema_to_be_used = object.fetch(:oneOf).sample
|
63
|
+
object = set_object(object, schema_to_be_used)
|
64
|
+
end
|
65
|
+
|
51
66
|
if object.has_key?(:properties)
|
52
67
|
object.fetch(:properties).each do |k, v|
|
53
68
|
obj[k] = nil unless v.has_key?(:type)
|
@@ -60,7 +60,6 @@ describe JsonTestData::JsonSchema do
|
|
60
60
|
|
61
61
|
describe "bigger examples" do
|
62
62
|
context "when schema describes an object" do
|
63
|
-
|
64
63
|
let(:schema) do
|
65
64
|
{
|
66
65
|
:"$schema" => "http://json-schema.org/draft-04/schema#",
|
@@ -108,6 +107,69 @@ describe JsonTestData::JsonSchema do
|
|
108
107
|
end
|
109
108
|
end
|
110
109
|
|
110
|
+
describe "multiple possible schemas" do
|
111
|
+
let(:schema) do
|
112
|
+
{
|
113
|
+
:"$schema" => "http://json-schema.org/draft-04/schema#",
|
114
|
+
:type => "object",
|
115
|
+
:oneOf => [
|
116
|
+
{ :"$ref" => "#/definitions/foo" },
|
117
|
+
{ :"$ref" => "#/definitions/bar" }
|
118
|
+
],
|
119
|
+
:definitions => {
|
120
|
+
:foo => {
|
121
|
+
:type => "object",
|
122
|
+
:required => [ "name", "age" ],
|
123
|
+
:properties => {
|
124
|
+
:name => { :type => "string", :enum => [ "Foo" ] },
|
125
|
+
:age => { :type => "integer" }
|
126
|
+
}
|
127
|
+
},
|
128
|
+
:bar => {
|
129
|
+
:type => "object",
|
130
|
+
:required => [ "first_name", "last_name" ],
|
131
|
+
:properties => {
|
132
|
+
:first_name => { :type => "string", :enum => [ "Bar" ] },
|
133
|
+
:last_name => { :type => "string" }
|
134
|
+
}
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}.to_json
|
138
|
+
end
|
139
|
+
|
140
|
+
let(:foo_schema) do
|
141
|
+
{
|
142
|
+
:type => "object",
|
143
|
+
:required => [ "name", "age" ],
|
144
|
+
:properties => {
|
145
|
+
:name => { :type => "string", :enum => [ "Foo" ] },
|
146
|
+
:age => { :type => "integer" }
|
147
|
+
}
|
148
|
+
}.to_json
|
149
|
+
end
|
150
|
+
|
151
|
+
let(:bar_schema) do
|
152
|
+
{
|
153
|
+
:type => "object",
|
154
|
+
:required => [ "first_name", "last_name" ],
|
155
|
+
:properties => {
|
156
|
+
:first_name => { :type => "string", :enum => [ "Bar" ] },
|
157
|
+
:last_name => { :type => "string" }
|
158
|
+
}
|
159
|
+
}
|
160
|
+
end
|
161
|
+
|
162
|
+
it "generates an object" do
|
163
|
+
output = JsonTestData::JsonSchema.new(schema).generate_example
|
164
|
+
expect(JSON.parse(output)).to be_a Hash
|
165
|
+
end
|
166
|
+
|
167
|
+
it "matches one of the given schemas" do
|
168
|
+
output = JsonTestData::JsonSchema.new(schema).generate_example
|
169
|
+
expect(JSON.parse(output)).to match_one_of([foo_schema, bar_schema])
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
111
173
|
describe "data type support" do
|
112
174
|
context "numbers" do
|
113
175
|
|
@@ -1,8 +1,14 @@
|
|
1
1
|
require "rspec/expectations"
|
2
2
|
require "json-schema"
|
3
3
|
|
4
|
-
RSpec::Matchers.define :match_schema do |
|
4
|
+
RSpec::Matchers.define :match_schema do |schema|
|
5
5
|
match do |actual|
|
6
|
-
JSON::Validator.validate(
|
6
|
+
JSON::Validator.validate!(schema, actual)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec::Matchers.define :match_one_of do |schemas|
|
11
|
+
match do |actual|
|
12
|
+
JSON::Validator.validate(schemas[0], actual) || JSON::Validator.validate(schemas[1], actual)
|
7
13
|
end
|
8
14
|
end
|
data/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_test_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dana Scheider
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: regexp-examples
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- README.md
|
126
126
|
- Rakefile
|
127
127
|
- features/array_constraints.feature
|
128
|
+
- features/conditional_properties.feature
|
128
129
|
- features/data_types.feature
|
129
130
|
- features/generate_data.feature
|
130
131
|
- features/generate_empty_objects.feature
|
@@ -173,9 +174,10 @@ rubyforge_project:
|
|
173
174
|
rubygems_version: 2.5.1
|
174
175
|
signing_key:
|
175
176
|
specification_version: 4
|
176
|
-
summary: json_test_data-0.
|
177
|
+
summary: json_test_data-1.0.0
|
177
178
|
test_files:
|
178
179
|
- features/array_constraints.feature
|
180
|
+
- features/conditional_properties.feature
|
179
181
|
- features/data_types.feature
|
180
182
|
- features/generate_data.feature
|
181
183
|
- features/generate_empty_objects.feature
|