-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm_test.go
95 lines (81 loc) · 2.1 KB
/
orm_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
package ormx
import (
"context"
"testing"
"time"
"github.com/cloudfly/ormx/test"
)
func init() {
if err := Init(context.TODO(), test.Provider); err != nil {
panic(err)
}
}
type TestRow struct {
ID int64 `db:"id"`
Producer string `db:"producer,insert"`
Resource string `db:"resource,insert"`
Action string `db:"action,insert"`
Message string `db:"message,insert"`
CreatedTime time.Time `db:"created_time"`
UpdatedTime time.Time `db:"updated_time"`
}
func (tr TestRow) Table() string {
return "test"
}
type TestRowPatch struct {
Action *string `db:"action,insert"`
Message *string `db:"message,insert"`
}
func TestSimple(t *testing.T) {
var (
ctx = context.Background()
err error
row = TestRow{
Producer: "unittest",
Resource: "insert",
Action: "test",
Message: "test message",
}
patch = TestRowPatch{}
)
t.Run("insert", func(t *testing.T) {
row.ID, err = InsertOne(ctx, "", row)
test.NoError(t, err)
t.Log("the new row id is", row.ID)
})
t.Run("select", func(t *testing.T) {
var row2 TestRow
err = GetByID(ctx, &row2, "", row.ID)
test.NoError(t, err)
test.Equal(t, row.ID, row2.ID)
test.Equal(t, row.Producer, row2.Producer)
test.Equal(t, row.Resource, row2.Resource)
test.Equal(t, row.Action, row2.Action)
test.Equal(t, row.Message, row2.Message)
})
t.Run("update", func(t *testing.T) {
newAction := "patch"
patch.Action = &newAction
err = PatchByID(ctx, row.Table(), row.ID, patch)
test.NoError(t, err)
var row2 TestRow
// FromMaster, do not use cache
err = GetByID(FromMaster(ctx), &row2, "", row.ID)
test.NoError(t, err)
t.Log(row2)
test.Equal(t, row.ID, row2.ID)
test.Equal(t, row.Producer, row2.Producer)
test.Equal(t, row.Resource, row2.Resource)
test.Equal(t, newAction, row2.Action)
test.Equal(t, row.Message, row2.Message)
})
t.Run("delete", func(t *testing.T) {
newAction := "patch"
patch.Action = &newAction
err = DeleteByID(ctx, row.Table(), row.ID)
test.NoError(t, err)
exist, err := Exist(ctx, row.Table(), row.ID)
test.NoError(t, err)
test.Equal(t, false, exist)
})
}