Skip to content

Commit

Permalink
refactor: all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Feb 4, 2024
1 parent af53668 commit 79fb91e
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 236 deletions.
29 changes: 7 additions & 22 deletions internal/rest/databaseRoutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})

}
Expand Down
29 changes: 7 additions & 22 deletions internal/rest/defaultRoutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})

}
Expand Down
35 changes: 12 additions & 23 deletions internal/rest/setupRoutes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package routes
package routes_test

import (
routes "meta-x/internal/rest"
"meta-x/utils"
"testing"

"github.com/gofiber/fiber/v2"
Expand All @@ -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)
Expand Down
29 changes: 7 additions & 22 deletions internal/rest/tableRoutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})

}
Expand Down
1 change: 0 additions & 1 deletion internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 0 additions & 27 deletions lib/errors_test.go

This file was deleted.

29 changes: 0 additions & 29 deletions lib/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
17 changes: 0 additions & 17 deletions utils/envVars.go

This file was deleted.

23 changes: 0 additions & 23 deletions utils/envVars_test.go

This file was deleted.

4 changes: 3 additions & 1 deletion utils/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
49 changes: 0 additions & 49 deletions utils/payloadReader_test.go

This file was deleted.

5 changes: 5 additions & 0 deletions utils/testHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ func SliceOfPointersToSliceOfValues[T any](s []*T) []T {
}
return v
}

type FiberRoute struct {
Method string
Path string
}
Loading

0 comments on commit 79fb91e

Please sign in to comment.