Skip to content

Commit

Permalink
feat: auto provision databases for tests (#3448)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored Nov 21, 2024
1 parent 1136d05 commit 18b5486
Show file tree
Hide file tree
Showing 38 changed files with 1,027 additions and 152 deletions.
20 changes: 20 additions & 0 deletions backend/controller/leases/testdata/go/leases/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ require (
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/XSAM/otelsql v0.35.0 // indirect
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/kong v1.4.0 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/repr v0.4.0 // indirect
github.com/alecthomas/types v0.17.0 // indirect
github.com/amacneil/dbmate/v2 v2.23.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.32.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.23 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.23 // indirect
Expand All @@ -32,12 +35,20 @@ require (
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/block/scaffolder v1.3.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/danieljoos/wincred v1.2.2 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v27.3.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
github.com/hashicorp/cronexpr v1.1.2 // indirect
Expand All @@ -49,12 +60,20 @@ require (
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/swaggest/jsonschema-go v0.3.72 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.6 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.57.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 // indirect
Expand All @@ -71,6 +90,7 @@ require (
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.27.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/grpc v1.68.0 // indirect
Expand Down
72 changes: 72 additions & 0 deletions backend/controller/leases/testdata/go/leases/go.sum

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions backend/controller/sql/database_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ import (
func TestDatabase(t *testing.T) {
in.Run(t,
in.WithLanguages("go", "java"),
in.WithFTLConfig("database/ftl-project.toml"),
in.WithProvisioner(),
// deploy real module against "testdb"
in.CopyModule("database"),
in.CreateDBAction("database", "testdb", false),
in.Deploy("database"),
in.Call[in.Obj, in.Obj]("database", "insert", in.Obj{"data": "hello"}, nil),
in.QueryRow("testdb", "SELECT data FROM requests", "hello"),
in.Call[in.Obj, in.Obj]("database", "insert", in.Obj{"data": "hello", "id": 1}, nil),
in.QueryRow("database_testdb", "SELECT data FROM requests", "hello"),

// run tests which should only affect "testdb_test"
in.CreateDBAction("database", "testdb", true),
in.IfLanguage("go", in.ExecModuleTest("database")),
in.QueryRow("testdb", "SELECT data FROM requests", "hello"),
in.QueryRow("database_testdb", "SELECT data FROM requests", "hello"),
)
}

Expand All @@ -35,12 +33,15 @@ func TestMySQL(t *testing.T) {
in.WithProvisioner(),
// deploy real module against "testdb"
in.CopyModule("mysql"),
in.CreateDBAction("mysql", "testdb", false),
in.Deploy("mysql"),
in.Call[in.Obj, in.Obj]("mysql", "insert", in.Obj{"data": "hello"}, nil),
in.Call[in.Obj, in.Obj]("mysql", "query", map[string]any{}, func(t testing.TB, response in.Obj) {
assert.Equal(t, "hello", response["data"])
}),
in.IfLanguage("go", in.ExecModuleTest("mysql")),
in.Call[in.Obj, in.Obj]("mysql", "query", map[string]any{}, func(t testing.TB, response in.Obj) {
assert.Equal(t, "hello", response["data"])
}),
)
}

Expand Down
4 changes: 3 additions & 1 deletion backend/controller/sql/testdata/go/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (MyDbConfig) Name() string { return "testdb" }

type InsertRequest struct {
Data string
Id int
}

type InsertResponse struct{}
Expand All @@ -31,14 +32,15 @@ func Insert(ctx context.Context, req InsertRequest, db ftl.DatabaseHandle[MyDbCo
func persistRequest(ctx context.Context, req InsertRequest, db ftl.DatabaseHandle[MyDbConfig]) error {
_, err := db.Get(ctx).Exec(`CREATE TABLE IF NOT EXISTS requests
(
id SERIAL PRIMARY KEY,
data TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc'),
updated_at TIMESTAMPTZ NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc')
);`)
if err != nil {
return err
}
_, err = db.Get(ctx).Exec("INSERT INTO requests (data) VALUES ($1);", req.Data)
_, err = db.Get(ctx).Exec("INSERT INTO requests (id,data) VALUES ($1, $2);", req.Id, req.Data)
if err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions backend/controller/sql/testdata/go/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
func TestDatabase(t *testing.T) {
ctx := ftltest.Context(
ftltest.WithCallsAllowedWithinModule(),
ftltest.WithProjectFile("ftl-project.toml"),
ftltest.WithDatabase[MyDbConfig](),
)

Expand All @@ -25,7 +24,6 @@ func TestDatabase(t *testing.T) {

ctx = ftltest.Context(
ftltest.WithCallsAllowedWithinModule(),
ftltest.WithProjectFile("ftl-project.toml"),
ftltest.WithDatabase[MyDbConfig](),
)

Expand All @@ -40,8 +38,7 @@ func TestDatabase(t *testing.T) {
func TestOptionOrdering(t *testing.T) {
ctx := ftltest.Context(
ftltest.WithCallsAllowedWithinModule(),
ftltest.WithDatabase[MyDbConfig](), // <--- consumes DSNs
ftltest.WithProjectFile("ftl-project.toml"), // <--- provides DSNs
ftltest.WithDatabase[MyDbConfig](), // <--- consumes DSNs
)

_, err := ftltest.Call[InsertClient, InsertRequest, InsertResponse](ctx, InsertRequest{Data: "unit test 1"})
Expand Down
11 changes: 0 additions & 11 deletions backend/controller/sql/testdata/go/database/ftl-project.toml

This file was deleted.

20 changes: 20 additions & 0 deletions backend/controller/sql/testdata/go/database/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ require (
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/XSAM/otelsql v0.35.0 // indirect
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/kong v1.4.0 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/repr v0.4.0 // indirect
github.com/alecthomas/types v0.17.0 // indirect
github.com/amacneil/dbmate/v2 v2.23.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.32.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.23 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.23 // indirect
Expand All @@ -29,12 +32,20 @@ require (
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/block/scaffolder v1.3.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/danieljoos/wincred v1.2.2 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v27.3.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
github.com/hashicorp/cronexpr v1.1.2 // indirect
Expand All @@ -46,12 +57,20 @@ require (
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/swaggest/jsonschema-go v0.3.72 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.6 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.57.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 // indirect
Expand All @@ -69,6 +88,7 @@ require (
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.27.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/grpc v1.68.0 // indirect
Expand Down
Loading

0 comments on commit 18b5486

Please sign in to comment.