github.com/status-im/status-go@v1.1.0/db/history_test.go (about)

     1  package db
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/status-im/status-go/eth-node/types"
    10  )
    11  
    12  func TestTopicHistoryStoreLoadFromKey(t *testing.T) {
    13  	db, err := NewMemoryDBNamespace(TopicHistoryBucket)
    14  	require.NoError(t, err)
    15  	th := TopicHistory{
    16  		db:       db,
    17  		Topic:    types.TopicType{1, 1, 1},
    18  		Duration: 10 * time.Hour,
    19  	}
    20  	require.NoError(t, th.Save())
    21  	now := time.Now()
    22  	th.Current = now
    23  	require.NoError(t, th.Save())
    24  
    25  	th, err = LoadTopicHistoryFromKey(db, th.Key())
    26  	require.NoError(t, err)
    27  	require.Equal(t, now.Unix(), th.Current.Unix())
    28  }
    29  
    30  func TestTopicHistorySameRange(t *testing.T) {
    31  	now := time.Now()
    32  	testCases := []struct {
    33  		description string
    34  		result      bool
    35  		histories   [2]TopicHistory
    36  	}{
    37  		{
    38  			description: "SameDurationCurrentNotSet",
    39  			result:      true,
    40  			histories: [2]TopicHistory{
    41  				{Duration: time.Minute}, {Duration: time.Minute},
    42  			},
    43  		},
    44  		{
    45  			description: "DifferentDurationCurrentNotset",
    46  			result:      false,
    47  			histories: [2]TopicHistory{
    48  				{Duration: time.Minute}, {Duration: time.Hour},
    49  			},
    50  		},
    51  		{
    52  			description: "SameCurrent",
    53  			result:      true,
    54  			histories: [2]TopicHistory{
    55  				{Current: now}, {Current: now},
    56  			},
    57  		},
    58  		{
    59  			description: "DifferentCurrent",
    60  			result:      false,
    61  			histories: [2]TopicHistory{
    62  				{Current: now}, {Current: now.Add(time.Hour)},
    63  			},
    64  		},
    65  	}
    66  	for _, tc := range testCases {
    67  		t.Run(tc.description, func(t *testing.T) {
    68  			require.Equal(t, tc.result, tc.histories[0].SameRange(tc.histories[1]))
    69  		})
    70  	}
    71  }
    72  
    73  func TestAddHistory(t *testing.T) {
    74  	topic := types.TopicType{1, 1, 1}
    75  	now := time.Now()
    76  
    77  	topicdb, err := NewMemoryDBNamespace(TopicHistoryBucket)
    78  	require.NoError(t, err)
    79  	requestdb, err := NewMemoryDBNamespace(HistoryRequestBucket)
    80  	require.NoError(t, err)
    81  
    82  	th := TopicHistory{db: topicdb, Topic: topic, Current: now}
    83  	id := types.Hash{1}
    84  
    85  	req := HistoryRequest{requestDB: requestdb, topicDB: topicdb, ID: id}
    86  	req.AddHistory(th)
    87  	require.NoError(t, req.Save())
    88  
    89  	req = HistoryRequest{requestDB: requestdb, topicDB: topicdb, ID: id}
    90  	require.NoError(t, req.Load())
    91  
    92  	require.Len(t, req.Histories(), 1)
    93  	require.Equal(t, th.Topic, req.Histories()[0].Topic)
    94  }
    95  
    96  func TestRequestIncludesMethod(t *testing.T) {
    97  	topicOne := types.TopicType{1}
    98  	topicTwo := types.TopicType{2}
    99  	testCases := []struct {
   100  		description string
   101  		result      bool
   102  		topics      []TopicHistory
   103  		input       TopicHistory
   104  	}{
   105  		{
   106  			description: "EmptyTopic",
   107  			result:      false,
   108  			input:       TopicHistory{Topic: topicOne},
   109  		},
   110  		{
   111  			description: "MatchesTopic",
   112  			result:      true,
   113  			topics:      []TopicHistory{{Topic: topicOne}},
   114  			input:       TopicHistory{Topic: topicOne},
   115  		},
   116  		{
   117  			description: "NotMatchesTopic",
   118  			result:      false,
   119  			topics:      []TopicHistory{{Topic: topicOne}},
   120  			input:       TopicHistory{Topic: topicTwo},
   121  		},
   122  	}
   123  
   124  	for _, tc := range testCases {
   125  		t.Run(tc.description, func(t *testing.T) {
   126  			req := HistoryRequest{}
   127  			for _, t := range tc.topics {
   128  				req.AddHistory(t)
   129  			}
   130  			require.Equal(t, tc.result, req.Includes(tc.input))
   131  		})
   132  	}
   133  }