Skip to content

Commit

Permalink
refactor: server & errors testing ♻️
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Apr 24, 2024
1 parent a8aa6c3 commit fd37ae5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 65 deletions.
2 changes: 1 addition & 1 deletion cmd/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions internal/handlers/errors_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
19 changes: 6 additions & 13 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
23 changes: 8 additions & 15 deletions internal/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,23 @@ 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 {
log.Fatal(err)
}
}()

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)
}

}
34 changes: 0 additions & 34 deletions lib/errors.go

This file was deleted.

0 comments on commit fd37ae5

Please sign in to comment.