Skip to content

Commit 56a8384

Browse files
committed
clean up test suite
1 parent d7ad19f commit 56a8384

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

internal/server/handlers/weather_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,31 @@ import (
1515

1616
func TestCurrentWeather(t *testing.T) {
1717
testCases := map[string]struct {
18-
arguments map[string]any
19-
errString string
20-
wait string
18+
fillWeatherService func(mocksWeather *mocks.MockWeatherService)
19+
arguments map[string]any
20+
errString string
21+
wait string
2122
}{
2223
"empty_city": {
2324
wait: "city must be a string",
2425
},
2526
"city_not_found": {
27+
fillWeatherService: func(mocksWeather *mocks.MockWeatherService) {
28+
mocksWeather.EXPECT().
29+
Current(context.Background(), "Tokyo").
30+
Return("", errors.New("weather API not available. Code: 400"))
31+
},
2632
arguments: map[string]any{
2733
"city": "Tokyo",
2834
},
2935
errString: "weather API not available. Code: 400",
3036
},
3137
"successful_request": {
38+
fillWeatherService: func(mocksWeather *mocks.MockWeatherService) {
39+
mocksWeather.EXPECT().
40+
Current(context.Background(), "London").
41+
Return("<h1>London weather data</h1>", nil)
42+
},
3243
arguments: map[string]any{
3344
"city": "London",
3445
},
@@ -40,31 +51,21 @@ func TestCurrentWeather(t *testing.T) {
4051
defer ctrl.Finish()
4152

4253
mocksWeather := mocks.NewMockWeatherService(ctrl)
43-
mocksWeather.EXPECT().
44-
Current(context.Background(), "Tokyo").
45-
Return("", errors.New("weather API not available. Code: 400"))
46-
mocksWeather.EXPECT().
47-
Current(context.Background(), "London").
48-
Return("<h1>London weather data</h1>", nil)
54+
4955
svc := mocks.NewMockServices(ctrl)
50-
svc.EXPECT().Weather().Return(mocksWeather).Times(2)
56+
svc.EXPECT().Weather().Return(mocksWeather).AnyTimes()
5157

5258
handler := CurrentWeather(svc)
5359

5460
for name, tc := range testCases {
5561
t.Run(name, func(t *testing.T) {
56-
request := mcp.CallToolRequest{
57-
Params: struct {
58-
Name string `json:"name"`
59-
Arguments map[string]any `json:"arguments,omitempty"`
60-
Meta *struct {
61-
ProgressToken mcp.ProgressToken `json:"progressToken,omitempty"`
62-
} `json:"_meta,omitempty"`
63-
}{
64-
Arguments: tc.arguments,
65-
},
62+
if tc.fillWeatherService != nil {
63+
tc.fillWeatherService(mocksWeather)
6664
}
6765

66+
var request mcp.CallToolRequest
67+
request.Params.Arguments = tc.arguments
68+
6869
result, err := handler(context.Background(), request)
6970
if err != nil {
7071
assert.EqualError(t, err, tc.errString)

internal/server/tools/weather_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7-
"go.uber.org/mock/gomock"
8-
9-
mocks "github.com/TuanKiri/weather-mcp-server/internal/server/services/mock"
107
)
118

129
func TestCurrentWeather(t *testing.T) {
13-
ctrl := gomock.NewController(t)
14-
15-
svc := mocks.NewMockServices(ctrl)
16-
17-
tool, handler := CurrentWeather(svc)
10+
tool, handler := CurrentWeather(nil)
1811

1912
assert.Equal(t, "current_weather", tool.Name)
2013
assert.NotEmpty(t, tool.Description)

0 commit comments

Comments
 (0)