Skip to content

Commit 0c8ec89

Browse files
authored
Merge branch 'master' into test/log
2 parents 48f9807 + 4420143 commit 0c8ec89

16 files changed

+3247
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package hook
19+
20+
import (
21+
"context"
22+
"database/sql/driver"
23+
"testing"
24+
25+
"github.com/stretchr/testify/assert"
26+
27+
"seata.apache.org/seata-go/pkg/datasource/sql/types"
28+
)
29+
30+
func TestNewLoggerSQLHook(t *testing.T) {
31+
hook := NewLoggerSQLHook()
32+
assert.NotNil(t, hook)
33+
assert.IsType(t, &loggerSQLHook{}, hook)
34+
}
35+
36+
func TestLoggerSQLHook_Type(t *testing.T) {
37+
hook := &loggerSQLHook{}
38+
assert.Equal(t, types.SQLType(types.SQLTypeUnknown), hook.Type())
39+
}
40+
41+
func TestLoggerSQLHook_Before(t *testing.T) {
42+
tests := []struct {
43+
name string
44+
execCtx *types.ExecContext
45+
wantErr bool
46+
}{
47+
{
48+
name: "basic query with empty tx context",
49+
execCtx: &types.ExecContext{
50+
Query: "SELECT * FROM users",
51+
TxCtx: &types.TransactionContext{},
52+
Values: nil,
53+
},
54+
wantErr: false,
55+
},
56+
{
57+
name: "query with tx context",
58+
execCtx: &types.ExecContext{
59+
Query: "INSERT INTO users VALUES (?, ?)",
60+
TxCtx: &types.TransactionContext{
61+
XID: "test-xid-123",
62+
LocalTransID: "local-tx-456",
63+
},
64+
Values: []driver.Value{1, "test"},
65+
},
66+
wantErr: false,
67+
},
68+
{
69+
name: "query with named values",
70+
execCtx: &types.ExecContext{
71+
Query: "UPDATE users SET name = :name WHERE id = :id",
72+
TxCtx: &types.TransactionContext{
73+
XID: "test-xid-789",
74+
LocalTransID: "local-tx-012",
75+
},
76+
NamedValues: []driver.NamedValue{
77+
{Name: "name", Value: "John"},
78+
{Name: "id", Value: 1},
79+
},
80+
},
81+
wantErr: false,
82+
},
83+
{
84+
name: "query with both named values and values",
85+
execCtx: &types.ExecContext{
86+
Query: "DELETE FROM users WHERE id = ?",
87+
TxCtx: &types.TransactionContext{
88+
XID: "test-xid-345",
89+
LocalTransID: "local-tx-678",
90+
},
91+
NamedValues: []driver.NamedValue{
92+
{Name: "id", Value: 1},
93+
},
94+
Values: []driver.Value{1},
95+
},
96+
wantErr: false,
97+
},
98+
{
99+
name: "empty query",
100+
execCtx: &types.ExecContext{
101+
Query: "",
102+
TxCtx: &types.TransactionContext{
103+
XID: "test-xid-empty",
104+
LocalTransID: "local-tx-empty",
105+
},
106+
},
107+
wantErr: false,
108+
},
109+
}
110+
111+
for _, tt := range tests {
112+
t.Run(tt.name, func(t *testing.T) {
113+
hook := &loggerSQLHook{}
114+
ctx := context.Background()
115+
err := hook.Before(ctx, tt.execCtx)
116+
117+
if tt.wantErr {
118+
assert.Error(t, err)
119+
} else {
120+
assert.NoError(t, err)
121+
}
122+
})
123+
}
124+
}
125+
126+
func TestLoggerSQLHook_After(t *testing.T) {
127+
tests := []struct {
128+
name string
129+
execCtx *types.ExecContext
130+
wantErr bool
131+
}{
132+
{
133+
name: "after execution - success",
134+
execCtx: &types.ExecContext{
135+
Query: "SELECT * FROM users",
136+
TxCtx: &types.TransactionContext{
137+
XID: "test-xid-after",
138+
LocalTransID: "local-tx-after",
139+
},
140+
},
141+
wantErr: false,
142+
},
143+
{
144+
name: "after execution - empty tx context",
145+
execCtx: &types.ExecContext{
146+
Query: "SELECT * FROM products",
147+
TxCtx: &types.TransactionContext{},
148+
},
149+
wantErr: false,
150+
},
151+
}
152+
153+
for _, tt := range tests {
154+
t.Run(tt.name, func(t *testing.T) {
155+
hook := &loggerSQLHook{}
156+
ctx := context.Background()
157+
err := hook.After(ctx, tt.execCtx)
158+
159+
if tt.wantErr {
160+
assert.Error(t, err)
161+
} else {
162+
assert.NoError(t, err)
163+
}
164+
})
165+
}
166+
}
167+
168+
func TestLoggerSQLHook_Integration(t *testing.T) {
169+
hook := NewLoggerSQLHook()
170+
ctx := context.Background()
171+
172+
execCtx := &types.ExecContext{
173+
Query: "UPDATE accounts SET balance = balance - ? WHERE id = ?",
174+
TxCtx: &types.TransactionContext{
175+
XID: "integration-test-xid",
176+
LocalTransID: "integration-test-local",
177+
},
178+
Values: []driver.Value{100.50, 42},
179+
}
180+
181+
// Test Before hook
182+
err := hook.Before(ctx, execCtx)
183+
assert.NoError(t, err)
184+
185+
// Test After hook
186+
err = hook.After(ctx, execCtx)
187+
assert.NoError(t, err)
188+
}

0 commit comments

Comments
 (0)