Skip to content

Commit

Permalink
passing with factories
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Apr 13, 2018
1 parent c0b99ea commit 06862a5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 120 deletions.
33 changes: 32 additions & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
json do
{
"id": "file:/#{name}.json#",
"description": "An object containing some #{name} data",
"type": "object",
"required": ["id"],
"properties": {
Expand All @@ -60,7 +61,7 @@
end

trait :referencing_objects do
association :items, factory: [:schema, :object]
association :items, factory: [:schema, :object], name: "object"

initialize_with do
FakeSchema.new(name, {
Expand All @@ -71,6 +72,36 @@
end
end

trait :referencing_definitions do
association :items, factory: [:schema, :object], name: "object"

transient do
plural { items.name.pluralize }
end

initialize_with do
FakeSchema.new(name, {
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "file:/#{name}.json#",
"type": "object",
"definitions": {
plural => {
"type": "array",
"items": { "$ref": "file:/#{items.name}.json#" },
"description": "A collection of #{plural}",
"example": [build(:response, :object)],
},
},
"required": [plural],
"properties": {
plural => {
"$ref": "#/definitions/#{plural}"
},
},
})
end
end

initialize_with do
schema_body_as_json = attributes.fetch(:json, nil)
schema_body = attributes.except(:json, :name)
Expand Down
93 changes: 34 additions & 59 deletions spec/json_matchers/match_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,65 +176,40 @@
end
end

it "supports $ref" do
create(:schema, {
name: "user",
"id": "file:/user.json#",
"type": "object",
"required": ["id"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"address": { "type": "string" },
},
})
schema = create(:schema, {
"id": "file:/users/index.json#",
"type": "object",
"definitions": {
"users": {
"description": "A collection of users",
"example": [{ "id": "1" }],
"type": "array",
"items": { "$ref": "file:/user.json#" },
},
},
"required": ["users"],
"properties": { "users": { "$ref": "#/definitions/users" } },
})

valid_response = build(:response, {
"users": [{ "id": 1, "name": "Me!", "address": "Here!" }],
})
invalid_response = build(:response, {
"users": [{ "id": "invalid", "name": "You!", "address": "There!" }],
})

expect(valid_response).to match_json_schema(schema)
expect(invalid_response).not_to match_json_schema(schema)
end

it "supports the 'id' keyword" do
create(:schema, {
name: "nested",
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "file:/nested.json#",
"type": "object",
"required": ["b"],
"properties": { "b": { "type": "string" } },
})
schema = create(:schema, {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"a": { "$ref": "file:/nested.json#" },
},
})
response_json = build(:response, { "a": { "b": "foo" } })
invalid_response_json = build(:response, { "a": { "b": 4 } })

expect(response_json).to match_json_schema(schema)
expect(invalid_response_json).not_to match_json_schema(schema)
it "validates against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :object)
json_as_array = [json.to_h]

expect(json_as_array).to match_json_schema(schema)
end

it "fails against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :invalid_object)
json_as_array = [json.to_h]

expect(json_as_array).not_to match_json_schema(schema)
end

it "validates against a schema referencing with 'definitions'" do
schema = create(:schema, :referencing_definitions)

json = build(:response, :object)
json_as_hash = { "objects" => [json] }

expect(json_as_hash).to match_json_schema(schema)
end

it "fails against a schema referencing with 'definitions'" do
schema = create(:schema, :referencing_definitions)

json = build(:response, :invalid_object)
json_as_hash = { "objects" => [json] }

expect(json_as_hash).not_to match_json_schema(schema)
end

def raise_error_containing(schema_or_body)
Expand Down
2 changes: 1 addition & 1 deletion spec/support/fake_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def to_h
JSON.parse(body)
end

def to_json
def to_json(*)
body
end
end
93 changes: 34 additions & 59 deletions test/json_matchers/minitest/assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,65 +145,40 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase
end
end

test "supports $ref" do
create(:schema, {
name: "user",
"id": "file:/user.json#",
"type": "object",
"required": ["id"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"address": { "type": "string" },
},
})
schema = create(:schema, {
"id": "file:/users/index.json#",
"type": "object",
"definitions": {
"users": {
"description": "A collection of users",
"example": [{ "id": "1" }],
"type": "array",
"items": { "$ref": "file:/user.json#" },
},
},
"required": ["users"],
"properties": { "users": { "$ref": "#/definitions/users" } },
})

valid_response = build(:response, {
"users": [{ "id": 1, "name": "Me!", "address": "Here!" }],
})
invalid_response = build(:response, {
"users": [{ "id": "invalid", "name": "You!", "address": "There!" }],
})

assert_matches_json_schema(valid_response, schema)
refute_matches_json_schema(invalid_response, schema)
end

test "supports the 'id' keyword" do
create(:schema, {
name: "nested",
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "file:/nested.json#",
"type": "object",
"required": ["b"],
"properties": { "b": { "type": "string" } },
})
schema = create(:schema, {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"a": { "$ref": "file:/nested.json#" },
},
})
response_json = build(:response, { "a": { "b": "foo" } })
invalid_response_json = build(:response, { "a": { "b": 4 } })

assert_matches_json_schema(response_json, schema)
refute_matches_json_schema(invalid_response_json, schema)
test "asserts valid JSON against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :object)
json_as_array = [json.to_h]

assert_matches_json_schema(json_as_array, schema)
end

test "refutes valid JSON against a schema that uses $ref" do
schema = create(:schema, :referencing_objects)

json = build(:response, :invalid_object)
json_as_array = [json.to_h]

refute_matches_json_schema(json_as_array, schema)
end

test "validates against a schema referencing with 'definitions'" do
schema = create(:schema, :referencing_definitions)

json = build(:response, :object)
json_as_hash = { "objects" => [json] }

assert_matches_json_schema(json_as_hash, schema)
end

test "fails against a schema referencing with 'definitions'" do
schema = create(:schema, :referencing_definitions)

json = build(:response, :invalid_object)
json_as_hash = { "objects" => [json] }

refute_matches_json_schema(json_as_hash, schema)
end

def assert_raises_error_containing(schema_or_body)
Expand Down

0 comments on commit 06862a5

Please sign in to comment.