Skip to content

Commit

Permalink
test: use TestMockData in tag_test (golift#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuochi authored Mar 17, 2022
1 parent 062d755 commit 3acbb1e
Show file tree
Hide file tree
Showing 6 changed files with 572 additions and 807 deletions.
275 changes: 114 additions & 161 deletions lidarr/tag_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package lidarr_test

import (
"net/http"
"net/http/httptest"
"path"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -13,20 +12,14 @@ import (
func TestGetTags(t *testing.T) {
t.Parallel()

tests := []struct {
responseStatus int
name string
expectedPath string
responseBody string
withError error
expectedResponse []*starr.Tag
}{
tests := []*starr.TestMockData{
{
name: "200",
expectedPath: "/api/v1/tag",
responseStatus: 200,
responseBody: "[{\"label\": \"flac\",\"id\": 1},{\"label\": \"mp3\",\"id\": 2}]",
expectedResponse: []*starr.Tag{
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"),
ExpectedMethod: "GET",
ResponseStatus: 200,
ResponseBody: `[{"label": "flac","id": 1},{"label": "mp3","id": 2}]`,
WithResponse: []*starr.Tag{
{
Label: "flac",
ID: 1,
Expand All @@ -36,244 +29,204 @@ func TestGetTags(t *testing.T) {
ID: 2,
},
},
withError: nil,
WithError: nil,
},
{
name: "404",
expectedPath: "/api/v1/tag",
responseStatus: 404,
responseBody: `{"message": "NotFound"}`,
withError: starr.ErrInvalidStatusCode,
expectedResponse: nil,
Name: "404",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"),
ExpectedMethod: "GET",
ResponseStatus: 404,
ResponseBody: `{"message": "NotFound"}`,
WithError: starr.ErrInvalidStatusCode,
WithResponse: []*starr.Tag(nil),
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
mockServer := test.GetMockServer(t)
client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.GetTags()
assert.ErrorIs(t, err, test.withError, "error is not the same as expected")
assert.EqualValues(t, output, test.expectedResponse, "response is not the same as expected")
assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
})
}
}

func TestGetTag(t *testing.T) {
t.Parallel()

tests := []struct {
responseStatus int
tagID int
name string
expectedPath string
responseBody string
withError error
expectedResponse *starr.Tag
}{
tests := []*starr.TestMockData{
{
name: "200",
tagID: 1,
expectedPath: "/api/v1/tag/1",
responseStatus: 200,
responseBody: "{\"label\": \"flac\",\"id\": 1}",
expectedResponse: &starr.Tag{
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedMethod: "GET",
ResponseStatus: 200,
WithRequest: 1,
ResponseBody: `{"label": "flac","id": 1}`,
WithResponse: &starr.Tag{
Label: "flac",
ID: 1,
},
withError: nil,
WithError: nil,
},
{
name: "404",
tagID: 1,
expectedPath: "/api/v1/tag/1",
responseStatus: 404,
responseBody: `{"message": "NotFound"}`,
withError: starr.ErrInvalidStatusCode,
expectedResponse: nil,
Name: "404",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedMethod: "GET",
ResponseStatus: 404,
WithRequest: 1,
ResponseBody: `{"message": "NotFound"}`,
WithResponse: (*starr.Tag)(nil),
WithError: starr.ErrInvalidStatusCode,
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
mockServer := test.GetMockServer(t)
client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.GetTag(test.tagID)
assert.ErrorIs(t, err, test.withError, "error is not the same as expected")
assert.EqualValues(t, output, test.expectedResponse, "response is not the same as expected")
output, err := client.GetTag(test.WithRequest.(int))
assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
})
}
}

func TestAddTag(t *testing.T) {
t.Parallel()

tests := []struct {
responseStatus int
name string
expectedPath string
responseBody string
withError error
tag *starr.Tag
expectedResponse *starr.Tag
}{
tests := []*starr.TestMockData{
{
name: "200",
expectedPath: "/api/v1/tag",
responseStatus: 200,
tag: &starr.Tag{
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"),
ExpectedMethod: "POST",
ResponseStatus: 200,
WithRequest: &starr.Tag{
Label: "flac",
},
responseBody: "{\"label\": \"flac\",\"id\": 1}",
expectedResponse: &starr.Tag{
ExpectedRequest: `{"label":"flac"}` + "\n",
ResponseBody: `{"label": "flac","id": 1}`,
WithResponse: &starr.Tag{
Label: "flac",
ID: 1,
},
withError: nil,
WithError: nil,
},
{
name: "404",
expectedPath: "/api/v1/tag",
responseStatus: 404,
responseBody: `{"message": "NotFound"}`,
withError: starr.ErrInvalidStatusCode,
expectedResponse: nil,
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"),
ExpectedMethod: "POST",
ResponseStatus: 404,
WithRequest: &starr.Tag{
Label: "flac",
},
ExpectedRequest: `{"label":"flac"}` + "\n",
ResponseBody: `{"message": "NotFound"}`,
WithError: starr.ErrInvalidStatusCode,
WithResponse: (*starr.Tag)(nil),
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
mockServer := test.GetMockServer(t)
client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.AddTag(test.tag)
assert.ErrorIs(t, err, test.withError, "error is not the same as expected")
assert.EqualValues(t, output, test.expectedResponse, "response is not the same as expected")
output, err := client.AddTag(test.WithRequest.(*starr.Tag))
assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
})
}
}

func TestUpdateTag(t *testing.T) {
t.Parallel()

tests := []struct {
responseStatus int
name string
expectedPath string
responseBody string
withError error
tag *starr.Tag
expectedResponse *starr.Tag
}{
tests := []*starr.TestMockData{
{
name: "200",
expectedPath: "/api/v1/tag/1",
responseStatus: 200,
tag: &starr.Tag{
Label: "flac",
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedMethod: "PUT",
ResponseStatus: 200,
WithRequest: &starr.Tag{
ID: 1,
},
responseBody: "{\"label\": \"flac\",\"id\": 1}",
expectedResponse: &starr.Tag{
Label: "flac",
},
ExpectedRequest: `{"id":1,"label":"flac"}` + "\n",
ResponseBody: `{"id": 1,"label": "flac"}`,
WithResponse: &starr.Tag{
ID: 1,
Label: "flac",
},
withError: nil,
WithError: nil,
},
{
name: "404",
expectedPath: "/api/v1/tag/1",
tag: &starr.Tag{
Label: "flac",
Name: "404",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedMethod: "PUT",
ResponseStatus: 404,
WithRequest: &starr.Tag{
ID: 1,
Label: "flac",
},
responseStatus: 404,
responseBody: `{"message": "NotFound"}`,
withError: starr.ErrInvalidStatusCode,
expectedResponse: nil,
ExpectedRequest: `{"id":1,"label":"flac"}` + "\n",
ResponseBody: `{"message": "NotFound"}`,
WithError: starr.ErrInvalidStatusCode,
WithResponse: (*starr.Tag)(nil),
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
mockServer := test.GetMockServer(t)
client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.UpdateTag(test.tag)
assert.ErrorIs(t, err, test.withError, "error is not the same as expected")
assert.EqualValues(t, output, test.expectedResponse, "response is not the same as expected")
output, err := client.UpdateTag(test.WithRequest.(*starr.Tag))
assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
})
}
}

func TestDeleteTag(t *testing.T) {
t.Parallel()

tests := []struct {
responseStatus int
tagID int
name string
expectedPath string
responseBody string
withError error
}{
tests := []*starr.TestMockData{
{
name: "200",
tagID: 1,
expectedPath: "/api/v1/tag/1",
responseStatus: 200,
responseBody: "{}",
withError: nil,
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 200,
ResponseBody: "{}",
WithError: nil,
},
{
name: "404",
tagID: 1,
expectedPath: "/api/v1/tag/1",
responseStatus: 404,
responseBody: `{"message": "NotFound"}`,
withError: starr.ErrInvalidStatusCode,
Name: "404",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 404,
ResponseBody: `{"message": "NotFound"}`,
WithError: starr.ErrInvalidStatusCode,
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
mockServer := test.GetMockServer(t)
client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
err := client.DeleteTag(test.tagID)
assert.ErrorIs(t, err, test.withError, "error is not the same as expected")
err := client.DeleteTag(test.WithRequest.(int))
assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
})
}
}
Loading

0 comments on commit 3acbb1e

Please sign in to comment.