2019-04-30 06:46:12 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
|
|
|
|
statusproto "github.com/status-im/status-protocol-go/types"
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTopicHistoryStoreLoadFromKey(t *testing.T) {
|
|
|
|
db, err := NewMemoryDBNamespace(TopicHistoryBucket)
|
|
|
|
require.NoError(t, err)
|
|
|
|
th := TopicHistory{
|
|
|
|
db: db,
|
2019-10-09 14:22:53 +00:00
|
|
|
Topic: whispertypes.TopicType{1, 1, 1},
|
2019-04-30 06:46:12 +00:00
|
|
|
Duration: 10 * time.Hour,
|
|
|
|
}
|
|
|
|
require.NoError(t, th.Save())
|
|
|
|
now := time.Now()
|
|
|
|
th.Current = now
|
|
|
|
require.NoError(t, th.Save())
|
|
|
|
|
|
|
|
th, err = LoadTopicHistoryFromKey(db, th.Key())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, now.Unix(), th.Current.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopicHistorySameRange(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
result bool
|
|
|
|
histories [2]TopicHistory
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "SameDurationCurrentNotSet",
|
|
|
|
result: true,
|
|
|
|
histories: [2]TopicHistory{
|
|
|
|
{Duration: time.Minute}, {Duration: time.Minute},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "DifferentDurationCurrentNotset",
|
|
|
|
result: false,
|
|
|
|
histories: [2]TopicHistory{
|
|
|
|
{Duration: time.Minute}, {Duration: time.Hour},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "SameCurrent",
|
|
|
|
result: true,
|
|
|
|
histories: [2]TopicHistory{
|
|
|
|
{Current: now}, {Current: now},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "DifferentCurrent",
|
|
|
|
result: false,
|
|
|
|
histories: [2]TopicHistory{
|
|
|
|
{Current: now}, {Current: now.Add(time.Hour)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
require.Equal(t, tc.result, tc.histories[0].SameRange(tc.histories[1]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddHistory(t *testing.T) {
|
2019-10-09 14:22:53 +00:00
|
|
|
topic := whispertypes.TopicType{1, 1, 1}
|
2019-04-30 06:46:12 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
topicdb, err := NewMemoryDBNamespace(TopicHistoryBucket)
|
|
|
|
require.NoError(t, err)
|
|
|
|
requestdb, err := NewMemoryDBNamespace(HistoryRequestBucket)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
th := TopicHistory{db: topicdb, Topic: topic, Current: now}
|
2019-10-09 14:22:53 +00:00
|
|
|
id := statusproto.Hash{1}
|
2019-04-30 06:46:12 +00:00
|
|
|
|
|
|
|
req := HistoryRequest{requestDB: requestdb, topicDB: topicdb, ID: id}
|
|
|
|
req.AddHistory(th)
|
|
|
|
require.NoError(t, req.Save())
|
|
|
|
|
|
|
|
req = HistoryRequest{requestDB: requestdb, topicDB: topicdb, ID: id}
|
|
|
|
require.NoError(t, req.Load())
|
|
|
|
|
|
|
|
require.Len(t, req.Histories(), 1)
|
|
|
|
require.Equal(t, th.Topic, req.Histories()[0].Topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestIncludesMethod(t *testing.T) {
|
2019-10-09 14:22:53 +00:00
|
|
|
topicOne := whispertypes.TopicType{1}
|
|
|
|
topicTwo := whispertypes.TopicType{2}
|
2019-04-30 06:46:12 +00:00
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
result bool
|
|
|
|
topics []TopicHistory
|
|
|
|
input TopicHistory
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "EmptyTopic",
|
|
|
|
result: false,
|
|
|
|
input: TopicHistory{Topic: topicOne},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "MatchesTopic",
|
|
|
|
result: true,
|
|
|
|
topics: []TopicHistory{{Topic: topicOne}},
|
|
|
|
input: TopicHistory{Topic: topicOne},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "NotMatchesTopic",
|
|
|
|
result: false,
|
|
|
|
topics: []TopicHistory{{Topic: topicOne}},
|
|
|
|
input: TopicHistory{Topic: topicTwo},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
req := HistoryRequest{}
|
|
|
|
for _, t := range tc.topics {
|
|
|
|
req.AddHistory(t)
|
|
|
|
}
|
|
|
|
require.Equal(t, tc.result, req.Includes(tc.input))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|