Skip to content

Commit b9becdd

Browse files
Turned 3 more Orders event handlers tests into event-driven
This helps with changes at the domain/process level, where we don't need to change the tests.
1 parent a686775 commit b9becdd

File tree

3 files changed

+74
-31
lines changed

3 files changed

+74
-31
lines changed

rails_application/test/orders/item_added_to_refund_test.rb

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,52 @@ def test_add_item_to_refund
2727

2828
private
2929

30+
def event_store
31+
Rails.configuration.event_store
32+
end
33+
3034
def prepare_product(product_id, price)
31-
run_command(
32-
ProductCatalog::RegisterProduct.new(
33-
product_id: product_id,
34-
)
35+
event_store.publish(
36+
ProductCatalog::ProductRegistered.new(
37+
data: { product_id: product_id }
38+
)
3539
)
36-
run_command(
37-
ProductCatalog::NameProduct.new(
38-
product_id: product_id,
39-
name: "Async Remote"
40+
event_store.publish(
41+
ProductCatalog::ProductNamed.new(
42+
data: { product_id: product_id, name: "Async Remote" }
4043
)
4144
)
42-
run_command(Pricing::SetPrice.new(product_id: product_id, price: price))
45+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: price }))
4346
end
4447

4548
def place_order(order_id, product_id, price)
46-
run_command(Pricing::AddPriceItem.new(order_id:, product_id:, price:))
47-
run_command(Pricing::AcceptOffer.new(order_id:))
49+
event_store.publish(
50+
Pricing::PriceItemAdded.new(
51+
data: {
52+
order_id: order_id,
53+
product_id: product_id,
54+
base_price: price,
55+
price: price,
56+
base_total_value: price,
57+
total_value: price
58+
}
59+
)
60+
)
61+
event_store.publish(
62+
Pricing::OfferAccepted.new(
63+
data: {
64+
order_id: order_id,
65+
order_lines: [{ product_id: product_id, quantity: 1 }]
66+
}
67+
)
68+
)
4869
end
4970

5071
def create_draft_refund(refund_id, order_id)
51-
run_command(
52-
Ordering::CreateDraftRefund.new(refund_id: refund_id, order_id: order_id)
72+
draft_refund_created = Ordering::DraftRefundCreated.new(
73+
data: { refund_id: refund_id, order_id: order_id, refundable_products: [] }
5374
)
75+
CreateDraftRefund.new.call(draft_refund_created)
5476
end
5577

5678
def item_added_to_refund(refund_id, order_id, product_id)

rails_application/test/orders/order_paid_test.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@ def test_order_confirmed
1212
product_id = SecureRandom.uuid
1313

1414
create_product(product_id, "Async Remote", 10)
15-
run_command(Crm::RegisterCustomer.new(customer_id: customer_id, name: "John Doe"))
16-
run_command(Pricing::AddPriceItem.new(order_id: order_id, product_id: product_id, price: 10))
17-
run_command(Pricing::AcceptOffer.new(order_id: order_id))
18-
run_command(
19-
Crm::AssignCustomerToOrder.new(customer_id: customer_id, order_id: order_id)
15+
event_store.publish(Crm::CustomerRegistered.new(data: { customer_id: customer_id, name: "John Doe" }))
16+
event_store.publish(
17+
Pricing::PriceItemAdded.new(
18+
data: {
19+
order_id: order_id,
20+
product_id: product_id,
21+
base_price: 10,
22+
price: 10,
23+
base_total_value: 10,
24+
total_value: 10
25+
}
26+
)
27+
)
28+
event_store.publish(
29+
Pricing::OfferAccepted.new(
30+
data: {
31+
order_id: order_id,
32+
order_lines: [{ product_id: product_id, quantity: 1 }]
33+
}
34+
)
35+
)
36+
event_store.publish(
37+
Crm::CustomerAssignedToOrder.new(data: { customer_id: customer_id, order_id: order_id })
2038
)
2139

2240
event_store.publish(Processes::TotalOrderValueUpdated.new(data: { order_id: order_id, discounted_amount: 0, total_amount: 10 }))
@@ -38,9 +56,13 @@ def test_order_confirmed
3856
private
3957

4058
def create_product(product_id, name, price)
41-
run_command(ProductCatalog::RegisterProduct.new(product_id: product_id))
42-
run_command(ProductCatalog::NameProduct.new(product_id: product_id, name: name))
43-
run_command(Pricing::SetPrice.new(product_id: product_id, price: price))
59+
event_store.publish(ProductCatalog::ProductRegistered.new(data: { product_id: product_id }))
60+
event_store.publish(ProductCatalog::ProductNamed.new(data: { product_id: product_id, name: name }))
61+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: price }))
62+
end
63+
64+
def event_store
65+
Rails.configuration.event_store
4466
end
4567
end
4668
end

rails_application/test/orders/product_price_changed_test.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_reflects_change
88
product_id = prepare_product
99
unchanged_product_id = prepare_product
1010

11-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 100))
11+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 100 }))
1212

1313
assert_equal 100, Product.find_by_uid(product_id).price
1414
assert_equal 50, Product.find_by_uid(unchanged_product_id).price
@@ -24,18 +24,17 @@ def event_store
2424

2525
def prepare_product
2626
product_id = SecureRandom.uuid
27-
run_command(
28-
ProductCatalog::RegisterProduct.new(
29-
product_id: product_id,
30-
)
27+
event_store.publish(
28+
ProductCatalog::ProductRegistered.new(
29+
data: { product_id: product_id }
30+
)
3131
)
32-
run_command(
33-
ProductCatalog::NameProduct.new(
34-
product_id: product_id,
35-
name: "test"
32+
event_store.publish(
33+
ProductCatalog::ProductNamed.new(
34+
data: { product_id: product_id, name: "test" }
3635
)
3736
)
38-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 50))
37+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 50 }))
3938

4039
product_id
4140
end

0 commit comments

Comments
 (0)