-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_test.go
85 lines (70 loc) · 1.96 KB
/
common_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
package postgres_test
import (
"crypto/rand"
"encoding/json"
"flag"
"testing"
)
// -----------------------------------------------------------------------------
var (
pgUrl string
pgHost string
pgPort uint
pgUsername string
pgPassword string
pgDatabaseName string
)
var (
testJSON TestJSON
testBLOB []byte
testJSONBytes []byte
)
// -----------------------------------------------------------------------------
func init() {
flag.StringVar(&pgUrl, "url", "", "Specifies the Postgres URL.")
flag.StringVar(&pgHost, "host", "127.0.0.1", "Specifies the Postgres server host. (Defaults to '127.0.0.1')")
flag.UintVar(&pgPort, "port", 5432, "Specifies the Postgres server port. (Defaults to 5432)")
flag.StringVar(&pgUsername, "user", "postgres", "Specifies the user name. (Defaults to 'postgres')")
flag.StringVar(&pgPassword, "password", "", "Specifies the user password.")
flag.StringVar(&pgDatabaseName, "db", "", "Specifies the database name.")
testJSON = TestJSON{
Id: 1,
Text: "demo",
}
testBLOB = make([]byte, 1024)
_, _ = rand.Read(testBLOB)
testJSONBytes, _ = json.Marshal(testJSON)
}
// -----------------------------------------------------------------------------
func checkSettings(t *testing.T) {
if len(pgHost) == 0 {
t.Fatalf("Server host not specified")
}
if pgPort > 65535 {
t.Fatalf("Server port not specified or invalid")
}
if len(pgUsername) == 0 {
t.Fatalf("User name to access database server not specified")
}
if len(pgPassword) == 0 {
t.Fatalf("User password to access database server not specified")
}
if len(pgDatabaseName) == 0 {
t.Fatalf("Database name not specified")
}
}
func addressOf[T any](x T) *T {
return &x
}
func jsonReEncode(src string) (string, error) {
var v interface{}
err := json.Unmarshal([]byte(src), &v)
if err == nil {
var reencoded []byte
reencoded, err = json.Marshal(v)
if err == nil {
return string(reencoded), nil
}
}
return "", err
}