@@ -3,11 +3,11 @@ package core
33import (
44 "context"
55 "errors"
6- "fmt"
7- "io"
6+ "html/template"
87 "testing"
98
109 "github.com/stretchr/testify/assert"
10+ "github.com/stretchr/testify/require"
1111 "go.uber.org/mock/gomock"
1212
1313 "github.com/TuanKiri/weather-mcp-server/internal/server/services/mock"
@@ -20,7 +20,6 @@ func TestCurrentWeather(t *testing.T) {
2020 errString string
2121 wait string
2222 setupWeatherAPI func (weatherAPI * mock.MockWeatherAPIProvider )
23- setupRenderer func (renderer * mock.MockTemplateRenderer )
2423 }{
2524 "city_not_found" : {
2625 city : "Tokyo" ,
@@ -33,12 +32,8 @@ func TestCurrentWeather(t *testing.T) {
3332 },
3433 "successful_result" : {
3534 city : "London" ,
36- wait : "{Location:London, United Kingdom " +
37- "Icon:https://cdn.weatherapi.com/weather/64x64/day/113.png " +
38- "Condition:Sunny " +
39- "Temperature:18 " +
40- "Humidity:45 " +
41- "WindSpeed:4}" ,
35+ wait : "London, United Kingdom Sunny 18 45 4 " +
36+ "https://cdn.weatherapi.com/weather/64x64/day/113.png" ,
4237 setupWeatherAPI : func (weatherAPI * mock.MockWeatherAPIProvider ) {
4338 weatherAPI .EXPECT ().
4439 Current (context .Background (), "London" ).
@@ -49,7 +44,7 @@ func TestCurrentWeather(t *testing.T) {
4944 },
5045 Current : models.Current {
5146 TempC : 18.4 ,
52- WindKph : 4 ,
47+ WindKph : 4.2 ,
5348 Humidity : 45 ,
5449 Condition : models.Condition {
5550 Text : "Sunny" ,
@@ -58,22 +53,17 @@ func TestCurrentWeather(t *testing.T) {
5853 },
5954 }, nil )
6055 },
61- setupRenderer : func (renderer * mock.MockTemplateRenderer ) {
62- renderer .EXPECT ().
63- ExecuteTemplate (gomock .Any (), "weather.html" , gomock .Any ()).
64- Do (func (wr io.Writer , _ string , data any ) {
65- if wr != nil {
66- wr .Write (fmt .Appendf ([]byte {}, "%+v" , data ))
67- }
68- })
69- },
7056 },
7157 }
7258
59+ renderer , err := template .New ("weather.html" ).Parse (
60+ "{{ .Location }} {{ .Condition }} {{ .Temperature }} " +
61+ "{{ .Humidity }} {{ .WindSpeed }} {{ .Icon }}" )
62+ require .NoError (t , err )
63+
7364 ctrl := gomock .NewController (t )
7465 defer ctrl .Finish ()
7566
76- renderer := mock .NewMockTemplateRenderer (ctrl )
7767 weatherAPI := mock .NewMockWeatherAPIProvider (ctrl )
7868
7969 svc := New (renderer , weatherAPI )
@@ -84,10 +74,6 @@ func TestCurrentWeather(t *testing.T) {
8474 tc .setupWeatherAPI (weatherAPI )
8575 }
8676
87- if tc .setupRenderer != nil {
88- tc .setupRenderer (renderer )
89- }
90-
9177 data , err := svc .Weather ().Current (context .Background (), tc .city )
9278 if err != nil {
9379 assert .EqualError (t , err , tc .errString )
0 commit comments