2022-09-10 11:39:07 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2022-10-22 22:08:02 +00:00
|
|
|
"path"
|
2022-09-10 11:39:07 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-10 11:39:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const testEncryptionPass = "password123"
|
|
|
|
|
|
|
|
func TestNewConnector(t *testing.T) {
|
2022-10-22 22:08:02 +00:00
|
|
|
cStrings := map[string]string{
|
|
|
|
"filesystem": path.Join(t.TempDir(), "storage.db"),
|
|
|
|
"memory": "file::memory:?cache=shared",
|
2022-09-10 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
for name := range cStrings {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
dbc, err := New("sqlite", cStrings[name], testEncryptionPass)
|
|
|
|
require.NoError(t, err, "creating database connector")
|
|
|
|
t.Cleanup(func() { dbc.Close() })
|
2022-09-10 11:39:07 +00:00
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
row := dbc.DB().Raw("SELECT count(1) AS tables FROM sqlite_master WHERE type='table' AND name='core_kvs';")
|
2022-09-10 11:39:07 +00:00
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
var count int
|
|
|
|
assert.NoError(t, row.Scan(&count).Error, "reading table count result")
|
2022-09-10 11:39:07 +00:00
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
assert.Equal(t, 1, count)
|
|
|
|
})
|
2022-09-10 11:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
func TestPatchSQLiteConnString(t *testing.T) {
|
|
|
|
for in, out := range map[string]string{
|
|
|
|
"storage.db": "storage.db?_pragma=locking_mode(EXCLUSIVE)&_pragma=synchronous(FULL)",
|
|
|
|
"file::memory:?cache=shared": "file::memory:?_pragma=locking_mode(EXCLUSIVE)&_pragma=synchronous(FULL)&cache=shared",
|
|
|
|
} {
|
|
|
|
cs, err := patchSQLiteConnString(in)
|
|
|
|
require.NoError(t, err, "patching conn string %q", in)
|
|
|
|
assert.Equal(t, out, cs, "patching conn string %q", in)
|
2022-09-10 11:39:07 +00:00
|
|
|
}
|
|
|
|
}
|