Skip to content

Commit 56ca116

Browse files
authored
Fix test issues: properly check mock expectations and improve log capture (#3)
- connection_test.go: Fix TestConnectionManagerMultipleConnections to properly check ExpectationsWereMet() errors instead of silently discarding them with blank identifiers - logging_test.go: Improve log output capture by also redirecting the standard log package output, and add proper error checking for empty output
1 parent 775a7dd commit 56ca116

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

cmd/mysql-mcp-server/connection_test.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@ func TestConnectionManagerWithMockDB(t *testing.T) {
6969
}
7070
defer mockDB.Close()
7171

72-
// Expect ping
73-
mock.ExpectPing()
74-
7572
cm := NewConnectionManager()
76-
73+
7774
// Manually add the mock connection (bypassing AddConnectionWithPoolConfig)
7875
cm.connections["test"] = mockDB
7976
cm.configs["test"] = config.ConnectionConfig{
@@ -138,9 +135,6 @@ func TestConnectionManagerMultipleConnections(t *testing.T) {
138135
}
139136
defer mockDB2.Close()
140137

141-
mock1.ExpectPing()
142-
mock2.ExpectPing()
143-
144138
cm := NewConnectionManager()
145139

146140
// Add connections manually
@@ -186,8 +180,12 @@ func TestConnectionManagerMultipleConnections(t *testing.T) {
186180
}
187181

188182
cm.Close()
189-
_ = mock1.ExpectationsWereMet()
190-
_ = mock2.ExpectationsWereMet()
183+
if err := mock1.ExpectationsWereMet(); err != nil {
184+
t.Errorf("mock1 unfulfilled expectations: %v", err)
185+
}
186+
if err := mock2.ExpectationsWereMet(); err != nil {
187+
t.Errorf("mock2 unfulfilled expectations: %v", err)
188+
}
191189
}
192190

193191
func TestGetDBWithConnManager(t *testing.T) {
@@ -266,14 +264,12 @@ func TestConnectionConfigStruct(t *testing.T) {
266264

267265
func TestConnectionManagerConcurrency(t *testing.T) {
268266
// Create mock database
269-
mockDB, mock, err := sqlmock.New()
267+
mockDB, _, err := sqlmock.New()
270268
if err != nil {
271269
t.Fatalf("failed to create mock: %v", err)
272270
}
273271
defer mockDB.Close()
274272

275-
mock.ExpectPing()
276-
277273
cm := NewConnectionManager()
278274
cm.connections["test"] = mockDB
279275
cm.configs["test"] = config.ConnectionConfig{Name: "test", DSN: "test:test@tcp(localhost)/test"}
@@ -306,4 +302,3 @@ func TestConnectionManagerConcurrency(t *testing.T) {
306302
<-done
307303
}
308304
}
309-

cmd/mysql-mcp-server/logging_test.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44
import (
55
"bytes"
66
"encoding/json"
7+
"log"
78
"os"
89
"path/filepath"
910
"strings"
@@ -34,47 +35,56 @@ func TestNewQueryTimer(t *testing.T) {
3435
}
3536

3637
func TestQueryTimerLogSuccess(t *testing.T) {
37-
// Capture stderr
38+
// Capture stderr and log output
3839
oldStderr := os.Stderr
40+
oldLogOutput := log.Writer()
3941
r, w, _ := os.Pipe()
4042
os.Stderr = w
43+
log.SetOutput(w)
4144

4245
timer := NewQueryTimer("test_query")
4346
timer.LogSuccess(5, "SELECT * FROM test")
4447

4548
w.Close()
4649
os.Stderr = oldStderr
50+
log.SetOutput(oldLogOutput)
4751

4852
var buf bytes.Buffer
4953
buf.ReadFrom(r)
5054
output := buf.String()
5155

52-
// Should contain log output
53-
if !strings.Contains(output, "test_query") && !strings.Contains(output, "query executed") {
54-
// Either JSON or plain log format should work
55-
t.Logf("Log output: %s", output)
56+
// Should contain log output - either JSON or plain log format should work
57+
if output == "" {
58+
t.Error("expected log output, got empty string")
59+
} else if !strings.Contains(output, "test_query") && !strings.Contains(output, "query executed") {
60+
t.Errorf("expected output to contain 'test_query' or 'query executed', got: %s", output)
5661
}
5762
}
5863

5964
func TestQueryTimerLogError(t *testing.T) {
60-
// Capture stderr
65+
// Capture stderr and log output
6166
oldStderr := os.Stderr
67+
oldLogOutput := log.Writer()
6268
r, w, _ := os.Pipe()
6369
os.Stderr = w
70+
log.SetOutput(w)
6471

6572
timer := NewQueryTimer("test_query")
6673
timer.LogError(os.ErrNotExist, "SELECT * FROM test")
6774

6875
w.Close()
6976
os.Stderr = oldStderr
77+
log.SetOutput(oldLogOutput)
7078

7179
var buf bytes.Buffer
7280
buf.ReadFrom(r)
7381
output := buf.String()
7482

75-
// Should contain error info
76-
if !strings.Contains(output, "test_query") && !strings.Contains(output, "failed") {
77-
t.Logf("Log output: %s", output)
83+
// Should contain error info - either tool name or failure indicator
84+
if output == "" {
85+
t.Error("expected log output, got empty string")
86+
} else if !strings.Contains(output, "test_query") && !strings.Contains(output, "failed") {
87+
t.Errorf("expected output to contain 'test_query' or 'failed', got: %s", output)
7888
}
7989
}
8090

@@ -295,4 +305,3 @@ func TestAuditEntryWithError(t *testing.T) {
295305
t.Error("error message should be in JSON output")
296306
}
297307
}
298-

0 commit comments

Comments
 (0)