From fd37ae532846ff0e2b49b68d15f6e97bac861d33 Mon Sep 17 00:00:00 2001 From: Kareem Ebrahim Date: Wed, 24 Apr 2024 19:43:46 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20server=20=20&=20errors=20testing=20?= =?UTF-8?q?=E2=99=BB=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/mysql.go | 2 +- cmd/pg.go | 2 +- cmd/sqlite3.go | 2 +- internal/handlers/errors_test.go | 21 ++++++++++++++++++++ internal/server.go | 19 ++++++------------ internal/server_test.go | 23 ++++++++------------- lib/errors.go | 34 -------------------------------- 7 files changed, 38 insertions(+), 65 deletions(-) create mode 100644 internal/handlers/errors_test.go delete mode 100644 lib/errors.go diff --git a/cmd/mysql.go b/cmd/mysql.go index dfc1e28..6b45219 100644 --- a/cmd/mysql.go +++ b/cmd/mysql.go @@ -40,7 +40,7 @@ var mysqlCommand = &cobra.Command{ return err } provider := db.NewMySQLProvider(conn) - server := internal.NewServer(provider, port, make(chan bool, 1)) + server := internal.NewServer(provider, port) if err := server.Serve(); err != nil { return err diff --git a/cmd/pg.go b/cmd/pg.go index d4e4573..0068db0 100644 --- a/cmd/pg.go +++ b/cmd/pg.go @@ -41,7 +41,7 @@ var pgCommand = &cobra.Command{ return err } provider := db.NewPgProvider(conn) - server := internal.NewServer(provider, port, make(chan bool, 1)) + server := internal.NewServer(provider, port) if err := server.Serve(); err != nil { return err diff --git a/cmd/sqlite3.go b/cmd/sqlite3.go index 3da051a..7810935 100644 --- a/cmd/sqlite3.go +++ b/cmd/sqlite3.go @@ -29,7 +29,7 @@ var sqlite3Command = &cobra.Command{ } provider := db.NewSQLiteProvider(conn) - server := internal.NewServer(provider, port, make(chan bool, 1)) + server := internal.NewServer(provider, port) if err = server.Serve(); err != nil { return err } diff --git a/internal/handlers/errors_test.go b/internal/handlers/errors_test.go new file mode 100644 index 0000000..90d89be --- /dev/null +++ b/internal/handlers/errors_test.go @@ -0,0 +1,21 @@ +package handlers + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/kareemmahlees/meta-x/models" + "github.com/kareemmahlees/meta-x/utils" + "github.com/stretchr/testify/assert" +) + +func TestHttpError(t *testing.T) { + rr := httptest.NewRecorder() + httpError(rr, http.StatusOK, "something") + + assert.Equal(t, rr.Code, http.StatusOK) + + body := utils.DecodeBody[models.ErrResp](rr.Result().Body) + assert.Equal(t, body.Message, "something") +} diff --git a/internal/server.go b/internal/server.go index 0370d93..187a139 100644 --- a/internal/server.go +++ b/internal/server.go @@ -13,26 +13,22 @@ import ( "github.com/kareemmahlees/meta-x/internal/graph" "github.com/kareemmahlees/meta-x/internal/handlers" httpSwagger "github.com/swaggo/http-swagger" - - "github.com/gofiber/fiber/v2" ) type Server struct { - storage db.Storage - port int - listenCh chan bool - app *fiber.App + storage db.Storage + port int + router *chi.Mux } -func NewServer(storage db.Storage, port int, listenCh chan bool) *Server { - return &Server{storage, port, listenCh, nil} +func NewServer(storage db.Storage, port int) *Server { + return &Server{storage, port, nil} } func (s *Server) Serve() error { - // see https://github.com/99designs/gqlgen/issues/1664#issuecomment-1616620967 - // Create a gqlgen handler h := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{Storage: s.storage}})) r := chi.NewRouter() + s.router = r r.Use(middleware.Logger) r.Post("/graphql", h.ServeHTTP) @@ -41,9 +37,6 @@ func (s *Server) Serve() error { httpSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", s.port)), )) - app := fiber.New(fiber.Config{DisableStartupMessage: true}) - s.app = app - defaultHandler := handlers.NewDefaultHandler() dbHandler := handlers.NewDBHandler(s.storage) tableHandler := handlers.NewTableHandler(s.storage) diff --git a/internal/server_test.go b/internal/server_test.go index d2e1c94..b7157e3 100644 --- a/internal/server_test.go +++ b/internal/server_test.go @@ -43,8 +43,7 @@ func (ms *MockStorage) DeleteColumn(tableName string, data models.DeleteColumnPa } func TestServe(t *testing.T) { - listenCh := make(chan bool, 1) - server := NewServer(NewMockStorage(), 5522, listenCh) + server := NewServer(NewMockStorage(), 5522) go func() { if err := server.Serve(); err != nil { @@ -52,21 +51,15 @@ func TestServe(t *testing.T) { } }() - assert.True(t, <-listenCh) + testRoutes := []string{"/graphql/*", "/playground/*", "/swagger/*"} + registerdRoutes := []string{} - testRoutes := []string{"graphql", "playground", "swagger"} + for _, route := range server.router.Routes() { + registerdRoutes = append(registerdRoutes, route.Pattern) + } for _, route := range testRoutes { - foundRoute := server.app.GetRoute(route) - assert.NotEmpty(t, foundRoute) - - // request := utils.RequestTesting[any]{ - // ReqMethod: http.MethodGet, - // ReqUrl: fmt.Sprintf("/%s", route), - // } - // _, res := request.RunRequest(server.app) - - // assert.NotEqual(t, http.StatusNotFound, res.StatusCode) - + assert.Contains(t, registerdRoutes, route) } + } diff --git a/lib/errors.go b/lib/errors.go deleted file mode 100644 index bc5d1ea..0000000 --- a/lib/errors.go +++ /dev/null @@ -1,34 +0,0 @@ -package lib - -import ( - "github.com/gofiber/fiber/v2" - "github.com/kareemmahlees/meta-x/models" -) - -func BadRequestErr(c *fiber.Ctx, errMsg any) error { - return c.Status(fiber.StatusBadRequest).JSON(models.ErrResp{ - Code: fiber.StatusBadRequest, - Message: errMsg, - }) -} - -func UnprocessableEntityErr(c *fiber.Ctx, errMsg any) error { - return c.Status(fiber.StatusUnprocessableEntity).JSON(models.ErrResp{ - Code: fiber.StatusUnprocessableEntity, - Message: errMsg, - }) -} - -func ForbiddenErr(c *fiber.Ctx, errMsg any) error { - return c.Status(fiber.StatusForbidden).JSON(models.ErrResp{ - Code: fiber.StatusForbidden, - Message: errMsg, - }) -} - -func InternalServerErr(c *fiber.Ctx, errMsg any) error { - return c.Status(fiber.StatusInternalServerError).JSON(models.ErrResp{ - Code: fiber.StatusInternalServerError, - Message: errMsg, - }) -}