From 10378a5827d0b0cea312546d2a9a57bbe71085ec Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 21 Oct 2021 15:14:14 +0200 Subject: [PATCH] test: Add persistence module test --- waku/persistence/rendezvous_test.go | 34 +++++++++++++++++++ waku/persistence/store_test.go | 52 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 waku/persistence/rendezvous_test.go create mode 100644 waku/persistence/store_test.go diff --git a/waku/persistence/rendezvous_test.go b/waku/persistence/rendezvous_test.go new file mode 100644 index 00000000..d9ab0884 --- /dev/null +++ b/waku/persistence/rendezvous_test.go @@ -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) +} diff --git a/waku/persistence/store_test.go b/waku/persistence/store_test.go new file mode 100644 index 00000000..87bd35db --- /dev/null +++ b/waku/persistence/store_test.go @@ -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) +}