test: Add persistence module test

This commit is contained in:
Anthony Laibe 2021-10-21 15:14:14 +02:00
parent 041a5dae67
commit 10378a5827
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package persistence
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestRendezVousWithLevelDb(t *testing.T) {
dbPath := "/tmp/rendezvoustest"
rdv, err := NewRendezVousLevelDB(dbPath)
require.NoError(t, err)
err = rdv.Put([]byte("key"), []byte("value"))
require.NoError(t, err)
val, err := rdv.db.Get([]byte("key"), nil)
require.NoError(t, err)
require.Equal(t, []byte("value"), val)
it := rdv.NewIterator(nil)
ok := it.Next()
require.True(t, ok)
require.Equal(t, []byte("key"), it.Key())
require.Equal(t, []byte("value"), it.Value())
ok = it.Next()
require.False(t, ok)
err = rdv.Delete([]byte("key"))
require.NoError(t, err)
_, err = rdv.db.Get([]byte("key"), nil)
require.Error(t, err)
}

View File

@ -0,0 +1,52 @@
package persistence
import (
"database/sql"
"log"
"testing"
_ "github.com/mattn/go-sqlite3" // Blank import to register the sqlite3 driver
"github.com/status-im/go-waku/waku/v2/protocol/pb"
"github.com/stretchr/testify/require"
)
func NewMock() *sql.DB {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
return db
}
func TestDbStore(t *testing.T) {
db := NewMock()
option := WithDB(db)
store, err := NewDBStore(option)
require.NoError(t, err)
res, err := store.GetAll()
require.NoError(t, err)
require.Empty(t, res)
err = store.Put(
&pb.Index{
Digest: []byte("digest"),
ReceiverTime: 1.0,
SenderTime: 1.0,
},
"test",
&pb.WakuMessage{
Payload: []byte("payload"),
ContentTopic: "contenttopic",
Version: 1,
Timestamp: 1.0,
Proof: []byte("proof"),
},
)
require.NoError(t, err)
res, err = store.GetAll()
require.NoError(t, err)
require.NotEmpty(t, res)
}