2019-04-30 06:46:12 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-04-30 06:46:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createInMemStore(t *testing.T) HistoryStore {
|
|
|
|
db, err := NewMemoryDB()
|
|
|
|
require.NoError(t, err)
|
2019-05-06 06:33:19 +00:00
|
|
|
return NewHistoryStore(LevelDBStorage{db: db})
|
2019-04-30 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetNewHistory(t *testing.T) {
|
2019-11-23 17:57:05 +00:00
|
|
|
topic := types.TopicType{1}
|
2019-04-30 06:46:12 +00:00
|
|
|
duration := time.Hour
|
|
|
|
store := createInMemStore(t)
|
|
|
|
th, err := store.GetHistory(topic, duration)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, duration, th.Duration)
|
|
|
|
require.Equal(t, topic, th.Topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetExistingHistory(t *testing.T) {
|
2019-11-23 17:57:05 +00:00
|
|
|
topic := types.TopicType{1}
|
2019-04-30 06:46:12 +00:00
|
|
|
duration := time.Hour
|
|
|
|
store := createInMemStore(t)
|
|
|
|
th, err := store.GetHistory(topic, duration)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
th.Current = now
|
|
|
|
require.NoError(t, th.Save())
|
|
|
|
|
|
|
|
th, err = store.GetHistory(topic, duration)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, now.Unix(), th.Current.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewHistoryRequest(t *testing.T) {
|
|
|
|
store := createInMemStore(t)
|
2019-11-23 17:57:05 +00:00
|
|
|
id := types.Hash{1}
|
2022-03-28 10:10:40 +00:00
|
|
|
_, err := store.GetRequest(id)
|
2019-04-30 06:46:12 +00:00
|
|
|
require.Error(t, err)
|
2022-03-28 10:10:40 +00:00
|
|
|
req := store.NewRequest()
|
2019-04-30 06:46:12 +00:00
|
|
|
req.ID = id
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
th, err := store.GetHistory(types.TopicType{1}, time.Hour)
|
2019-04-30 06:46:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
req.AddHistory(th)
|
|
|
|
require.NoError(t, req.Save())
|
|
|
|
|
|
|
|
req, err = store.GetRequest(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, req.Histories(), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAllRequests(t *testing.T) {
|
|
|
|
store := createInMemStore(t)
|
2019-11-23 17:57:05 +00:00
|
|
|
idOne := types.Hash{1}
|
|
|
|
idTwo := types.Hash{2}
|
2019-04-30 06:46:12 +00:00
|
|
|
|
|
|
|
req := store.NewRequest()
|
|
|
|
req.ID = idOne
|
|
|
|
require.NoError(t, req.Save())
|
|
|
|
|
|
|
|
all, err := store.GetAllRequests()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, all, 1)
|
|
|
|
|
|
|
|
req = store.NewRequest()
|
|
|
|
req.ID = idTwo
|
|
|
|
require.NoError(t, req.Save())
|
|
|
|
|
|
|
|
all, err = store.GetAllRequests()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, all, 2)
|
|
|
|
}
|