Skip to content

Commit 3a3f8d6

Browse files
author
danascheider
committed
Begin generating actual test objects
1 parent 21f3329 commit 3a3f8d6

4 files changed

Lines changed: 87 additions & 28 deletions

File tree

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
--color
2+
--format documentation
23
--require spec_helper

features/generate_data.feature

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Feature: Generate test data
2-
Scenario: Simple object
2+
Scenario: Empty object
33
Given the following JSON schema:
44
"""json
55
{
@@ -14,7 +14,7 @@ Feature: Generate test data
1414
{}
1515
"""
1616

17-
Scenario: Simple array
17+
Scenario: Empty array
1818
Given the following JSON schema:
1919
"""json
2020
{
@@ -28,3 +28,20 @@ Feature: Generate test data
2828
"""json
2929
[]
3030
"""
31+
32+
Scenario: Simple object
33+
Given the following JSON schema:
34+
"""json
35+
{
36+
"$schema": "http://json-schema.org/draft-04/schema#",
37+
"type": "object",
38+
"properties": {
39+
"name": "string"
40+
}
41+
}
42+
"""
43+
When I run the JSON data generator
44+
Then the JSON output should be:
45+
"""json
46+
{ "name": "string" }
47+
"""

lib/json_test_data/json_schema.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@ def initialize(schema)
77
end
88

99
def generate_example
10-
@schema.fetch(:type) == "object" ? {}.to_json : [].to_json
10+
@schema.fetch(:type) == "object" ? generate_object : generate_array
1111
end
12+
13+
private
14+
15+
def generate_object
16+
obj = {}
17+
18+
schema.fetch(:properties).each {|key, value| obj[key] = value.fetch(:type) }
19+
20+
obj.to_json
21+
end
22+
23+
def generate_array
24+
[].to_json
25+
end
1226
end
1327
end

spec/json_test_data/json_schema_spec.rb

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,70 @@
1515
end
1616

1717
describe "#generate_example" do
18-
context "when schema describes an object" do
19-
let(:schema) do
20-
JsonTestData::JsonSchema.new(
21-
{
22-
"$schema" => "http://json-schema.org/draft-04/schema#",
23-
"type" => "object",
24-
"properties" => {}
25-
}.to_json
26-
)
27-
end
18+
describe "trivial examples" do
19+
context "when schema describes an object" do
20+
let(:schema) do
21+
JsonTestData::JsonSchema.new(
22+
{
23+
"$schema" => "http://json-schema.org/draft-04/schema#",
24+
"type" => "object",
25+
"properties" => {}
26+
}.to_json
27+
)
28+
end
2829

29-
let(:output) do
30-
{}.to_json
30+
let(:output) do
31+
{}.to_json
32+
end
33+
34+
it "generates an object" do
35+
expect(schema.generate_example).to eql output
36+
end
3137
end
3238

33-
it "generates an object" do
34-
expect(schema.generate_example).to eql output
39+
context "when schema describes an array" do
40+
let(:schema) do
41+
JsonTestData::JsonSchema.new(
42+
{
43+
"$schema" => "http://json-schema.org/draft-04/schema#",
44+
"type" => "array",
45+
"items" => []
46+
}.to_json
47+
)
48+
end
49+
50+
let(:output) do
51+
[].to_json
52+
end
53+
54+
it "generates an object" do
55+
expect(schema.generate_example).to eql output
56+
end
3557
end
3658
end
3759

38-
context "when schema describes an array" do
60+
describe "bigger examples" do
3961
let(:schema) do
40-
JsonTestData::JsonSchema.new(
41-
{
42-
"$schema" => "http://json-schema.org/draft-04/schema#",
43-
"type" => "array",
44-
"items" => []
45-
}.to_json
46-
)
62+
{
63+
:"$schema" => "http://json-schema.org/draft-04/schema#",
64+
:type => "object",
65+
:properties => {
66+
:name => {
67+
:type => "string"
68+
}
69+
}
70+
}.to_json
4771
end
4872

4973
let(:output) do
50-
[].to_json
74+
{ "name" => "string" }.to_json
5175
end
5276

53-
it "generates an object" do
54-
expect(schema.generate_example).to eql output
77+
context "when schema describes an object" do
78+
it "generates the right object" do
79+
json_schema = JsonTestData::JsonSchema.new(schema)
80+
expect(json_schema.generate_example).to eql output
81+
end
5582
end
5683
end
5784
end

0 commit comments

Comments
 (0)