Created
November 1, 2020 11:55
-
-
Save RicardoLinck/3229698300ad3b6ed9342054d694fdc8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestConfiguration_FetchData(t *testing.T) { | |
t.Run("errors when invalid api version", func(t *testing.T) { | |
c := Configuration{ | |
baseURL: "", | |
apiVersion: 3, | |
fetcher: nil, | |
} | |
_, err := c.FetchData() | |
assert.Error(t, ErrInvalidAPIVersion, err) | |
}) | |
t.Run("errors when fetcher returns error", func(t *testing.T) { | |
errFromFetcher := errors.New("mock error") | |
mf := &mockFetcher{func(string) (io.ReadCloser, error) { | |
return ioutil.NopCloser(bytes.NewBufferString("")), errFromFetcher | |
}, 0} | |
c := Configuration{ | |
baseURL: "", | |
apiVersion: 1, | |
fetcher: mf, | |
} | |
_, err := c.FetchData() | |
assert.Error(t, errFromFetcher, err) | |
assert.Equal(t, 1, mf.calls) | |
}) | |
t.Run("decodes and maps from v1 response", func(t *testing.T) { | |
mf := &mockFetcher{func(string) (io.ReadCloser, error) { | |
return ioutil.NopCloser(bytes.NewBufferString(` | |
{"user_name":"test_user", | |
"dob":"1999-05-23T18:25:43.511Z", | |
"fav_sports":[{"name":"football","order":2},{"name":"hockey", "order":1}]} | |
`)), nil | |
}, 0} | |
c := Configuration{ | |
baseURL: "", | |
apiVersion: 1, | |
fetcher: mf, | |
} | |
got, err := c.FetchData() | |
assert.Nil(t, err) | |
assert.Equal(t, 1, mf.calls) | |
want := Person{ | |
Name: "test_user", | |
Age: 21, | |
Sports: []string{"hockey", "football"}, | |
} | |
assert.Equal(t, want, got) | |
}) | |
t.Run("decodes and maps from v2 response", func(t *testing.T) { | |
mf := &mockFetcher{func(string) (io.ReadCloser, error) { | |
return ioutil.NopCloser(bytes.NewBufferString(` | |
{"name":"test_user", | |
"age":21, | |
"sports":["football","hockey"]} | |
`)), nil | |
}, 0} | |
c := Configuration{ | |
baseURL: "", | |
apiVersion: 2, | |
fetcher: mf, | |
} | |
got, err := c.FetchData() | |
assert.Nil(t, err) | |
assert.Equal(t, 1, mf.calls) | |
want := Person{ | |
Name: "test_user", | |
Age: 21, | |
Sports: []string{"football", "hockey"}, | |
} | |
assert.Equal(t, want, got) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment