Handle non-positive amounts (#55)

This commit is contained in:
Hakan Ensari 2024-11-21 13:15:06 +01:00
parent 0a88f09c73
commit 4a419d020f
No known key found for this signature in database
3 changed files with 14 additions and 1 deletions

View File

@ -14,7 +14,10 @@ class Query
def amount
return unless @params[:amount]
@params[:amount].to_f
value = @params[:amount].to_f
raise ArgumentError, "invalid amount" unless value.positive?
value
end
def base

View File

@ -23,6 +23,11 @@ describe "the server" do
_(last_response).must_be(:unprocessable?)
end
it "will not process an invalid amount" do
get "/latest?amount=0&from=USD&to=EUR"
_(last_response).must_be(:unprocessable?)
end
it "will not process a date before 2000" do
get "/1999-01-01"
_(last_response).must_be(:not_found?)

View File

@ -13,6 +13,11 @@ describe Query do
_(query.amount).must_equal(100.0)
end
it "requires a positive amount" do
_ { Query.new(amount: "0").to_h }.must_raise(ArgumentError)
_ { Query.new(amount: "-1").to_h }.must_raise(ArgumentError)
end
it "defaults amount to nothing" do
query = Query.new
_(query.amount).must_be_nil