forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_test.go
119 lines (92 loc) · 2.78 KB
/
engine_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
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"xorm.io/xorm/schemas"
)
func TestPingContext(t *testing.T) {
assert.NoError(t, prepareEngine())
ctx, canceled := context.WithTimeout(context.Background(), time.Nanosecond)
defer canceled()
time.Sleep(time.Nanosecond)
err := testEngine.(*Engine).PingContext(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(), "context deadline exceeded")
}
func TestAutoTransaction(t *testing.T) {
assert.NoError(t, prepareEngine())
type TestTx struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
Created time.Time `xorm:"created"`
}
assert.NoError(t, testEngine.Sync2(new(TestTx)))
engine := testEngine.(*Engine)
// will success
engine.Transaction(func(session *Session) (interface{}, error) {
_, err := session.Insert(TestTx{Msg: "hi"})
assert.NoError(t, err)
return nil, nil
})
has, err := engine.Exist(&TestTx{Msg: "hi"})
assert.NoError(t, err)
assert.EqualValues(t, true, has)
// will rollback
_, err = engine.Transaction(func(session *Session) (interface{}, error) {
_, err := session.Insert(TestTx{Msg: "hello"})
assert.NoError(t, err)
return nil, fmt.Errorf("rollback")
})
assert.Error(t, err)
has, err = engine.Exist(&TestTx{Msg: "hello"})
assert.NoError(t, err)
assert.EqualValues(t, false, has)
}
func TestDump(t *testing.T) {
assert.NoError(t, prepareEngine())
type TestDumpStruct struct {
Id int64
Name string
}
assertSync(t, new(TestDumpStruct))
testEngine.Insert([]TestDumpStruct{
{Name: "1"},
{Name: "2\n"},
{Name: "3;"},
{Name: "4\n;\n''"},
{Name: "5'\n"},
})
fp := fmt.Sprintf("%v.sql", testEngine.Dialect().URI().DBType)
os.Remove(fp)
assert.NoError(t, testEngine.DumpAllToFile(fp))
assert.NoError(t, prepareEngine())
sess := testEngine.NewSession()
defer sess.Close()
assert.NoError(t, sess.Begin())
_, err := sess.ImportFile(fp)
assert.NoError(t, err)
assert.NoError(t, sess.Commit())
for _, tp := range []schemas.DBType{schemas.SQLITE, schemas.MYSQL, schemas.POSTGRES, schemas.MSSQL} {
name := fmt.Sprintf("dump_%v.sql", tp)
t.Run(name, func(t *testing.T) {
assert.NoError(t, testEngine.DumpAllToFile(name, tp))
})
}
}
func TestSetSchema(t *testing.T) {
assert.NoError(t, prepareEngine())
if testEngine.Dialect().URI().DBType == schemas.POSTGRES {
oldSchema := testEngine.Dialect().URI().Schema
testEngine.SetSchema("my_schema")
assert.EqualValues(t, "my_schema", testEngine.Dialect().URI().Schema)
testEngine.SetSchema(oldSchema)
assert.EqualValues(t, oldSchema, testEngine.Dialect().URI().Schema)
}
}