-
Notifications
You must be signed in to change notification settings - Fork 16
/
transaction_test.go
111 lines (83 loc) · 2.25 KB
/
transaction_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
package transaction
// Core
// Test
// Copyright © 2017-2018 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
import (
"testing"
)
func TestCreditPrepare(t *testing.T) {
tr := New()
tr.Start()
tn := tr.Begin()
tn = tn.Credit(123, "USD", 5)
if len(tn.reqs) != 1 {
t.Error("When preparing a transaction, the credit operation is lost.")
}
if tn.reqs[0].amount != -5 {
t.Error("In the lending operation, the amount.")
}
if tn.reqs[0].id != 123 {
t.Error("In the lending operation, the ID.")
}
if tn.reqs[0].key != "USD" {
t.Error("In the lending operation, the name of the value.")
}
}
func TestTransactionExe(t *testing.T) {
tr := New()
tr.Start()
tr.AddUnit(123)
tr.hasp = stateClosed
if tr.Begin().Debit(123, "USD", 5).End() != ErrCodeCoreCatch {
t.Error("Resource is locked and can not allow operation")
}
tr.hasp = stateOpen
if tr.Begin().Debit(123, "USD", 5).End() != Ok {
t.Error("Error executing a transaction")
}
tr.storage.getUnit(123).getAccount("USD").counter = -1
if tr.Begin().Debit(123, "USD", 5).End() != ErrCodeTransactionCatch {
t.Error("The transaction could not cath the account")
}
tr.storage.delUnit(123)
if tr.Begin().Debit(123, "USD", 5).End() == Ok {
t.Error("The requested unit does not exist")
}
}
func TestTransactionCatch(t *testing.T) {
tr := New()
tr.Start()
tr.AddUnit(1)
tr.AddUnit(2)
tr.Begin().Debit(1, "USD", 5).End()
tr.Begin().Debit(2, "USD", 5).End()
tn := tr.Begin().Credit(1, "USD", 1)
tn.reqs[0].account = tr.storage.getUnit(1).getAccount("USD")
if tn.catch() != Ok {
t.Error("TestTransactionCatch")
}
tr.storage.getUnit(1).getAccount("USD").counter = -1
tn2 := tr.Begin().Credit(1, "USD", 2)
tn2.reqs[0].account = tr.storage.getUnit(1).getAccount("USD")
if tn2.catch() == Ok {
t.Error("TestTransactionCatch 222")
}
}
func TestTransactionRollback(t *testing.T) {
tr := New()
tr.Start()
tr.AddUnit(123)
tr.Begin().Debit(123, "USD", 7).End()
tn := newTransaction(&tr)
tn.Credit(123, "USD", 1)
tn.fill()
//tn.catch()
tn.exeTransaction()
if num, _ := tr.TotalAccount(123, "USD"); num != 6 {
t.Error("Credit operation is not carried out")
}
tn.rollback(1)
if num, _ := tr.TotalAccount(123, "USD"); num != 7 {
t.Error("Not rolled back")
}
}