github.com/wrgl/wrgl@v0.14.0/pkg/ref/mock/store.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright © 2022 Wrangle Ltd 3 4 package refmock 5 6 import ( 7 "database/sql" 8 "fmt" 9 "testing" 10 11 _ "github.com/mattn/go-sqlite3" 12 13 "github.com/stretchr/testify/require" 14 "github.com/wrgl/wrgl/pkg/ref" 15 refsql "github.com/wrgl/wrgl/pkg/ref/sql" 16 "github.com/wrgl/wrgl/pkg/sqlutil" 17 "github.com/wrgl/wrgl/pkg/testutils" 18 ) 19 20 func NewStore(t *testing.T) (ref.Store, func()) { 21 t.Helper() 22 db, err := sql.Open( 23 "sqlite3", 24 fmt.Sprintf("file:%x.db?cache=shared&mode=memory", testutils.SecureRandomBytes(4)), 25 ) 26 require.NoError(t, err) 27 require.NoError(t, sqlutil.RunInTx(db, func(tx *sql.Tx) error { 28 for _, stmt := range refsql.CreateTableStmts { 29 if _, err := tx.Exec(stmt); err != nil { 30 return err 31 } 32 } 33 return nil 34 })) 35 return refsql.NewStore(db), func() { 36 require.NoError(t, db.Close()) 37 } 38 }