Handle more edge cases

This commit is contained in:
hakanensari 2014-05-16 20:35:16 +01:00
parent 88447a1856
commit 3c146f643f
3 changed files with 33 additions and 12 deletions

View File

@ -9,7 +9,9 @@ class Snapshot
def quote
self.date = if date
Currency.where("date <= '#{date}'").order(:date).last.date
last_date = Currency.where("date <= '#{date}'").order(:date).last
raise ArgumentError.new('Date too old') unless last_date
last_date.date
else
Currency.last_date
end
@ -28,6 +30,7 @@ class Snapshot
def rebase(rates)
if base.upcase! != 'EUR'
denominator = rates.update('EUR' => 1.0).delete(base)
raise ArgumentError.new('Invalid base') unless denominator
rates.each do |iso_code, rate|
rates[iso_code] = round(rate / denominator)

View File

@ -7,7 +7,6 @@ describe 'the API' do
let(:app) { Sinatra::Application }
let(:json) { Yajl::Parser.new.parse last_response.body }
let(:status) { last_response.status }
it 'returns latest snapshot' do
get '/latest'
@ -34,14 +33,4 @@ describe 'the API' do
get '/2010-01-01'
json['rates'].wont_be :empty?
end
it 'considers an invalid date unprocessable' do
get '/2010-31-01'
last_response.must_be :unprocessable?
end
it 'handles unfound pages' do
get '/'
last_response.status.must_equal 404
end
end

29
spec/edge_cases_spec.rb Normal file
View File

@ -0,0 +1,29 @@
require_relative 'helper'
require 'rack/test'
require 'api'
describe 'the API' do
include Rack::Test::Methods
let(:app) { Sinatra::Application }
it 'handles unfound pages' do
get '/'
last_response.status.must_equal 404
end
it 'will not process an invalid date' do
get '/2010-31-01'
last_response.must_be :unprocessable?
end
it 'will not process a date before 2000' do
get '/1999-01-01'
last_response.must_be :unprocessable?
end
it 'will not process an invalid base' do
get '/latest?base=UAH'
last_response.must_be :unprocessable?
end
end