diff options
author | Julien Dessaux | 2021-09-15 21:29:12 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-09-15 21:29:12 +0200 |
commit | e8b2b01e7c697435f3d20657cdef0ecd54b62d14 (patch) | |
tree | 4a4239c21fd2a9b0949abf7c74a3e88c91f6eb82 /pkg/navitia_api_client | |
parent | Rewrote config tests for consistency (diff) | |
download | trains-e8b2b01e7c697435f3d20657cdef0ecd54b62d14.tar.gz trains-e8b2b01e7c697435f3d20657cdef0ecd54b62d14.tar.bz2 trains-e8b2b01e7c697435f3d20657cdef0ecd54b62d14.zip |
Rewrote navitia_api_client tests for consistency
Diffstat (limited to '')
-rw-r--r-- | pkg/navitia_api_client/departures_test.go | 26 | ||||
-rw-r--r-- | pkg/navitia_api_client/error.go | 22 | ||||
-rw-r--r-- | pkg/navitia_api_client/error_test.go | 11 | ||||
-rw-r--r-- | pkg/navitia_api_client/stops_test.go | 24 |
4 files changed, 44 insertions, 39 deletions
diff --git a/pkg/navitia_api_client/departures_test.go b/pkg/navitia_api_client/departures_test.go index 3cf9eab..0e92fe7 100644 --- a/pkg/navitia_api_client/departures_test.go +++ b/pkg/navitia_api_client/departures_test.go @@ -3,11 +3,9 @@ package navitia_api_client import ( "net/http" "net/http/httptest" - "reflect" "testing" "git.adyxax.org/adyxax/trains/pkg/model" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -18,10 +16,10 @@ func TestGetDepartures(t *testing.T) { inputNewCLient string inputGetDepartures string expected []model.Departure - expectedError interface{} + expectedError error }{ - {"invalid characters in token should fail", "}", "test", nil, &HttpClientError{}}, - {"unreachable server should fail", "https://", "test", nil, &HttpClientError{}}, + {"invalid characters in token should fail", "}", "test", nil, HttpClientError{}}, + {"unreachable server should fail", "https://", "test", nil, HttpClientError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { @@ -29,11 +27,11 @@ func TestGetDepartures(t *testing.T) { valid, err := client.GetDepartures(tc.inputGetDepartures) if tc.expectedError != nil { require.Error(t, err) - assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError)) - assert.Equal(t, tc.expected, valid) + requireErrorTypeMatch(t, err, tc.expectedError) + require.Equal(t, tc.expected, valid) } else { require.NoError(t, err) - assert.Equal(t, tc.expected, valid) + require.Equal(t, tc.expected, valid) } }) } @@ -43,10 +41,10 @@ func TestGetDepartures(t *testing.T) { inputFilename string inputGetDepartures string expected []model.Departure - expectedError interface{} + expectedError error }{ - {"invalid json should fail", "test_data/invalid.json", "test", nil, &JsonDecodeError{}}, - {"invalid date should fail", "test_data/invalid_date.json", "test", nil, &DateParsingError{}}, + {"invalid json should fail", "test_data/invalid.json", "test", nil, JsonDecodeError{}}, + {"invalid date should fail", "test_data/invalid_date.json", "test", nil, DateParsingError{}}, } for _, tc := range testCasesFilename { t.Run(tc.name, func(t *testing.T) { @@ -55,11 +53,11 @@ func TestGetDepartures(t *testing.T) { valid, err := client.GetDepartures(tc.inputGetDepartures) if tc.expectedError != nil { require.Error(t, err) - assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError)) - assert.Equal(t, tc.expected, valid) + requireErrorTypeMatch(t, err, tc.expectedError) + require.Equal(t, tc.expected, valid) } else { require.NoError(t, err) - assert.Equal(t, tc.expected, valid) + require.Equal(t, tc.expected, valid) } }) } diff --git a/pkg/navitia_api_client/error.go b/pkg/navitia_api_client/error.go index 31080de..2600aec 100644 --- a/pkg/navitia_api_client/error.go +++ b/pkg/navitia_api_client/error.go @@ -8,12 +8,12 @@ type ApiError struct { request string } -func (e *ApiError) Error() string { +func (e ApiError) Error() string { return fmt.Sprintf("Navitia Api error return code %d - %s", e.code, e.request) } func newApiError(code int, request string) error { - return &ApiError{ + return ApiError{ code: code, request: request, } @@ -25,11 +25,11 @@ type HttpClientError struct { err error } -func (e *HttpClientError) Error() string { return fmt.Sprintf("Navitia HttpClient error %s", e.msg) } -func (e *HttpClientError) Unwrap() error { return e.err } +func (e HttpClientError) Error() string { return fmt.Sprintf("Navitia HttpClient error %s", e.msg) } +func (e HttpClientError) Unwrap() error { return e.err } func newHttpClientError(msg string, err error) error { - return &HttpClientError{ + return HttpClientError{ msg: msg, err: err, } @@ -41,11 +41,11 @@ type JsonDecodeError struct { err error } -func (e *JsonDecodeError) Error() string { return fmt.Sprintf("Navitia JsonDecode error %s", e.msg) } -func (e *JsonDecodeError) Unwrap() error { return e.err } +func (e JsonDecodeError) Error() string { return fmt.Sprintf("Navitia JsonDecode error %s", e.msg) } +func (e JsonDecodeError) Unwrap() error { return e.err } func newJsonDecodeError(msg string, err error) error { - return &JsonDecodeError{ + return JsonDecodeError{ msg: msg, err: err, } @@ -57,13 +57,13 @@ type DateParsingError struct { err error } -func (e *DateParsingError) Error() string { +func (e DateParsingError) Error() string { return fmt.Sprintf("Navitia date parsing error %s", e.date) } -func (e *DateParsingError) Unwrap() error { return e.err } +func (e DateParsingError) Unwrap() error { return e.err } func newDateParsingError(date string, err error) error { - return &DateParsingError{ + return DateParsingError{ date: date, err: err, } diff --git a/pkg/navitia_api_client/error_test.go b/pkg/navitia_api_client/error_test.go index 5ecf8c2..67dfb9e 100644 --- a/pkg/navitia_api_client/error_test.go +++ b/pkg/navitia_api_client/error_test.go @@ -1,6 +1,15 @@ package navitia_api_client -import "testing" +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/require" +) + +func requireErrorTypeMatch(t *testing.T, err error, expected error) { + require.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(expected), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(expected)) +} func TestErrorsCoverage(t *testing.T) { apiErr := ApiError{} diff --git a/pkg/navitia_api_client/stops_test.go b/pkg/navitia_api_client/stops_test.go index c3a91f2..b04946f 100644 --- a/pkg/navitia_api_client/stops_test.go +++ b/pkg/navitia_api_client/stops_test.go @@ -3,11 +3,9 @@ package navitia_api_client import ( "net/http" "net/http/httptest" - "reflect" "testing" "git.adyxax.org/adyxax/trains/pkg/model" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -17,10 +15,10 @@ func TestGetStops(t *testing.T) { name string inputNewCLient string expected []model.Stop - expectedError interface{} + expectedError error }{ - {"invalid characters in token should fail", "}", nil, &HttpClientError{}}, - {"unreachable server should fail", "https://", nil, &HttpClientError{}}, + {"invalid characters in token should fail", "}", nil, HttpClientError{}}, + {"unreachable server should fail", "https://", nil, HttpClientError{}}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { @@ -28,11 +26,11 @@ func TestGetStops(t *testing.T) { valid, err := client.GetStops() if tc.expectedError != nil { require.Error(t, err) - assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError)) - assert.Equal(t, tc.expected, valid) + requireErrorTypeMatch(t, err, tc.expectedError) + require.Equal(t, tc.expected, valid) } else { require.NoError(t, err) - assert.Equal(t, tc.expected, valid) + require.Equal(t, tc.expected, valid) } }) } @@ -41,9 +39,9 @@ func TestGetStops(t *testing.T) { name string inputFilename string expected []model.Stop - expectedError interface{} + expectedError error }{ - {"invalid json should fail", "test_data/invalid.json", nil, &JsonDecodeError{}}, + {"invalid json should fail", "test_data/invalid.json", nil, JsonDecodeError{}}, } for _, tc := range testCasesFilename { t.Run(tc.name, func(t *testing.T) { @@ -52,11 +50,11 @@ func TestGetStops(t *testing.T) { valid, err := client.GetStops() if tc.expectedError != nil { require.Error(t, err) - assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError)) - assert.Equal(t, tc.expected, valid) + requireErrorTypeMatch(t, err, tc.expectedError) + require.Equal(t, tc.expected, valid) } else { require.NoError(t, err) - assert.Equal(t, tc.expected, valid) + require.Equal(t, tc.expected, valid) } }) } |