diff --git a/app/flows/am_i_getting_minimum_wage_flow/start.erb b/app/flows/am_i_getting_minimum_wage_flow/start.erb index 2578884830d..3cac3e2b315 100644 --- a/app/flows/am_i_getting_minimum_wage_flow/start.erb +++ b/app/flows/am_i_getting_minimum_wage_flow/start.erb @@ -13,7 +13,7 @@ - you’re getting paid the National Living Wage - your employer owes you past payments from the previous year because of underpayment - ^You must be at least 23 years old to get the National Living Wage.^ + ^You must be at least <%= this_year_living_wage_min_age("minimum_wage") %> years old to get the National Living Wage.^ Check [National Minimum Wage and National Living Wage rates](/national-minimum-wage-rates) before <%= last_year_end_text("minimum_wage") %>. <% end %> diff --git a/app/flows/minimum_wage_calculator_employers_flow/start.erb b/app/flows/minimum_wage_calculator_employers_flow/start.erb index 6febce1e9e8..774e3e91472 100644 --- a/app/flows/minimum_wage_calculator_employers_flow/start.erb +++ b/app/flows/minimum_wage_calculator_employers_flow/start.erb @@ -13,7 +13,7 @@ - you’re paying a worker the National Living Wage - you owe your employee payments from the previous year because you underpaid them - ^Your employee must be at least 23 years old to get the National Living Wage.^ + ^Your employee must be at least <%= this_year_living_wage_min_age("minimum_wage") %> years old to get the National Living Wage.^ Check [National Minimum Wage and National Living Wage rates](/national-minimum-wage-rates) before <%= last_year_end_text("minimum_wage") %>. <% end %> diff --git a/app/flows/shared/minimum_wage_flow.rb b/app/flows/shared/minimum_wage_flow.rb index 04bd7e9bc7d..81c6784b597 100644 --- a/app/flows/shared/minimum_wage_flow.rb +++ b/app/flows/shared/minimum_wage_flow.rb @@ -6,8 +6,7 @@ def define option "past_payment" on_response do |response| - self.calculator = SmartAnswer::Calculators::MinimumWageCalculator.new - calculator.date = calculator.previous_period_start_date if response == "past_payment" + self.calculator = SmartAnswer::Calculators::MinimumWageCalculator.new(past_or_current_payment: response) self.accommodation_charge = nil end diff --git a/config/smart_answers/rates/minimum_wage.yml b/config/smart_answers/rates/minimum_wage.yml index f90b5e9717d..c2d98f363b8 100644 --- a/config/smart_answers/rates/minimum_wage.yml +++ b/config/smart_answers/rates/minimum_wage.yml @@ -2,38 +2,40 @@ # make sure we have that at least. If adding a new set of data at # the end, you may have to update dates/values in the test file. --- -- :start_date: 2022-04-01 - :end_date: 2023-03-31 +- :start_date: 2023-04-01 + :end_date: 2024-03-31 :minimum_rates: - :min_age: 0 :max_age: 18 - :rate: 4.81 + :rate: 5.28 - :min_age: 18 :max_age: 21 - :rate: 6.83 + :rate: 7.49 - :min_age: 21 :max_age: 23 - :rate: 9.18 + :rate: 10.18 - :min_age: 23 :max_age: 1000 - :rate: 9.50 - :apprentice_rate: 4.81 - :accommodation_rate: 8.70 + :rate: 10.42 + :apprentice_rate: 5.28 + :accommodation_rate: 9.10 + :living_wage_min_age: 23 -- :start_date: 2023-04-01 +- :start_date: 2024-04-01 :end_date: 3000-01-01 :minimum_rates: - :min_age: 0 :max_age: 18 - :rate: 5.28 + :rate: 6.40 - :min_age: 18 :max_age: 21 - :rate: 7.49 + :rate: 8.60 - :min_age: 21 :max_age: 23 - :rate: 10.18 + :rate: 11.44 - :min_age: 23 :max_age: 1000 - :rate: 10.42 - :apprentice_rate: 5.28 - :accommodation_rate: 9.10 + :rate: 11.44 + :apprentice_rate: 6.40 + :accommodation_rate: 9.99 + :living_wage_min_age: 21 diff --git a/lib/smart_answer/calculators/minimum_wage_calculator.rb b/lib/smart_answer/calculators/minimum_wage_calculator.rb index 0b6dd91d0b5..39d1fe8dc79 100644 --- a/lib/smart_answer/calculators/minimum_wage_calculator.rb +++ b/lib/smart_answer/calculators/minimum_wage_calculator.rb @@ -8,28 +8,25 @@ class MinimumWageCalculator :accommodation_cost, :job_requirements_charge, :unpaid_additional_hours - attr_reader :date def initialize(params = {}) + raise ArgumentError, "Missing past_or_current_payment argument" unless params[:past_or_current_payment] + raise ArgumentError, "Invalid past_or_current_payment value: #{params[:past_or_current_payment]}" unless %w[past_payment current_payment].include? params[:past_or_current_payment] + @age = params[:age] - @date = params[:date] || SmartAnswer::DateHelper.current_day + @past_or_current_payment = params[:past_or_current_payment] @basic_hours = params[:basic_hours].to_f @basic_pay = params[:basic_pay].to_f @is_apprentice = params[:is_apprentice] @pay_frequency = params[:pay_frequency] || 7 @accommodation_cost = 0 - @minimum_wage_data = rates_for_date(@date) + @minimum_wage_data = rates @job_requirements_charge = false @unpaid_additional_hours = false end - def date=(date) - @date = date - @minimum_wage_data = rates_for_date(@date) - end - def previous_period_start_date - data.previous_period(date:)[:start_date] + data.previous_period[:start_date] end def valid_age?(age) @@ -53,8 +50,8 @@ def valid_accommodation_usage?(accommodation_usage) end def valid_age_for_living_wage?(age) - (age.to_i >= 23 && date >= Date.parse("2021-04-01")) || - age.to_i >= 25 + living_wage_min_age = @minimum_wage_data[:living_wage_min_age] + age.to_i >= living_wage_min_age end def basic_rate @@ -82,7 +79,7 @@ def total_pay end def total_entitlement - minimum_hourly_rate * total_hours + (minimum_hourly_rate * total_hours).round(2) end def historical_entitlement @@ -102,19 +99,18 @@ def accommodation_adjustment(charge, number_of_nights) number_of_nights = number_of_nights.to_i accommodation_cost = if charge > 0 # rubocop:disable Style/NumericPredicate - charged_accomodation_adjustment(charge, number_of_nights) + charged_accommodation_adjustment(charge, number_of_nights) else free_accommodation_adjustment(number_of_nights) end @accommodation_cost = (accommodation_cost * weekly_multiplier).round(2) end - def per_hour_minimum_wage(date = @date) - data = rates_for_date(date) + def per_hour_minimum_wage if @is_apprentice - data[:apprentice_rate] + @minimum_wage_data[:apprentice_rate] else - rates = data[:minimum_rates] + rates = @minimum_wage_data[:minimum_rates] rate_data = rates.find do |r| @age >= r[:min_age] && @age < r[:max_age] end @@ -123,15 +119,15 @@ def per_hour_minimum_wage(date = @date) end def free_accommodation_rate - @minimum_wage_data.accommodation_rate + @minimum_wage_data[:accommodation_rate] end def apprentice_rate - @minimum_wage_data.apprentice_rate + @minimum_wage_data[:apprentice_rate] end def eligible_for_living_wage? - valid_age_for_living_wage?(age) && date >= Date.parse("2016-04-01") + valid_age_for_living_wage?(age) end def under_school_leaving_age? @@ -152,7 +148,7 @@ def free_accommodation_adjustment(number_of_nights) (free_accommodation_rate * number_of_nights).round(2) end - def charged_accomodation_adjustment(charge, number_of_nights) + def charged_accommodation_adjustment(charge, number_of_nights) if charge < free_accommodation_rate 0 else @@ -162,8 +158,12 @@ def charged_accomodation_adjustment(charge, number_of_nights) private - def rates_for_date(date = Time.zone.today) - data.rates(date) + def rates + if @past_or_current_payment == "past_payment" + data.previous_period + elsif @past_or_current_payment == "current_payment" + data.current_period + end end def data diff --git a/lib/smart_answer/calculators/rates_query.rb b/lib/smart_answer/calculators/rates_query.rb index 4853b4e9c7a..555b8b84849 100644 --- a/lib/smart_answer/calculators/rates_query.rb +++ b/lib/smart_answer/calculators/rates_query.rb @@ -13,17 +13,22 @@ def initialize(rates_data) @data = rates_data end - def previous_period(date: nil) - date ||= SmartAnswer::DateHelper.current_day + def previous_period previous_period = nil data.each do |rates_hash| - break if rates_hash[:start_date] <= date && rates_hash[:end_date] >= date - - previous_period = rates_hash + previous_period = rates_hash if !previous_period || rates_hash[:start_date] <= previous_period[:start_date] end previous_period end + def current_period + current_period = nil + data.each do |rates_hash| + current_period = rates_hash if !current_period || rates_hash[:start_date] > current_period[:start_date] + end + current_period + end + def rates(date = nil) date ||= SmartAnswer::DateHelper.current_day relevant_rates = data.find do |rates_hash| diff --git a/lib/smart_answer/rate_dates_helper.rb b/lib/smart_answer/rate_dates_helper.rb index 39f32ec506b..cf12de849a4 100644 --- a/lib/smart_answer/rate_dates_helper.rb +++ b/lib/smart_answer/rate_dates_helper.rb @@ -8,9 +8,18 @@ def last_year_end_text(rates_file) last_year(rates_file)[:end_date].strftime("%B %Y").to_s end + def this_year_living_wage_min_age(rates_file) + this_year(rates_file)[:living_wage_min_age] + end + + def this_year(rates_file) + data = SmartAnswer::Calculators::RatesQuery.from_file(rates_file) + data.current_period + end + def last_year(rates_file) data = SmartAnswer::Calculators::RatesQuery.from_file(rates_file) - data.previous_period(date: SmartAnswer::DateHelper.current_day) + data.previous_period end end end diff --git a/test/unit/calculators/minimum_wage_calculator_test.rb b/test/unit/calculators/minimum_wage_calculator_test.rb index 9c13b0c8f3c..aa1f00ee54d 100644 --- a/test/unit/calculators/minimum_wage_calculator_test.rb +++ b/test/unit/calculators/minimum_wage_calculator_test.rb @@ -2,9 +2,22 @@ module SmartAnswer::Calculators class MinimumWageCalculatorTest < ActiveSupport::TestCase - context "Validation" do + context "Initialisation" do + should "reject invalid 'past_or_current_payment' values" do + assert_raises(ArgumentError, "Invalid past_or_current_payment value: 'bad value'") do + MinimumWageCalculator.new(past_or_current_payment: "bad value") + end + end + should "reject missing 'past_or_current_payment' argument" do + assert_raises(ArgumentError, "Missing past_or_current_payment argument") do + MinimumWageCalculator.new + end + end + end + + context "Any tax year validation" do setup do - @calculator = MinimumWageCalculator.new + @calculator = MinimumWageCalculator.new(past_or_current_payment: "current_payment") end context "for age" do @@ -88,6 +101,12 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase assert @calculator.valid_accommodation_usage?(7) end end + end + + context "Past tax year validation" do + setup do + @calculator = MinimumWageCalculator.new(past_or_current_payment: "past_payment") + end context "for age for living wage" do should "not accept ages below 23" do @@ -98,60 +117,67 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase assert @calculator.valid_age_for_living_wage?(23) assert @calculator.valid_age_for_living_wage?(24) end + end + end - context "when a date is before 1 April 2021" do - setup { @calculator.date = Date.parse("2020-04-01") } + context "Current tax year validation" do + setup do + @calculator = MinimumWageCalculator.new(past_or_current_payment: "current_payment") + end - should "not accept ages below 25" do - assert_not @calculator.valid_age_for_living_wage?(23) - end + context "for age for living wage" do + should "not accept ages below minimum" do + assert_not @calculator.valid_age_for_living_wage?(20) + end - should "accept ages 25 or above" do - assert @calculator.valid_age_for_living_wage?(25) - assert @calculator.valid_age_for_living_wage?(26) - end + should "accept minimum age or above" do + assert @calculator.valid_age_for_living_wage?(21) + assert @calculator.valid_age_for_living_wage?(22) end end end context "#eligible_for_living_wage?" do - setup do - @calculator = MinimumWageCalculator.new - end - - should "return true if the age is 25 or over" do - %w[25 26].each do |age| - @calculator.age = age - assert @calculator.eligible_for_living_wage? + context "in the past tax year" do + should "return true if the age is the minimum for the living wage or higher" do + %w[23 24].each do |age| + @calculator = MinimumWageCalculator.new(past_or_current_payment: "past_payment") + @calculator.age = age + assert @calculator.eligible_for_living_wage? + end end - end - should "return true if the age is 23 or over, and the date is on or after 2022-04-01" do - %w[23 24].each do |age| - @calculator.date = Date.parse("2022-04-01") - @calculator.age = age - assert @calculator.eligible_for_living_wage? + should "return false if age is lower than the minimum or nil" do + %w[nil 0 22].each do |age| + @calculator = MinimumWageCalculator.new(past_or_current_payment: "past_payment") + @calculator.age = age + assert_not @calculator.eligible_for_living_wage? + end end end - should "return false if age is lower than 24 or nil, and date is before 2021-04-01" do - %w[nil 0 24].each do |age| - @calculator.date = Date.parse("2020-04-01") - @calculator.age = age - assert_not @calculator.eligible_for_living_wage? + context "in the current tax year" do + should "return true if the age is the minimum for the living wage or higher" do + %w[21 22].each do |age| + @calculator = MinimumWageCalculator.new(past_or_current_payment: "current_payment") + @calculator.age = age + assert @calculator.eligible_for_living_wage? + end end - end - should "return false if age is over 25, and date is on or before 2016-04-01" do - @calculator.date = Date.parse("2016-03-30") - @calculator.age = 26 - assert_not @calculator.eligible_for_living_wage? + should "return false if age is lower than the minimum or nil" do + %w[nil 0 20].each do |age| + @calculator = MinimumWageCalculator.new(past_or_current_payment: "current_payment") + @calculator.age = age + assert_not @calculator.eligible_for_living_wage? + end + end end end context "#under_school_leaving_age?" do setup do - @calculator = MinimumWageCalculator.new + @calculator = MinimumWageCalculator.new(past_or_current_payment: "current_payment") end should "return true if age is lower than 16" do @@ -172,9 +198,9 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase @basic_hours = 39 @calculator = MinimumWageCalculator.new( age: @age, - date: Date.parse("2023-04-01"), basic_pay: @basic_pay, basic_hours: @basic_hours, + past_or_current_payment: "current_payment", ) end @@ -186,11 +212,11 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase context "minimum hourly rate" do should "be the minimum wage per hour for the age and year" do - assert_equal 7.49, @calculator.minimum_hourly_rate + assert_equal 8.60, @calculator.minimum_hourly_rate end context "when eligible for living wage?" do should "return the national living wage rate" do - @calculator = MinimumWageCalculator.new(age: 25) + @calculator = MinimumWageCalculator.new(age: 25, past_or_current_payment: "past_payment") assert_equal 10.42, @calculator.minimum_hourly_rate end end @@ -221,26 +247,25 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase end end - context "adjust for accommodation" do + context "adjustment for accommodation" do setup do @calculator.accommodation_adjustment(12.00, 4) end should "calculate the accommodation cost" do - assert_equal(-11.6, @calculator.accommodation_cost) + expected = ((9.99 * 4) - (12.00 * 4)) * 1 + assert_equal(expected, @calculator.accommodation_cost) end - should "be included the total pay calculation" do - assert_equal 175.86, @calculator.total_pay + should "be included in the total pay calculation" do + expected = ((@basic_pay / @basic_hours) * @basic_hours + @calculator.accommodation_cost).round(2) + assert_equal expected, @calculator.total_pay end end context "historical entitlement" do - setup do - @historical_entitlement = 292.11 - end should "be minimum wage for the year multiplied by total hours" do - assert_equal @historical_entitlement, @calculator.historical_entitlement + assert_equal 335.4, @calculator.historical_entitlement end end @@ -250,36 +275,34 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase # Scenario 1 context "minimum wage calculator for a 24 yr old low hourly rate" do setup do - # NOTE: test_date included as all minimum wage calculations are date sensitive - test_date = Date.parse("2012-08-01") @calculator = MinimumWageCalculator.new( age: 24, pay_frequency: 7, basic_pay: 168, basic_hours: 40, - date: test_date, + past_or_current_payment: "current_payment", ) end - should "have a total hourly rate of 4.20" do - assert_equal 10.42, @calculator.minimum_hourly_rate + should "have a minimum hourly rate of 11.44" do + assert_equal 11.44, @calculator.minimum_hourly_rate end end # Scenario 2 - context "minimum wage calculator for a 23 yr old working 70 hours over a fortnight after 01/10/2012" do + context "minimum wage calculator for a 23 yr old working 70 hours over a fortnight" do setup do @calculator = MinimumWageCalculator.new( age: 23, pay_frequency: 14, - date: Date.parse("2022-10-01"), basic_pay: 420, basic_hours: 70, + past_or_current_payment: "past_payment", ) end - should "calculate total hourly rate" do - assert_equal 9.5, @calculator.minimum_hourly_rate + should "calculate minimum hourly rate" do + assert_equal 10.42, @calculator.minimum_hourly_rate end end @@ -288,7 +311,7 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase setup do @calculator = MinimumWageCalculator.new( age: 24, - date: Date.parse("2011-10-01"), + past_or_current_payment: "current_payment", pay_frequency: 7, basic_pay: 100, basic_hours: 40, @@ -296,19 +319,19 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase end should "calculate total hourly rate" do - assert_equal 10.42, @calculator.minimum_hourly_rate + assert_equal 11.44, @calculator.minimum_hourly_rate assert_not @calculator.minimum_wage_or_above?, "should be below the minimum wage" end should "adjust for free accommodation" do @calculator.accommodation_adjustment(0, 5) - assert_equal 45.5, @calculator.accommodation_cost + assert_equal 49.95, @calculator.accommodation_cost assert_not @calculator.minimum_wage_or_above?, "should be below the minimum wage" end should "adjust for accommodation charged above the threshold" do @calculator.accommodation_adjustment(12, 5) - assert_equal(-14.5, @calculator.accommodation_cost) + assert_equal(-10.05, @calculator.accommodation_cost) assert_not @calculator.minimum_wage_or_above?, "should be below the minimum wage" end @@ -321,65 +344,63 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase end context "per hour minimum wage" do - context "from 1 Apr 2023" do + context "for current tax year" do setup do - @calculator = MinimumWageCalculator.new(date: Date.parse("2023-04-01")) + @calculator = MinimumWageCalculator.new(past_or_current_payment: "current_payment") end should "be correct for those under 18" do [0, 17].each do |age| @calculator.age = age - assert_equal 5.28, @calculator.per_hour_minimum_wage + assert_equal 6.40, @calculator.per_hour_minimum_wage end end should "be correct for 18 to 20 year olds" do [18, 20].each do |age| @calculator.age = age - assert_equal 7.49, @calculator.per_hour_minimum_wage + assert_equal 8.60, @calculator.per_hour_minimum_wage end end should "be correct for 21 to 22 year olds" do [21, 22].each do |age| @calculator.age = age - assert_equal 10.18, @calculator.per_hour_minimum_wage + assert_equal 11.44, @calculator.per_hour_minimum_wage end end should "be correct for those aged 23 and over" do [23, 100].each do |age| @calculator.age = age - assert_equal 10.42, @calculator.per_hour_minimum_wage + assert_equal 11.44, @calculator.per_hour_minimum_wage end end should "have correct apprentice rate" do - assert_equal 5.28, @calculator.apprentice_rate + assert_equal 6.40, @calculator.apprentice_rate end should "have correct accommodation rate" do - assert_equal 9.1, @calculator.free_accommodation_rate + assert_equal 9.99, @calculator.free_accommodation_rate end end end context "accommodation adjustment" do setup do - # NOTE: test_date must be included as results are date sensitive - test_date = Date.parse("2022-08-01") - @calculator = MinimumWageCalculator.new age: 22, date: test_date + @calculator = MinimumWageCalculator.new age: 22, past_or_current_payment: "past_payment" end should "return 0 for accommodation charged under the threshold" do assert_equal 0, @calculator.accommodation_adjustment("3.50", 5) end should "return the number of nights times the threshold if the accommodation is free" do - assert_equal((8.70 * 4), @calculator.accommodation_adjustment("0", 4)) + assert_equal((9.10 * 4), @calculator.accommodation_adjustment("0", 4)) end should "subtract the charged fee from the free fee where the accommodation costs more than the threshold" do charge = 10.12 number_of_nights = 5 - free_adjustment = (8.70 * number_of_nights).round(2) + free_adjustment = (9.10 * number_of_nights).round(2) charged_adjustment = @calculator.accommodation_adjustment(charge, number_of_nights) assert_equal((free_adjustment - (charge * number_of_nights)).round(2), charged_adjustment) assert charged_adjustment.negative? # this should always be less than zero @@ -387,43 +408,29 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase end context "total_pay and basic_rate calculations" do - setup do + should "[with accommodation_adjustment] return rate total" do @calculator = MinimumWageCalculator.new( age: 25, pay_frequency: 5, basic_pay: 260, basic_hours: 40, + past_or_current_payment: "current_payment", ) - end - - context "test date sensitive vars" do - setup do - test_date = Date.parse("2012-08-01") - @calculator = MinimumWageCalculator.new( - age: 25, - pay_frequency: 5, - basic_pay: 260, - basic_hours: 40, - date: test_date, - ) - end - - should "[with accommodation_adjustment] return rate total (216.39)" do - @calculator.accommodation_adjustment(20, 4) - assert_equal 228.87, @calculator.total_pay - end + @calculator.accommodation_adjustment(20, 4) + assert_equal 231.41, @calculator.total_pay end end context "non-historical minimum wage" do - should "return today's minimum wage rate for 25 year old" do + should "return today's minimum wage rate for 24 year old" do @calculator = MinimumWageCalculator.new( age: 24, pay_frequency: 7, basic_pay: 312, basic_hours: 39, + past_or_current_payment: "current_payment", ) - assert_equal 10.42, @calculator.minimum_hourly_rate + assert_equal 11.44, @calculator.minimum_hourly_rate end should "return today's minimum wage rate for 19 year old" do @calculator = MinimumWageCalculator.new( @@ -431,8 +438,9 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase pay_frequency: 7, basic_pay: 312, basic_hours: 39, + past_or_current_payment: "current_payment", ) - assert_equal 7.49, @calculator.minimum_hourly_rate + assert_equal 8.60, @calculator.minimum_hourly_rate end should "return today's minimum wage rate for 17 year old" do @calculator = MinimumWageCalculator.new( @@ -440,8 +448,9 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase pay_frequency: 7, basic_pay: 312, basic_hours: 39, + past_or_current_payment: "current_payment", ) - assert_equal 5.28, @calculator.minimum_hourly_rate + assert_equal 6.40, @calculator.minimum_hourly_rate end end @@ -452,11 +461,11 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase pay_frequency: 7, basic_pay: 100, basic_hours: 39, - date: Date.parse("5 Aug 2012"), + past_or_current_payment: "current_payment", ) end should "return total_entitlement" do - assert_equal 406.38, @calculator.total_entitlement + assert_equal 446.16, @calculator.total_entitlement end should "return total_underpayment" do assert_equal 100.0, @calculator.total_pay @@ -467,7 +476,7 @@ class MinimumWageCalculatorTest < ActiveSupport::TestCase pay_frequency: 7, basic_pay: 300, basic_hours: 39, - date: Date.parse("5 Aug 2012"), + past_or_current_payment: "current_payment", ) assert_equal 300.0, @calculator.total_pay end diff --git a/test/unit/calculators/rates_query_test.rb b/test/unit/calculators/rates_query_test.rb index 997a3affdd4..03373fa1920 100644 --- a/test/unit/calculators/rates_query_test.rb +++ b/test/unit/calculators/rates_query_test.rb @@ -73,19 +73,30 @@ class RatesQueryTest < ActiveSupport::TestCase end context "#previous_period" do - setup do - @test_rate = RatesQuery.from_file( - "exact_date_rates", - load_path: "test/fixtures/rates", - ) + should "be rates with earliest start date when loaded in ascending date order" do + rates = RatesQuery.new([{ start_date: Date.parse("2012-01-01"), rate: "earliest" }, { start_date: Date.parse("2013-01-01"), rate: "latest" }]) + + assert_equal "earliest", rates.previous_period[:rate] end - should "be nil for 2013-01-31" do - assert_nil @test_rate.previous_period(date: Date.parse("2013-01-31")) + should "be rates with earliest start date when loaded in descending date order" do + rates = RatesQuery.new([{ start_date: Date.parse("2013-01-01"), rate: "latest" }, { start_date: Date.parse("2012-01-01"), rate: "earliest" }]) + + assert_equal "earliest", rates.previous_period[:rate] + end + end + + context "#current_period" do + should "be rates with latest start date when loaded in ascending date order" do + rates = RatesQuery.new([{ start_date: Date.parse("2012-01-01"), rate: "earliest" }, { start_date: Date.parse("2013-01-01"), rate: "latest" }]) + + assert_equal "latest", rates.current_period[:rate] end - should "be 2013-01-31 for 2013-02-01" do - assert_equal Date.parse("2012-01-01"), @test_rate.previous_period(date: Date.parse("2013-02-01"))[:start_date] + should "be rates with latest start date when loaded in descending date order" do + rates = RatesQuery.new([{ start_date: Date.parse("2013-01-01"), rate: "latest" }, { start_date: Date.parse("2012-01-01"), rate: "earliest" }]) + + assert_equal "latest", rates.current_period[:rate] end end end