Added tests for repository
This commit is contained in:
@@ -20,14 +20,14 @@ module Pruning
|
|||||||
with_retries(max_tries: NUMBER_OF_RETRIES, rescue: [Pruning::Exceptions::ServerErrorOnOrigin]) do
|
with_retries(max_tries: NUMBER_OF_RETRIES, rescue: [Pruning::Exceptions::ServerErrorOnOrigin]) do
|
||||||
begin
|
begin
|
||||||
resp = @client.get(url(name))
|
resp = @client.get(url(name))
|
||||||
rescue RestClient::ExceptionWithResponse => e
|
rescue RestClient::ExceptionWithResponse => e
|
||||||
raise Pruning::Exceptions::ServerErrorOnOrigin if e.response.code != 404
|
raise Pruning::Exceptions::ServerErrorOnOrigin if e.response.code != 404
|
||||||
raise Pruning::Exceptions::OriginCannotFindTheResource
|
raise Pruning::Exceptions::OriginCannotFindTheResource
|
||||||
end
|
|
||||||
|
|
||||||
return JSON(resp.body)
|
|
||||||
end
|
end
|
||||||
raise Pruning::Exceptions::UnexpectedError
|
|
||||||
|
return JSON(resp.body)
|
||||||
|
end
|
||||||
|
raise Pruning::Exceptions::UnexpectedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
0
test/api/tree_test.rb
Normal file
0
test/api/tree_test.rb
Normal file
113
test/repos/tree_test.rb
Normal file
113
test/repos/tree_test.rb
Normal file
@@ -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
|
||||||
@@ -1,15 +1,12 @@
|
|||||||
ENV['RACK_ENV'] = 'test'
|
ENV['RACK_ENV'] = 'test'
|
||||||
|
|
||||||
|
|
||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
require 'dotenv/load'
|
require 'dotenv/load'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
require 'bogus/minitest'
|
|
||||||
|
|
||||||
require_relative '../lib/api/app'
|
require_relative '../lib/api/app'
|
||||||
|
|
||||||
Bogus.configure { |config| config.search_modules << Pruning }
|
|
||||||
Thread.abort_on_exception = true
|
Thread.abort_on_exception = true
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user