Skip to content

Commit

Permalink
Make test code clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael committed Aug 10, 2023
1 parent 4cccf4a commit bdaf359
Showing 1 changed file with 19 additions and 53 deletions.
72 changes: 19 additions & 53 deletions example/weather/services/front/front_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"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"
Expand Down Expand Up @@ -34,61 +37,19 @@ func TestForecast(t *testing.T) {
fmock.AddGetForecast(c.forecastFunc)
s := New(fmock, lmock)
result, err := s.Forecast(context.Background(), testIP)
if (c.expectedError != nil) && (err.Error() != c.expectedError.Error()) {
t.Errorf("Forecast: got error %s, expected %s", err, c.expectedError)
}
if !equal(result, c.expectedResult) {
t.Errorf("Forecast: got %#v, expected %v", result, c.expectedResult)
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())
})
}
}

func equal(a, b *genfront.Forecast2) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if a.Location.Lat != b.Location.Lat {
return false
}
if a.Location.Long != b.Location.Long {
return false
}
if a.Location.City != b.Location.City {
return false
}
if a.Location.State != b.Location.State {
return false
}
if len(a.Periods) != len(b.Periods) {
return false
}
for i, p := range a.Periods {
if p.Name != b.Periods[i].Name {
return false
}
if p.StartTime != b.Periods[i].StartTime {
return false
}
if p.EndTime != b.Periods[i].EndTime {
return false
}
if p.Temperature != b.Periods[i].Temperature {
return false
}
if p.TemperatureUnit != b.Periods[i].TemperatureUnit {
return false
}
if p.Summary != b.Periods[i].Summary {
return false
}
}
return true
}

var (
testIP = "8.8.8.8"
testLocation = &genfront.Location{
Expand All @@ -100,9 +61,14 @@ var (

testForecast = &genfront.Forecast2{
Location: testLocation,
Periods: []*genfront.Period{
{"morning", "2022-01-22T21:57:40+00:00", "2022-01-22T21:57:40+00:00", 10, "C", "cool"},
},
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")
Expand Down

0 comments on commit bdaf359

Please sign in to comment.