-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmssql_z_unit_init_test.go
172 lines (150 loc) · 4.01 KB
/
mssql_z_unit_init_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
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package mssql_test
import (
"context"
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
)
var (
db gdb.DB
dblink gdb.DB
dbErr gdb.DB
ctx context.Context
)
const (
TableSize = 10
TableName = "t_user"
TestSchema1 = "test1"
TestSchema2 = "test2"
TableNamePrefix1 = "gf_"
TestDbUser = "sa"
TestDbPass = "LoremIpsum86" // "theone@123"
CreateTime = "2018-10-24 10:00:00"
)
func init() {
node := gdb.ConfigNode{
Host: "127.0.0.1", // 192.168.5.72 127.0.0.1
Port: "1433",
User: TestDbUser,
Pass: TestDbPass,
Name: "test", // "QPLogDB",
Type: "mssql",
Role: "master",
Charset: "utf8",
Weight: 1,
MaxIdleConnCount: 10,
MaxOpenConnCount: 10,
}
nodeLink := gdb.ConfigNode{
Type: "mssql",
Name: "master",
Link: fmt.Sprintf(
"mssql:%s:%s@tcp(%s:%s)/%s?encrypt=disable",
node.User, node.Pass, node.Host, node.Port, node.Name,
),
}
nodeErr := gdb.ConfigNode{
Type: "mssql",
Link: fmt.Sprintf(
"mssql:%s:%s@tcp(%s:%s)/%s?encrypt=disable",
node.User, "node.Pass", node.Host, node.Port, node.Name),
}
gdb.AddConfigNode(gdb.DefaultGroupName, node)
if r, err := gdb.New(node); err != nil {
gtest.Fatal(err)
} else {
db = r
}
gdb.AddConfigNode("dblink", nodeLink)
if r, err := gdb.New(nodeLink); err != nil {
gtest.Fatal(err)
} else {
dblink = r
}
gdb.AddConfigNode("dbErr", nodeErr)
if r, err := gdb.New(nodeErr); err != nil {
gtest.Fatal(err)
} else {
dbErr = r
}
ctx = context.Background()
}
func createTable(table ...string) (name string) {
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf("user_%d", gtime.Timestamp())
}
dropTable(name)
if _, err := db.Exec(context.Background(), fmt.Sprintf(`
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='%s' and xtype='U')
CREATE TABLE %s (
ID numeric(10,0) NOT NULL,
PASSPORT VARCHAR(45) NULL,
PASSWORD VARCHAR(32) NULL,
NICKNAME VARCHAR(45) NULL,
CREATE_TIME datetime NULL,
CREATED_AT datetimeoffset NULL,
UPDATED_AT datetimeoffset NULL,
PRIMARY KEY (ID))
`, name, name)); err != nil {
gtest.Fatal(err)
}
db.Schema("test")
return
}
func createInitTable(table ...string) (name string) {
name = createTable(table...)
array := garray.New(true)
for i := 1; i <= TableSize; i++ {
array.Append(g.Map{
"id": i,
"passport": fmt.Sprintf(`user_%d`, i),
"password": fmt.Sprintf(`pass_%d`, i),
"nickname": fmt.Sprintf(`name_%d`, i),
"create_time": "2018-10-24 10:00:00",
})
}
result, err := db.Insert(context.Background(), name, array.Slice())
gtest.AssertNil(err)
n, e := result.RowsAffected()
gtest.Assert(e, nil)
gtest.Assert(n, TableSize)
return
}
func dropTable(table string) {
if _, err := db.Exec(context.Background(), fmt.Sprintf(`
IF EXISTS (SELECT * FROM sysobjects WHERE name='%s' and xtype='U')
DROP TABLE %s
`, table, table)); err != nil {
gtest.Fatal(err)
}
}
// createInsertAndGetIdTableForTest test for InsertAndGetId
func createInsertAndGetIdTableForTest() (name string) {
if _, err := db.Exec(context.Background(), `
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='ip_to_id' and xtype='U')
begin
CREATE TABLE [ip_to_id](
[id] [int] IDENTITY(1,1) NOT NULL,
[ip] [varchar](128) NULL,
CONSTRAINT [PK_ip_to_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
end
`); err != nil {
gtest.Fatal(err)
}
db.Schema(db.GetConfig().Name)
return
}