Skip to content

Commit

Permalink
feat(internal/database): add method prepareStore
Browse files Browse the repository at this point in the history
  • Loading branch information
jinghancc committed Nov 16, 2021
1 parent c53877f commit d725743
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions internal/database/offline/postgres/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"fmt"
"testing"

"github.com/oom-ai/oomstore/internal/database/offline"
"github.com/oom-ai/oomstore/internal/database/offline/postgres"
"github.com/oom-ai/oomstore/internal/database/test/runtime_pg"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
"github.com/stretchr/testify/require"
)

func initDB(t *testing.T) {
func prepareStore(t *testing.T) (context.Context, offline.Store) {
return prepareDB(t)
}

func prepareDB(t *testing.T) (context.Context, *postgres.DB) {
ctx := context.Background()
opt := runtime_pg.PostgresDbOpt
store, err := postgres.Open(&types.PostgresOpt{
Host: opt.Host,
Expand All @@ -19,26 +26,16 @@ func initDB(t *testing.T) {
Password: opt.Password,
Database: "test",
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer store.Close()

if _, err := store.ExecContext(context.Background(), fmt.Sprintf("drop database if exists %s", opt.Database)); err != nil {
t.Fatal(err)
}

if _, err = store.ExecContext(context.Background(), fmt.Sprintf("CREATE DATABASE %s", opt.Database)); err != nil {
t.Fatal(err)
}
}
_, err = store.ExecContext(context.Background(), fmt.Sprintf("DROP DATABASE IF EXISTS %s", opt.Database))
require.NoError(t, err)

func initAndOpenDB(t *testing.T) *postgres.DB {
initDB(t)
_, err = store.ExecContext(context.Background(), fmt.Sprintf("CREATE DATABASE %s", opt.Database))
require.NoError(t, err)

db, err := postgres.Open(&runtime_pg.PostgresDbOpt)
if err != nil {
t.Fatal(err)
}
return db
require.NoError(t, err)
return ctx, db
}

0 comments on commit d725743

Please sign in to comment.