Repository navigation
Expand file tree
/
Copy pathdaemon_test.go
More file actions
137 lines (111 loc) · 3.61 KB
/
Copy pathdaemon_test.go
File metadata and controls
137 lines (111 loc) · 3.61 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
package sqlmcp
import (
"strings"
"testing"
"webtyp.com/context"
"webtyp.com/ddl"
"webtyp.com/mcp"
"webtyp.com/model"
"webtyp.com/orm"
"webtyp.com/storage"
)
func TestDaemonProvider_Tools(t *testing.T) {
p := NewDaemonProvider()
tools := p.Tools()
if len(tools) != 4 {
t.Errorf("expected 4 tools, got %d", len(tools))
}
names := map[string]bool{}
for _, tool := range tools {
names[tool.Name] = true
}
for _, name := range []string{"db_schema", "db_query", "db_exec", "db_export_schema"} {
if !names[name] {
t.Errorf("missing tool %s", name)
}
}
}
func TestDaemonProvider_NoDB(t *testing.T) {
p := NewDaemonProvider()
ctx := context.Background()
for _, tool := range p.Tools() {
_, err := tool.Execute(ctx, mcp.Request{})
if err == nil || !strings.Contains(err.Error(), "no database configured") {
t.Errorf("tool %s: expected 'no database configured' error, got %v", tool.Name, err)
}
}
}
// mockConn implements storage.Conn + ddl.SchemaInspector for the daemon provider tests.
type mockConn struct {
queryCalled bool
execCalled bool
}
func (m *mockConn) Exec(query string, args ...any) (storage.Result, error) {
m.execCalled = true
return storage.Affected(0), nil
}
func (m *mockConn) QueryRow(query string, args ...any) storage.Scanner {
return &mockScanner{}
}
func (m *mockConn) Query(query string, args ...any) (storage.Rows, error) {
m.queryCalled = true
return &mockRows{}, nil
}
func (m *mockConn) Close() error { return nil }
func (m *mockConn) Compile(q storage.Query, mdl model.Model) (storage.Plan, error) {
return storage.Plan{}, nil
}
func (m *mockConn) Tables() ([]string, error) { return []string{"users"}, nil }
func (m *mockConn) Columns(table string) ([]ddl.ColumnInfo, error) {
return []ddl.ColumnInfo{{Name: "id", Type: "INTEGER", PK: true}}, nil
}
type mockScanner struct{}
func (m *mockScanner) Scan(dest ...any) error { return nil }
type mockRows struct{}
func (m *mockRows) Columns() ([]string, error) { return []string{"col1"}, nil }
func (m *mockRows) Next() bool { return false }
func (m *mockRows) Scan(dest ...any) error { return nil }
func (m *mockRows) Close() error { return nil }
func (m *mockRows) Err() error { return nil }
func TestDaemonProvider_WithDB(t *testing.T) {
p := NewDaemonProvider()
conn := &mockConn{}
db := orm.New(conn)
p.SetDB(db)
p.SetExportFunc(func() (string, error) { return "-- exported schema", nil })
ctx := context.Background()
// Test db_query
reqQuery := mcp.Request{Params: mcp.CallToolParams{Arguments: `{"SQL": "SELECT 1"}`}}
_, err := p.Tools()[1].Execute(ctx, reqQuery) // db_query is at index 1
if err != nil {
t.Errorf("db_query failed: %v", err)
}
if !conn.queryCalled {
t.Errorf("db_query did not call conn.Query")
}
// Test db_exec
reqExec := mcp.Request{Params: mcp.CallToolParams{Arguments: `{"SQL": "UPDATE x SET y=1"}`}}
_, err = p.Tools()[2].Execute(ctx, reqExec) // db_exec is at index 2
if err != nil {
t.Errorf("db_exec failed: %v", err)
}
if !conn.execCalled {
t.Errorf("db_exec did not call conn.Exec")
}
// Test db_schema
_, err = p.Tools()[0].Execute(ctx, mcp.Request{}) // db_schema is at index 0
if err != nil {
t.Errorf("db_schema failed: %v", err)
}
// Test db_export_schema
_, err = p.Tools()[3].Execute(ctx, mcp.Request{}) // db_export_schema is at index 3
if err != nil {
t.Errorf("db_export_schema failed: %v", err)
}
// Test SetDB(nil)
p.SetDB(nil)
_, err = p.Tools()[1].Execute(ctx, reqQuery)
if err == nil || !strings.Contains(err.Error(), "no database configured") {
t.Errorf("expected error after SetDB(nil), got %v", err)
}
}