77 "testing"
88
99 "github.com/philippgille/gokv/badgerdb"
10+ "github.com/philippgille/gokv/encoding"
1011 "github.com/philippgille/gokv/test"
1112)
1213
@@ -15,13 +16,17 @@ import (
1516func TestStore (t * testing.T ) {
1617 // Test with JSON
1718 t .Run ("JSON" , func (t * testing.T ) {
18- store := createStore (t , badgerdb .JSON )
19+ store , path := createStore (t , encoding .JSON )
20+ defer os .RemoveAll (path )
21+ defer store .Close ()
1922 test .TestStore (store , t )
2023 })
2124
2225 // Test with gob
2326 t .Run ("gob" , func (t * testing.T ) {
24- store := createStore (t , badgerdb .Gob )
27+ store , path := createStore (t , encoding .Gob )
28+ defer os .RemoveAll (path )
29+ defer store .Close ()
2530 test .TestStore (store , t )
2631 })
2732}
@@ -30,13 +35,17 @@ func TestStore(t *testing.T) {
3035func TestTypes (t * testing.T ) {
3136 // Test with JSON
3237 t .Run ("JSON" , func (t * testing.T ) {
33- store := createStore (t , badgerdb .JSON )
38+ store , path := createStore (t , encoding .JSON )
39+ defer os .RemoveAll (path )
40+ defer store .Close ()
3441 test .TestTypes (store , t )
3542 })
3643
3744 // Test with gob
3845 t .Run ("gob" , func (t * testing.T ) {
39- store := createStore (t , badgerdb .Gob )
46+ store , path := createStore (t , encoding .Gob )
47+ defer os .RemoveAll (path )
48+ defer store .Close ()
4049 test .TestTypes (store , t )
4150 })
4251}
@@ -45,7 +54,9 @@ func TestTypes(t *testing.T) {
4554// The store works with a single file, so everything should be locked properly.
4655// The locking is implemented in the BadgerDB package, but test it nonetheless.
4756func TestStoreConcurrent (t * testing.T ) {
48- store := createStore (t , badgerdb .JSON )
57+ store , path := createStore (t , encoding .JSON )
58+ defer os .RemoveAll (path )
59+ defer store .Close ()
4960
5061 goroutineCount := 1000
5162
@@ -54,22 +65,11 @@ func TestStoreConcurrent(t *testing.T) {
5465
5566// TestErrors tests some error cases.
5667func TestErrors (t * testing.T ) {
57- // Test with a bad MarshalFormat enum value
58-
59- store := createStore (t , badgerdb .MarshalFormat (19 ))
60- err := store .Set ("foo" , "bar" )
61- if err == nil {
62- t .Error ("An error should have occurred, but didn't" )
63- }
64- // TODO: store some value for "foo", so retrieving the value works.
65- // Just the unmarshalling should fail.
66- // _, err = store.Get("foo", new(string))
67- // if err == nil {
68- // t.Error("An error should have occurred, but didn't")
69- // }
70-
7168 // Test empty key
72- err = store .Set ("" , "bar" )
69+ store , path := createStore (t , encoding .JSON )
70+ defer os .RemoveAll (path )
71+ defer store .Close ()
72+ err := store .Set ("" , "bar" )
7373 if err == nil {
7474 t .Error ("Expected an error" )
7575 }
@@ -88,15 +88,19 @@ func TestNil(t *testing.T) {
8888 // Test setting nil
8989
9090 t .Run ("set nil with JSON marshalling" , func (t * testing.T ) {
91- store := createStore (t , badgerdb .JSON )
91+ store , path := createStore (t , encoding .JSON )
92+ defer os .RemoveAll (path )
93+ defer store .Close ()
9294 err := store .Set ("foo" , nil )
9395 if err == nil {
9496 t .Error ("Expected an error" )
9597 }
9698 })
9799
98100 t .Run ("set nil with Gob marshalling" , func (t * testing.T ) {
99- store := createStore (t , badgerdb .Gob )
101+ store , path := createStore (t , encoding .Gob )
102+ defer os .RemoveAll (path )
103+ defer store .Close ()
100104 err := store .Set ("foo" , nil )
101105 if err == nil {
102106 t .Error ("Expected an error" )
@@ -105,9 +109,11 @@ func TestNil(t *testing.T) {
105109
106110 // Test passing nil or pointer to nil value for retrieval
107111
108- createTest := func (mf badgerdb. MarshalFormat ) func (t * testing.T ) {
112+ createTest := func (codec encoding. Codec ) func (t * testing.T ) {
109113 return func (t * testing.T ) {
110- store := createStore (t , mf )
114+ store , path := createStore (t , codec )
115+ defer os .RemoveAll (path )
116+ defer store .Close ()
111117
112118 // Prep
113119 err := store .Set ("foo" , test.Foo {Bar : "baz" })
@@ -133,13 +139,14 @@ func TestNil(t *testing.T) {
133139 }
134140 }
135141 }
136- t .Run ("get with nil / nil value parameter" , createTest (badgerdb .JSON ))
137- t .Run ("get with nil / nil value parameter" , createTest (badgerdb .Gob ))
142+ t .Run ("get with nil / nil value parameter" , createTest (encoding .JSON ))
143+ t .Run ("get with nil / nil value parameter" , createTest (encoding .Gob ))
138144}
139145
140146// TestClose tests if the close method returns any errors.
141147func TestClose (t * testing.T ) {
142- store := createStore (t , badgerdb .JSON )
148+ store , path := createStore (t , encoding .JSON )
149+ defer os .RemoveAll (path )
143150 err := store .Close ()
144151 if err != nil {
145152 t .Error (err )
@@ -176,16 +183,17 @@ func TestNonExistingDir(t *testing.T) {
176183 }
177184}
178185
179- func createStore (t * testing.T , mf badgerdb.MarshalFormat ) badgerdb.Store {
186+ func createStore (t * testing.T , codec encoding.Codec ) (badgerdb.Store , string ) {
187+ randPath := generateRandomTempDBpath (t )
180188 options := badgerdb.Options {
181- Dir : generateRandomTempDBpath ( t ) ,
182- MarshalFormat : mf ,
189+ Dir : randPath ,
190+ Codec : codec ,
183191 }
184192 store , err := badgerdb .NewStore (options )
185193 if err != nil {
186194 t .Fatal (err )
187195 }
188- return store
196+ return store , randPath
189197}
190198
191199func generateRandomTempDBpath (t * testing.T ) string {
0 commit comments