-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider_test.go
91 lines (85 loc) · 1.98 KB
/
provider_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
package persistencemysql
import (
"database/sql"
"reflect"
"testing"
"time"
"github.com/go-sql-driver/mysql"
"github.com/oklog/ulid/v2"
"github.com/ytake/protoactor-go-persistence-mysql/testdata"
)
func mysqlConfig() mysql.Config {
jst, _ := time.LoadLocation("Asia/Tokyo")
return mysql.Config{
DBName: "sample",
User: "user",
Passwd: "passw@rd",
Addr: "localhost:3306",
Net: "tcp",
ParseTime: true,
Collation: "utf8mb4_bin",
Loc: jst,
}
}
func TestProvider_PersistEvent(t *testing.T) {
config := mysqlConfig()
db, _ := sql.Open("mysql", config.FormatDSN())
t.Cleanup(func() {
db.Exec("TRUNCATE journals")
db.Close()
})
provider, _ := New(3, NewTable(), db, nil)
evt := &testdata.UserCreated{
UserID: ulid.Make().String(),
UserName: "test",
Email: "",
}
provider.PersistEvent("user", 1, evt)
var evv *testdata.UserCreated
provider.GetEvents("user", 1, 4, func(e interface{}) {
ev, ok := e.(*testdata.UserCreated)
if !ok {
t.Error("unexpected type")
}
evv = ev
})
if !reflect.DeepEqual(evt, evv) {
t.Errorf("unexpected event %v", evv)
}
var evv2 *testdata.UserCreated
provider.GetEvents("user", 1, 0, func(e interface{}) {
ev, ok := e.(*testdata.UserCreated)
if !ok {
t.Error("unexpected type")
}
evv2 = ev
})
if !reflect.DeepEqual(evt, evv2) {
t.Errorf("unexpected event %v", evv2)
}
}
func TestProvider_PersistSnapshot(t *testing.T) {
config := mysqlConfig()
db, _ := sql.Open("mysql", config.FormatDSN())
t.Cleanup(func() {
db.Exec("TRUNCATE snapshots")
db.Close()
})
provider, _ := New(3, NewTable(), db, nil)
evt := &testdata.UserCreated{
UserID: ulid.Make().String(),
UserName: "test",
Email: "",
}
provider.PersistSnapshot("user", 1, evt)
snapshot, idx, ok := provider.GetSnapshot("user")
if !ok {
t.Error("snapshot not found")
}
if idx != 1 {
t.Errorf("unexpected index %d", idx)
}
if !reflect.DeepEqual(snapshot, evt) {
t.Errorf("unexpected snapshot %v", snapshot)
}
}