diff --git a/internal/rest/databaseRoutes_test.go b/internal/rest/databaseRoutes_test.go index 6a5d072..7439b64 100644 --- a/internal/rest/databaseRoutes_test.go +++ b/internal/rest/databaseRoutes_test.go @@ -83,32 +83,17 @@ func (suite *DatabaseRoutesTestSuite) TestRegisterDatabaseRoutes() { app := fiber.New() routes.RegisterDatabasesRoutes(app, nil) - var routes []struct { - method string - params []string - path string - } - + var routes []utils.FiberRoute for _, route := range app.GetRoutes() { - routes = append(routes, struct { - method string - params []string - path string - }{ - method: route.Method, - params: route.Params, - path: route.Path, + routes = append(routes, utils.FiberRoute{ + Method: route.Method, + Path: route.Path, }) } - assert.Contains(t, routes, struct { - method string - params []string - path string - }{ - method: "GET", - params: []string(nil), - path: "/database", + assert.Contains(t, routes, utils.FiberRoute{ + Method: "GET", + Path: "/database", }) } diff --git a/internal/rest/defaultRoutes_test.go b/internal/rest/defaultRoutes_test.go index 30c839a..6615bb9 100644 --- a/internal/rest/defaultRoutes_test.go +++ b/internal/rest/defaultRoutes_test.go @@ -16,32 +16,17 @@ func TestRegisterDefaultRoutes(t *testing.T) { RegisterDefaultRoutes(app) - var routes []struct { - method string - params []string - path string - } - + var routes []utils.FiberRoute for _, route := range app.GetRoutes() { - routes = append(routes, struct { - method string - params []string - path string - }{ - method: route.Method, - params: route.Params, - path: route.Path, + routes = append(routes, utils.FiberRoute{ + Method: route.Method, + Path: route.Path, }) } - assert.Contains(t, routes, struct { - method string - params []string - path string - }{ - method: "GET", - params: []string(nil), - path: "/health", + assert.Contains(t, routes, utils.FiberRoute{ + Method: "GET", + Path: "/health", }) } diff --git a/internal/rest/setupRoutes_test.go b/internal/rest/setupRoutes_test.go index ecb2f24..86258d6 100644 --- a/internal/rest/setupRoutes_test.go +++ b/internal/rest/setupRoutes_test.go @@ -1,6 +1,8 @@ -package routes +package routes_test import ( + routes "meta-x/internal/rest" + "meta-x/utils" "testing" "github.com/gofiber/fiber/v2" @@ -9,34 +11,21 @@ import ( func TestSetup(t *testing.T) { app := fiber.New() - Setup(app, nil) + routes.Setup(app, nil) - var routes []struct { - method string - params []string - path string - } + var routes []utils.FiberRoute for _, route := range app.GetRoutes() { - routes = append(routes, struct { - method string - params []string - path string - }{ - method: route.Method, - params: route.Params, - path: route.Path, + routes = append(routes, utils.FiberRoute{ + Method: route.Method, + Path: route.Path, }) } - testData := []struct { - method string - params []string - path string - }{ - {method: "GET", params: []string(nil), path: "/health"}, - {method: "GET", params: []string(nil), path: "/databases"}, - {method: "GET", params: []string(nil), path: "/tables"}, + testData := []utils.FiberRoute{ + {Method: "GET", Path: "/health"}, + {Method: "GET", Path: "/database"}, + {Method: "GET", Path: "/table"}, } for _, test := range testData { assert.Contains(t, routes, test) diff --git a/internal/rest/tableRoutes_test.go b/internal/rest/tableRoutes_test.go index a219466..596ba9e 100644 --- a/internal/rest/tableRoutes_test.go +++ b/internal/rest/tableRoutes_test.go @@ -101,32 +101,17 @@ func (suite *TableRoutesTestSuite) TestRegisterTablesRoutes() { routes.RegisterTablesRoutes(app, nil) - var routes []struct { - method string - params []string - path string - } - + var routes []utils.FiberRoute for _, route := range app.GetRoutes() { - routes = append(routes, struct { - method string - params []string - path string - }{ - method: route.Method, - params: route.Params, - path: route.Path, + routes = append(routes, utils.FiberRoute{ + Method: route.Method, + Path: route.Path, }) } - assert.Contains(t, routes, struct { - method string - params []string - path string - }{ - method: "GET", - params: []string(nil), - path: "/table", + assert.Contains(t, routes, utils.FiberRoute{ + Method: "GET", + Path: "/table", }) } diff --git a/internal/server.go b/internal/server.go index 0a3760c..ba2b9dc 100644 --- a/internal/server.go +++ b/internal/server.go @@ -57,7 +57,6 @@ func InitDBAndServer(provider, cfg string, port int) error { fmt.Println(utils.NewStyle("Playground", "#B6B5B5"), fmt.Sprintf("http://localhost:%d/playground\n", port)) if err := app.Listen(fmt.Sprintf(":%d", port)); err != nil { - // log.Error(err) return err } return nil diff --git a/lib/errors_test.go b/lib/errors_test.go deleted file mode 100644 index d298c62..0000000 --- a/lib/errors_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package lib - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestResponseError400(t *testing.T) { - respErr := ResponseError400("error 400") - - val, ok := respErr["status"] - assert.True(t, ok) - assert.Equal(t, val, 400) - - assert.Equal(t, "error 400", respErr["error"]) -} - -func TestResponseError500(t *testing.T) { - respErr := ResponseError500("error 500") - - val, ok := respErr["status"] - assert.True(t, ok) - assert.Equal(t, val, 500) - - assert.Equal(t, "error 500", respErr["error"]) -} diff --git a/lib/validator_test.go b/lib/validator_test.go index 76cb2c3..94c4567 100644 --- a/lib/validator_test.go +++ b/lib/validator_test.go @@ -60,32 +60,3 @@ func TestNotEmpty(t *testing.T) { } } } - -func TestUpdateTableDataValidator(t *testing.T) { - baseStruct := UpdateTableProps{} - testData := []struct { - Type string - Data any - willFail bool - }{ - {Type: "add", Data: map[string]string{"foo": "bar"}, willFail: false}, - {Type: "add", Data: []string{"foo", "bar"}, willFail: true}, - {Type: "modify", Data: map[string]string{"foo": "bar"}, willFail: false}, - {Type: "modify", Data: []string{"foo", "bar"}, willFail: true}, - {Type: "delete", Data: []string{"foo", "bar"}, willFail: false}, - {Type: "delete", Data: map[string]string{"foo": "bar"}, willFail: true}, - } - - for _, test := range testData { - baseStruct.Operation.Type = test.Type - baseStruct.Operation.Data = test.Data - - errs := ValidateStruct(baseStruct) - - if test.willFail { - assert.Greater(t, len(errs), 0) - } else { - assert.Zero(t, len(errs)) - } - } -} diff --git a/utils/envVars.go b/utils/envVars.go deleted file mode 100644 index 411625f..0000000 --- a/utils/envVars.go +++ /dev/null @@ -1,17 +0,0 @@ -package utils - -import ( - "fmt" - "os" -) - -func GetEnvVar(key string, allowUnset bool) (string, error) { - envVar, exists := os.LookupEnv(key) - - if !exists && allowUnset { - return "", nil - } else if !exists && !allowUnset { - return "", fmt.Errorf("%s Env Var is missing", key) - } - return envVar, nil -} diff --git a/utils/envVars_test.go b/utils/envVars_test.go deleted file mode 100644 index f033f04..0000000 --- a/utils/envVars_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package utils - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGetEnvVar(t *testing.T) { - os.Setenv("TEST_VAR", "test_val") - val, err := GetEnvVar("TEST_VAR", false) - assert.Nil(t, err) - assert.Equal(t, val, "test_val") - - val, err = GetEnvVar("DOESN'T_EXIST", false) - assert.NotNil(t, err) - assert.Equal(t, val, "") - - val ,err = GetEnvVar("SHOULDN'T_ERROR",true) - assert.Nil(t,err) - assert.Equal(t,val,"") -} diff --git a/utils/handlers.go b/utils/handlers.go index 7cfefab..a0b3ba0 100644 --- a/utils/handlers.go +++ b/utils/handlers.go @@ -8,7 +8,9 @@ import ( "github.com/valyala/fasthttp/fasthttpadaptor" ) -func RouteHandler(db *sqlx.DB, routeHandlerFunc func(*fiber.Ctx, *sqlx.DB) error) fiber.Handler { +type RouteHandlerFunc func(*fiber.Ctx, *sqlx.DB) error + +func RouteHandler(db *sqlx.DB, routeHandlerFunc RouteHandlerFunc) fiber.Handler { fn := func(c *fiber.Ctx) error { return routeHandlerFunc(c, db) } diff --git a/utils/payloadReader_test.go b/utils/payloadReader_test.go deleted file mode 100644 index 18b638c..0000000 --- a/utils/payloadReader_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package utils - -import ( - "bytes" - "io" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestReadBody(t *testing.T) { - testBody := io.NopCloser(bytes.NewBufferString(` - { - "name":"foo", - "age": 123 - }`)) - - singleJsonBodyRes := DecodeBody[map[string]any](testBody) - - val, ok := singleJsonBodyRes["name"] - assert.True(t, ok) - assert.Equal(t, val, "foo") - - val, ok = singleJsonBodyRes["age"] - assert.True(t, ok) - assert.Equal(t, int(val.(float64)), 123) - - testBody = io.NopCloser(bytes.NewBufferString(` - [ - { - "name":"foo", - "age" : 123 - }, - { - "name":"bar", - "age" : 123 - } - ]`)) - - listOfJsonRes := DecodeBody[[]map[string]any](testBody) - - val, ok = listOfJsonRes[1]["name"] - assert.True(t, ok) - assert.Equal(t, val, "bar") - - val, ok = singleJsonBodyRes["age"] - assert.True(t, ok) - assert.Equal(t, int(val.(float64)), 123) -} diff --git a/utils/testHelpers.go b/utils/testHelpers.go index a261927..9bc30d9 100644 --- a/utils/testHelpers.go +++ b/utils/testHelpers.go @@ -106,3 +106,8 @@ func SliceOfPointersToSliceOfValues[T any](s []*T) []T { } return v } + +type FiberRoute struct { + Method string + Path string +} diff --git a/utils/testHelpers_test.go b/utils/testHelpers_test.go new file mode 100644 index 0000000..0a7c865 --- /dev/null +++ b/utils/testHelpers_test.go @@ -0,0 +1,89 @@ +package utils_test + +import ( + "context" + "io" + "meta-x/lib" + "meta-x/utils" + "strings" + "testing" + + "github.com/jmoiron/sqlx" + "github.com/stretchr/testify/assert" + + _ "github.com/go-sql-driver/mysql" + _ "github.com/lib/pq" +) + +func TestCreatePostgresContainer(t *testing.T) { + ctx := context.Background() + pgContainer, err := utils.CreatePostgresContainer(ctx) + defer pgContainer.Terminate(ctx) + + assert.Nil(t, err) + + con, err := sqlx.Open(lib.PSQL, pgContainer.ConnectionString) + assert.Nil(t, err) + + defer con.Close() + + err = con.Ping() + assert.Nil(t, err) +} + +func TestCreateMySQLContainer(t *testing.T) { + ctx := context.Background() + mysqlContainer, err := utils.CreateMySQLContainer(ctx) + defer mysqlContainer.Terminate(ctx) + + assert.Nil(t, err) + + con, err := sqlx.Open(lib.MYSQL, mysqlContainer.ConnectionString) + assert.Nil(t, err) + + defer con.Close() + + err = con.Ping() + assert.Nil(t, err) +} + +type mockBody struct { + Name string `json:"name"` + Age int `json:"age"` +} + +func TestDecodeBody(t *testing.T) { + testBody := io.NopCloser(strings.NewReader(` + { + "name":"foo", + "age": 123 + }`)) + + decodedBody := utils.DecodeBody[mockBody](testBody) + + name := decodedBody.Name + assert.Equal(t, name, "foo") + + age := decodedBody.Age + assert.Equal(t, age, 123) + + testBody = io.NopCloser(strings.NewReader(` + [ + { + "name":"foo", + "age" : 123 + }, + { + "name":"bar", + "age" : 123 + } + ]`)) + + decodedBody2 := utils.DecodeBody[[]mockBody](testBody) + + name = decodedBody2[1].Name + assert.Equal(t, name, "bar") + + age = decodedBody2[1].Age + assert.Equal(t, age, 123) +}