Skip to content

Commit

Permalink
Update format of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tdawe committed Dec 17, 2024
1 parent 503195d commit 044c1d8
Showing 1 changed file with 44 additions and 72 deletions.
116 changes: 44 additions & 72 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,23 @@ import (
)

func TestGet(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
method string
path string
headers map[string]string
body interface{}
expectedErr error
expectedBody string
}{
{
name: "successful GET request",
"successful GET request": {
method: http.MethodGet,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "failed GET request",
"failed GET request": {
method: http.MethodGet,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
Expand All @@ -58,8 +55,8 @@ func TestGet(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a test server to handle the request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Check failure on line 61 in api/api_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
for k, v := range tt.headers {
Expand Down Expand Up @@ -123,26 +120,23 @@ func TestGet(t *testing.T) {
}

func TestPost(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
method string
path string
headers map[string]string
body interface{}
expectedErr error
expectedBody string
}{
{
name: "successful POST request",
"successful POST request": {
method: http.MethodPost,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "failed POST request",
"failed POST request": {
method: http.MethodPost,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
Expand All @@ -152,8 +146,8 @@ func TestPost(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a test server to handle the request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != tt.method {
Expand Down Expand Up @@ -218,26 +212,23 @@ func TestPost(t *testing.T) {
}

func TestPut(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
method string
path string
headers map[string]string
body interface{}
expectedErr error
expectedBody string
}{
{
name: "successful PUT request",
"successful PUT request": {
method: http.MethodPut,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "failed PUT request",
"failed PUT request": {
method: http.MethodPut,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
Expand All @@ -247,8 +238,8 @@ func TestPut(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a test server to handle the request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != tt.method {
Expand Down Expand Up @@ -313,24 +304,21 @@ func TestPut(t *testing.T) {
}

func TestDelete(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
method string
path string
headers map[string]string
body interface{}
expectedErr error
}{
{
name: "successful DELETE request",
"successful DELETE request": {
method: http.MethodDelete,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
},
{
name: "failed DELETE request",
"failed DELETE request": {
method: http.MethodDelete,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
Expand All @@ -339,8 +327,8 @@ func TestDelete(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a test server to handle the request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != tt.method {
Expand Down Expand Up @@ -392,24 +380,21 @@ func TestDelete(t *testing.T) {
}

func TestDo(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
method string
path string
body interface{}
expectedErr error
expectedBody string
}{
{
name: "successful GET request",
"successful GET request": {
method: http.MethodGet,
path: "/api/test",
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "failed GET request",
"failed GET request": {
method: http.MethodGet,
path: "/api/test",
body: nil,
Expand All @@ -418,8 +403,8 @@ func TestDo(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a test server to handle the request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != tt.method {
Expand Down Expand Up @@ -478,44 +463,39 @@ func TestDo(t *testing.T) {
}

func TestDoWithHeaders(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
method string
path string
headers map[string]string
body interface{}
expectedErr error
expectedBody string
}{
{
name: "successful GET request",
"successful GET request": {
method: http.MethodGet,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "successful POST request",
"successful POST request": {
method: http.MethodPost,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "successful PUT request",
"successful PUT request": {
method: http.MethodPut,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "successful DELETE request",
"successful DELETE request": {
method: http.MethodDelete,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
Expand All @@ -524,8 +504,8 @@ func TestDoWithHeaders(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a test server to handle the request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != tt.method {
Expand Down Expand Up @@ -610,36 +590,31 @@ func (r *TestReadCloser) Close() error {
}

func TestDoAndGetResponseBody(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
method string
path string
headers map[string]string
body interface{}
expectedErr error
expectedBody string
}{
{
name: "successful GET request",
"successful GET request": {
method: http.MethodGet,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: nil,
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},
{
name: "successful GET request",
"successful GET request with custom body": {
method: http.MethodGet,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
body: SamplePayload{},
expectedErr: nil,
expectedBody: `{"message":"success"}`,
},

{
name: "successful GET request",
"successful GET request with readcloser body": {
method: http.MethodGet,
path: "/api/test",
headers: map[string]string{"Content-Type": "application/json"},
Expand All @@ -651,8 +626,8 @@ func TestDoAndGetResponseBody(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a test server to handle the request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != tt.method {
Expand Down Expand Up @@ -721,13 +696,12 @@ func TestDoAndGetResponseBody(t *testing.T) {
}

func TestParseJSONError(t *testing.T) {
tests := []struct {
tests := map[string]struct {
name string
response *http.Response
expectedErr error
}{
{
name: "JSON response",
"JSON response": {
response: &http.Response{
StatusCode: http.StatusBadRequest,
Header: http.Header{"Content-Type": []string{"application/json"}},
Expand All @@ -738,8 +712,7 @@ func TestParseJSONError(t *testing.T) {
Message: "Bad Request",
},
},
{
name: "HTML response",
"HTML response": {
response: &http.Response{
StatusCode: http.StatusBadRequest,
Status: "Bad Request",
Expand All @@ -751,8 +724,7 @@ func TestParseJSONError(t *testing.T) {
Message: "Bad Request",
},
},
{
name: "No content type",
"No content type": {
response: &http.Response{
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(`{"message":"Bad Request"}`)),
Expand All @@ -764,8 +736,8 @@ func TestParseJSONError(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
c := &client{}
err := c.ParseJSONError(tt.response)
if !reflect.DeepEqual(err, tt.expectedErr) {
Expand Down

0 comments on commit 044c1d8

Please sign in to comment.