Permalink
Browse files

Improve TestHelper so it also validates request param declarations.

  • Loading branch information...
1 parent 167ea8d commit d6a0081268816238585084f49921cf476e091d77 @myronmarston myronmarston committed Sep 21, 2012
@@ -159,6 +159,10 @@ def initialize(endpoint, version, message_type, definition)
make_schema_strict!(@schema)
end
+ def request?
+ message_type == "request"
+ end
+
def endpoint_name
@endpoint.name
end
@@ -190,12 +194,12 @@ def parse_request_params(request_params)
request_params_parser.parse(request_params)
end
- private
-
def request_params_parser
@request_params_parser ||= RequestParamsParser.new(self)
end
+ private
+
def make_schema_strict!(raw_schema, modify_object=true)
return unless Hash === raw_schema
@@ -1,32 +1,55 @@
require 'interpol'
require 'rack/mock'
+require 'interpol/request_params_parser'
module Interpol
module TestHelper
module Common
def define_interpol_example_tests(&block)
config = Configuration.default.customized_duplicate(&block)
- each_example_from(config.endpoints) do |endpoint, definition, example, example_index|
- description = "#{endpoint.name} (v #{definition.version}) has " +
- "valid data for example #{example_index + 1}"
- example = filtered_example(config, endpoint, example)
- define_test(description) { example.validate! }
+ each_definition_from(config.endpoints) do |endpoint, definition|
+ define_definition_test(endpoint, definition)
+
+ each_example_from(definition) do |example, example_index|
+ define_example_test(config, endpoint, definition, example, example_index)
+ end
end
end
private
- def each_example_from(endpoints)
+ def each_definition_from(endpoints)
endpoints.each do |endpoint|
endpoint.definitions.each do |definition|
- definition.examples.each_with_index do |example, index|
- yield endpoint, definition, example, index
- end
+ yield endpoint, definition
end
end
end
+ def each_example_from(definition)
+ definition.examples.each_with_index do |example, index|
+ yield example, index
+ end
+ end
+
+ def define_example_test(config, endpoint, definition, example, example_index)
+ description = "#{endpoint.name} (v #{definition.version}) has " +
+ "valid data for example #{example_index + 1}"
+ example = filtered_example(config, endpoint, example)
+ define_test(description) { example.validate! }
+ end
+
+ def define_definition_test(endpoint, definition)
+ return unless definition.request?
+
+ description = "#{endpoint.name} (v #{definition.version}) request " +
+ "definition has valid params schema"
+ define_test description do
+ definition.request_params_parser # it will raise an error if it is invalid
+ end
+ end
+
def filtered_example(config, endpoint, example)
path = endpoint.route.gsub(':', '') # turn path params into static segments
rack_env = ::Rack::MockRequest.env_for(path)
@@ -223,6 +223,23 @@ def build_hash(hash = {})
end
end
+ describe "#request?" do
+ it 'returns true if message_type == request' do
+ ed = EndpointDefinition.new(endpoint, version, 'request', build_hash)
+ ed.should be_request
+ end
+
+ it 'returns false if message_type == response' do
+ ed = EndpointDefinition.new(endpoint, version, 'response', build_hash)
+ ed.should_not be_request
+ end
+
+ it 'returns false if message_type == something else' do
+ ed = EndpointDefinition.new(endpoint, version, 'something_else', build_hash)
+ ed.should_not be_request
+ end
+ end
+
describe "#parse_request_params" do
let(:parser_class) { fire_replaced_class_double("Interpol::RequestParamsParser") }
let(:parser) { fire_double("Interpol::RequestParamsParser") }
@@ -12,6 +12,7 @@ module Interpol
method: GET
definitions:
- versions: ["1.0"]
+ message_type: response
schema:
type: object
properties:
@@ -34,7 +35,7 @@ module Interpol
end
end
- it 'generates a test per data example per definition per endpoint' do
+ it 'generates a test per data example per definition per endpoint + a test per definition' do
write_file "#{dir}/e1.yml", endpoint_definition_yml.gsub('["1.0"]', '["1.0", "2.0"]')
write_file "#{dir}/e2.yml", endpoint_definition_yml.gsub("project_list", "project_list_2")
num_tests_from(test_group).should eq(9)
@@ -65,6 +66,43 @@ module Interpol
run(test_group)
results_from(test_group).should =~ ['passed', 'passed', 'passed']
end
+
+ context 'request path schema validation' do
+ let_without_indentation(:endpoint_definition_yml) do <<-EOF
+ ---
+ name: project_list
+ route: /users/:user_id/projects
+ method: GET
+ definitions:
+ - versions: ["1.0"]
+ message_type: request
+ path_params:
+ type: object
+ properties:
+ user_id: { type: string }
+ schema: {}
+ examples: {}
+ EOF
+ end
+
+ it 'generates a test per endpoint definition' do
+ write_file "#{dir}/e1.yml", endpoint_definition_yml.gsub('["1.0"]', '["1.0", "2.0"]')
+ write_file "#{dir}/e2.yml", endpoint_definition_yml.gsub("project_list", "project_list_2")
+ num_tests_from(test_group).should eq(3)
+ end
+
+ it 'generates tests that pass if the params are declared correctly' do
+ write_file "#{dir}/e1.yml", endpoint_definition_yml
+ run(test_group)
+ results_from(test_group).should == ['passed']
+ end
+
+ it 'generates tests that fail if the params are declared incorrectly' do
+ write_file "#{dir}/e1.yml", endpoint_definition_yml.gsub('object', 'oject')
+ run(test_group)
+ results_from(test_group).should == ['failed']
+ end
+ end
end
describe "RSpec" do

0 comments on commit d6a0081

Please sign in to comment.