Skip to content

Commit

Permalink
Assert Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Apr 18, 2016
1 parent dd41e1b commit b15a566
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions lib/active_model_serializers/test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,36 @@ module Schema
# get :index
# assert_response_schema
def assert_response_schema(schema_path = nil, message = nil)
matcher = AssertResponseSchema.new(schema_path, response, message)
matcher = AssertResponseSchema.new(schema_path, request, response, message)
assert(matcher.call, matcher.message)
end

def assert_request_schema(schema_path = nil, message = nil)
matcher = AssertRequestSchema.new(schema_path, request, response, message)
assert(matcher.call, matcher.message)
end

def assert_request_response_schema(schema_path = nil, message = nil)
assert_request_schema(schema_path, message)
assert_response_schema(schema_path, message)
end

def assert_schema(payload, schema_path = nil, message = nil)
matcher = AssertSchema.new(schema_path, request, response, message, payload)
assert(matcher.call, matcher.message)
end

MissingSchema = Class.new(Errno::ENOENT)
InvalidSchemaError = Class.new(StandardError)

class AssertResponseSchema
attr_reader :schema_path, :response, :message
class AssertSchema
attr_reader :schema_path, :request, :response, :message, :payload

def initialize(schema_path, response, message)
def initialize(schema_path, request, response, message, payload = nil)
require_json_schema!
@request = request
@response = response
@payload = payload
@schema_path = schema_path || schema_path_default
@message = message
@document_store = JsonSchema::DocumentStore.new
Expand All @@ -31,7 +48,7 @@ def initialize(schema_path, response, message)

def call
json_schema.expand_references!(store: document_store)
status, errors = json_schema.validate(response_body)
status, errors = json_schema.validate(payload)
@message ||= errors.map(&:to_s).to_sentence
status
end
Expand All @@ -41,11 +58,11 @@ def call
attr_reader :document_store

def controller_path
response.request.filtered_parameters[:controller]
request.filtered_parameters[:controller]
end

def action
response.request.filtered_parameters[:action]
request.filtered_parameters[:action]
end

def schema_directory
Expand All @@ -68,6 +85,10 @@ def response_body
load_json(response.body)
end

def request_params
request.env['action_dispatch.request.request_parameters']
end

def json_schema
@json_schema ||= JsonSchema.parse!(schema_data)
end
Expand Down Expand Up @@ -98,6 +119,18 @@ def require_json_schema!
raise LoadError, "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
end
end
class AssertResponseSchema < AssertSchema
def initialize(*)
super
@payload = response_body
end
end
class AssertRequestSchema < AssertSchema
def initialize(*)
super
@payload = request_params
end
end
end
end
end

0 comments on commit b15a566

Please sign in to comment.