Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve testing #93

Merged
merged 6 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion autobatch/autobatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ func NewAutoBatching(d ds.Batching, size int) *Datastore {

// Delete deletes a key/value
func (d *Datastore) Delete(k ds.Key) error {
_, found := d.buffer[k]
delete(d.buffer, k)

return d.child.Delete(k)
err := d.child.Delete(k)
if found && err == ds.ErrNotFound {
return nil
}
return err
}

// Get retrieves a value given a key.
Expand Down
22 changes: 3 additions & 19 deletions autobatch/autobatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,11 @@ import (
"testing"

ds "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)

func TestBasicPuts(t *testing.T) {
d := NewAutoBatching(ds.NewMapDatastore(), 16)

k := ds.NewKey("test")
v := []byte("hello world")

err := d.Put(k, v)
if err != nil {
t.Fatal(err)
}

out, err := d.Get(k)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(out, v) {
t.Fatal("wasnt the same! ITS NOT THE SAME")
}
func TestAutobatch(t *testing.T) {
dstest.SubtestAll(t, NewAutoBatching(ds.NewMapDatastore(), 16))
}

func TestFlushing(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions basic_ds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package datastore_test

import (
"testing"

dstore "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)

func TestMapDatastore(t *testing.T) {
ds := dstore.NewMapDatastore()
dstest.SubtestAll(t, ds)
}

func TestNullDatastore(t *testing.T) {
ds := dstore.NewNullDatastore()
// The only test that passes. Nothing should be found.
dstest.SubtestNotFounds(t, ds)
}
5 changes: 5 additions & 0 deletions delayed/delayed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

datastore "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
delay "github.com/ipfs/go-ipfs-delay"
)

Expand All @@ -24,3 +25,7 @@ func TestDelayed(t *testing.T) {
t.Fatal("There should have been a delay of 1 second in put and in get")
}
}

func TestDelayedAll(t *testing.T) {
dstest.SubtestAll(t, New(datastore.NewMapDatastore(), delay.Fixed(time.Millisecond)))
}
12 changes: 12 additions & 0 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package sync

import (
"testing"

ds "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)

func TestSync(t *testing.T) {
dstest.SubtestAll(t, MutexWrap(ds.NewMapDatastore()))
}
42 changes: 42 additions & 0 deletions test/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dstest

import (
"reflect"
"runtime"
"testing"

dstore "github.com/ipfs/go-datastore"
)

// BasicSubtests is a list of all basic tests.
var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
SubtestBasicPutGet,
SubtestNotFounds,
SubtestManyKeysAndQuery,
}

// BatchSubtests is a list of all basic batching datastore tests.
var BatchSubtests = []func(t *testing.T, ds dstore.Batching){
RunBatchTest,
RunBatchDeleteTest,
}

func getFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

// SubtestAll tests the given datastore against all the subtests.
func SubtestAll(t *testing.T, ds dstore.Datastore) {
for _, f := range BasicSubtests {
t.Run(getFunctionName(f), func(t *testing.T) {
f(t, ds)
})
}
if _, ok := ds.(dstore.Batching); ok {
for _, f := range BasicSubtests {
t.Run(getFunctionName(f), func(t *testing.T) {
f(t, ds)
})
}
}
}