From 4a419d020ff45d266effd16f2f4289664ebc6164 Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Thu, 21 Nov 2024 13:15:06 +0100 Subject: [PATCH] Handle non-positive amounts (#55) --- lib/query.rb | 5 ++++- spec/edge_cases_spec.rb | 5 +++++ spec/query_spec.rb | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/query.rb b/lib/query.rb index a155612..7f85bc7 100644 --- a/lib/query.rb +++ b/lib/query.rb @@ -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 diff --git a/spec/edge_cases_spec.rb b/spec/edge_cases_spec.rb index cf5bff7..f2492bf 100644 --- a/spec/edge_cases_spec.rb +++ b/spec/edge_cases_spec.rb @@ -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?) diff --git a/spec/query_spec.rb b/spec/query_spec.rb index be3fa63..f16f85f 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -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