Skip to content

feat(fxsql): Prevent database connection close on tests #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions fxsql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ modules:
- "connection:ping"
```

For security reasons, you should avoid to hardcode DSN sensible parts (like the password) in your config files, you can use the [env vars placeholders](https://github.com/ankorstore/yokai/tree/main/fxconfig#configuration-env-var-placeholders) instead:

```yaml
# ./configs/config.yaml
modules:
sql:
driver: mysql
dsn: "${MYSQL_USER}:${MYSQL_PASSWORD}@tcp(${MYSQL_HOST}:${MYSQL_PORT})/${MYSQL_DATABASE}?parseTime=True"
```

Available SQL operations:

- `connection:begin`
Expand Down Expand Up @@ -331,4 +341,6 @@ modules:
dsn: ":memory:"
```

In `test` mode, the module won't automatically close the database connection on shutdown, to allow database manipulation after the `RunTest()` execution.

You can find tests examples in this module own [tests](module_test.go).
2 changes: 1 addition & 1 deletion fxsql/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewFxSQLDatabase(p FxSQLDatabaseParam) (*sql.DB, error) {
// lifecycles
p.LifeCycle.Append(fx.Hook{
OnStop: func(_ context.Context) error {
if yokaisql.FetchSystem(p.Config.GetString("modules.sql.driver")) != yokaisql.SqliteSystem {
if !p.Config.IsTestEnv() {
return db.Close()
}

Expand Down
7 changes: 7 additions & 0 deletions fxsql/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func TestModule(t *testing.T) {
t.Setenv("APP_ENV", "test")
t.Setenv("APP_CONFIG_PATH", "testdata/config")
t.Setenv("SQL_DRIVER", "sqlite")
t.Setenv("SQL_DSN", ":memory:")
Expand Down Expand Up @@ -193,9 +194,13 @@ func TestModuleWithMigrationShutdown(t *testing.T) {

err := app.Start(ctx)
assert.NoError(t, err)

err = app.Stop(ctx)
assert.NoError(t, err)
}

func TestModuleErrorWithInvalidDriver(t *testing.T) {
t.Setenv("APP_ENV", "test")
t.Setenv("APP_CONFIG_PATH", "testdata/config")
t.Setenv("SQL_DRIVER", "invalid")

Expand All @@ -222,6 +227,7 @@ func TestModuleErrorWithInvalidDriver(t *testing.T) {
}

func TestModuleErrorWithInvalidDsn(t *testing.T) {
t.Setenv("APP_ENV", "test")
t.Setenv("APP_CONFIG_PATH", "testdata/config")
t.Setenv("SQL_DRIVER", "mysql")
t.Setenv("SQL_DSN", "invalid")
Expand Down Expand Up @@ -249,6 +255,7 @@ func TestModuleErrorWithInvalidDsn(t *testing.T) {
}

func TestModuleErrorWithInvalidSeed(t *testing.T) {
t.Setenv("APP_ENV", "test")
t.Setenv("APP_CONFIG_PATH", "testdata/config")
t.Setenv("SQL_DRIVER", "sqlite")
t.Setenv("SQL_DSN", ":memory:")
Expand Down
10 changes: 10 additions & 0 deletions fxsql/testdata/config/config.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
modules:
log:
level: debug
output: test
trace:
processor:
type: test
config:
hook_name: "test hook"
seed_value: "test seed value"
10 changes: 0 additions & 10 deletions fxsql/testdata/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
app:
name: test
version: 0.1.0
modules:
log:
level: debug
output: test
trace:
processor:
type: test
sql:
driver: ${SQL_DRIVER}
dsn: ${SQL_DSN}
Expand All @@ -25,6 +18,3 @@ modules:
arguments: true
exclude:
- "connection:ping"
config:
hook_name: "test hook"
seed_value: "test seed value"
Loading