Skip to content

Commit

Permalink
according to the docs only draft orders should be expirable
Browse files Browse the repository at this point in the history
2nd step towards #267
  • Loading branch information
pjurewicz committed May 6, 2024
1 parent 031bf3b commit bb57902
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 80 deletions.
4 changes: 1 addition & 3 deletions ecommerce/ordering/lib/ordering/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ def reject
end

def confirm
raise OrderHasExpired if @state.equal?(:expired)
raise NotPlaced unless @state.equal?(:placed)
apply OrderConfirmed.new(data: { order_id: @id })
end

def expire
raise AlreadyConfirmed if @state.equal?(:confirmed)
raise AlreadySubmitted unless @state.equal?(:draft)
apply OrderExpired.new(data: { order_id: @id })
end

Expand All @@ -73,7 +72,6 @@ def remove_item(product_id)
end

def cancel
raise OrderHasExpired if @state.equal?(:expired)
raise AlreadyConfirmed if @state.equal?(:confirmed)
raise NotPlaced unless @state.equal?(:placed)
apply OrderCancelled.new(data: { order_id: @id })
Expand Down
23 changes: 0 additions & 23 deletions ecommerce/ordering/test/cancel_order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,5 @@ def test_confirmed_order_cant_be_cancelled
act(CancelOrder.new(order_id: aggregate_id))
end
end

def test_expired_order_cant_be_cancelled
aggregate_id = SecureRandom.uuid
product_id = SecureRandom.uuid
customer_id = SecureRandom.uuid

arrange(
AddItemToBasket.new(
order_id: aggregate_id,
product_id: product_id
),
SubmitOrder.new(
order_id: aggregate_id,
order_number: "2018/12/1",
customer_id: customer_id
),
SetOrderAsExpired.new(order_id: aggregate_id)
)

assert_raises(Order::OrderHasExpired) do
act(CancelOrder.new(order_id: aggregate_id))
end
end
end
end
22 changes: 0 additions & 22 deletions ecommerce/ordering/test/confirm_order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,5 @@ def test_submitted_order_will_be_confirmed
act(ConfirmOrder.new(order_id: aggregate_id))
end
end

def test_expired_order_cannot_be_confirmed
aggregate_id = SecureRandom.uuid
product_id = SecureRandom.uuid
customer_id = SecureRandom.uuid
arrange(
AddItemToBasket.new(
order_id: aggregate_id,
product_id: product_id
),
SubmitOrder.new(
order_id: aggregate_id,
order_number: "2018/12/1",
customer_id: customer_id
),
SetOrderAsExpired.new(order_id: aggregate_id)
)

assert_raises(Order::OrderHasExpired) do
act(ConfirmOrder.new(order_id: aggregate_id))
end
end
end
end
6 changes: 3 additions & 3 deletions ecommerce/ordering/test/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ def test_disallowed_order_state_transitions
submitted_order.reject
assert_raises(Order::InvalidState) { submitted_order.cancel }
assert_raises(Order::InvalidState) { submitted_order.confirm }
submitted_order.expire
assert_raises(Order::InvalidState) { submitted_order.expire }

placed_order.submit(NumberGenerator.new.call)
assert_raises(Order::InvalidState) { placed_order.accept }
assert_raises(Order::InvalidState) { placed_order.reject }
placed_order.cancel
placed_order.confirm
placed_order.expire
assert_raises(Order::InvalidState) { placed_order.expire }

confirmed_order.submit(NumberGenerator.new.call)
assert_raises(Order::InvalidState) { confirmed_order.accept }
Expand All @@ -77,7 +77,7 @@ def test_disallowed_order_state_transitions
assert_raises(Order::InvalidState) { expired_order.reject }
assert_raises(Order::InvalidState) { expired_order.cancel }
assert_raises(Order::InvalidState) { expired_order.confirm }
expired_order.expire
assert_raises(Order::InvalidState) { expired_order.expire }

cancelled_order.submit(NumberGenerator.new.call)
assert_raises(Order::InvalidState) { cancelled_order.accept }
Expand Down
31 changes: 2 additions & 29 deletions ecommerce/ordering/test/set_order_as_expired_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_draft_order_will_expire
) { act(SetOrderAsExpired.new(order_id: aggregate_id)) }
end

def test_submitted_order_will_expire
def test_submitted_order_will_not_expire
aggregate_id = SecureRandom.uuid
stream = "Ordering::Order$#{aggregate_id}"
product_id = SecureRandom.uuid
Expand All @@ -40,34 +40,7 @@ def test_submitted_order_will_expire
)
)

assert_events(
stream,
OrderExpired.new(data: { order_id: aggregate_id })
) { act(SetOrderAsExpired.new(order_id: aggregate_id)) }
end

def test_confirmed_order_cannot_expire
aggregate_id = SecureRandom.uuid
product_id = SecureRandom.uuid
customer_id = SecureRandom.uuid

arrange(
AddItemToBasket.new(
order_id: aggregate_id,
product_id: product_id
),
SubmitOrder.new(
order_id: aggregate_id,
order_number: "2018/12/1",
customer_id: customer_id
),
AcceptOrder.new(order_id: aggregate_id),
ConfirmOrder.new(order_id: aggregate_id)
)

assert_raises(Order::AlreadyConfirmed) do
act(SetOrderAsExpired.new(order_id: aggregate_id))
end
assert_raises(Order::AlreadySubmitted) { act(SetOrderAsExpired.new(order_id: aggregate_id)) }
end
end
end

0 comments on commit bb57902

Please sign in to comment.