|
| 1 | +require_relative "test_helper" |
| 2 | + |
| 3 | +module Pricing |
| 4 | + class MultiplePromotionTest < Test |
| 5 | + cover "Pricing*" |
| 6 | + |
| 7 | + def test_given_multiple_discounts_applied_when_time_promotion_is_removed_then_other_promotion_is_included |
| 8 | + order_id = SecureRandom.uuid |
| 9 | + product_id = SecureRandom.uuid |
| 10 | + given( |
| 11 | + [ |
| 12 | + TimePromotionCreated.new( |
| 13 | + data: { |
| 14 | + time_promotion_id: SecureRandom.uuid, |
| 15 | + discount: 10, |
| 16 | + start_time: Time.current - 1, |
| 17 | + end_time: Time.current + 1 |
| 18 | + } |
| 19 | + ), |
| 20 | + Pricing::PriceItemAdded.new( |
| 21 | + data: { |
| 22 | + product_id: product_id, |
| 23 | + order_id: order_id, |
| 24 | + base_price: 1000, |
| 25 | + price: 1000, |
| 26 | + base_total_value: 1000, |
| 27 | + total_value: 1000 |
| 28 | + } |
| 29 | + ), |
| 30 | + PercentageDiscountSet.new( |
| 31 | + data: { |
| 32 | + order_id: order_id, |
| 33 | + type: Discounts::TIME_PROMOTION_DISCOUNT, |
| 34 | + amount: 10, |
| 35 | + base_total_value: 1000, |
| 36 | + total_value: 900 |
| 37 | + } |
| 38 | + ), |
| 39 | + PercentageDiscountSet.new( |
| 40 | + data: { |
| 41 | + order_id: order_id, |
| 42 | + type: Discounts::GENERAL_DISCOUNT, |
| 43 | + amount: 50, |
| 44 | + base_total_value: 1000, |
| 45 | + total_value: 450 |
| 46 | + } |
| 47 | + ) |
| 48 | + ] |
| 49 | + ) |
| 50 | + |
| 51 | + assert_events_contain( |
| 52 | + stream_name(order_id), |
| 53 | + PercentageDiscountRemoved.new( |
| 54 | + data: { |
| 55 | + order_id: order_id, |
| 56 | + type: Discounts::GENERAL_DISCOUNT, |
| 57 | + base_total_value: 1000, |
| 58 | + total_value: 900 |
| 59 | + } |
| 60 | + ) |
| 61 | + ) { run_command(RemovePercentageDiscount.new(order_id: order_id)) } |
| 62 | + end |
| 63 | + |
| 64 | + private |
| 65 | + |
| 66 | + def given(events) |
| 67 | + events.each do |event| |
| 68 | + event_store.append( |
| 69 | + event, |
| 70 | + stream_name: stream_name(event.data[:order_id]) |
| 71 | + ) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def stream_name(order_id) |
| 76 | + "Pricing::Offer$#{order_id}" |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments