From 96bf7f06a6f562dd86c617c676ccd2b9e0d1c168 Mon Sep 17 00:00:00 2001 From: "rich.d.rich" Date: Fri, 20 Jan 2017 16:20:26 +1300 Subject: [PATCH] Fix tests after merge --- test/examples/exercise4.rb | 12 ++--- test/examples/exercise4_doc.rb | 4 +- test/integration/exercise1_response_test.rb | 49 +++++++-------------- test/integration/exercise2_response_test.rb | 4 +- test/integration/exercise3_response_test.rb | 3 +- test/integration/exercise4_doco_test.rb | 2 +- test/integration/exercise4_response_test.rb | 29 ++++++++++-- 7 files changed, 54 insertions(+), 49 deletions(-) diff --git a/test/examples/exercise4.rb b/test/examples/exercise4.rb index 625657d..754dca9 100644 --- a/test/examples/exercise4.rb +++ b/test/examples/exercise4.rb @@ -6,15 +6,15 @@ module Exercise4 def endpoint group 'Test_Exercise' - http_method 'GET' - path '/examples/exercise1' + method 'GET' + path '/examples/exercise4' title 'Exercise API 4' description 'Exercise definition DSL #4' - request_example('"/api/examples/exercise4"') - response_example <<~END_EXAMPLE - { - } + request_example('/api/examples/exercise4') + response_example(<<~END_EXAMPLE + { + } END_EXAMPLE end diff --git a/test/examples/exercise4_doc.rb b/test/examples/exercise4_doc.rb index bbf9e84..9dab496 100644 --- a/test/examples/exercise4_doc.rb +++ b/test/examples/exercise4_doc.rb @@ -1,5 +1,5 @@ =begin -@api {GET} /examples/exercise1.json Exercise API 4 +@api {GET} /examples/exercise4.json Exercise API 4 @apiName Exercise4 @apiGroup Test_Exercise @@ -13,7 +13,7 @@ @apiParamExample Request Example -{GET} "/api/examples/exercise4" + {GET} "/api/examples/exercise4" @apiSuccess (200 Success) {String} a_not diff --git a/test/integration/exercise1_response_test.rb b/test/integration/exercise1_response_test.rb index f7620eb..97f841a 100644 --- a/test/integration/exercise1_response_test.rb +++ b/test/integration/exercise1_response_test.rb @@ -30,44 +30,27 @@ class Exercise1ResponseTest < MiniTest::Test end should 'respond with a composed block' do - response = @controller.show + response = Examples::Exercise1Controller.new.show - assert_equal({ 'extra' => 150, - 'defaulted' => 123, - 'data' => [ - { 'n' => 1, 'name' => 'one', 'alt_name' => 'one' }, - { 'n' => 2, 'name' => 'two', 'alt_name' => 'two' } - ], - 'links' => { 'other' => 'http://test.api/exercise1/other/123/other_api?extra=50', - 'self' => 'http://test.api/exercise1/examples/exercise1?defaulted=123&extra=50' } }, + assert_equal({ 'extra' => 150, 'defaulted' => 123, 'data' => [{ 'n' => 1, 'name' => 'one' }, { 'n' => 2, 'name' => 'two' }] }, response) end should 'respond with a composed block when no param' do - @controller.params = {} - response = @controller.show + controller = Examples::Exercise1Controller.new + controller.params = {} + response = controller.show - assert_equal({ 'extra' => 1334, 'defaulted' => 123, - 'data' => [ - { 'n' => 1, 'name' => 'one', 'alt_name' => 'one' }, - { 'n' => 2, 'name' => 'two', 'alt_name' => 'two' } - ], - 'links' => { 'other' => 'http://test.api/exercise1/other/123/other_api?extra=1234', - 'self' => 'http://test.api/exercise1/examples/exercise1?defaulted=123&extra=1234' } }, + assert_equal({ 'extra' => 100, 'defaulted' => 123, 'data' => [{ 'n' => 1, 'name' => 'one' }, { 'n' => 2, 'name' => 'two' }] }, response) end should 'override a default param from query' do - @controller.params = { defaulted: 888 } - response = @controller.show + controller = Examples::Exercise1Controller.new + controller.params = { defaulted: 888 } + response = controller.show - assert_equal({ 'extra' => 1234 + 100, 'defaulted' => 888, - 'data' => [ - { 'n' => 1, 'name' => 'one', 'alt_name' => 'one' }, - { 'n' => 2, 'name' => 'two', 'alt_name' => 'two' } - ], - 'links' => { 'other' => 'http://test.api/exercise1/other/888/other_api?extra=1234', - 'self' => 'http://test.api/exercise1/examples/exercise1?defaulted=888&extra=1234' } }, + assert_equal({ 'extra' => 100, 'defaulted' => 888, 'data' => [{ 'n' => 1, 'name' => 'one' }, { 'n' => 2, 'name' => 'two' }] }, response) end @@ -89,12 +72,12 @@ class Exercise1ResponseTest < MiniTest::Test assert_equal(422, response.status_code) end - should 'return an error hash when provoked' do - @controller.params = { return_error_hash: true } - response = @controller.show - - assert_equal({ e: 'Fail', reason: 'wanted' }, response.status_error_info) - assert_equal(422, response.status_code) + should 'return an error when provoked' do + controller = Examples::Exercise1Controller.new + controller.params = { return_error: true } + response = controller.show + assert_equal response.status_code, 422 + assert_equal response.status_message, 'Eek!' end end end diff --git a/test/integration/exercise2_response_test.rb b/test/integration/exercise2_response_test.rb index 2b76f87..79b519c 100644 --- a/test/integration/exercise2_response_test.rb +++ b/test/integration/exercise2_response_test.rb @@ -27,7 +27,7 @@ class Exercise2ResponseTest < MiniTest::Test controller.params = { required: 2.718, version: 'v2.1.0-mobile' } response = controller.show - assert_equal({ 'all' => 'all_versions', 'v2_1_only' => 2.1, "params" => { "required" => 2.718, "version" => "v2.1.0-mobile" }, "options" => { "test" => "hello" } }, + assert_equal({ 'all' => 'all_versions', 'v2_1_only' => 2.1 }, response) end @@ -36,7 +36,7 @@ class Exercise2ResponseTest < MiniTest::Test controller.params = { required: 3.142, version: 'v2.0-mobile' } response = controller.show - assert_equal({ 'all' => 'all_versions', 'v2_0_only' => 2.0, "params" => { "required" => 3.142, "version" => "v2.0-mobile" }, "options" => { "test" => "hello" } }, + assert_equal({ 'all' => 'all_versions', 'v2_0_only' => 2.0 }, response) end diff --git a/test/integration/exercise3_response_test.rb b/test/integration/exercise3_response_test.rb index fe2f58c..0a5d167 100644 --- a/test/integration/exercise3_response_test.rb +++ b/test/integration/exercise3_response_test.rb @@ -28,8 +28,7 @@ class Exercise3ResponseTest < MiniTest::Test controller.params = { required: 2.718, version: 'v2.1.0-mobile' } response = controller.my_method - assert_equal({ 'ok' => true }, - response) + assert_equal({ 'ok' => true }, response) end end end diff --git a/test/integration/exercise4_doco_test.rb b/test/integration/exercise4_doco_test.rb index 3926cc3..1ea6c85 100644 --- a/test/integration/exercise4_doco_test.rb +++ b/test/integration/exercise4_doco_test.rb @@ -8,7 +8,7 @@ module Integration class Exercise4DocoTest < MiniTest::Test - context 'Documentation of Exercise1' do + context 'Documentation of Exercise4' do setup do Flappi.configure do |conf| conf.version_plan = nil diff --git a/test/integration/exercise4_response_test.rb b/test/integration/exercise4_response_test.rb index a3dade1..1e78a9b 100644 --- a/test/integration/exercise4_response_test.rb +++ b/test/integration/exercise4_response_test.rb @@ -7,7 +7,29 @@ require_relative '../examples/exercise4' module Examples - class Exercise4Controller < ExampleController + class Exercise4Controller + attr_accessor :params + attr_accessor :last_render_params + + def initialize + self.params = {} + end + + def show + Flappi.build_and_respond(self) + end + + def request + OpenStruct.new(query_parameters: params) + end + + def respond_to + yield JsonFormatter.new + end + + def render(params) + self.last_render_params = params + end end end @@ -20,10 +42,11 @@ class Exercise4ResponseTest < MiniTest::Test end end - should 'respond with a composed block' do + should 'respond with only selected fields' do response = Examples::Exercise4Controller.new.show - assert_equal({ 'a_not' => 100, 'b_how' => 100 }, response) + assert_equal({ "a_not"=>100, "b_how"=>100 }, + response) end end end