This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fsm.test.sql
703 lines (618 loc) · 24.4 KB
/
fsm.test.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
-- Finite state machine tests.
---------------------- ABSTRACT STATE MACHINES -----------------------------------------------
create or replace function lib_test.test_case_fsm_abstract_state_machine_create() returns void as $$
declare
abstract_machine__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_test.assert_not_null(abstract_machine__id, 'abstract machine not created');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_update() returns void as $$
declare
abstract_machine__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_machine_update(abstract_machine__id, 'creation_order2', 'description');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_update_with_invalid_id() returns void as $$
declare
abstract_machine__id uuid;
begin
begin
perform lib_fsm.abstract_machine_update(public.gen_random_uuid(), 'creation_order2', 'description');
exception
when sqlstate '42P01' then
perform lib_test.assert_equal(sqlerrm, 'abstract_machine__id not found');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_delete() returns void as $$
declare
abstract_machine__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_machine_delete(abstract_machine__id);
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_delete_with_invalid_id_should_not_fail() returns void as $$
begin
-- this operation must be idempotent
perform lib_fsm.abstract_machine_delete(public.gen_random_uuid());
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_update_empty_name() returns void as $$
declare
abstract_machine__id uuid;
begin
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_machine_update(abstract_machine__id, null, '');
exception
when not_null_violation then
perform lib_test.assert_equal(sqlerrm, 'null value in column "name" violates not-null constraint');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_update_invalid_name() returns void as
$$
declare
abstract_machine__id uuid;
begin
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_machine_update(abstract_machine__id, 'aa', '');
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'new row for relation "abstract_state_machine" violates check constraint "abstract_state_machine_name_check"');
return;
end; perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_create_empty_name() returns void as $$
begin
begin
perform lib_fsm.abstract_machine_create(null, '');
exception
when not_null_violation then
perform lib_test.assert_equal(sqlerrm, 'null value in column "name" violates not-null constraint');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_machine_create_invalid_name() returns void as
$$
begin
begin
perform lib_fsm.abstract_machine_create('aa', '');
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'new row for relation "abstract_state_machine" violates check constraint "abstract_state_machine_name_check"');
return;
end; perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
---------------------- ABSTRACT STATES -----------------------------------------------
create or replace function lib_test.test_case_fsm_abstract_state_create() returns void as
$$
declare
abstract_state__id uuid;
begin
abstract_state__id = lib_fsm.abstract_state_create(
lib_fsm.abstract_machine_create('creation_order', null),
'drafted',
'command currently being draft',
true);
perform lib_test.assert_not_null(abstract_state__id, 'function call yield a UUID');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_first_abstract_state_must_be_initial() returns void as
$$
declare
abstract_machine__id uuid;
abstract_state__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
begin
-- will raise before it's the first abstract state of the abstract machine and that initial=false (default)
perform lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', 'command currently being draft');
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'a state machine must have one initial state. none found.');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_no_more_than_one_initial_state_per_abstract_machine() returns void as
$$
declare
abstract_machine__id uuid;
abstract_state__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', 'command currently being draft', 'true');
begin
-- will raise before it's the second abstract state with initial=true for the same abstract machine
perform lib_fsm.abstract_state_create(abstract_machine__id, 'signed', 'command currently being draft', 'true');
exception
when unique_violation then
perform lib_test.assert_equal(sqlerrm, 'duplicate key value violates unique constraint "abstract_state_abstract_machine_id_is_initial"');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_create_empty_name() returns void as
$$
declare
begin
begin
perform lib_fsm.abstract_state_create(
lib_fsm.abstract_machine_create('creation_order', null),
null,
'command currently being draft', true);
exception
when not_null_violation then
perform lib_test.assert_equal(sqlerrm, 'domain lib_fsm.abstract_state_identifier does not allow null values');
return;
end; perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_abstract_state_create_invalid_name() returns void as
$$
declare
begin
begin
perform lib_fsm.abstract_state_create(
lib_fsm.abstract_machine_create('creation_order', null),
'aa',
'command currently being draft', true);
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'value for domain lib_fsm.abstract_state_identifier violates check constraint "abstract_state_identifier_check"');
return;
end; perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
---------------------- ABSTRACT TRANSITIONS --------------------------------------------
create or replace function lib_test.test_case_fsm_transition_create() returns void as
$$
declare
abstract_machine__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_transition_create(
lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true),
'submit',
lib_fsm.abstract_state_create(abstract_machine__id, 'submitted'));
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_null_event() returns void as
$$
declare
abstract_machine__id uuid;
begin
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_transition_create(
lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true),
null,
lib_fsm.abstract_state_create(abstract_machine__id, 'submitted'));
exception
when not_null_violation then
perform lib_test.assert_equal(sqlerrm, 'null value in column "event" violates not-null constraint');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_invalid_event_name() returns void as
$$
declare
abstract_machine__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_transition_create(
lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true),
'a',
lib_fsm.abstract_state_create(abstract_machine__id, 'submitted'));
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_invalid_from_state() returns void as
$$
declare
abstract_machine__id uuid;
begin
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_transition_create(
null,
'send',
lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true));
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'both from and to state must have the same machine_id');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_invalid_to_state() returns void as
$$
declare
abstract_machine__id uuid;
begin
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_transition_create(
lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true),
'send',
null);
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'both from and to state must have the same machine_id');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_invalid_to_and_from_state() returns void as
$$
declare
abstract_machine__id uuid;
begin
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
perform lib_fsm.abstract_transition_create(
null,
'send',
null);
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'both from and to state must have the same machine_id');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_when_from_to_state_mismatch() returns void as
$$
declare
abstract_machine__id uuid;
abstract_machine__id2 uuid;
begin
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
abstract_machine__id2 = lib_fsm.abstract_machine_create('resiliation_order', null);
perform lib_fsm.abstract_transition_create(
lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true),
'submit',
lib_fsm.abstract_state_create(abstract_machine__id2, 'submitted'));
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'a state machine must have one initial state. none found.');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_with_same_from_and_to() returns void as
$$
declare
abstract_machine__id uuid;
abstract_state__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
abstract_state__id = lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true);
perform lib_fsm.abstract_transition_create(
abstract_state__id,
'submit',
abstract_state__id);
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_create_with_same_from_and_event() returns void as
$$
declare
abstract_machine__id uuid;
abstract_state__id uuid;
begin
abstract_machine__id = lib_fsm.abstract_machine_create('creation_order', null);
abstract_state__id = lib_fsm.abstract_state_create(abstract_machine__id, 'drafted', '', true);
perform lib_fsm.abstract_transition_create(
abstract_state__id,
'submit',
lib_fsm.abstract_state_create(abstract_machine__id, 'submitted'));
begin
perform lib_fsm.abstract_transition_create(
abstract_state__id,
'submit',
lib_fsm.abstract_state_create(abstract_machine__id, 'sent'));
exception
when unique_violation then
perform lib_test.assert_equal(sqlerrm, 'duplicate key value violates unique constraint "abstract_transition_from_abstract_state__id_event_key"');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
---------------------- STATE MACHINES -----------------------------------------------
create or replace function lib_test.test_case_fsm_state_machine_create_with_abstract_state__id() returns void as
$$
declare
abstract_state__id uuid;
state_machine__id uuid;
begin
abstract_state__id =
lib_fsm.abstract_state_create(lib_fsm.abstract_machine_create('creation_order', null), 'drafted', '', true);
state_machine__id = lib_fsm.state_machine_create(abstract_state__id);
perform lib_test.assert_not_null(state_machine__id, 'state machine not created');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_create_with_abstract_state_machine__id() returns void as
$$
declare
state_machine__id uuid;
state record;
begin
state_machine__id = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
perform lib_test.assert_not_null(state_machine__id, 'state machine not created');
state = lib_fsm.state_machine_get(state_machine__id);
perform lib_test.assert_true(state.name = 'starting');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_create_with_invalid_id() returns void as
$$
begin
begin
perform lib_fsm.state_machine_create(public.gen_random_uuid());
exception
when foreign_key_violation then
perform lib_test.assert_equal(sqlerrm, 'insert or update on table "state_machine" violates foreign key constraint "state_machine_abstract_state__id_fkey"');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_delete() returns void as $$
declare
state_machine__id uuid;
begin
state_machine__id = lib_fsm.state_machine_create(lib_fsm.abstract_state_create(lib_fsm.abstract_machine_create('creation_order', null), 'drafted', '', true));
perform lib_fsm.state_machine_delete(state_machine__id);
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_delete_with_invalid_id_should_not_fail() returns void as $$
begin
-- this operation must be idempotent
perform lib_fsm.state_machine_delete(public.gen_random_uuid());
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_get() returns void as
$$
declare
state lib_fsm.state_machine_state;
state_machine__id$ uuid;
begin
state_machine__id$ = lib_fsm.state_machine_create(
lib_fsm.abstract_state_create(lib_fsm.abstract_machine_create('creation_order', null), 'drafted', null, true)
);
state = lib_fsm.state_machine_get(state_machine__id$);
perform lib_test.assert_true(state.name = 'drafted');
perform lib_test.assert_true(state.description is null);
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_get_unknown_machine() returns void as
$$
declare
state record;
begin
begin
state = lib_fsm.state_machine_get(public.gen_random_uuid());
exception
-- 404
when sqlstate '42P01' then
perform lib_test.assert_equal(sqlerrm, 'state_machine__id not found');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_does_not_belong_to_abstract() returns void as
$$
declare
state record;
begin
begin
state = lib_fsm.state_machine_belongs_to_abstract_machine('603c3f8b-17a9-4cb6-b71e-ff69b4325eb9'::uuid, '081d831f-8f88-4650-aebe-4360599d4bdd'::uuid);
exception
when check_violation then
perform lib_test.assert_equal(sqlerrm, 'state machine is not bound to required abstract machine');
return;
end;
perform lib_test.fail('state machine not belonging to abstract machine family should raise an error');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_does_belong_to_abstract() returns void as
$$
declare
state record;
begin
perform lib_test.assert_equal(lib_fsm.state_machine_belongs_to_abstract_machine('603c3f8b-17a9-4cb6-b71e-ff69b4325eb9'::uuid, '081d831f-8f88-4650-aebe-4360599d4bdc'::uuid), true);
end;
$$ language plpgsql;
----------------------------- STATE MACHINE TRANSITION ------------------------------------
create or replace function lib_test.test_case_fsm_state_machine_transition_check() returns void as
$$
declare
state_machine__id uuid;
state record;
begin
state_machine__id = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
state = lib_fsm.state_machine_transition(state_machine__id, 'create');
perform lib_test.assert_true(state.name = 'awaiting_payment');
state = lib_fsm.state_machine_get(state_machine__id);
perform lib_test.assert_true(state.name = 'awaiting_payment');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_transition_check_dry_run() returns void as
$$
declare
state_machine__id uuid;
state record;
begin
-- use the state machine from fsm/_fixtures
state_machine__id = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
state = lib_fsm.state_machine_transition(state_machine__id, 'create', true);
perform lib_test.assert_true(state.name = 'awaiting_payment');
state = lib_fsm.state_machine_get(state_machine__id);
perform lib_test.assert_true(state.name = 'starting');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_transition_invalid_event() returns void as
$$
declare
state_machine__id uuid;
begin
begin
-- use the state machine from fsm/_fixtures
state_machine__id = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
perform lib_fsm.state_machine_transition(state_machine__id, 'invalid_event');
exception
when sqlstate 'P0001' then
perform lib_test.assert_equal(sqlerrm, 'Invalid event for this machine');
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_get_transitions_for_unknown_state_machine() returns void as
$$
declare
transitions jsonb;
begin
select jsonb_agg(t)
into transitions
from lib_fsm.state_machine_get_next_transitions('081d831f-0000-0000-0000-4360599d4bdc') t;
perform lib_test.assert_null(transitions, 'a non-exisiting state machine should not have any transitions');
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_state_machine_get_next_transitions() returns void as
$$
declare
state_machine__id uuid;
transitions jsonb;
begin
-- create a state machine directly from an abstract_state_machine ("awaiting_payment")
state_machine__id = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4abb'::uuid);
select jsonb_agg(t) into transitions from lib_fsm.state_machine_get_next_transitions(state_machine__id) t;
-- transitions should old
perform lib_test.assert_equal(transitions, '[
{
"abstract_machine__id": "081d831f-8f88-4650-aebe-4360599d4bdc",
"from_abstract_state__id": "081d831f-8f88-4650-aebe-4360599d4abb",
"from_state_name": "awaiting_payment",
"from_state_description": null,
"event": "pay",
"description": null,
"to_abstract_state__id": "081d831f-8f88-4650-aebe-4360599d4acc",
"to_state_name": "awaiting_shipment",
"to_state_description": null
},
{
"abstract_machine__id": "081d831f-8f88-4650-aebe-4360599d4bdc",
"from_abstract_state__id": "081d831f-8f88-4650-aebe-4360599d4abb",
"from_state_name": "awaiting_payment",
"from_state_description": null,
"event": "cancel",
"description": null,
"to_abstract_state__id": "081d831f-8f88-4650-aebe-4360599d4add",
"to_state_name": "canceled",
"to_state_description": null
}
]'::jsonb);
end;
$$ language plpgsql;
----------------------------- STATE MACHINE RECORDED EVENTS ------------------------------------
create or replace function lib_test.test_case_fsm_init_event_should_be_historically_saved() returns void as
$$
declare
state_machine__id$ uuid;
state$ record;
begin
state_machine__id$ = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
select event_name, state_name, state_description into state$
from lib_fsm.state_machine_events sme
where sme.state_machine__id = state_machine__id$;
perform lib_test.assert_null(state$.event_name, format($_$event id for state machine %s should be null, because it's the initial event$_$, state_machine__id$));
perform lib_test.assert_equal(state$.state_name, 'starting', format($_$event id for state machine %s$_$, state_machine__id$));
perform lib_test.assert_null(state$.state_description, $_$state description should be null$_$);
end ;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_transition_event_should_be_historically_saved() returns void as
$$
declare
state_machine__id$ uuid;
state$ jsonb;
begin
state_machine__id$ = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
perform lib_fsm.state_machine_transition(state_machine__id$, 'create');
-- both previous operation will have the SAME created_at, so we cannot order on it
-- so we concat everything and extract the second line from the log
with cte as (select sme.event_name, sme.state_name, sme.state_description
from lib_fsm.state_machine_events sme
where sme.state_machine__id = state_machine__id$)
select jsonb_agg(cte)
into state$
from cte;
perform lib_test.assert_equal(state$->1, '{"event_name": "create", "state_name": "awaiting_payment", "state_description": null}'::jsonb);
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_dry_run_should_not_be_historically_saved() returns void as
$$
declare
state_machine__id$ uuid;
state$ jsonb;
begin
-- use the state machine from fsm/_fixtures
state_machine__id$ = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
perform lib_fsm.state_machine_transition(state_machine__id$, 'create', true);
with cte as (select sme.event_name, sme.state_name, sme.state_description
from lib_fsm.state_machine_events sme
where sme.state_machine__id = state_machine__id$)
select jsonb_agg(cte)
into state$
from cte;
perform lib_test.assert_equal(state$, '[{"event_name": null, "state_name": "starting", "state_description": null}]'::jsonb);
end;
$$ language plpgsql;
create or replace function lib_test.test_case_fsm_invalid_event_should_not_be_historically_saved() returns void as
$$
declare
state_machine__id$ uuid;
state$ jsonb;
begin
-- use the state machine from fsm/_fixtures
state_machine__id$ = lib_fsm.state_machine_create('081d831f-8f88-4650-aebe-4360599d4bdc'::uuid);
begin
perform lib_fsm.state_machine_transition(state_machine__id$, 'toto');
exception
when sqlstate 'P0001' then
with cte as (select sme.event_name, sme.state_name, sme.state_description
from lib_fsm.state_machine_events sme
where sme.state_machine__id = state_machine__id$)
select jsonb_agg(cte)
into state$
from cte;
perform lib_test.assert_equal(state$, '[{"event_name": null, "state_name": "starting", "state_description": null}]'::jsonb);
return;
end;
perform lib_test.fail('should not go there');
end;
$$ language plpgsql;