Skip to content

Commit

Permalink
test: improve testHelpers_test.go ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Feb 13, 2024
1 parent 6a4ab98 commit fa62f03
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
6 changes: 3 additions & 3 deletions internal/rest/tableRoutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (suite *TableRoutesTestSuite) TestHandleAddColumn() {
con := suite.getConnection(provider)
routes.RegisterTablesRoutes(app, con)

reqBody := utils.EncodeBody(models.AddModifyColumnPayload{ColName: fmt.Sprintf("test%d", idx), Type: "varchar(255)"})
reqBody, _ := utils.EncodeBody(models.AddModifyColumnPayload{ColName: fmt.Sprintf("test%d", idx), Type: "varchar(255)"})

req := httptest.NewRequest(http.MethodPost, "http://localhost:5522/table/test/column/add", reqBody)
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -231,7 +231,7 @@ func (suite *TableRoutesTestSuite) TestHandleModifyColumn() {
con := suite.getConnection(provider)
routes.RegisterTablesRoutes(app, con)

reqBody := utils.EncodeBody(models.AddModifyColumnPayload{ColName: "name", Type: fmt.Sprintf("varchar(5%d)", idx)})
reqBody, _ := utils.EncodeBody(models.AddModifyColumnPayload{ColName: "name", Type: fmt.Sprintf("varchar(5%d)", idx)})

req := httptest.NewRequest(http.MethodPut, "http://localhost:5522/table/test/column/modify", reqBody)
req.Header.Set("Content-Type", "application/json")
Expand All @@ -257,7 +257,7 @@ func (suite *TableRoutesTestSuite) TestHandleDeleteColumn() {
con := suite.getConnection(provider)
routes.RegisterTablesRoutes(app, con)

reqBody := utils.EncodeBody(models.DeleteColumnPayload{ColName: "name"})
reqBody, _ := utils.EncodeBody(models.DeleteColumnPayload{ColName: "name"})

req := httptest.NewRequest(http.MethodDelete, "http://localhost:5522/table/test/column/delete", reqBody)
req.Header.Set("Content-Type", "application/json")
Expand Down
12 changes: 4 additions & 8 deletions utils/testHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"io"
"log"
"time"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -82,22 +81,19 @@ func NewTestingFiberApp(provider string) *fiber.App {
return app
}

func EncodeBody[T any](body T) *bytes.Buffer {
func EncodeBody[T any](body T) (*bytes.Buffer, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(body)
if err != nil {
log.Fatal(err)
return nil, err
}
return &buf
return &buf, nil
}

func DecodeBody[K any](body io.ReadCloser) K {
var parsedPayload K
decoder := json.NewDecoder(body)
err := decoder.Decode(&parsedPayload)
if err != nil {
log.Fatal(err)
}
_ = decoder.Decode(&parsedPayload)
return parsedPayload
}

Expand Down
22 changes: 21 additions & 1 deletion utils/testHelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"context"
"errors"
"io"
"log"
"meta-x/lib"
Expand Down Expand Up @@ -80,16 +81,31 @@ func TestNewTestingFiberApp(t *testing.T) {

func TestEncodeBody(t *testing.T) {
mockBody := "test"
encodedBody := EncodeBody(mockBody)
encodedBody, err := EncodeBody(mockBody)

assert.Equal(t, 7, encodedBody.Len())
assert.Nil(t, err)

_, err = EncodeBody(make(chan any)) // make encoding fail
assert.NotNil(t, err)
}

type mockBody struct {
Name string `json:"name"`
Age int `json:"age"`
}

// A mocked Reader that implements io.ReadClose which always returns an error
type errReader int

func (errReader) Read(p []byte) (n int, err error) {
return 0, errors.New("test error")
}

func (errReader) Close() error {
return errors.New("test error")
}

func TestDecodeBody(t *testing.T) {
testBody := io.NopCloser(strings.NewReader(`
{
Expand Down Expand Up @@ -124,6 +140,10 @@ func TestDecodeBody(t *testing.T) {

age = decodedBody2[1].Age
assert.Equal(t, age, 123)

decodedBody3 := DecodeBody[string](errReader(0))

assert.Empty(t, decodedBody3)
}

func TestSliceOfPointersToSliceOfValues(t *testing.T) {
Expand Down

0 comments on commit fa62f03

Please sign in to comment.