Repository navigation
Expand file tree
/
Copy pathpgsql_z_unit_open_test.go
More file actions
179 lines (167 loc) · 3.99 KB
/
Copy pathpgsql_z_unit_open_test.go
File metadata and controls
179 lines (167 loc) · 3.99 KB
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
173
174
175
176
177
178
179
// Copyright GoFrame Author(https://goframe.org). 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 pgsql_test
import (
"testing"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/contrib/drivers/pgsql/v2"
)
// Test_Open tests the Open method with various configurations
func Test_Open_WithNamespace(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "127.0.0.1",
Port: "5432",
Name: "test",
Namespace: "public",
}
db, err := driver.Open(config)
t.AssertNil(err)
t.AssertNE(db, nil)
if db != nil {
db.Close()
}
})
}
// Test_Open_WithTimezone tests Open with timezone configuration
func Test_Open_WithTimezone(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "127.0.0.1",
Port: "5432",
Name: "test",
Timezone: "Asia/Shanghai",
}
db, err := driver.Open(config)
t.AssertNil(err)
t.AssertNE(db, nil)
if db != nil {
db.Close()
}
})
}
// Test_Open_WithExtra tests Open with extra configuration
func Test_Open_WithExtra(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "127.0.0.1",
Port: "5432",
Name: "test",
Extra: "connect_timeout=10",
}
db, err := driver.Open(config)
t.AssertNil(err)
t.AssertNE(db, nil)
if db != nil {
db.Close()
}
})
}
// Test_Open_WithInvalidExtra tests Open with invalid extra configuration
func Test_Open_WithInvalidExtra(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "127.0.0.1",
Port: "5432",
Name: "test",
// Invalid extra format with invalid URL encoding that will cause parse error
Extra: "%Q=%Q&b",
}
_, err := driver.Open(config)
t.AssertNE(err, nil)
})
}
// Test_Open_WithFullConfig tests Open with all configuration options
func Test_Open_WithFullConfig(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "127.0.0.1",
Port: "5432",
Name: "test",
Namespace: "public",
Timezone: "UTC",
Extra: "connect_timeout=10",
}
db, err := driver.Open(config)
t.AssertNil(err)
t.AssertNE(db, nil)
if db != nil {
db.Close()
}
})
}
// Test_Open_WithoutPort tests Open without port
func Test_Open_WithoutPort(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "127.0.0.1",
Name: "test",
}
db, err := driver.Open(config)
t.AssertNil(err)
t.AssertNE(db, nil)
if db != nil {
db.Close()
}
})
}
// Test_Open_WithoutName tests Open without database name
func Test_Open_WithoutName(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "127.0.0.1",
Port: "5432",
}
db, err := driver.Open(config)
t.AssertNil(err)
t.AssertNE(db, nil)
if db != nil {
db.Close()
}
})
}
// Test_Open_InvalidHost tests Open with invalid host
func Test_Open_InvalidHost(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
driver := pgsql.Driver{}
config := &gdb.ConfigNode{
User: "postgres",
Pass: "12345678",
Host: "invalid_host_that_does_not_exist",
Port: "5432",
Name: "test",
}
// Note: sql.Open doesn't actually connect, so no error here
// The error would occur when actually using the connection
db, err := driver.Open(config)
t.AssertNil(err)
if db != nil {
db.Close()
}
})
}