1
0

Add rule_applied_in_month and l_shipment count to shipment repo

- Add method to check whehter a rule was applied to a shipment in a given month
- Add a method to count the l shipments for a specific provider in a given month
This commit is contained in:
Tim Kächele 2021-03-07 19:23:39 +01:00
parent c43263a49b
commit 5c3bb4d41a
3 changed files with 193 additions and 6 deletions

View File

@ -1,20 +1,27 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'planet_express/version' require 'planet_express/date'
require 'planet_express/version'
require 'planet_express/repository' require 'planet_express/repository'
require 'planet_express/discount'
require 'planet_express/discount_budget' require 'planet_express/discount_budget'
require 'planet_express/discount_budget_for_month_repository' require 'planet_express/discount_budget_for_month_repository'
require 'planet_express/invalid_shipment'
require 'planet_express/shipment'
require 'planet_express/shipment_repository'
require 'planet_express/shipment_reader'
require 'planet_express/shipping_option' require 'planet_express/shipping_option'
require 'planet_express/shipping_option_repository' require 'planet_express/shipping_option_repository'
require 'planet_express/shipment' require 'planet_express/rule'
require 'planet_express/invalid_shipment' # require 'planet_express/s_shipment_discount_rule'
require 'planet_express/shipment_reader' # require 'planet_express/l_shipment_discount_rule'
require 'planet_express/shipment_discount_calculator'
module PlanetExpress module PlanetExpress
class Error < StandardError; end
# Your code goes here...
end end

View File

@ -0,0 +1,30 @@
module PlanetExpress
class ShipmentRepository < Repository
def rule_applied_in_month?(month, rule)
valid_shipments.any? do |shipment|
shipment.month == month &&
shipment.discounts.any? { |discount| discount.rule.instance_of?(rule) }
end
end
def l_shipment_count_for_provider_and_month(provider, month)
valid_shipments.select do |shipment|
shipment.month == month &&
shipment.l_shipment? &&
shipment.provider == provider
end.length
end
private
def valid_shipments
valid_shipments = []
data.each do |shipment|
shipment.if_valid do |valid_shipment|
valid_shipments.push(valid_shipment)
end
end
valid_shipments
end
end
end

View File

@ -0,0 +1,150 @@
# frozen_string_literal: true
RSpec.describe PlanetExpress::ShipmentRepository do
let(:items) { [] }
subject { described_class.new(data: items) }
describe 'rule_applied_in_month?' do
context 'empty repository' do
let(:items) { [] }
it 'returns false' do
result = subject.rule_applied_in_month?(Date.today.begin_of_month,
PlanetExpress::Rule)
expect(result).to eq(false)
end
end
context 'with invalid shipments' do
let(:items) { [PlanetExpress::InvalidShipment.new("")] }
it 'returns false' do
result = subject.rule_applied_in_month?(Date.today.begin_of_month,
PlanetExpress::Rule)
expect(result).to eq(false)
end
end
context 'with valid shipment matching' do
let(:rule) do
Object.new
end
let(:shipment) do
shipment = PlanetExpress::Shipment.new(Date.today, build(:shipping_option))
shipment.add_discount(PlanetExpress::Discount.new(50, rule))
shipment
end
let(:items) { [shipment] }
it 'returns true' do
result = subject.rule_applied_in_month?(Date.today.begin_of_month,
rule.class)
expect(result).to eq(true)
end
end
context 'with valid shipment not matching' do
let(:rule) do
Object.new
end
let(:other_rule) do
-> () {}
end
let(:shipment) do
shipment = PlanetExpress::Shipment.new(Date.today, build(:shipping_option))
shipment.add_discount(PlanetExpress::Discount.new(50, rule))
shipment
end
let(:items) { [shipment] }
it 'returns false' do
result = subject.rule_applied_in_month?(Date.today.begin_of_month,
other_rule.class)
expect(result).to eq(false)
end
end
end
describe '#l_shipment_count_for_provider_and_month' do
context 'empty repository' do
let(:items) { [] }
it 'returns 0' do
result = subject.l_shipment_count_for_provider_and_month(
'LP',
Date.today.begin_of_month)
expect(result).to eq(0)
end
end
context 'with invalid shipments' do
let(:items) { [PlanetExpress::InvalidShipment.new("")] }
it 'returns 0' do
result = subject.l_shipment_count_for_provider_and_month(
'LP',
Date.today.begin_of_month)
expect(result).to eq(0)
end
end
context 'with valid shipment matching' do
let(:shipment) do
PlanetExpress::Shipment.new(Date.today,
build(:shipping_option,
package_size: 'L'))
end
let(:provider) { shipment.provider }
let(:items) { [shipment] }
it 'returns 1' do
result = subject.l_shipment_count_for_provider_and_month(provider,
Date.today.begin_of_month)
expect(result).to eq(1)
end
end
context 'with valid shipment not matching' do
let(:shipment) do
PlanetExpress::Shipment.new(Date.today,
build(:shipping_option,
package_size: 'L'))
end
let(:provider) { shipment.provider }
let(:items) { [shipment] }
context 'different provider' do
it 'returns 0' do
result = subject.l_shipment_count_for_provider_and_month('MP_XXX_TEST',
Date.today.begin_of_month)
expect(result).to eq(0)
end
end
context 'different month' do
it 'returns 0' do
result = subject.l_shipment_count_for_provider_and_month(provider,
Date.today.next_month.begin_of_month)
expect(result).to eq(0)
end
end
end
end
end