-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfront_test.go
130 lines (117 loc) · 3.93 KB
/
front_test.go
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
package front
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"goa.design/clue/example/weather/services/front/clients/forecaster"
mockforecaster "goa.design/clue/example/weather/services/front/clients/forecaster/mocks"
"goa.design/clue/example/weather/services/front/clients/locator"
mocklocator "goa.design/clue/example/weather/services/front/clients/locator/mocks"
genfront "goa.design/clue/example/weather/services/front/gen/front"
)
func TestForecast(t *testing.T) {
cases := []struct {
name string
locationFunc mocklocator.ClientGetLocationFunc
forecastFunc mockforecaster.ClientGetForecastFunc
expectedResult *genfront.Forecast2
expectedError error
}{
{"success", getLocationInUSFunc(t), getForecastFunc(t), testForecast, nil},
{"forecast error", getLocationInUSFunc(t), getForecastErrorFunc(t), nil, errForecast},
{"location not in US", getLocationNotInUSFunc(t), nil, nil, genfront.MakeNotUsa(fmt.Errorf("IP not in the US (NOT US)"))},
{"location error", getLocationErrorFunc(t), nil, nil, errLocation},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
lmock := mocklocator.NewClient(t)
lmock.AddGetLocation(c.locationFunc)
fmock := mockforecaster.NewClient(t)
fmock.AddGetForecast(c.forecastFunc)
s := New(fmock, lmock, nil)
result, err := s.Forecast(context.Background(), testIP)
if c.expectedError != nil {
assert.Nil(t, result)
require.NotNil(t, err)
assert.Equal(t, c.expectedError.Error(), err.Error())
return
}
assert.NoError(t, err)
assert.Equal(t, c.expectedResult, result)
assert.False(t, lmock.HasMore())
assert.False(t, fmock.HasMore())
})
}
}
var (
testIP = "8.8.8.8"
testLocation = &genfront.Location{
Lat: 23.0,
Long: -32.0,
City: "Test City",
State: "Test State",
}
testForecast = &genfront.Forecast2{
Location: testLocation,
Periods: []*genfront.Period{{
Name: "morning",
StartTime: "2022-01-22T21:57:40+00:00",
EndTime: "2022-01-22T21:57:40+00:00",
Temperature: 10,
TemperatureUnit: "C",
Summary: "cool",
}},
}
errForecast = fmt.Errorf("test forecast error")
errLocation = fmt.Errorf("test location error")
)
func getLocationInUSFunc(t *testing.T) mocklocator.ClientGetLocationFunc {
return func(ctx context.Context, ip string) (*locator.WorldLocation, error) {
assert.Equal(t, testIP, ip)
return &locator.WorldLocation{
Lat: testLocation.Lat,
Long: testLocation.Long,
City: testLocation.City,
Region: testLocation.State,
Country: "United States",
}, nil
}
}
func getLocationNotInUSFunc(t *testing.T) mocklocator.ClientGetLocationFunc {
return func(ctx context.Context, ip string) (*locator.WorldLocation, error) {
assert.Equal(t, testIP, ip)
return &locator.WorldLocation{
Lat: testLocation.Lat,
Long: testLocation.Long,
City: testLocation.City,
Region: testLocation.State,
Country: "NOT US",
}, nil
}
}
func getLocationErrorFunc(t *testing.T) mocklocator.ClientGetLocationFunc {
return func(ctx context.Context, ip string) (*locator.WorldLocation, error) {
assert.NotEmpty(t, ip)
return nil, errLocation
}
}
func getForecastFunc(t *testing.T) mockforecaster.ClientGetForecastFunc {
return func(ctx context.Context, lat float64, long float64) (*forecaster.Forecast, error) {
assert.Equal(t, testLocation.Lat, lat)
assert.Equal(t, testLocation.Long, long)
lval := forecaster.Location(*testForecast.Location)
ps := make([]*forecaster.Period, len(testForecast.Periods))
for i, p := range testForecast.Periods {
pval := forecaster.Period(*p)
ps[i] = &pval
}
return &forecaster.Forecast{Location: &lval, Periods: ps}, nil
}
}
func getForecastErrorFunc(t *testing.T) mockforecaster.ClientGetForecastFunc {
return func(ctx context.Context, lat float64, long float64) (*forecaster.Forecast, error) {
return nil, errForecast
}
}