-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset_test.go
130 lines (112 loc) · 3.48 KB
/
reset_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
package mysqlx
import (
"database/sql"
"errors"
"testing"
"github.com/renthraysk/mysqlx/authentication/mysql41"
"github.com/renthraysk/mysqlx/errs"
)
func DB(t *testing.T, opt Option, q string) (string, error) {
connector, err := New("tcp", "127.0.0.1:33060",
WithAuthentication(mysql41.New()),
WithUserPassword("usernative", "passwordnative"),
opt,
)
if err != nil {
return "", err
}
db := sql.OpenDB(connector)
defer db.Close()
if err := db.Ping(); err != nil {
return "", err
}
var v string
err = db.QueryRow(q).Scan(&v)
return v, err
}
func DBSQLMode(t *testing.T, mode string) (string, error) {
return DB(t, WithSQLMode(mode), "SELECT @@sql_mode")
}
func TestInitSQLMode(t *testing.T) {
// Test setting @@sql_mode twice with two difference values, incase the mysql server default is one or the other.
{
const expected = "REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI"
got, err := DBSQLMode(t, "ANSI")
if err != nil {
t.Fatalf("failed to set @@sql_mode: %s", err)
}
if got != expected {
t.Fatalf("failed to set @@sql_mode to ANSI, expected %s got %s", expected, got)
}
}
{
const expected = "STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_ENGINE_SUBSTITUTION"
got, err := DBSQLMode(t, "TRADITIONAL")
if err != nil {
t.Fatalf("failed to set @@sql_mode: %s", err)
}
if got != expected {
t.Fatalf("failed to set @@sql_mode to ANSI, expected %s got %s", expected, got)
}
}
}
func TestInitSQLModeError(t *testing.T) {
_, err := DBSQLMode(t, "NONSENSE")
if err == nil {
t.Fatalf("failed to trigger error setting @@sql_mode to an invalid value")
}
var ers errs.Errors
if !errors.As(err, &ers) {
t.Fatalf("expected mysqlx.Errors, got %T", err)
}
// Error happens on the third statement... first is an expect open, 2nd is session-reset(keepOpen=true) or just ping (pre mysql 8.0.16)
if e, ok := (ers)[2].(*errs.Error); !ok ||
e.Code != errs.ErWrongValueForVar ||
e.Msg != "Variable 'sql_mode' can't be set to the value of 'NONSENSE'" {
t.Fatalf("unexpected error: %s", err)
}
}
func TestInitSessionVars(t *testing.T) {
{
got, err := DB(t, WithSessionVars(SessionVars{
"group_concat_max_len": 1234,
}), "SELECT @@group_concat_max_len")
if err != nil {
t.Fatalf("failed to set group_concat_max_len: %s", err)
}
if got != "1234" {
t.Fatalf("failed to set group_concat_max_len, expected %s, got %s", "1234", got)
}
}
{
got, err := DB(t, WithSessionVars(SessionVars{
"group_concat_max_len": 12345,
}), "SELECT @@group_concat_max_len")
if err != nil {
t.Fatalf("failed to set group_concat_max_len: %s", err)
}
if got != "12345" {
t.Fatalf("failed to set group_concat_max_len, expected %s, got %s", "12345", got)
}
}
}
func TestDefaultTxIso(t *testing.T) {
{
got, err := DB(t, WithDefaultTxIsolation(sql.LevelReadUncommitted), "SELECT @@SESSION.transaction_isolation")
if err != nil {
t.Fatalf("failed to set session isolation level: %s", err)
}
if got != "READ-UNCOMMITTED" {
t.Fatalf("failed to set session isolation level, expected %s, got %s", "READ-UNCOMMITTED", got)
}
}
{
got, err := DB(t, WithDefaultTxIsolation(sql.LevelReadCommitted), "SELECT @@SESSION.transaction_isolation")
if err != nil {
t.Fatalf("failed to set session isolation level: %s", err)
}
if got != "READ-COMMITTED" {
t.Fatalf("failed to set session isolation level, expected %s, got %s", "READ-COMMITTED", got)
}
}
}