Added tests for repository

This commit is contained in:
Senad Uka
2018-08-13 20:40:50 +02:00
parent 696d02a97c
commit e64d7509cd
4 changed files with 120 additions and 10 deletions

View File

@@ -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

0
test/api/tree_test.rb Normal file
View File

113
test/repos/tree_test.rb Normal file
View 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

View File

@@ -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