-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo_test.go
124 lines (113 loc) · 3.12 KB
/
demo_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
package service
import (
"context"
"testing"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"github.com/zjn-zjn/fisher/basic"
"github.com/zjn-zjn/fisher/model"
)
const (
ItemTypeGold basic.ItemType = 1
OfficialAccountTypeBank basic.OfficialAccountType = 100000000
OfficialAccountTypeFee basic.OfficialAccountType = 100000000000
TransferSceneBuyGoods basic.TransferScene = 1
ChangeTypeSpend basic.ChangeType = 1
ChangeTypeSellGoodsIncome basic.ChangeType = 2
ChangeTypeSellGoodsCopyright basic.ChangeType = 3
)
func Init(t *testing.T) {
dsn1 := "root:ERcxF3&72#32q@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
dsn2 := "root:ERcxF3&72#32q@tcp(127.0.0.1:3306)/test2?charset=utf8mb4&parseTime=True&loc=Local"
// 连接数据库
db1, err := gorm.Open(mysql.Open(dsn1), &gorm.Config{})
if err != nil {
t.Fatalf("failed to connect database: %v", err)
}
db2, err := gorm.Open(mysql.Open(dsn2), &gorm.Config{})
if err != nil {
t.Fatalf("failed to connect database: %v", err)
}
err = basic.InitWithConf(&basic.TransferConf{
DBs: []*gorm.DB{db1, db2},
StateSplitNum: 3,
RecordSplitNum: 3,
AccountSplitNum: 3,
})
if err != nil {
t.Fatalf("failed to init conf: %v", err)
}
}
func TestTransfer(t *testing.T) {
Init(t)
ctx := context.Background()
accountIdOne, accountIdTwo := int64(100000000001), int64(100000000002)
err := Transfer(ctx, &model.TransferReq{
FromAccounts: []*model.TransferItem{
{
AccountId: int64(OfficialAccountTypeBank),
Amount: 100,
ChangeType: ChangeTypeSpend,
Comment: "transfer deduct",
},
},
TransferId: 1,
ItemType: ItemTypeGold,
UseHalfSuccess: true,
ToAccounts: []*model.TransferItem{
{
AccountId: accountIdOne,
Amount: 90,
ChangeType: ChangeTypeSellGoodsIncome,
Comment: "transfer sell goods income",
},
{
AccountId: accountIdTwo,
Amount: 10,
ChangeType: ChangeTypeSellGoodsCopyright,
Comment: "transfer sell goods copyright",
},
},
TransferScene: TransferSceneBuyGoods,
Comment: "transfer goods",
})
if err != nil {
if basic.Is(err, basic.AlreadyRolledBackErr) {
t.Logf("transfer has been rolled back: %v", err)
}
if basic.Is(err, basic.ParamsErr) {
t.Logf("transfer params error: %v", err)
}
if basic.Is(err, basic.DBFailedErr) {
t.Logf("transfer db failed: %v", err)
}
if basic.Is(err, basic.StateMutationErr) {
t.Logf("transfer state mutation error: %v", err)
}
if basic.Is(err, basic.InsufficientAmountErr) {
t.Logf("transfer insufficient amount: %v", err)
}
t.Logf("failed to item transfer: %v", err)
}
time.Sleep(time.Second)
}
func TestRollback(t *testing.T) {
Init(t)
ctx := context.Background()
err := Rollback(ctx, &model.RollbackReq{
TransferId: 1,
TransferScene: TransferSceneBuyGoods,
})
if err != nil {
t.Fatalf("failed to rollback transfer: %v", err)
}
}
func TestInspection(t *testing.T) {
Init(t)
ctx := context.Background()
errs := Inspection(ctx, time.Now().UnixMilli())
if len(errs) > 0 {
t.Fatalf("failed to item transfer inspection: %v", errs)
}
}