-
Notifications
You must be signed in to change notification settings - Fork 0
/
expenses_test.go
898 lines (740 loc) · 21.1 KB
/
expenses_test.go
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
// Copyright (c) 2021 Alexandru Catrina
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.s
package expenses
import (
"bytes"
"os"
"testing"
"time"
"github.com/google/uuid"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var (
// MariaDB and MySQL
MySQL_USER = os.Getenv("MYSQL_DB_USER")
MySQL_PASS = os.Getenv("MYSQL_DB_PASS")
MySQL_ADDR = os.Getenv("MYSQL_DB_ADDR")
MySQL_NAME = os.Getenv("MYSQL_DB_NAME")
// PostgreSQL
PostgreSQL_USER = os.Getenv("PSQL_DB_USER")
PostgreSQL_PASS = os.Getenv("PSQL_DB_PASS")
PostgreSQL_HOST = os.Getenv("PSQL_DB_HOST")
PostgreSQL_PORT = os.Getenv("PSQL_DB_PORT")
PostgreSQL_NAME = os.Getenv("PSQL_DB_NAME")
)
var (
_Postgres = postgres.Open("host=" + PostgreSQL_HOST + " user=" + PostgreSQL_USER + " password=" + PostgreSQL_PASS + " dbname=" + PostgreSQL_NAME + " port=" + PostgreSQL_PORT + " sslmode=disable")
_MySQL = mysql.Open(MySQL_USER + ":" + MySQL_PASS + "@tcp(" + MySQL_ADDR + ")/" + MySQL_NAME + "?charset=utf8mb4&parseTime=True")
_SQLite = sqlite.Open("file::memory:?cache=shared")
)
func begin(layer gorm.Dialector) (db *gorm.DB) {
var err error
if db, err = gorm.Open(layer, &gorm.Config{}); err != nil {
panic(err)
}
Install(db)
return db
}
func close(db *gorm.DB) {
Uninstall(db)
// TODO(lexndru): how to close DB?
}
func testActorsAPI(t *testing.T, db *gorm.DB) {
a := NewActor("Alexandru")
newActors := Actors{a}
if err := newActors.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
var fewActors Actors
if err := fewActors.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(fewActors) != 1 || fewActors[0].Name != newActors[0].Name {
t.Fatal("Pulled actor doesn't match with pushed actor")
}
moreActors := Actors{
Actor{
Name: "XYZ",
Flags: 2,
Headers: "image=/path/to/img",
},
Actor{
Name: "Hypermarket",
Headers: "image=/path/to/img",
},
}
fewActors[0].Flags = 29 // push must upsert
fewActors = append(fewActors, moreActors...)
if err := fewActors.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
if err := fewActors.Pull(PullContext{Storage: db, Limit: 5}); err != nil {
t.Fatal(err)
}
if len(fewActors) != 3 {
t.Fatalf("Pushed 3 actors and got back %d instead\n", len(fewActors))
}
}
func testLabelsAPI(t *testing.T, db *gorm.DB) {
newLabels := Labels{
Label{
Name: "Tabletă",
},
}
if err := newLabels.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
var fewLabels Labels
if err := fewLabels.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(fewLabels) != 1 || fewLabels[0].Name != newLabels[0].Name {
t.Fatal("Pulled label doesn't match with pushed label")
}
moreLabels := Labels{
Label{
Name: "Alimente",
},
Label{
Name: "Pâine",
Parent: &Label{Name: "Alimente"},
},
}
fewLabels[0].Parent = &Label{
Name: "Electronice",
Parent: &Label{Name: "Bunuri"},
}
fewLabels = append(fewLabels, moreLabels...)
if err := fewLabels.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
if err := fewLabels.Pull(PullContext{Storage: db, Limit: 10}); err != nil {
t.Fatal(err)
}
if len(fewLabels) != 5 {
t.Fatalf("Pushed 5 labels and got back %d instead\n", len(fewLabels))
}
}
func testTransactionsAPI(t *testing.T, db *gorm.DB) {
date, _ := time.Parse("2006-01-02", "2021-04-22")
trx := NewTransaction(date, -3000, NewLabel("Alimente", nil), NewActor("Alexandru"), NewActor("Magazin"), nil, "signature/0")
newTransactions := Transactions{trx}
if err := newTransactions.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
var fewTransactions Transactions
if err := fewTransactions.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(fewTransactions) != 1 || fewTransactions[0].UUID == nil || *fewTransactions[0].UUID == "" {
t.Fatal("Pulled transaction doesn't match with pushed transaction")
}
if fewTransactions[0].Signature == "" {
t.Fatal("Expected signature on first transaction but got nothing")
}
recentTransactionWithUUID := fewTransactions[0] // this should not get duplicated, but updated
recentTransactionWithUUID.LabelName = "?"
recentTransactionWithUUID.Flags = 29
recentTransactionWithUUID.Details = []*Details{
{
LabelName: "Pâine",
Amount: 1000,
},
{
LabelName: "Prăjitură",
Amount: 2000,
},
}
moreTransactions := Transactions{
Transaction{
Date: date.AddDate(0, 0, 1),
Amount: 5000, // 50.00
LabelName: "Transfer",
SenderName: "?",
ReceiverName: "Alexandru",
},
Transaction{
Date: date.AddDate(0, 1, 0),
Amount: -15000, // -150.00
LabelName: "?",
SenderName: "Alexandru Catrina",
ReceiverName: "Magazin",
Details: []*Details{
{
LabelName: "Alimente",
Amount: 5000, // 50.00
},
{
LabelName: "Hrană pentru animale",
Amount: 8000, // 80.00
},
{
LabelName: "Apă",
Amount: 2000, // 20.00
},
},
},
}
moreTransactions = append(moreTransactions, recentTransactionWithUUID) // 2 inserts and 1 update
if err := moreTransactions.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
if err := moreTransactions.Pull(PullContext{Storage: db, Limit: 5}); err != nil {
t.Fatal(err)
}
if len(moreTransactions) != 3 {
t.Fatalf("Pushed 3 transactions and got back %d instead\n", len(fewTransactions))
}
numDetails := 0
for _, trx := range moreTransactions {
for _, ls := range trx.Details {
if ls.Amount > 0 {
numDetails++
}
}
if trx.UUID == recentTransactionWithUUID.UUID {
if trx.Signature == "" {
t.Fatalf("Expected signature to be kept after upsert but instead got blank")
}
}
}
if numDetails != 5 { // counted all by myself... must be correct :)
t.Fatalf("Expected 5 details on 2 transactions but instead got %d", numDetails)
}
}
func testTransactionJustAppendFlag(t *testing.T, db *gorm.DB) {
date, _ := time.Parse("2006-01-02", "2021-04-27")
generatedId := uuid.New().String()
trxs := Transactions{
Transaction{
UUID: &generatedId,
Date: date,
Amount: 1,
LabelName: "?",
SenderName: "?",
ReceiverName: "?",
},
}
if err := trxs.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
var transactionsCopy Transactions
if err := transactionsCopy.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(transactionsCopy) != 1 {
t.Fail()
}
if *transactionsCopy[0].UUID != generatedId {
t.Fatal("Transaction UUID does not match with generated one")
}
var theSameTrx Transaction
theSameTrx = Transaction{
UUID: &generatedId,
Date: date,
Amount: 1,
LabelName: "new label for this transaction",
SenderName: "?",
ReceiverName: "?",
}
if err := (&Transactions{theSameTrx}).Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
var transactionsCopy2 Transactions
if err := transactionsCopy2.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(transactionsCopy2) != 1 {
t.Fail()
}
if transactionsCopy2[0].LabelName != "new label for this transaction" {
t.Fatal("Expected transaction to have new label")
}
theSameTrx = Transaction{
UUID: &generatedId,
Date: date,
Amount: 1,
LabelName: "?",
SenderName: "changed to another sender",
ReceiverName: "?",
}
if err := (&Transactions{theSameTrx}).Push(PushContext{Storage: db, BatchSize: 10, JustAppend: true}); err != nil {
t.Fatal(err)
}
var transactionsCopy3 Transactions
if err := transactionsCopy3.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(transactionsCopy3) != 1 {
t.Fail()
}
if transactionsCopy2[0].SenderName != "?" {
t.Fatal("Expected transaction to have the original sender name")
}
}
func testIncorrectAmountForTransaction(t *testing.T, db *gorm.DB) {
date, _ := time.Parse("2006-01-02", "2021-04-23")
lb := NewLabel("?", nil)
ls := make(map[Label]int64, 1)
ls[lb] = 50
trx := NewTransaction(date, -100, NewLabel("?", nil), NewActor("?"), NewActor("?"), ls, "signature/1")
incorrectTransactions := Transactions{trx}
if err := incorrectTransactions.Push(PushContext{Storage: db, BatchSize: 10}); err == nil {
t.Fatal("Expected to fail since amount doesn't add up, but instead got not error")
}
var transactionsCopy Transactions
if err := transactionsCopy.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(transactionsCopy) != 0 {
t.Fatalf("Expected 0 transactions after push because of incorrect sum but got %d instead\n", len(transactionsCopy))
}
incorrectTransactions[0].Details[0].Amount = 150
incorrectTransactions[0].Details = append(incorrectTransactions[0].Details, &Details{
Amount: -50,
LabelName: "??",
})
if err := incorrectTransactions.Push(PushContext{Storage: db, BatchSize: 10}); err == nil {
t.Fatal("Should have failed because of negative details amount")
}
incorrectTransactions[0].Details = []*Details{{Amount: 100, LabelName: "???"}}
if err := incorrectTransactions.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
if err := incorrectTransactions.Pull(PullContext{Storage: db, Limit: 2}); err != nil {
t.Fatal(err)
}
if len(incorrectTransactions) != 1 {
t.Fatal("Expected 1 transaction to be updated with correct values but instead it got duplicated")
}
}
func testPrimaryRequestsWithTransactions(t *testing.T, db *gorm.DB) {
date, _ := time.Parse("2006-01-02", "2021-04-25")
trxs := Transactions{
Transaction{
Date: date,
Amount: -3000, // 30.00
Label: &Label{
Name: "?",
},
Sender: &Actor{Name: "Alexandru Catrina"},
Receiver: &Actor{Name: "Piață"},
Details: []*Details{
{
Label: &Label{Name: "Apă"},
Amount: 1000, // 10.00
},
{
Label: &Label{
Name: "Hrană pentru animale",
Parent: &Label{Name: "Petshop"},
},
Amount: 2000, // 20.00
},
},
},
Transaction{
Date: date,
Amount: 5000, // 50.00
Label: &Label{
Name: "Încasare",
Parent: &Label{
Name: "Transfer intern",
Parent: &Label{Name: "Intrări"},
},
},
Sender: &Actor{Name: "Alexandru Catrina"},
Receiver: &Actor{Name: "Catrina Alexandru"},
},
}
if _, err := NewPushRequest(&trxs, PushContext{Storage: db, BatchSize: 1}); err != nil {
t.Fatal(err)
} else {
var trxProbe Transactions
if _, err := NewPullRequest(&trxProbe, PullContext{Storage: db, Limit: 10}); err != nil {
t.Fatal(err)
} else {
if len(trxs) != len(trxProbe) {
t.Fatal("Expected same number of transactions")
}
for _, a := range trxs {
var foundIt bool
for _, b := range trxProbe {
if a.Amount == b.Amount && a.SenderName == b.SenderName && a.ReceiverName == b.ReceiverName {
foundIt = true
}
}
if !foundIt {
t.Fatal("Expected pull transaction to be found in pushed transaction")
}
}
}
}
}
func testPrimaryRequestsWithLabelsTree(t *testing.T, db *gorm.DB) {
expectedJson := `[{"name":"9","parent":"8","flags":0,"headers":""},{"name":"10","parent":null,"flags":0,"headers":""}]`
labels := Labels{
Label{
Name: "9",
Parent: &Label{
Name: "8",
Parent: &Label{
Name: "7",
Parent: &Label{
Name: "6",
Parent: &Label{
Name: "5",
Parent: &Label{
Name: "4",
Parent: &Label{
Name: "3",
Parent: &Label{
Name: "2",
Parent: &Label{
Name: "1",
Parent: &Label{Name: "0"},
},
},
},
},
},
},
},
},
},
Label{Name: "10"},
}
if out, err := NewPushRequest(&labels, PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
} else {
if expectedJson != string(out) {
t.Fatal("Expected output doesn't match with provided output")
}
}
}
func testLabelParentUpsert(t *testing.T, db *gorm.DB) {
parent3 := NewLabel("Label #3", nil)
parent1 := NewLabel("Label #1", nil)
lb5 := NewLabel("Label #5", &parent3)
lb2 := NewLabel("Label #2", &parent1)
lbs := Labels{lb5, lb2}
if err := lbs.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
var lbCopy Labels
if err := lbCopy.Pull(PullContext{Storage: db}); err != nil {
t.Fatal(err)
}
if len(lbCopy) != 4 {
t.Fatalf("Expected 4 labels but got %v instead\n", len(lbCopy))
}
for _, lb := range lbCopy {
if lb.Name == "Label #5" && lb.Parent.Name != "Label #3" {
t.Fatalf("Expected Label #5 to have parent Label #3 but instead has %v", lb.Parent)
}
if lb.Name == "Label #2" && lb.Parent.Name != "Label #1" {
t.Fatalf("Expected Label #2 to have parent Label #1 but instead has %v", lb.Parent)
}
}
dupLbs := Labels{
Label{
Name: "Label #1",
Parent: &lb5,
},
}
if err := dupLbs.Push(PushContext{Storage: db, BatchSize: 10}); err != nil {
t.Fatal(err)
}
var lbCopy2 Labels
if err := lbCopy2.Pull(PullContext{Storage: db}); err != nil {
t.Fatal(err)
}
if len(lbCopy2) != 4 {
t.Fatalf("Expected to have still 4 labels but got %v instead\n", len(lbCopy2))
}
for _, lb := range lbCopy2 {
if lb.Name == "Label #1" && (lb.Parent == nil || lb.Parent.Name != "Label #5") {
t.Fatalf("Expected Label #1 to have parent Label #5 but instead has %v", lb.Parent)
}
if lb.Name == "Label #5" && (lb.Parent == nil || lb.Parent.Name != "Label #3") {
t.Fatalf("Expected Label #5 to have parent Label #3 but instead has %v", lb.Parent)
}
if lb.Name == "Label #2" && (lb.Parent == nil || lb.Parent.Name != "Label #1") {
t.Fatalf("Expected Label #2 to have parent Label #1 but instead has %v", lb.Parent)
}
}
}
func testEmptyNameChecks(t *testing.T, db *gorm.DB) {
b := NewActor("")
if err := (&Actors{b}).Push(PushContext{Storage: db, BatchSize: 1}); err == nil {
t.Fatal("Expected push to fail because actor has empty name")
}
l := NewLabel("", nil)
if err := (&Labels{l}).Push(PushContext{Storage: db, BatchSize: 1}); err == nil {
t.Fatal("Expected push to fail because label has empty name")
}
}
func TestActorsAPI_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
testActorsAPI(t, db)
}
func TestLabelsAPI_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
testLabelsAPI(t, db)
}
func TestTransactionsAPI_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
testTransactionsAPI(t, db)
}
func TestIncorrectAmountForTransaction_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
// silent intentionally errors
testIncorrectAmountForTransaction(t, db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)}))
}
func TestPrimaryRequestsWithTransactions_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
testPrimaryRequestsWithTransactions(t, db)
}
func TestPrimaryRequestsWithLabelsTree_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
testPrimaryRequestsWithLabelsTree(t, db)
}
func TestTransactionJustAppendFlag_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
testTransactionJustAppendFlag(t, db)
}
func TestLabelParentUpsert_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
testLabelParentUpsert(t, db)
}
func TestEmptyNameChecks_Postgres(t *testing.T) {
db := begin(_Postgres)
defer close(db)
// silent intentionally errors
testEmptyNameChecks(t, db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)}))
}
func TestActorsAPI_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
testActorsAPI(t, db)
}
func TestLabelsAPI_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
testLabelsAPI(t, db)
}
func TestTransactionsAPI_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
testTransactionsAPI(t, db)
}
func TestIncorrectAmountForTransaction_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
// silent intentionally errors
testIncorrectAmountForTransaction(t, db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)}))
}
func TestPrimaryRequestsWithTransactions_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
testPrimaryRequestsWithTransactions(t, db)
}
func TestPrimaryRequestsWithLabelsTree_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
testPrimaryRequestsWithLabelsTree(t, db)
}
func TestTransactionJustAppendFlag_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
testTransactionJustAppendFlag(t, db)
}
func TestLabelParentUpsert_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
testLabelParentUpsert(t, db)
}
func TestEmptyNameChecks_MySQL(t *testing.T) {
db := begin(_MySQL)
defer close(db)
// silent intentionally errors
testEmptyNameChecks(t, db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)}))
}
func TestActorsAPI_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
testActorsAPI(t, db)
}
func TestLabelsAPI_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
testLabelsAPI(t, db)
}
func TestTransactionsAPI_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
testTransactionsAPI(t, db)
}
func TestTransactionJustAppendFlag_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
testTransactionJustAppendFlag(t, db)
}
func TestIncorrectAmountForTransaction_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
// silent intentionally errors
testIncorrectAmountForTransaction(t, db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)}))
}
func TestPrimaryRequestsWithTransactions_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
testPrimaryRequestsWithTransactions(t, db)
}
func TestPrimaryRequestsWithLabelsTree_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
testPrimaryRequestsWithLabelsTree(t, db)
}
func TestLabelParentUpsert_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
testLabelParentUpsert(t, db)
}
func TestEmptyNameChecks_SQLite(t *testing.T) {
db := begin(_SQLite)
defer close(db)
// silent intentionally errors
testEmptyNameChecks(t, db.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)}))
}
func TestIncorrectTransactions_Json(t *testing.T) {
input := `{
"amount": "100",
"date": 31231435,
"sender": "?!!",
"receiver": "??!",
"label" "???"
}`
var trx Transactions
if err := FromJson([]byte(input), &trx); err == nil {
t.Fatal("Expected failure from invalid JSON but instead it worked")
}
if len(trx) != 0 {
t.Fatal("Expected no transactions because of inccorect JSON input")
}
}
func TestEncodeDecodeTransactions_Json(t *testing.T) {
date, _ := time.Parse("2006-01-02", "2021-04-24")
oneTransaction := Transactions{
Transaction{
Date: date,
Amount: -100, // 1.00
LabelName: "?",
SenderName: "?",
ReceiverName: "?",
Signature: "?",
Details: []*Details{
{
LabelName: "?",
Amount: 50,
},
{
LabelName: "?",
Amount: 30,
},
{
LabelName: "?",
Amount: 20,
},
},
},
}
expectedJson := `[
{
"date": "2021-04-24T00:00:00Z",
"amount": -100,
"label": "?",
"sender": "?",
"receiver": "?",
"signature": "?",
"flags": 0,
"headers": "",
"details": [
{
"label": "?",
"amount": 50,
"flags": 0,
"headers": ""
},
{
"label": "?",
"amount": 30,
"flags": 0,
"headers": ""
},
{
"label": "?",
"amount": 20,
"flags": 0,
"headers": ""
}
]
}
]`
if out, err := ToJson(oneTransaction); err != nil {
t.Fatal(err)
} else {
var tmp Transactions
if err := FromJson([]byte(expectedJson), &tmp); err != nil {
t.Fatal(err)
}
if out2, err := ToJson(tmp); err != nil {
t.Fatal(err)
} else {
if !bytes.Equal(out, out2) {
t.Fatal("Expected JSON results to be the same")
}
}
}
}
func TestEncodeDecodeLabels_Json(t *testing.T) {
input := `[
{
"name": "test with null parent",
"parent": null
}
]`
var lb Labels
if err := FromJson([]byte(input), &lb); err != nil {
t.Fatal(err)
}
}