@@ -4,18 +4,23 @@ module Processes
44 class InvoiceGenerationTest < ProcessTest
55 cover "Processes::InvoiceGeneration"
66
7+ def setup
8+ super
9+
10+ @vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
11+ @product_1_id = SecureRandom . uuid
12+ event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : @product_1_id , vat_rate : @vat_rate } ) )
13+ end
14+
715 def test_calculates_sub_amounts
8- product_1_id = SecureRandom . uuid
916 product_2_id = SecureRandom . uuid
10- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
11-
12- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
13- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_2_id , vat_rate : vat_rate } ) )
17+
18+ event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_2_id , vat_rate : @vat_rate } ) )
1419
1520 process = InvoiceGeneration . new ( event_store , command_bus )
1621
1722 events = [
18- price_item_added ( product_1_id , 20 , 20 ) ,
23+ price_item_added ( @ product_1_id, 20 , 20 ) ,
1924 price_item_added ( product_2_id , 30 , 30 ) ,
2025 price_item_added ( product_2_id , 30 , 30 ) ,
2126 order_placed
@@ -29,16 +34,16 @@ def test_calculates_sub_amounts
2934 expected_commands = [
3035 Invoicing ::AddInvoiceItem . new (
3136 invoice_id : order_id ,
32- product_id : product_1_id ,
37+ product_id : @ product_1_id,
3338 quantity : 1 ,
34- vat_rate : vat_rate ,
39+ vat_rate : @ vat_rate,
3540 unit_price : 20 . to_d
3641 ) ,
3742 Invoicing ::AddInvoiceItem . new (
3843 invoice_id : order_id ,
3944 product_id : product_2_id ,
4045 quantity : 2 ,
41- vat_rate : vat_rate ,
46+ vat_rate : @ vat_rate,
4247 unit_price : 30 . to_d
4348 )
4449 ]
@@ -50,17 +55,14 @@ def test_calculates_sub_amounts
5055 end
5156
5257 def test_calculates_sub_amounts_with_discount
53- product_1_id = SecureRandom . uuid
5458 product_2_id = SecureRandom . uuid
55- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
56-
57- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
58- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_2_id , vat_rate : vat_rate } ) )
59+
60+ event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_2_id , vat_rate : @vat_rate } ) )
5961
6062 process = InvoiceGeneration . new ( event_store , command_bus )
6163
6264 events = [
63- price_item_added ( product_1_id , 20 , 20 ) ,
65+ price_item_added ( @ product_1_id, 20 , 20 ) ,
6466 price_item_added ( product_2_id , 30 , 30 ) ,
6567 price_item_added ( product_2_id , 30 , 30 ) ,
6668 percentage_discount_set ( "general_discount" , 10 ) ,
@@ -75,16 +77,16 @@ def test_calculates_sub_amounts_with_discount
7577 expected_commands = [
7678 Invoicing ::AddInvoiceItem . new (
7779 invoice_id : order_id ,
78- product_id : product_1_id ,
80+ product_id : @ product_1_id,
7981 quantity : 1 ,
80- vat_rate : vat_rate ,
82+ vat_rate : @ vat_rate,
8183 unit_price : 18 . to_d
8284 ) ,
8385 Invoicing ::AddInvoiceItem . new (
8486 invoice_id : order_id ,
8587 product_id : product_2_id ,
8688 quantity : 2 ,
87- vat_rate : vat_rate ,
89+ vat_rate : @ vat_rate,
8890 unit_price : 27 . to_d
8991 )
9092 ]
@@ -96,15 +98,10 @@ def test_calculates_sub_amounts_with_discount
9698 end
9799
98100 def test_calculates_sub_amounts_with_100_percent_discount
99- product_1_id = SecureRandom . uuid
100- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
101-
102- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
103-
104101 process = InvoiceGeneration . new ( event_store , command_bus )
105102
106103 events = [
107- price_item_added ( product_1_id , 20 , 20 ) ,
104+ price_item_added ( @ product_1_id, 20 , 20 ) ,
108105 percentage_discount_set ( "general_discount" , 100 ) ,
109106 order_placed
110107 ]
@@ -116,23 +113,18 @@ def test_calculates_sub_amounts_with_100_percent_discount
116113
117114 assert_command ( Invoicing ::AddInvoiceItem . new (
118115 invoice_id : order_id ,
119- product_id : product_1_id ,
116+ product_id : @ product_1_id,
120117 quantity : 1 ,
121- vat_rate : vat_rate ,
118+ vat_rate : @ vat_rate,
122119 unit_price : 0 . to_d
123120 ) )
124121 end
125122
126123 def test_calculates_sub_amounts_with_multiple_discounts
127- product_1_id = SecureRandom . uuid
128- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
129-
130- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
131-
132124 process = InvoiceGeneration . new ( event_store , command_bus )
133125
134126 events = [
135- price_item_added ( product_1_id , 100 , 100 ) ,
127+ price_item_added ( @ product_1_id, 100 , 100 ) ,
136128 percentage_discount_set ( "general_discount" , 10 ) ,
137129 percentage_discount_set ( "time_promotion" , 15 ) ,
138130 order_placed
@@ -145,25 +137,21 @@ def test_calculates_sub_amounts_with_multiple_discounts
145137
146138 assert_command ( Invoicing ::AddInvoiceItem . new (
147139 invoice_id : order_id ,
148- product_id : product_1_id ,
140+ product_id : @ product_1_id,
149141 quantity : 1 ,
150- vat_rate : vat_rate ,
142+ vat_rate : @ vat_rate,
151143 unit_price : 75 . to_d
152144 ) )
153145 end
154146
155147 def test_calculates_sub_amounts_after_item_removal
156- product_1_id = SecureRandom . uuid
157148 product_2_id = SecureRandom . uuid
158- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
159-
160- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
161- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_2_id , vat_rate : vat_rate } ) )
149+ event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_2_id , vat_rate : @vat_rate } ) )
162150
163151 process = InvoiceGeneration . new ( event_store , command_bus )
164152
165153 events = [
166- price_item_added ( product_1_id , 20 , 20 ) ,
154+ price_item_added ( @ product_1_id, 20 , 20 ) ,
167155 price_item_added ( product_2_id , 30 , 30 ) ,
168156 price_item_added ( product_2_id , 30 , 30 ) ,
169157 price_item_removed ( product_2_id , 30 ) ,
@@ -178,16 +166,16 @@ def test_calculates_sub_amounts_after_item_removal
178166 expected_commands = [
179167 Invoicing ::AddInvoiceItem . new (
180168 invoice_id : order_id ,
181- product_id : product_1_id ,
169+ product_id : @ product_1_id,
182170 quantity : 1 ,
183- vat_rate : vat_rate ,
171+ vat_rate : @ vat_rate,
184172 unit_price : 20 . to_d
185173 ) ,
186174 Invoicing ::AddInvoiceItem . new (
187175 invoice_id : order_id ,
188176 product_id : product_2_id ,
189177 quantity : 1 ,
190- vat_rate : vat_rate ,
178+ vat_rate : @ vat_rate,
191179 unit_price : 30 . to_d
192180 )
193181 ]
@@ -199,15 +187,10 @@ def test_calculates_sub_amounts_after_item_removal
199187 end
200188
201189 def test_calculates_sub_amounts_with_discount_changed
202- product_1_id = SecureRandom . uuid
203- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
204-
205- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
206-
207190 process = InvoiceGeneration . new ( event_store , command_bus )
208191
209192 events = [
210- price_item_added ( product_1_id , 100 , 100 ) ,
193+ price_item_added ( @ product_1_id, 100 , 100 ) ,
211194 percentage_discount_set ( "general_discount" , 10 ) ,
212195 percentage_discount_changed ( "general_discount" , 20 ) ,
213196 order_placed
@@ -220,23 +203,18 @@ def test_calculates_sub_amounts_with_discount_changed
220203
221204 assert_command ( Invoicing ::AddInvoiceItem . new (
222205 invoice_id : order_id ,
223- product_id : product_1_id ,
206+ product_id : @ product_1_id,
224207 quantity : 1 ,
225- vat_rate : vat_rate ,
208+ vat_rate : @ vat_rate,
226209 unit_price : 80 . to_d
227210 ) )
228211 end
229212
230213 def test_calculates_sub_amounts_with_discount_removed
231- product_1_id = SecureRandom . uuid
232- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
233-
234- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
235-
236214 process = InvoiceGeneration . new ( event_store , command_bus )
237215
238216 events = [
239- price_item_added ( product_1_id , 100 , 100 ) ,
217+ price_item_added ( @ product_1_id, 100 , 100 ) ,
240218 percentage_discount_set ( "general_discount" , 10 ) ,
241219 percentage_discount_removed ( "general_discount" ) ,
242220 order_placed
@@ -249,23 +227,18 @@ def test_calculates_sub_amounts_with_discount_removed
249227
250228 assert_command ( Invoicing ::AddInvoiceItem . new (
251229 invoice_id : order_id ,
252- product_id : product_1_id ,
230+ product_id : @ product_1_id,
253231 quantity : 1 ,
254- vat_rate : vat_rate ,
232+ vat_rate : @ vat_rate,
255233 unit_price : 100 . to_d
256234 ) )
257235 end
258236
259237 def test_calculates_sub_amounts_with_over_100_percent_discount
260- product_1_id = SecureRandom . uuid
261- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
262-
263- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
264-
265238 process = InvoiceGeneration . new ( event_store , command_bus )
266239
267240 events = [
268- price_item_added ( product_1_id , 100 , 100 ) ,
241+ price_item_added ( @ product_1_id, 100 , 100 ) ,
269242 percentage_discount_set ( "general_discount" , 60 ) ,
270243 percentage_discount_set ( "time_promotion" , 50 ) ,
271244 order_placed
@@ -278,23 +251,18 @@ def test_calculates_sub_amounts_with_over_100_percent_discount
278251
279252 assert_command ( Invoicing ::AddInvoiceItem . new (
280253 invoice_id : order_id ,
281- product_id : product_1_id ,
254+ product_id : @ product_1_id,
282255 quantity : 1 ,
283- vat_rate : vat_rate ,
256+ vat_rate : @ vat_rate,
284257 unit_price : 0 . to_d
285258 ) )
286259 end
287260
288261 def test_calculates_sub_amounts_with_discount_type_replacement
289- product_1_id = SecureRandom . uuid
290- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
291-
292- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
293-
294262 process = InvoiceGeneration . new ( event_store , command_bus )
295263
296264 events = [
297- price_item_added ( product_1_id , 100 , 100 ) ,
265+ price_item_added ( @ product_1_id, 100 , 100 ) ,
298266 percentage_discount_set ( "general_discount" , 10 ) ,
299267 percentage_discount_set ( "time_promotion" , 20 ) ,
300268 percentage_discount_changed ( "general_discount" , 30 ) ,
@@ -308,23 +276,18 @@ def test_calculates_sub_amounts_with_discount_type_replacement
308276
309277 assert_command ( Invoicing ::AddInvoiceItem . new (
310278 invoice_id : order_id ,
311- product_id : product_1_id ,
279+ product_id : @ product_1_id,
312280 quantity : 1 ,
313- vat_rate : vat_rate ,
281+ vat_rate : @ vat_rate,
314282 unit_price : 50 . to_d
315283 ) )
316284 end
317285
318286 def test_discount_removal_preserves_other_discounts
319- product_1_id = SecureRandom . uuid
320- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
321-
322- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
323-
324287 process = InvoiceGeneration . new ( event_store , command_bus )
325288
326289 events = [
327- price_item_added ( product_1_id , 100 , 100 ) ,
290+ price_item_added ( @ product_1_id, 100 , 100 ) ,
328291 percentage_discount_set ( "general_discount" , 10 ) ,
329292 percentage_discount_set ( "time_promotion" , 20 ) ,
330293 percentage_discount_removed ( "general_discount" ) ,
@@ -338,23 +301,18 @@ def test_discount_removal_preserves_other_discounts
338301
339302 assert_command ( Invoicing ::AddInvoiceItem . new (
340303 invoice_id : order_id ,
341- product_id : product_1_id ,
304+ product_id : @ product_1_id,
342305 quantity : 1 ,
343- vat_rate : vat_rate ,
306+ vat_rate : @ vat_rate,
344307 unit_price : 80 . to_d
345308 ) )
346309 end
347310
348311 def test_discount_set_replaces_existing_discount_type
349- product_1_id = SecureRandom . uuid
350- vat_rate = Infra ::Types ::VatRate . new ( rate : 20 , code : "20" )
351-
352- event_store . publish ( Taxes ::VatRateSet . new ( data : { product_id : product_1_id , vat_rate : vat_rate } ) )
353-
354312 process = InvoiceGeneration . new ( event_store , command_bus )
355313
356314 events = [
357- price_item_added ( product_1_id , 100 , 100 ) ,
315+ price_item_added ( @ product_1_id, 100 , 100 ) ,
358316 percentage_discount_set ( "general_discount" , 10 ) ,
359317 percentage_discount_set ( "general_discount" , 25 ) ,
360318 order_placed
@@ -367,9 +325,9 @@ def test_discount_set_replaces_existing_discount_type
367325
368326 assert_command ( Invoicing ::AddInvoiceItem . new (
369327 invoice_id : order_id ,
370- product_id : product_1_id ,
328+ product_id : @ product_1_id,
371329 quantity : 1 ,
372- vat_rate : vat_rate ,
330+ vat_rate : @ vat_rate,
373331 unit_price : 75 . to_d
374332 ) )
375333 end
0 commit comments