Skip to content

Commit

Permalink
feat(pgtesting): add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Dec 13, 2022
1 parent 21d0698 commit d251947
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
31 changes: 1 addition & 30 deletions pgtesting/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
# How to use ?

```
package main
import (
"context"
"os"
"testing"
"github.com/formancehq/go-libs/pgtesting/pkg"
)
func TestMain(m *testing.M) {
if err := pgtesting.CreatePostgresServer(); err != nil {
log.Fatal(err)
}
code := m.Run()
if err := pgtesting.DestroyPostgresServer(); err != nil {
log.Fatal(err)
}
os.Exit(code)
}
func TestXXX(t *testing.T) {
t.Parallel()
database := mbstesting.NewPostgresDatabase(t)
// Use database.ConnString() to get connection string of the database
...
}
```
See [example](./pkg/postgres_test.go) for an example.
35 changes: 35 additions & 0 deletions pgtesting/pkg/postgres_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pgtesting

import (
"context"
"fmt"
"log"
"os"
"testing"

"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
if err := CreatePostgresServer(); err != nil {
log.Fatal(err)
}
code := m.Run()
if err := DestroyPostgresServer(); err != nil {
log.Fatal(err)
}
os.Exit(code)
}

func TestPostgres(t *testing.T) {
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
t.Parallel()
database := NewPostgresDatabase(t)
conn, err := pgx.Connect(context.Background(), database.ConnString())
require.NoError(t, err)
require.NoError(t, conn.Close(context.Background()))
})
}
}

0 comments on commit d251947

Please sign in to comment.