2022-03-23 18:47:00 +00:00
|
|
|
package appdatabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
|
|
|
)
|
|
|
|
|
2022-09-27 20:27:20 +00:00
|
|
|
const kdfIterationsNumberForTests = 3200
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// SetupTestSQLDB creates a temporary sqlite database file, initialises and then returns with a teardown func
|
|
|
|
func SetupTestSQLDB(prefix string) (*sql.DB, func() error, error) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-09-27 20:27:20 +00:00
|
|
|
db, err := InitializeDB(tmpfile.Name(), prefix, kdfIterationsNumberForTests)
|
2022-03-23 18:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, func() error {
|
|
|
|
err := db.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Remove(tmpfile.Name())
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupTestMemorySQLDB(prefix string) (*sql.DB, error) {
|
2022-09-27 20:27:20 +00:00
|
|
|
db, err := InitializeDB(sqlite.InMemoryPath, prefix, kdfIterationsNumberForTests)
|
2022-03-23 18:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|