From bdaf359125684f9d0642d22443b7789e103a186a Mon Sep 17 00:00:00 2001 From: Raphael Simon Date: Thu, 10 Aug 2023 12:03:34 -0700 Subject: [PATCH] Make test code clearer --- example/weather/services/front/front_test.go | 72 ++++++-------------- 1 file changed, 19 insertions(+), 53 deletions(-) diff --git a/example/weather/services/front/front_test.go b/example/weather/services/front/front_test.go index 6d9f2f27..0abffe3c 100644 --- a/example/weather/services/front/front_test.go +++ b/example/weather/services/front/front_test.go @@ -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" @@ -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{ @@ -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")