Skip to content

Commit

Permalink
test: testHelpers ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Feb 13, 2024
1 parent 9a34aae commit 6a4ab98
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/golangci/golangci-lint
rev: v1.54.2
rev: v1.56.1
hooks:
- id: golangci-lint
- id: golangci-lint-full
4 changes: 3 additions & 1 deletion utils/testHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func CreateMySQLContainer(ctx context.Context) (*MySQLContainer, error) {
}

func NewTestingFiberApp(provider string) *fiber.App {
app := fiber.New()
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
app.Use(func(c *fiber.Ctx) error {
c.Locals("provider", provider)
return c.Next()
Expand Down
59 changes: 51 additions & 8 deletions utils/testHelpers_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package utils_test
package utils

import (
"context"
"io"
"log"
"meta-x/lib"
"meta-x/utils"
"reflect"
"strings"
"testing"

"github.com/gofiber/fiber/v2"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"

Expand All @@ -17,7 +19,7 @@ import (

func TestCreatePostgresContainer(t *testing.T) {
ctx := context.Background()
pgContainer, err := utils.CreatePostgresContainer(ctx)
pgContainer, err := CreatePostgresContainer(ctx)
defer func() {
_ = pgContainer.Terminate(ctx)
}()
Expand All @@ -35,7 +37,7 @@ func TestCreatePostgresContainer(t *testing.T) {

func TestCreateMySQLContainer(t *testing.T) {
ctx := context.Background()
mysqlContainer, err := utils.CreateMySQLContainer(ctx)
mysqlContainer, err := CreateMySQLContainer(ctx)
defer func() {
_ = mysqlContainer.Terminate(ctx)
}()
Expand All @@ -46,9 +48,41 @@ func TestCreateMySQLContainer(t *testing.T) {
assert.Nil(t, err)

defer con.Close()
}

err = con.Ping()
assert.Nil(t, err)
func TestNewTestingFiberApp(t *testing.T) {
app := NewTestingFiberApp(lib.SQLITE3)
defer func() {
err := app.Shutdown()
if err != nil {
log.Fatal(err)
}
}()

listenCh := make(chan bool)

app.Hooks().OnListen(func(ld fiber.ListenData) error {
listenCh <- true
return nil
})

go func() {
if err := app.Listen(":5522"); err != nil {
listenCh <- false
log.Fatal(err)
}
}()

startedListening := <-listenCh

assert.True(t, startedListening)
}

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

assert.Equal(t, 7, encodedBody.Len())
}

type mockBody struct {
Expand All @@ -63,7 +97,7 @@ func TestDecodeBody(t *testing.T) {
"age": 123
}`))

decodedBody := utils.DecodeBody[mockBody](testBody)
decodedBody := DecodeBody[mockBody](testBody)

name := decodedBody.Name
assert.Equal(t, name, "foo")
Expand All @@ -83,11 +117,20 @@ func TestDecodeBody(t *testing.T) {
}
]`))

decodedBody2 := utils.DecodeBody[[]mockBody](testBody)
decodedBody2 := DecodeBody[[]mockBody](testBody)

name = decodedBody2[1].Name
assert.Equal(t, name, "bar")

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

func TestSliceOfPointersToSliceOfValues(t *testing.T) {
var testSlice []*string
testSlice = append(testSlice, new(string))

soptsov := SliceOfPointersToSliceOfValues(testSlice)

assert.IsType(t, reflect.SliceOf(reflect.TypeOf("")), reflect.TypeOf(soptsov))
}

0 comments on commit 6a4ab98

Please sign in to comment.