-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtest_luau.rs
1626 lines (1431 loc) · 46.8 KB
/
test_luau.rs
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::workdir::Workdir;
#[test]
fn luau_map() {
let wrk = Workdir::new("luau");
wrk.create(
"data.csv",
vec![
svec!["letter", "number"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map").arg("inc").arg("number + 1").arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "number", "inc"],
svec!["a", "13", "14"],
svec!["b", "24", "25"],
svec!["c", "72", "73"],
svec!["d", "7", "8"],
];
assert_eq!(got, expected);
}
#[test]
fn luau_map_multiple_columns() {
let wrk = Workdir::new("luau");
wrk.create(
"data.csv",
vec![
svec!["letter", "number"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("newcol1,newcol2,newcol3")
.arg("{number + 1, number + 2, number + 3}")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "number", "newcol1", "newcol2", "newcol3"],
svec!["a", "13", "14", "15", "16"],
svec!["b", "24", "25", "26", "27"],
svec!["c", "72", "73", "74", "75"],
svec!["d", "7", "8", "9", "10"],
];
assert_eq!(got, expected);
}
#[test]
fn luau_map_idx() {
let wrk = Workdir::new("luau");
wrk.create(
"data.csv",
vec![
svec!["letter", "number"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("inc")
.arg("number * _IDX")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "number", "inc"],
svec!["a", "13", "13"],
svec!["b", "24", "48"],
svec!["c", "72", "216"],
svec!["d", "7", "28"],
];
assert_eq!(got, expected);
}
#[test]
fn luau_aggregation() {
let wrk = Workdir::new("luau");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Total")
.arg("-x")
.arg("tot = (tot or 0) + Amount; return tot")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Total"],
svec!["a", "13", "13"],
svec!["b", "24", "37"],
svec!["c", "72", "109"],
svec!["d", "7", "116"],
];
assert_eq!(got, expected);
}
#[test]
fn luau_aggregation_with_begin() {
let wrk = Workdir::new("luau");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Total")
.arg("--begin")
.arg("tot = 0")
.arg("-x")
.arg("tot = tot + Amount; return tot")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Total"],
svec!["a", "13", "13"],
svec!["b", "24", "37"],
svec!["c", "72", "109"],
svec!["d", "7", "116"],
];
assert_eq!(got, expected);
}
#[test]
fn luau_aggregation_with_begin_end() {
let wrk = Workdir::new("luau");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Total")
.arg("--begin")
.arg("tot = 0; gtotal = 0; amt_array = {}")
.arg("-x")
.arg("amt_array[_IDX] = Amount; tot = tot + Amount; gtotal = gtotal + tot; return tot")
.arg("--end")
.arg(r#"return ("Min/Max: " .. math.min(unpack(amt_array)) .. "/" .. math.max(unpack(amt_array)) .. " Grand total of " .. _ROWCOUNT .. " rows: " .. gtotal)"#)
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Total"],
svec!["a", "13", "13"],
svec!["b", "24", "37"],
svec!["c", "72", "109"],
svec!["d", "7", "116"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 7/72 Grand total of 4 rows: 275\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_aggregation_with_embedded_begin_end() {
let wrk = Workdir::new("luau_embedded_begin_end");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Total")
.arg("-x")
.arg(
"BEGIN {tot = 0; gtotal = 0; amt_array = {}}! amt_array[_IDX] = Amount; tot = tot + \
Amount; gtotal = gtotal + tot; return tot\nEND {return (\"Min/Max: \" .. \
math.min(unpack(amt_array)) .. \"/\" .. math.max(unpack(amt_array)) .. \" Grand \
total of \" .. _ROWCOUNT .. \" rows: \" .. gtotal)}!",
)
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Total"],
svec!["a", "13", "13"],
svec!["b", "24", "37"],
svec!["c", "72", "109"],
svec!["d", "7", "116"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 7/72 Grand total of 4 rows: 275\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_aggregation_with_embedded_begin_end_using_file() {
let wrk = Workdir::new("luau_embedded_begin_end");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create_from_string(
"testbeginend.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables, setup functions
-- and load additional Lua libraries as required
running_total = 0;
grand_total = 0;
amount_array = {};
adjusted_array = {};
function margin(x: number, y: number ): number
return x * y;
end
function sum(numbers_array: table): number
local sum: number = 0;
for _, v in ipairs(numbers_array) do
sum = sum + v;
end
return sum;
end
}!
-- this is the MAIN script loop, which is executed for each row
-- note how we use the _IDX special variable to get the row index
amount_array[_IDX] = Amount;
running_total = running_total + Amount;
adjusted_array[_IDX] = Amount + margin(Amount, 0.25);
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
END {
-- and this is the END block, which is executed once at the end
grand_total = running_total;
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
adjusted_total = sum(adjusted_array);
-- note how we use the _ROWCOUNT special variable to get the number of rows
-- the value returned from the END script is sent to stderr
return (`Min/Max: {min_amount}/{max_amount} Grand total of {_ROWCOUNT} rows: {grand_total} adjusted: {adjusted_total}`);
}!
"#,
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testbeginend.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Running Total"],
svec!["a", "13", "13"],
svec!["b", "24", "37"],
svec!["c", "72", "109"],
svec!["d", "7", "116"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 7/72 Grand total of 4 rows: 116 adjusted: 145\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_register_lookup_table() {
let wrk = Workdir::new("luau_register_lookup_table");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create(
"us-states-lookup.csv",
vec![
svec![
"Abbreviation",
"Name",
"Capital",
"Population (2019)",
"area (square miles)",
"Sales Tax (2023)"
],
svec!["NJ", "New Jersey", "Trenton", "8,882,190", "8,723", "7"],
svec![
"NM",
"New Mexico",
"Santa Fe",
"2,096,829",
"121,590",
"5.13"
],
svec!["NY", "New York", "Albany", "19,453,561", "54,555", "4"],
],
);
wrk.create_from_string(
"testlookup.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
amount_array = {};
csv_indexed = qsv_autoindex();
us_states_lookup_headers = qsv_register_lookup("us_states", "us-states-lookup.csv")
-- note how we use the qsv_log function to log to the qsv log file
qsv_log("debug", " _INDEX:", _INDEX, " _ROWCOUNT:", _ROWCOUNT, " csv_indexed:", csv_indexed)
qsv_log("debug", "us_states_lookup_headers:", us_states_lookup_headers)
qsv_log("debug", "us_states lookup table:", us_states)
qsv_log("debug", "NY Capital:", us_states["NY"]["Capital"], " can be also: ", us_states.NY.Capital)
-- start from the end of the CSV file, set _INDEX to _LASTROW
_INDEX = _LASTROW;
}!
----------------------------------------------------------------------------
-- this is the MAIN script, which is executed for the row specified by _INDEX
-- As we are doing random access, to exit this loop, we need to set
-- _INDEX to less than zero or greater than _LASTROW
amount_with_nytax = Amount + Amount * (us_states.NY["Sales Tax (2023)"] / 100);
amount_array[_INDEX] = amount_with_nytax;
running_total = running_total + amount_with_nytax;
qsv_log("warn", "logging from Luau script! running_total:", running_total, " _INDEX:", _INDEX)
-- we modify _INDEX to do random access on the CSV file, in this case going backwards
-- the MAIN script ends when _INDEX is less than zero or greater than _LASTROW
_INDEX = _INDEX - 1;
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
----------------------------------------------------------------------------
END {
-- and this is the END block, which is executed once at the end
-- note how we use the _ROWCOUNT special variable to get the number of rows
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
grand_total = running_total;
return ("Min/Max: " .. min_amount .. "/" .. max_amount ..
" Grand total of " .. _ROWCOUNT .. " rows: " .. grand_total);
}!
"#);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testlookup.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Running Total"],
svec!["d", "7", "7.28"],
svec!["c", "72", "82.16"],
svec!["b", "24", "107.12"],
svec!["a", "13", "120.64"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 7.28/74.88 Grand total of 4 rows: 120.64\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_qsv_break() {
let wrk = Workdir::new("luau_qsv_break");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create_from_string(
"testbreak.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
amount_array = {};
}!
-- this is the MAIN script, which is executed for each row
-- note how we use the _IDX special variable to get the row index
if (tonumber(Amount) > 25) then
qsv_break("This is the break msg.");
else
amount_array[_IDX] = Amount;
running_total = running_total + Amount;
grand_total = grand_total + running_total;
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
end
END {
-- and this is the END block, which is executed once at the end
-- note how we use the _ROWCOUNT special variable to get the number of rows
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
return (`Min/Max: {min_amount}/{max_amount} Grand total of {_IDX - 1} rows: {grand_total}`);
}!
"#,
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testbreak.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Running Total"],
svec!["a", "13", "13"],
svec!["b", "24", "37"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end =
"This is the break msg.\nMin/Max: 13/24 Grand total of 2 rows: 50\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_qsv_skip() {
let wrk = Workdir::new("luau_qsv_skip");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create_from_string(
"testbreak.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
amount_array = {};
}!
-- this is the MAIN script, which is executed for each row
-- note how we use the _IDX special variable to get the row index
if (tonumber(Amount) > 25) then
amount_array[_IDX] = 0;
qsv_skip();
else
amount_array[_IDX] = Amount;
running_total = running_total + Amount;
grand_total = grand_total + running_total;
end
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
END {
-- and this is the END block, which is executed once at the end
-- note how we use the _ROWCOUNT special variable to get the number of rows
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
return (`Min/Max: {min_amount}/{max_amount} Grand total of {_IDX - 1} rows: {grand_total}`);
}!
"#,
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testbreak.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Running Total"],
svec!["a", "13", "13"],
svec!["b", "24", "37"],
svec!["d", "7", "44"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 0/24 Grand total of 3 rows: 94\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_insertrecord() {
let wrk = Workdir::new("luau_insertrecord");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create_from_string(
"testbeginend.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
amount_array = {};
}!
-- this is the MAIN script, which is executed for each row
-- note how we use the _IDX special variable to get the row index
amount_array[_IDX] = Amount;
running_total = running_total + Amount;
grand_total = grand_total + running_total;
qsv_insertrecord(`{letter}{_IDX}`, `{Amount}{_IDX}`, `{grand_total}`, `excess column, should not be inserted`)
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
END {
-- and this is the END block, which is executed once at the end
-- note how we use the _ROWCOUNT special variable to get the number of rows
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
-- we insert a record at the end of the table with qsv_insertrecord
-- with the grand total for Running Total, not counting the qsv_insertedrecords
qsv_insertrecord(`Grand Total`, ``, `{grand_total}`);
return (`Min/Max: {min_amount}/{max_amount} Grand total of {_ROWCOUNT} rows: {grand_total}`);
}!
"#,
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testbeginend.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Running Total"],
svec!["a", "13", "13"],
svec!["a1", "131", "13"],
svec!["b", "24", "37"],
svec!["b2", "242", "50"],
svec!["c", "72", "109"],
svec!["c3", "723", "159"],
svec!["d", "7", "116"],
svec!["d4", "74", "275"],
svec!["Grand Total", "", "275"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 7/72 Grand total of 4 rows: 275\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_insertrecord_random_access() {
let wrk = Workdir::new("luau_insertrecord_random_access");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create_from_string(
"testrandominsertrecord.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
amount_array = {};
qsv_autoindex();
_INDEX = _LASTROW;
}!
-- this is the MAIN script, which is executed for each row
-- note how we use the _IDX special variable to get the row index
amount_array[_IDX] = Amount;
running_total = running_total + Amount;
grand_total = grand_total + running_total;
qsv_insertrecord(`{letter}{_IDX}`, `{Amount}{_IDX}`, `{grand_total}`, `excess column, should not be inserted`)
_INDEX = _INDEX - 1;
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
END {
-- and this is the END block, which is executed once at the end
-- note how we use the _ROWCOUNT special variable to get the number of rows
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
-- we insert a record at the end of the table with qsv_insertrecord
-- with the grand total for Running Total, not counting the qsv_insertedrecords
qsv_insertrecord(`Grand Total`, ``, `{grand_total}`);
return (`Min/Max: {min_amount}/{max_amount} Grand total of {_ROWCOUNT} rows: {grand_total}`);
}!
"#,
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testrandominsertrecord.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
// let got2: String = wrk.stdout(&mut cmd);
// println!("got2: {got2:?}");
let expected = vec![
svec!["letter", "Amount", "Running Total"],
svec!["d", "7", "7"],
svec!["d3", "73", "7"],
svec!["c", "72", "79"],
svec!["c2", "722", "86"],
svec!["b", "24", "103"],
svec!["b1", "241", "189"],
svec!["a", "13", "116"],
svec!["a0", "130", "305"],
svec!["Grand Total", "", "305"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 7/72 Grand total of 4 rows: 305\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_test_string_interpolation_feature() {
let wrk = Workdir::new("luau_embedded_begin_end");
wrk.create(
"data.csv",
vec![
svec!["letter", "code digit"],
svec!["a", "2"],
svec!["b", "7"],
svec!["c", "1"],
svec!["d", "5"],
],
);
wrk.create_from_string(
"testbeginend.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
code_array = {};
}!
-- this is the MAIN script, which is executed for each row
-- note how we use the _IDX special variable to get the row index
code_array[_IDX] = col["code digit"];
running_total = running_total + col["code digit"];
grand_total = grand_total + running_total;
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
END {
return(`The lock combination is {table.concat(code_array)}. Again, {table.concat(code_array, ", ")}.`)
}!
"#,
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testbeginend.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "code digit", "Running Total"],
svec!["a", "2", "2"],
svec!["b", "7", "9"],
svec!["c", "1", "10"],
svec!["d", "5", "15"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "The lock combination is 2715. Again, 2, 7, 1, 5.\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_aggregation_with_embedded_begin_end_and_beginend_options() {
// when a main script has BEGIN/END blocks, and --begin/--end options are also specified,
// the --begin/--end options take precedence and the embedded BEGIN/END blocks are ignored.
let wrk = Workdir::new("luau_embedded_begin_end_options");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Total")
.arg("--begin")
.arg("tot = 1; gtotal = 0; amt_array = {}")
.arg("-x")
.arg(
"BEGIN {tot = 0; gtotal = 0; amt_array = {}}! amt_array[_IDX] = Amount; tot = tot + \
Amount; gtotal = gtotal + tot; return tot\nEND {return (\"Min/Max: \" .. \
math.min(unpack(amt_array)) .. \"/\" .. math.max(unpack(amt_array)) .. \" Grand \
total of \" .. _ROWCOUNT .. \" rows: \" .. gtotal)}!",
)
.arg("--end")
.arg(
"return (\"Minimum/Maximum: \" .. math.min(unpack(amt_array)) .. \"/\" .. \
math.max(unpack(amt_array)) .. \" Grand total of \" .. _ROWCOUNT .. \" rows: \" .. \
gtotal)",
)
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Total"],
svec!["a", "13", "14"],
svec!["b", "24", "38"],
svec!["c", "72", "110"],
svec!["d", "7", "117"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Minimum/Maximum: 7/72 Grand total of 4 rows: 279\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_aggregation_with_embedded_begin_end_using_file_random_access() {
let wrk = Workdir::new("luau_embedded_begin_end");
wrk.create_indexed(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create_from_string(
"testbeginend.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
amount_array = {};
-- note how we use the qsv_log function to log to the qsv log file
qsv_log("debug", " _INDEX:", _INDEX, " _ROWCOUNT:", _ROWCOUNT)
-- start from the end of the CSV file, set _INDEX to _LASTROW
_INDEX = _LASTROW;
}!
----------------------------------------------------------------------------
-- this is the MAIN script, which is executed for the row specified by _INDEX
-- As we are doing random access, to exit this loop, we need to set
-- _INDEX to less than zero or greater than _LASTROW
amount_array[_INDEX] = Amount;
running_total = running_total + Amount;
grand_total = grand_total + running_total;
qsv_log("warn", "logging from Luau script! running_total:", running_total, " _INDEX:", _INDEX)
-- we modify _INDEX to do random access on the CSV file, in this case going backwards
-- the MAIN script ends when _INDEX is less than zero or greater than _LASTROW
_INDEX = _INDEX - 1;
-- running_total is the value we "map" to the "Running Total" column of each row
return running_total;
----------------------------------------------------------------------------
END {
-- and this is the END block, which is executed once at the end
-- note how we use the _ROWCOUNT special variable to get the number of rows
min_amount = math.min(unpack(amount_array));
max_amount = math.max(unpack(amount_array));
return ("Min/Max: " .. min_amount .. "/" .. max_amount ..
" Grand total of " .. _ROWCOUNT .. " rows: " .. grand_total);
}!
"#,
);
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("Running Total")
.arg("-x")
.arg("file:testbeginend.luau")
.arg("data.csv");
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["letter", "Amount", "Running Total"],
svec!["d", "7", "7"],
svec!["c", "72", "79"],
svec!["b", "24", "103"],
svec!["a", "13", "116"],
];
assert_eq!(got, expected);
let end = wrk.output_stderr(&mut cmd);
let expected_end = "Min/Max: 7/72 Grand total of 4 rows: 305\n".to_string();
assert_eq!(end, expected_end);
wrk.assert_success(&mut cmd);
}
#[test]
fn luau_aggregation_with_embedded_begin_end_using_file_random_access_with_qsv_autoindex() {
let wrk = Workdir::new("luau_embedded_qsv_index");
wrk.create(
"data.csv",
vec![
svec!["letter", "Amount"],
svec!["a", "13"],
svec!["b", "24"],
svec!["c", "72"],
svec!["d", "7"],
],
);
wrk.create_from_string(
"testbeginend.luau",
r#"
BEGIN {
-- this is the BEGIN block, which is executed once at the beginning
-- where we typically initialize variables
running_total = 0;
grand_total = 0;
amount_array = {};
-- here we call the qsv_autoindex() helper function to create/update an index
-- so we can do random access on the CSV with the _INDEX special variable.
-- qsv_autoindex() should only be called from the BEGIN block
csv_indexed = qsv_autoindex();
-- note how we use the qsv_log function to log to the qsv log file
qsv_log("debug", " _INDEX:", _INDEX, " _ROWCOUNT:", _ROWCOUNT, " csv_indexed:", csv_indexed)
-- start from the end of the CSV file, set _INDEX to _LASTROW
_INDEX = _LASTROW;
}!
----------------------------------------------------------------------------
-- this is the MAIN script loop, which is executed for the row specified by _INDEX
-- As we are doing random access, to exit this loop, we need to set
-- _INDEX to less than zero or greater than _LASTROW
amount_array[_INDEX] = Amount;
running_total = running_total + Amount;
grand_total = grand_total + running_total;
qsv_log("warn", "logging from Luau script! running_total:", running_total, " _INDEX:", _INDEX);