Skip to content

Commit 1edd07a

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

10 files changed

+291
-166
lines changed

rails_application/test/orders/broadcast_test.rb

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,22 @@ def test_broadcast_add_item_to_basket
3030

3131
product_id = SecureRandom.uuid
3232
order_id = SecureRandom.uuid
33-
run_command(
34-
ProductCatalog::RegisterProduct.new(
35-
product_id: product_id
33+
event_store.publish(
34+
ProductCatalog::ProductRegistered.new(
35+
data: {
36+
product_id: product_id
37+
}
3638
)
3739
)
38-
run_command(
39-
ProductCatalog::NameProduct.new(
40-
product_id: product_id,
41-
name: "Async Remote"
40+
event_store.publish(
41+
ProductCatalog::ProductNamed.new(
42+
data: {
43+
product_id: product_id,
44+
name: "Async Remote"
45+
}
4246
)
4347
)
44-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
48+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 20 }))
4549

4650
in_memory_broadcast.result.clear
4751

@@ -68,18 +72,22 @@ def test_broadcast_remove_item_from_basket
6872
event_store = Rails.configuration.event_store
6973

7074
product_id = SecureRandom.uuid
71-
run_command(
72-
ProductCatalog::RegisterProduct.new(
73-
product_id: product_id
75+
event_store.publish(
76+
ProductCatalog::ProductRegistered.new(
77+
data: {
78+
product_id: product_id
79+
}
7480
)
7581
)
76-
run_command(
77-
ProductCatalog::NameProduct.new(
78-
product_id: product_id,
79-
name: "Async Remote"
82+
event_store.publish(
83+
ProductCatalog::ProductNamed.new(
84+
data: {
85+
product_id: product_id,
86+
name: "Async Remote"
87+
}
8088
)
8189
)
82-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
90+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 20 }))
8391
order_id = SecureRandom.uuid
8492

8593
event_store.publish(
@@ -123,18 +131,22 @@ def test_broadcast_update_order_value
123131
product_id = SecureRandom.uuid
124132
order_id = SecureRandom.uuid
125133
order_1_id = SecureRandom.uuid
126-
run_command(
127-
ProductCatalog::RegisterProduct.new(
128-
product_id: product_id
134+
event_store.publish(
135+
ProductCatalog::ProductRegistered.new(
136+
data: {
137+
product_id: product_id
138+
}
129139
)
130140
)
131-
run_command(
132-
ProductCatalog::NameProduct.new(
133-
product_id: product_id,
134-
name: "Async Remote"
141+
event_store.publish(
142+
ProductCatalog::ProductNamed.new(
143+
data: {
144+
product_id: product_id,
145+
name: "Async Remote"
146+
}
135147
)
136148
)
137-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
149+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 20 }))
138150
event_store.publish(
139151
Pricing::PriceItemAdded.new(
140152
data: {
@@ -197,18 +209,22 @@ def test_broadcast_update_discount
197209
product_id = SecureRandom.uuid
198210
order_id = SecureRandom.uuid
199211
order_1_id = SecureRandom.uuid
200-
run_command(
201-
ProductCatalog::RegisterProduct.new(
202-
product_id: product_id
212+
event_store.publish(
213+
ProductCatalog::ProductRegistered.new(
214+
data: {
215+
product_id: product_id
216+
}
203217
)
204218
)
205-
run_command(
206-
ProductCatalog::NameProduct.new(
207-
product_id: product_id,
208-
name: "Async Remote"
219+
event_store.publish(
220+
ProductCatalog::ProductNamed.new(
221+
data: {
222+
product_id: product_id,
223+
name: "Async Remote"
224+
}
209225
)
210226
)
211-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
227+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 20 }))
212228
event_store.publish(
213229
Pricing::PriceItemAdded.new(
214230
data: {

rails_application/test/orders/discount_test.rb

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,64 @@ def test_newest_event_is_always_applied
109109
private
110110

111111
def remove_percentage_discount(order_id)
112-
run_command(Pricing::RemovePercentageDiscount.new(order_id: order_id))
112+
event_store.publish(Pricing::PercentageDiscountRemoved.new(
113+
data: {
114+
order_id: order_id,
115+
type: Pricing::Discounts::GENERAL_DISCOUNT
116+
}
117+
))
113118
end
114119

115120
def set_percentage_discount(order_id)
116-
run_command(Pricing::SetPercentageDiscount.new(order_id: order_id, amount: 10))
121+
event_store.publish(Pricing::PercentageDiscountSet.new(
122+
data: {
123+
order_id: order_id,
124+
type: Pricing::Discounts::GENERAL_DISCOUNT,
125+
amount: 10
126+
}
127+
))
117128
end
118129

119130
def change_percentage_discount(order_id)
120-
run_command(Pricing::ChangePercentageDiscount.new(order_id: order_id, amount: 1))
131+
event_store.publish(Pricing::PercentageDiscountChanged.new(
132+
data: {
133+
order_id: order_id,
134+
type: Pricing::Discounts::GENERAL_DISCOUNT,
135+
amount: 1
136+
}
137+
))
121138
end
122139

123140
def item_added_to_basket(order_id, product_id)
124-
run_command(Pricing::AddPriceItem.new(product_id: product_id, order_id: order_id, price: 50))
141+
event_store.publish(Pricing::PriceItemAdded.new(
142+
data: {
143+
order_id: order_id,
144+
product_id: product_id,
145+
base_price: 50,
146+
price: 50,
147+
base_total_value: 50,
148+
total_value: 50
149+
}
150+
))
125151
end
126152

127153
def prepare_product(product_id)
128-
run_command(
129-
ProductCatalog::RegisterProduct.new(
130-
product_id: product_id,
154+
event_store.publish(
155+
ProductCatalog::ProductRegistered.new(
156+
data: {
157+
product_id: product_id
158+
}
131159
)
132160
)
133-
run_command(
134-
ProductCatalog::NameProduct.new(
135-
product_id: product_id,
136-
name: "test"
161+
event_store.publish(
162+
ProductCatalog::ProductNamed.new(
163+
data: {
164+
product_id: product_id,
165+
name: "test"
166+
}
137167
)
138168
)
139-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 50))
169+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 50 }))
140170
end
141171

142172
def customer_registered(customer_id)
@@ -148,13 +178,15 @@ def event_store
148178
end
149179

150180
def create_active_time_promotion
151-
run_command(
152-
Pricing::CreateTimePromotion.new(
153-
time_promotion_id: SecureRandom.uuid,
154-
discount: 50,
155-
start_time: Time.current - 1,
156-
end_time: Time.current + 1,
157-
label: "Last Minute"
181+
event_store.publish(
182+
Pricing::TimePromotionCreated.new(
183+
data: {
184+
time_promotion_id: SecureRandom.uuid,
185+
discount: 50,
186+
start_time: Time.current - 1,
187+
end_time: Time.current + 1,
188+
label: "Last Minute"
189+
}
158190
)
159191
)
160192
end

rails_application/test/orders/item_removed_from_basket_test.rb

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ def test_remove_item_when_quantity_gt_1
88
event_store = Rails.configuration.event_store
99

1010
product_id = SecureRandom.uuid
11-
run_command(
12-
ProductCatalog::RegisterProduct.new(
13-
product_id: product_id
11+
event_store.publish(
12+
ProductCatalog::ProductRegistered.new(
13+
data: {
14+
product_id: product_id
15+
}
1416
)
1517
)
16-
run_command(
17-
ProductCatalog::NameProduct.new(
18-
product_id: product_id,
19-
name: "something"
18+
event_store.publish(
19+
ProductCatalog::ProductNamed.new(
20+
data: {
21+
product_id: product_id,
22+
name: "something"
23+
}
2024
)
2125
)
22-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
26+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 20 }))
2327
customer_id = SecureRandom.uuid
24-
run_command(
25-
Crm::RegisterCustomer.new(customer_id: customer_id, name: "dummy")
28+
event_store.publish(
29+
Crm::CustomerRegistered.new(data: { customer_id: customer_id, name: "dummy" })
2630
)
2731
order_id = SecureRandom.uuid
2832
event_store.publish(
@@ -73,21 +77,25 @@ def test_remove_item_when_quantity_eq_1
7377
event_store = Rails.configuration.event_store
7478

7579
product_id = SecureRandom.uuid
76-
run_command(
77-
ProductCatalog::RegisterProduct.new(
78-
product_id: product_id
80+
event_store.publish(
81+
ProductCatalog::ProductRegistered.new(
82+
data: {
83+
product_id: product_id
84+
}
7985
)
8086
)
81-
run_command(
82-
ProductCatalog::NameProduct.new(
83-
product_id: product_id,
84-
name: "Async Remote"
87+
event_store.publish(
88+
ProductCatalog::ProductNamed.new(
89+
data: {
90+
product_id: product_id,
91+
name: "Async Remote"
92+
}
8593
)
8694
)
87-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
95+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 20 }))
8896
customer_id = SecureRandom.uuid
89-
run_command(
90-
Crm::RegisterCustomer.new(customer_id: customer_id, name: "dummy")
97+
event_store.publish(
98+
Crm::CustomerRegistered.new(data: { customer_id: customer_id, name: "dummy" })
9199
)
92100
order_id = SecureRandom.uuid
93101
event_store.publish(
@@ -122,37 +130,45 @@ def test_remove_item_when_there_is_another_item
122130
event_store = Rails.configuration.event_store
123131

124132
product_id = SecureRandom.uuid
125-
run_command(
126-
ProductCatalog::RegisterProduct.new(
127-
product_id: product_id
133+
event_store.publish(
134+
ProductCatalog::ProductRegistered.new(
135+
data: {
136+
product_id: product_id
137+
}
128138
)
129139
)
130-
run_command(
131-
ProductCatalog::NameProduct.new(
132-
product_id: product_id,
133-
name: "test"
140+
event_store.publish(
141+
ProductCatalog::ProductNamed.new(
142+
data: {
143+
product_id: product_id,
144+
name: "test"
145+
}
134146
)
135147
)
136-
run_command(Pricing::SetPrice.new(product_id: product_id, price: 20))
148+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: 20 }))
137149

138150
another_product_id = SecureRandom.uuid
139-
run_command(
140-
ProductCatalog::RegisterProduct.new(
141-
product_id: another_product_id
151+
event_store.publish(
152+
ProductCatalog::ProductRegistered.new(
153+
data: {
154+
product_id: another_product_id
155+
}
142156
)
143157
)
144-
run_command(
145-
ProductCatalog::NameProduct.new(
146-
product_id: another_product_id,
147-
name: "test2"
158+
event_store.publish(
159+
ProductCatalog::ProductNamed.new(
160+
data: {
161+
product_id: another_product_id,
162+
name: "test2"
163+
}
148164
)
149165
)
150-
run_command(
151-
Pricing::SetPrice.new(product_id: another_product_id, price: 20)
166+
event_store.publish(
167+
Pricing::PriceSet.new(data: { product_id: another_product_id, price: 20 })
152168
)
153169
customer_id = SecureRandom.uuid
154-
run_command(
155-
Crm::RegisterCustomer.new(customer_id: customer_id, name: "dummy")
170+
event_store.publish(
171+
Crm::CustomerRegistered.new(data: { customer_id: customer_id, name: "dummy" })
156172
)
157173
order_id = SecureRandom.uuid
158174
event_store.publish(

rails_application/test/orders/item_removed_from_refund_test.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,26 @@ def create_draft_refund(refund_id, order_id, refundable_products)
3737
end
3838

3939
def prepare_product(product_id, price)
40-
run_command(
41-
ProductCatalog::RegisterProduct.new(
42-
product_id: product_id,
43-
)
40+
event_store.publish(
41+
ProductCatalog::ProductRegistered.new(
42+
data: {
43+
product_id: product_id
44+
}
45+
)
4446
)
45-
run_command(
46-
ProductCatalog::NameProduct.new(
47-
product_id: product_id,
48-
name: "Async Remote"
47+
event_store.publish(
48+
ProductCatalog::ProductNamed.new(
49+
data: {
50+
product_id: product_id,
51+
name: "Async Remote"
52+
}
4953
)
5054
)
51-
run_command(Pricing::SetPrice.new(product_id: product_id, price: price))
55+
event_store.publish(Pricing::PriceSet.new(data: { product_id: product_id, price: price }))
56+
end
57+
58+
def event_store
59+
Rails.configuration.event_store
5260
end
5361

5462
def item_added_to_refund(refund_id, order_id, product_id)

0 commit comments

Comments
 (0)