Skip to content

Commit f277b58

Browse files
committed
Adapted tests to use encoding package
- Also added removal of existing DB files in BadgerDB tests to avoid filling the disk (2 GB per test function)
1 parent 82d7497 commit f277b58

File tree

19 files changed

+353
-561
lines changed

19 files changed

+353
-561
lines changed

badgerdb/badgerdb_test.go

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
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 (
1516
func 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) {
3035
func 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.
4756
func 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.
5667
func 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.
141147
func 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

191199
func generateRandomTempDBpath(t *testing.T) string {

bbolt/bbolt_test.go

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/philippgille/gokv/badgerdb"
1010
"github.com/philippgille/gokv/bbolt"
11+
"github.com/philippgille/gokv/encoding"
1112
"github.com/philippgille/gokv/test"
1213
)
1314

@@ -16,13 +17,13 @@ import (
1617
func TestStore(t *testing.T) {
1718
// Test with JSON
1819
t.Run("JSON", func(t *testing.T) {
19-
store := createStore(t, bbolt.JSON)
20+
store := createStore(t, encoding.JSON)
2021
test.TestStore(store, t)
2122
})
2223

2324
// Test with gob
2425
t.Run("gob", func(t *testing.T) {
25-
store := createStore(t, bbolt.Gob)
26+
store := createStore(t, encoding.Gob)
2627
test.TestStore(store, t)
2728
})
2829
}
@@ -31,13 +32,13 @@ func TestStore(t *testing.T) {
3132
func TestTypes(t *testing.T) {
3233
// Test with JSON
3334
t.Run("JSON", func(t *testing.T) {
34-
store := createStore(t, bbolt.JSON)
35+
store := createStore(t, encoding.JSON)
3536
test.TestTypes(store, t)
3637
})
3738

3839
// Test with gob
3940
t.Run("gob", func(t *testing.T) {
40-
store := createStore(t, bbolt.Gob)
41+
store := createStore(t, encoding.Gob)
4142
test.TestTypes(store, t)
4243
})
4344
}
@@ -46,7 +47,7 @@ func TestTypes(t *testing.T) {
4647
// The store works with a single file, so everything should be locked properly.
4748
// The locking is implemented in the bbolt package, but test it nonetheless.
4849
func TestStoreConcurrent(t *testing.T) {
49-
store := createStore(t, bbolt.JSON)
50+
store := createStore(t, encoding.JSON)
5051

5152
goroutineCount := 1000
5253

@@ -55,22 +56,9 @@ func TestStoreConcurrent(t *testing.T) {
5556

5657
// TestErrors tests some error cases.
5758
func TestErrors(t *testing.T) {
58-
// Test with a bad MarshalFormat enum value
59-
60-
store := createStore(t, bbolt.MarshalFormat(19))
61-
err := store.Set("foo", "bar")
62-
if err == nil {
63-
t.Error("An error should have occurred, but didn't")
64-
}
65-
// TODO: store some value for "foo", so retrieving the value works.
66-
// Just the unmarshalling should fail.
67-
// _, err = store.Get("foo", new(string))
68-
// if err == nil {
69-
// t.Error("An error should have occurred, but didn't")
70-
// }
71-
7259
// Test empty key
73-
err = store.Set("", "bar")
60+
store := createStore(t, encoding.JSON)
61+
err := store.Set("", "bar")
7462
if err == nil {
7563
t.Error("Expected an error")
7664
}
@@ -89,15 +77,15 @@ func TestNil(t *testing.T) {
8977
// Test setting nil
9078

9179
t.Run("set nil with JSON marshalling", func(t *testing.T) {
92-
store := createStore(t, bbolt.JSON)
80+
store := createStore(t, encoding.JSON)
9381
err := store.Set("foo", nil)
9482
if err == nil {
9583
t.Error("Expected an error")
9684
}
9785
})
9886

9987
t.Run("set nil with Gob marshalling", func(t *testing.T) {
100-
store := createStore(t, bbolt.Gob)
88+
store := createStore(t, encoding.Gob)
10189
err := store.Set("foo", nil)
10290
if err == nil {
10391
t.Error("Expected an error")
@@ -106,9 +94,9 @@ func TestNil(t *testing.T) {
10694

10795
// Test passing nil or pointer to nil value for retrieval
10896

109-
createTest := func(mf bbolt.MarshalFormat) func(t *testing.T) {
97+
createTest := func(codec encoding.Codec) func(t *testing.T) {
11098
return func(t *testing.T) {
111-
store := createStore(t, mf)
99+
store := createStore(t, codec)
112100

113101
// Prep
114102
err := store.Set("foo", test.Foo{Bar: "baz"})
@@ -134,13 +122,13 @@ func TestNil(t *testing.T) {
134122
}
135123
}
136124
}
137-
t.Run("get with nil / nil value parameter", createTest(bbolt.JSON))
138-
t.Run("get with nil / nil value parameter", createTest(bbolt.Gob))
125+
t.Run("get with nil / nil value parameter", createTest(encoding.JSON))
126+
t.Run("get with nil / nil value parameter", createTest(encoding.Gob))
139127
}
140128

141129
// TestClose tests if the close method returns any errors.
142130
func TestClose(t *testing.T) {
143-
store := createStore(t, bbolt.JSON)
131+
store := createStore(t, encoding.JSON)
144132
err := store.Close()
145133
if err != nil {
146134
t.Error(err)
@@ -176,10 +164,10 @@ func TestNonExistingDir(t *testing.T) {
176164
}
177165
}
178166

179-
func createStore(t *testing.T, mf bbolt.MarshalFormat) bbolt.Store {
167+
func createStore(t *testing.T, codec encoding.Codec) bbolt.Store {
180168
options := bbolt.Options{
181-
Path: generateRandomTempDbPath(t),
182-
MarshalFormat: mf,
169+
Path: generateRandomTempDbPath(t),
170+
Codec: codec,
183171
}
184172
store, err := bbolt.NewStore(options)
185173
if err != nil {

0 commit comments

Comments
 (0)