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.
feat: add ftltest.WithDatabase(…) to get a clean test db (#1436)
``` ftltest.Context( ftltest.WithDatabase(dbHandle), ) ``` This new option does the following: - reads the existing DSN but appends `_test` to the table name - clears out all tables in the db's public schema Integration test `TestDatabase()` has been updated to have real and test db set up, with a deployment writing to the real db and unit tests running on a test db.
- Loading branch information
Showing
6 changed files
with
150 additions
and
3 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
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,50 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/TBD54566975/ftl/go-runtime/ftl/ftltest" | ||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestDatabase(t *testing.T) { | ||
ctx := ftltest.Context( | ||
ftltest.WithDatabase(db), | ||
) | ||
|
||
Insert(ctx, InsertRequest{Data: "unit test 1"}) | ||
list, err := getAll(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, len(list)) | ||
assert.Equal(t, "unit test 1", list[0]) | ||
|
||
ctx = ftltest.Context( | ||
ftltest.WithDatabase(db), | ||
) | ||
|
||
Insert(ctx, InsertRequest{Data: "unit test 2"}) | ||
list, err = getAll(ctx) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, len(list)) | ||
assert.Equal(t, "unit test 2", list[0]) | ||
} | ||
|
||
func getAll(ctx context.Context) ([]string, error) { | ||
rows, err := db.Get(ctx).Query("SELECT data FROM requests ORDER BY created_at;") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
list := []string{} | ||
for rows.Next() { | ||
var data string | ||
err := rows.Scan(&data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
list = append(list, data) | ||
} | ||
return list, 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.