Starting to implement integration tests
parent
792ad0f10c
commit
7a38cb536a
@ -1,18 +1,57 @@
|
||||
//go:build integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var runIntegrationTestHandlers bool
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&runIntegrationTestHandlers, "integration-handlers", false, "run integration tests for http handlers")
|
||||
}
|
||||
"git.runcible.io/learning/pulley/internal/assert"
|
||||
"git.runcible.io/learning/pulley/internal/data"
|
||||
"git.runcible.io/learning/pulley/internal/testutil"
|
||||
)
|
||||
|
||||
func TestHttpListHandlers(t *testing.T) {
|
||||
if !runIntegrationTestHandlers {
|
||||
t.Skip("Skipping handler integration tests")
|
||||
pool := testutil.SetupTestDB(t)
|
||||
|
||||
movies := []data.Movie{
|
||||
{
|
||||
ID: 1337,
|
||||
Title: "Immortals",
|
||||
Runtime: data.Runtime(110),
|
||||
Genres: []string{"action", "adventure"},
|
||||
Year: 2011,
|
||||
Version: 1,
|
||||
},
|
||||
}
|
||||
|
||||
testutil.SeedMovies(t, pool, movies)
|
||||
|
||||
app := newTestApplication(pool)
|
||||
|
||||
t.Run("test list movies", func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
r, err := http.NewRequest(http.MethodGet, "/v1/movies", nil)
|
||||
assert.NilError(t, err)
|
||||
|
||||
app.routes().ServeHTTP(rec, r)
|
||||
|
||||
resp := rec.Result()
|
||||
assert.Equal(t, resp.StatusCode, http.StatusOK)
|
||||
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
assert.NilError(t, err)
|
||||
|
||||
content := make(map[string][]data.Movie)
|
||||
err = json.Unmarshal(body, &content)
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(content["movies"]), 1)
|
||||
assert.MovieEqual(t, content["movies"][0], movies[0])
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"git.runcible.io/learning/pulley/internal/config"
|
||||
"git.runcible.io/learning/pulley/internal/data"
|
||||
"git.runcible.io/learning/pulley/internal/database"
|
||||
)
|
||||
|
||||
func newTestApplication(pool database.PgxIface) application {
|
||||
cfg := config.ServiceConfig{Env: "test"}
|
||||
mockModels := data.NewModels(pool)
|
||||
// Discards log output from tests
|
||||
// logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: true}))
|
||||
|
||||
return application{config: cfg, logger: logger, models: mockModels}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue