lib.NewTestCtx includes a db
This commit is contained in:
20
src/lib/test.go
Normal file
20
src/lib/test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
_ "github.com/glebarez/sqlite"
|
||||
)
|
||||
|
||||
func NewTestCtx(t *testing.T) context.Context {
|
||||
d := t.TempDir()
|
||||
db, err := sql.Open("sqlite", path.Join(d, "db.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { db.Close() })
|
||||
return InjectDB(context.Background(), db)
|
||||
}
|
||||
27
src/lib/test_test.go
Normal file
27
src/lib/test_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package lib_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitea/price-is-wrong/src/lib"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTestCtx(t *testing.T) {
|
||||
var ctx context.Context
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
t.Run("subtest", func(t *testing.T) {
|
||||
defer wg.Done()
|
||||
ctx = lib.NewTestCtx(t)
|
||||
db := lib.ExtractDB(ctx)
|
||||
if _, err := db.Exec(`SELECT 1`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
wg.Wait()
|
||||
db := lib.ExtractDB(ctx)
|
||||
if _, err := db.Exec(`SELECT 1`); err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package lobby
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"gitea/price-is-wrong/src/lib"
|
||||
"io"
|
||||
)
|
||||
|
||||
@@ -9,11 +12,18 @@ type Storage interface {
|
||||
PlayerIDs(context.Context) ([]int, error)
|
||||
}
|
||||
|
||||
func NewDB(ctx context.Context) (DB, error) {
|
||||
return DB{}, io.EOF
|
||||
}
|
||||
type DB struct{ db *sql.DB }
|
||||
|
||||
type DB struct{}
|
||||
var _ Storage = DB{}
|
||||
|
||||
func NewDB(ctx context.Context) (DB, error) {
|
||||
db := lib.ExtractDB(ctx)
|
||||
if db == nil {
|
||||
return DB{}, fmt.Errorf("db in context expected")
|
||||
}
|
||||
|
||||
return DB{db: db}, nil
|
||||
}
|
||||
|
||||
func (db DB) PlayerIDs(ctx context.Context) ([]int, error) {
|
||||
return nil, io.EOF
|
||||
|
||||
6
src/state/lobby/storage_test.go
Normal file
6
src/state/lobby/storage_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package lobby_test
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDB(t *testing.T) {
|
||||
}
|
||||
Reference in New Issue
Block a user