Files
old-pruning-excersise/test/repos/tree_test.rb
2018-08-13 20:40:50 +02:00

114 lines
2.7 KiB
Ruby

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