|
| 1 | +require_relative "test_helper" |
| 2 | + |
| 3 | +module Pricing |
| 4 | + class ApplyTimePromotionTest < Test |
| 5 | + cover "Pricing*" |
| 6 | + |
| 7 | + def test_applies_biggest_time_promotion_discount |
| 8 | + order_id = SecureRandom.uuid |
| 9 | + product_id = SecureRandom.uuid |
| 10 | + create_inactive_time_promotion(60) |
| 11 | + create_active_time_promotion(10) |
| 12 | + create_active_time_promotion(50) |
| 13 | + create_active_time_promotion(30) |
| 14 | + |
| 15 | + assert_events_contain(stream_name(order_id), time_promotion_discount_set_event(order_id, 50)) do |
| 16 | + Pricing::ApplyTimePromotion.new.call(item_added_to_basket_event(order_id, product_id)) |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + def test_resets_time_promotion_discount |
| 21 | + order_id = SecureRandom.uuid |
| 22 | + product_id = SecureRandom.uuid |
| 23 | + set_time_promotion_discount(order_id, 50) |
| 24 | + |
| 25 | + assert_events_contain(stream_name(order_id), time_promotion_discount_reset_event(order_id)) do |
| 26 | + Pricing::ApplyTimePromotion.new.call(item_added_to_basket_event(order_id, product_id)) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + private |
| 31 | + |
| 32 | + def create_inactive_time_promotion(discount) |
| 33 | + run_command( |
| 34 | + Pricing::CreateTimePromotion.new( |
| 35 | + time_promotion_id: SecureRandom.uuid, |
| 36 | + discount: discount, |
| 37 | + start_time: Time.current - 2, |
| 38 | + end_time: Time.current - 1, |
| 39 | + label: "Past Promotion" |
| 40 | + ) |
| 41 | + ) |
| 42 | + end |
| 43 | + |
| 44 | + def create_active_time_promotion(discount) |
| 45 | + run_command( |
| 46 | + Pricing::CreateTimePromotion.new( |
| 47 | + time_promotion_id: SecureRandom.uuid, |
| 48 | + discount: discount, |
| 49 | + start_time: Time.current - 1, |
| 50 | + end_time: Time.current + 1, |
| 51 | + label: "Last Minute" |
| 52 | + ) |
| 53 | + ) |
| 54 | + end |
| 55 | + |
| 56 | + def item_added_to_basket_event(order_id, product_id) |
| 57 | + Pricing::PriceItemAdded.new( |
| 58 | + data: { |
| 59 | + product_id: product_id, |
| 60 | + order_id: order_id |
| 61 | + } |
| 62 | + ) |
| 63 | + end |
| 64 | + |
| 65 | + def set_time_promotion_discount(order_id, discount) |
| 66 | + run_command(SetTimePromotionDiscount.new(order_id: order_id, amount: 50)) |
| 67 | + end |
| 68 | + |
| 69 | + def time_promotion_discount_set_event(order_id, amount) |
| 70 | + TimePromotionDiscountSet.new( |
| 71 | + data: { |
| 72 | + order_id: order_id, |
| 73 | + amount: amount |
| 74 | + } |
| 75 | + ) |
| 76 | + end |
| 77 | + |
| 78 | + def time_promotion_discount_reset_event(order_id) |
| 79 | + TimePromotionDiscountReset.new( |
| 80 | + data: { |
| 81 | + order_id: order_id |
| 82 | + } |
| 83 | + ) |
| 84 | + end |
| 85 | + |
| 86 | + def stream_name(order_id) |
| 87 | + "Pricing::Offer$#{order_id}" |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments