generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module tests can declare module context values
- Loading branch information
Showing
3 changed files
with
138 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ftltest | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/TBD54566975/ftl/go-runtime/modulecontext" | ||
) | ||
|
||
type DBType int32 | ||
|
||
const ( | ||
DBTypePostgres = DBType(modulecontext.DBTypePostgres) | ||
) | ||
|
||
// ContextBuilder allows for building a context with configuration, secrets and DSN values set up for tests | ||
type ContextBuilder struct { | ||
builder *modulecontext.Builder | ||
} | ||
|
||
func NewContextBuilder(moduleName string) *ContextBuilder { | ||
return &ContextBuilder{ | ||
builder: modulecontext.NewBuilder(moduleName), | ||
} | ||
} | ||
|
||
func (b *ContextBuilder) AddConfig(name string, value any) *ContextBuilder { | ||
b.builder.AddConfig(name, value) | ||
return b | ||
} | ||
|
||
func (b *ContextBuilder) AddSecret(name string, value any) *ContextBuilder { | ||
b.builder.AddSecret(name, value) | ||
return b | ||
} | ||
|
||
func (b *ContextBuilder) AddDSN(name string, dbType DBType, dsn string) *ContextBuilder { | ||
b.builder.AddDSN(name, modulecontext.DBType(dbType), dsn) | ||
return b | ||
} | ||
|
||
func (b *ContextBuilder) Build() (context.Context, error) { | ||
return b.BuildWithContext(Context()) | ||
} | ||
|
||
func (b *ContextBuilder) BuildWithContext(ctx context.Context) (context.Context, error) { | ||
moduleCtx, err := b.builder.Build(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return moduleCtx.ApplyToContext(ctx), 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
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,43 @@ | ||
package modulecontext | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/TBD54566975/ftl/internal/log" | ||
"github.com/alecthomas/assert/v2" | ||
"github.com/alecthomas/types/optional" | ||
|
||
cf "github.com/TBD54566975/ftl/common/configuration" | ||
) | ||
|
||
func TestReadLatestValue(t *testing.T) { | ||
ctx := log.ContextWithNewDefaultLogger(context.Background()) | ||
|
||
moduleName := "test" | ||
b := NewBuilder(moduleName) | ||
b = b.AddConfig("c", "c-value1") | ||
b = b.AddConfig("c", "c-value2") | ||
b = b.AddSecret("s", "s-value1") | ||
b = b.AddSecret("s", "s-value2") | ||
b = b.AddDSN("d", DBTypePostgres, "postgres://localhost:54320/echo1?sslmode=disable&user=postgres&password=secret") | ||
b = b.AddDSN("d", DBTypePostgres, "postgres://localhost:54320/echo2?sslmode=disable&user=postgres&password=secret") | ||
moduleCtx, err := b.Build(ctx) | ||
assert.NoError(t, err, "there should be no build errors") | ||
ctx = moduleCtx.ApplyToContext(ctx) | ||
|
||
cm := cf.ConfigFromContext(ctx) | ||
sm := cf.SecretsFromContext(ctx) | ||
|
||
var str string | ||
|
||
err = cm.Get(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "c"}, &str) | ||
assert.NoError(t, err, "could not read config value") | ||
assert.Equal(t, "c-value2", str, "latest config value should be read") | ||
|
||
err = sm.Get(ctx, cf.Ref{Module: optional.Some(moduleName), Name: "s"}, &str) | ||
assert.NoError(t, err, "could not read secret value") | ||
assert.Equal(t, "s-value2", str, "latest secret value should be read") | ||
|
||
assert.Equal(t, moduleCtx.dbProvider.entries["d"].dsn, "postgres://localhost:54320/echo2?sslmode=disable&user=postgres&password=secret", "latest DSN should be read") | ||
} |