-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcore_test.go
331 lines (244 loc) · 6.13 KB
/
core_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
package transaction
// Core
// Test
// Copyright © 2017-2018 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
import (
"bytes"
"io/ioutil"
"os"
"strconv"
"testing"
)
func TestUsage(t *testing.T) {
path := "./test.tdb"
tr := New()
if !tr.Start() {
t.Error("Now the start is possible!")
}
tr.AddUnit(123)
tr.Begin().Debit(123, "USD", 7777).End()
tr.Save(path)
if _, err := tr.DelUnit(123); err == Ok {
t.Error(err)
}
if err := tr.Begin().Debit(123, "USD", 7777).End(); err != Ok {
t.Error(err)
}
tr.Stop()
os.Remove(path)
}
func TestTransfer(t *testing.T) {
tr := New()
tr.Start()
tr.AddUnit(14760464)
tr.AddUnit(2674560)
if err := tr.Begin().Debit(14760464, "USD", 11).End(); err != Ok {
t.Error(err)
}
if err := tr.Begin().Debit(2674560, "USD", 7).End(); err != Ok {
t.Error(err)
}
if err := tr.Begin().Credit(2674560, "USD", 2).End(); err != Ok {
t.Error(err)
}
if err := tr.Begin().
Credit(2674560, "USD", 4).
Debit(14760464, "USD", 4).
End(); err != Ok {
t.Error(err)
}
}
func TestCoreStart(t *testing.T) {
tr := New()
if !tr.Start() {
t.Error("Now the start is possible!")
}
tr.Stop()
trialLimit = trialStop
if tr.Start() {
t.Error("Now the start is possible!")
}
trialLimit = trialLimitConst
}
func TestCoreStop(t *testing.T) {
trialLimit = 200
tr := New()
if !tr.Start() {
t.Error("Now the start is possible!")
}
if ok, _ := tr.Stop(); !ok {
t.Error("Now the stop is possible!")
}
tr.Start()
trialLimit = trialStop
//tr.hasp = stateClosed
if ok, _ := tr.Stop(); ok {
t.Error("Due to the limitation of the number of iterations, stopping is impossible")
}
trialLimit = trialLimitConst
}
func TestCoreGetAccount(t *testing.T) {
tr := New()
if _, err := tr.getAccount(123, "USD"); err == Ok {
t.Error("We must get an error!")
}
tr.AddUnit(123)
if _, err := tr.getAccount(123, "USD"); err != Ok {
t.Error("We should not get an error!")
}
}
func TestCoreAddUnit(t *testing.T) {
tr := New()
tr.Start()
if tr.AddUnit(123) != Ok {
t.Error("Unable to add unit")
}
if tr.AddUnit(123) == Ok {
t.Error("You can not re-add a unit")
}
tr.hasp = stateClosed
if tr.AddUnit(456) == Ok {
t.Error("Due to the blocking, it was not possible to add a new unit.")
}
}
func TestCoreDelUnit(t *testing.T) {
tr := New()
tr.Start()
if _, err := tr.DelUnit(123); err == Ok {
t.Error("Removed non-existent unit")
}
tr.AddUnit(123)
if _, err := tr.DelUnit(123); err != Ok {
t.Error("The unit has not been deleted")
}
tr.AddUnit(456)
tr.Begin().Debit(456, "USD", 5).End()
tr.storage.getUnit(456).getAccount("USD").counter = 1
trialLimit = trialStop + 100
if _, err := tr.DelUnit(456); err == Ok {
t.Error("The unit has not been deleted")
}
tr.hasp = stateClosed
if _, err := tr.DelUnit(456); err == Ok {
t.Error("Due to the blocking, it was not possible to del a unit.")
}
trialLimit = trialLimitConst
}
func TestCoreTotalUnit(t *testing.T) {
tr := New()
tr.Start()
tr.AddUnit(123)
tr.Begin().Debit(123, "USD", 1).End()
arr, err := tr.TotalUnit(123)
if err != Ok {
t.Error("Failed to get information on the unit")
}
if b, ok := arr["USD"]; ok != true || b != 1 {
t.Error("The received information on the unit is erroneous")
}
if _, err := tr.TotalUnit(456); err == Ok {
t.Error("A unit does not exist, there must be an error")
}
tr.hasp = stateClosed
if _, err := tr.TotalUnit(123); err != ErrCodeCoreCatch {
t.Error("Resource is locked and can not allow operation")
}
tr.hasp = stateOpen
}
func TestCoreTotalAccount(t *testing.T) {
tr := New()
tr.Start()
tr.AddUnit(123)
tr.Begin().Debit(123, "USD", 1).End()
balance, err := tr.TotalAccount(123, "USD")
if err != Ok {
t.Error("Failed to get information on the account")
}
if balance != 1 {
t.Error("The received information on the account is erroneous")
}
if _, err := tr.TotalAccount(456, "USD"); err == Ok {
t.Error("A account does not exist, there must be an error")
}
if balance, err := tr.TotalAccount(123, "EUR"); err != Ok || balance != 0 {
t.Error("A account does not exist, there must be an error")
}
tr.hasp = stateClosed
if _, err := tr.TotalAccount(123, "USD"); err != ErrCodeCoreCatch {
t.Error("Resource is locked and can not allow operation")
}
tr.hasp = stateOpen
}
func TestCoreSave(t *testing.T) {
path := "./test.tdb"
tr := New()
tr.Start()
tr.AddUnit(123)
tr.Begin().Debit(123, "USD", 7).End()
trialLimit = trialStop
tr.hasp = stateClosed
if tr.Save(path) == Ok {
t.Error("The lock should prevent the file from being saved")
}
trialLimit = trialLimitConst
tr.hasp = stateOpen
if tr.Save(path) != Ok {
t.Error("There is no lock, saving should be successful")
}
endLine := []byte(endLineSymbol)
separator := []byte(separatorSymbol)
bs, err := ioutil.ReadFile(path)
if err != nil {
t.Error("Can not find saved file")
}
str := bytes.Split(bs, endLine)[0]
a := bytes.Split(str, separator)
if len(a) != 3 {
t.Error("Invalid number of columns")
}
id, err := strconv.ParseInt(string(a[0]), 10, 64)
if err != nil {
t.Error("Error converting string to integer (id account)")
}
balance, err := strconv.ParseInt(string(a[1]), 10, 64)
if err != nil {
t.Error("Error converting string to integer (balance account)")
}
if id != 123 {
t.Error("The account identifier does not match")
}
if balance != 7 {
t.Error("The account balance does not match")
}
os.Remove(path)
tr.Stop()
}
func TestCoreLoad(t *testing.T) {
path := "./test.tdb"
pathFake := "./testFake.tdb"
tr := New()
tr.Start()
tr.AddUnit(123)
tr.Begin().Debit(123, "USD", 7).End()
tr.Save(path)
tr.Stop()
tr2 := New()
if res, _ := tr2.Load(pathFake); res == Ok {
t.Errorf("The file `%s` does not exist", pathFake)
}
if res, _ := tr2.Load(path); res != Ok {
t.Errorf("The file `%s` does exist", pathFake)
}
tr2.Start()
if res, _ := tr2.Load(path); res != Ok {
t.Errorf("Error loading the database file (%d)", res)
}
balance, res := tr2.TotalAccount(123, "USD")
if balance != 7 {
t.Errorf("Error in account balance (%d)", balance)
}
if res != Ok {
t.Errorf("Error in the downloaded account (%d)", res)
}
os.Remove(path)
}