status-go/mailserver/mailserver_db_leveldb_test.go
2025-01-17 10:14:54 +01:00

49 lines
1.4 KiB
Go

package mailserver
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/rlp"
"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
wakuv1common "github.com/status-im/status-go/wakuv1/common"
)
func TestLevelDB_BuildIteratorWithTopic(t *testing.T) {
topic := []byte{0x01, 0x02, 0x03, 0x04}
db, err := NewLevelDB(t.TempDir())
require.NoError(t, err)
envelope, err := newTestEnvelope(topic)
require.NoError(t, err)
err = db.SaveEnvelope(envelope)
require.NoError(t, err)
iter, err := db.BuildIterator(CursorQuery{
start: NewDBKey(uint32(time.Now().Add(-time.Hour).Unix()), wakutypes.BytesToTopic(topic), types.Hash{}).Bytes(),
end: NewDBKey(uint32(time.Now().Add(time.Second).Unix()), wakutypes.BytesToTopic(topic), types.Hash{}).Bytes(),
topics: [][]byte{topic},
limit: 10,
})
topicsMap := make(map[wakutypes.TopicType]bool)
topicsMap[wakutypes.BytesToTopic(topic)] = true
require.NoError(t, err)
hasNext := iter.Next()
require.True(t, hasNext)
rawValue, err := iter.GetEnvelopeByTopicsMap(topicsMap)
require.NoError(t, err)
require.NotEmpty(t, rawValue)
var receivedEnvelope wakuv1common.Envelope
err = rlp.DecodeBytes(rawValue, &receivedEnvelope)
require.NoError(t, err)
require.EqualValues(t, wakuv1common.BytesToTopic(topic), receivedEnvelope.Topic)
err = iter.Release()
require.NoError(t, err)
require.NoError(t, iter.Error())
}