1
0

Implement discount budget for month repository

- Provide a repository to retrieve a discount budget
  for a given month
This commit is contained in:
Tim Kächele 2021-03-06 19:07:25 +01:00
parent 815615a03c
commit c4528b1c92
4 changed files with 70 additions and 2 deletions

View File

@ -5,6 +5,7 @@ require 'planet_express/version'
require 'planet_express/repository'
require 'planet_express/discount_budget'
require 'planet_express/discount_budget_for_month_repository'
require 'planet_express/shipping_option'
require 'planet_express/shipping_option_repository'

View File

@ -0,0 +1,35 @@
module PlanetExpress
class DiscountBudgetForMonthRepository
DEFAULT_BUDGET = 1000
attr_reader :data
def initialize(data: {})
@data = data
end
def for_month(date)
return budget_for_month(date) if budget_for_month?(date)
add_budget_for_month(date)
budget_for_month(date)
end
private
def budget_for_month(date)
data[start_of_month(date)]
end
def budget_for_month?(date)
!budget_for_month(date).nil?
end
def add_budget_for_month(date)
data[start_of_month(date)] = DiscountBudget.new(budget: DEFAULT_BUDGET)
end
def start_of_month(date)
Date.new(date.year, date.month, 1)
end
end
end

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
RSpec.describe PlanetExpress::DiscountBudgetForMonthRepository do
subject { described_class.new }
let(:march_date_a) { Date.new(2021, 3, 6) }
let(:march_date_b) { Date.new(2021, 3, 6) }
describe '#for_month' do
it 'returns the same discount budget for the same month' do
expect(subject.for_month(march_date_a).object_id).to(
eq(subject.for_month(march_date_b).object_id))
end
context 'not yet initialized month' do
it 'returns a discount budget' do
result = subject.for_month(march_date_a)
expect(result).to be_an_instance_of(PlanetExpress::DiscountBudget)
expect(result.available_budget).to eq(PlanetExpress::DiscountBudgetForMonthRepository::DEFAULT_BUDGET)
end
end
context 'other month' do
let(:april_date) { Date.new(2021, 4, 12) }
it 'returns a different discount budget' do
expect(subject.for_month(march_date_a).object_id).to_not(eq(
subject.for_month(april_date).object_id))
end
end
end
end

View File

@ -5,7 +5,6 @@ RSpec.describe PlanetExpress::DiscountBudget do
subject { described_class.new(budget: budget) }
describe '#apply_discount' do
context 'with sufficient remaining budget' do
it 'returns the full discount' do
expect(subject.apply_discount(120)).to eq(120)
@ -14,7 +13,7 @@ RSpec.describe PlanetExpress::DiscountBudget do
end
context 'with insufficient remaining budget' do
it 'returns the full discount' do
it 'returns only the remaining budget' do
expect(subject.apply_discount(650)).to eq(500)
expect(subject.available_budget).to eq(0)
end