diff --git a/lib/repos/tree.rb b/lib/repos/tree.rb index a639ac3..9f92c42 100644 --- a/lib/repos/tree.rb +++ b/lib/repos/tree.rb @@ -20,14 +20,14 @@ module Pruning with_retries(max_tries: NUMBER_OF_RETRIES, rescue: [Pruning::Exceptions::ServerErrorOnOrigin]) do begin resp = @client.get(url(name)) - rescue RestClient::ExceptionWithResponse => e - raise Pruning::Exceptions::ServerErrorOnOrigin if e.response.code != 404 - raise Pruning::Exceptions::OriginCannotFindTheResource - end - - return JSON(resp.body) + rescue RestClient::ExceptionWithResponse => e + raise Pruning::Exceptions::ServerErrorOnOrigin if e.response.code != 404 + raise Pruning::Exceptions::OriginCannotFindTheResource end - raise Pruning::Exceptions::UnexpectedError + + return JSON(resp.body) + end + raise Pruning::Exceptions::UnexpectedError end diff --git a/test/api/tree_test.rb b/test/api/tree_test.rb new file mode 100644 index 0000000..e69de29 diff --git a/test/repos/tree_test.rb b/test/repos/tree_test.rb new file mode 100644 index 0000000..9ae4d01 --- /dev/null +++ b/test/repos/tree_test.rb @@ -0,0 +1,113 @@ +require 'minitest/autorun' +require 'minitest/mock' +require 'retries' +require_relative '../test_helper' +require_relative '../../lib/repos/tree' +require_relative '../../lib/exceptions' + +class TestReposTree < MiniTest::Test + + def setup + # eliminates delay between retries + Retries.sleep_enabled = false + end + def test_fetching + rest_client = MiniTest::Mock.new + result = MiniTest::Mock.new + + url = 'http://something/' + full_request_url = "#{url}/tree/input" + rest_client.expect :get, result, [full_request_url] + result.expect :body, "[1,2,3]", [] + repo = Pruning::Repos::Tree.new(rest_client, url) + actual = repo.get('input') + rest_client.verify + result.verify + + assert_equal [1,2,3], actual + end + + def test_404 + rest_client = MiniTest::Mock.new + result = MiniTest::Mock.new + response = MiniTest::Mock.new + + + url = 'http://something/' + rest_client.expect(:get, result) do |arguments| + raise RestClient::ExceptionWithResponse, response + end + response.expect :code, 404, [] + repo = Pruning::Repos::Tree.new(rest_client, url) + + + assert_raises Pruning::Exceptions::OriginCannotFindTheResource do + repo.get('input') + end + + rest_client.verify + result.verify + response.verify + end + + def test_retries_working + rest_client = MiniTest::Mock.new + result = MiniTest::Mock.new + response = MiniTest::Mock.new + + url = 'http://something/' + full_request_url = "#{url}/tree/input" + + # two times fails + 2.times do + rest_client.expect(:get, result) do |arguments| + raise RestClient::ExceptionWithResponse, response + end + response.expect :code, 500, [] + end + + + # third times succeeds + rest_client.expect :get, result, [full_request_url] + result.expect :body, "[1,2,3]", [] + + repo = Pruning::Repos::Tree.new(rest_client, url) + + actual = repo.get('input') + + # ensures that there were 3 retries + rest_client.verify + result.verify + response.verify + + assert_equal [1,2,3], actual + end + + def test_retries_failing + rest_client = MiniTest::Mock.new + result = MiniTest::Mock.new + response = MiniTest::Mock.new + + url = 'http://something/' + full_request_url = "#{url}/tree/input" + + + # three times fails + 3.times do + rest_client.expect(:get, result) do |arguments| + raise RestClient::ExceptionWithResponse, response + end + response.expect :code, 500, [] + end + + repo = Pruning::Repos::Tree.new(rest_client, url) + assert_raises Pruning::Exceptions::ServerErrorOnOrigin do + repo.get('input') + end + + # ensures that there were 3 retries + rest_client.verify + result.verify + response.verify + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8a95ad6..28aa1ef 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,15 +1,12 @@ ENV['RACK_ENV'] = 'test' - require 'bundler/setup' require 'dotenv/load' require 'json' require 'minitest/autorun' -require 'bogus/minitest' require_relative '../lib/api/app' -Bogus.configure { |config| config.search_modules << Pruning } Thread.abort_on_exception = true