-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b95f33d
commit 91c058d
Showing
7 changed files
with
178 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"backend/graph" | ||
"context" | ||
"github.com/99designs/gqlgen/graphql/handler" | ||
"github.com/99designs/gqlgen/graphql/handler/transport" | ||
"github.com/99designs/gqlgen/graphql/playground" | ||
"github.com/gorilla/websocket" | ||
"github.com/labstack/echo/v4/middleware" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"backend/pkg/config" | ||
"backend/testutils" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
// Set up the test environment | ||
code := m.Run() | ||
os.Exit(code) | ||
} | ||
|
||
func TestGraphQLServer(t *testing.T) { | ||
ctx := context.Background() | ||
user := "test" | ||
password := "test" | ||
dbName := "test" | ||
|
||
// Setup the test database | ||
pg, cleanup, err := testutils.SetupTestDB(ctx, user, password, dbName) | ||
if err != nil { | ||
t.Fatalf("Failed to set up test database: %+v", err) | ||
} | ||
defer cleanup(migrationFilePath) | ||
|
||
// Run migrations | ||
if err := pg.RunGooseMigrationsUp(migrationFilePath); err != nil { | ||
t.Fatalf("failed to run migrations: %+v", err) | ||
} | ||
|
||
// Override the config values for testing | ||
config.Cfg.PGHost = pg.Config.Host | ||
config.Cfg.PGUser = pg.Config.User | ||
config.Cfg.PGPassword = pg.Config.Password | ||
config.Cfg.PGDBName = pg.Config.DBName | ||
config.Cfg.PGPort = pg.Config.Port | ||
config.Cfg.PGSSLMode = "disable" | ||
config.Cfg.Port = 8080 | ||
|
||
// Initialize Echo | ||
e := echo.New() | ||
e.Use(middleware.Logger()) | ||
e.Use(middleware.Recover()) | ||
|
||
// Create a new resolver with the database connection | ||
resolver := &graph.Resolver{ | ||
DB: pg.DB, | ||
} | ||
|
||
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: resolver})) | ||
|
||
// Setup WebSocket for subscriptions | ||
srv.AddTransport(&transport.Websocket{ | ||
Upgrader: websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: func(r *http.Request) bool { | ||
return true | ||
}, | ||
}, | ||
}) | ||
|
||
e.GET("/", func(c echo.Context) error { | ||
playground.Handler("GraphQL playground", "/query").ServeHTTP(c.Response(), c.Request()) | ||
return nil | ||
}) | ||
|
||
e.POST("/query", func(c echo.Context) error { | ||
srv.ServeHTTP(c.Response(), c.Request()) | ||
return nil | ||
}) | ||
|
||
// Create a test server | ||
ts := httptest.NewServer(e) | ||
defer ts.Close() | ||
|
||
// Make a simple GET request to the playground | ||
res, err := http.Get(ts.URL) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// GetFullPath takes a relative path and returns the full absolute path | ||
func GetFullPath(path string) (string, error) { | ||
cleanedPath := filepath.Clean(path) | ||
currentDir, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
fullPath := filepath.Join(currentDir, cleanedPath) | ||
return fullPath, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters