Skip to content

Commit 1a5c330

Browse files
committed
Add TestPutInMemoryDB to verify in-memory DB
1 parent fb4848d commit 1a5c330

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

test/box_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
package objectbox_test
1818

1919
import (
20-
"testing"
21-
20+
"errors"
2221
"github.com/objectbox/objectbox-go/test/assert"
2322
"github.com/objectbox/objectbox-go/test/model"
2423
"github.com/objectbox/objectbox-go/test/model/iot"
24+
"os"
25+
"testing"
2526
)
2627

2728
func TestBox(t *testing.T) {
@@ -168,6 +169,19 @@ func TestBoxBulk(t *testing.T) {
168169

169170
func TestPut(t *testing.T) {
170171
env := iot.NewTestEnv()
172+
RunTestPut(t, env)
173+
}
174+
175+
func TestPutInMemoryDB(t *testing.T) {
176+
var dir = "memory:iot-test"
177+
env := iot.NewTestEnvWithDir(t, dir)
178+
_, err := os.Stat(dir)
179+
assert.True(t, errors.Is(err, os.ErrNotExist)) // Must not exist in file system
180+
RunTestPut(t, env)
181+
}
182+
183+
// Not sure if this is the best way to "parameterize" test...
184+
func RunTestPut(t *testing.T, env *iot.TestEnv) {
171185
defer env.Close()
172186
box := iot.BoxForEvent(env.ObjectBox)
173187

test/model/iot/helper.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/ioutil"
2323
"os"
2424
"strconv"
25+
"testing"
2526
)
2627

2728
type TestEnv struct {
@@ -44,14 +45,23 @@ func NewTestEnv() *TestEnv {
4445
panic(err)
4546
}
4647

47-
objectBox, err := objectbox.NewBuilder().Directory(tempDir).Model(ObjectBoxModel()).Build()
48+
return NewTestEnvWithDir(nil, tempDir)
49+
}
50+
51+
// NewTestEnvWithDir creates an empty ObjectBox instance for a given dir
52+
func NewTestEnvWithDir(t *testing.T, directory string) *TestEnv {
53+
objectBox, err := objectbox.NewBuilder().Directory(directory).Model(ObjectBoxModel()).Build()
4854
if err != nil {
4955
panic(err)
5056
}
57+
if t != nil {
58+
cwd, _ := os.Getwd()
59+
t.Log("Creating store in directory:", directory, "(cwd:", cwd, ")")
60+
}
5161

5262
return &TestEnv{
5363
ObjectBox: objectBox,
54-
dir: tempDir,
64+
dir: directory,
5565
}
5666
}
5767

0 commit comments

Comments
 (0)