Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions internal/handlers/search_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ func TestSearch_TransactionHash(t *testing.T) {

txHash := "0x1234567890123456789012345678901234567890123456789012345678901234"

// Mock the 3 GetTransactions calls for different time ranges
// 1. Past 5 days (startOffsetDays=5, endOffsetDays=0)
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash
filter.FilterParams["hash"] == txHash &&
filter.FilterParams["block_timestamp_gte"] != "" &&
filter.FilterParams["block_timestamp_lte"] == ""
})).Return(storage.QueryResult[common.Transaction]{
Data: []common.Transaction{{
Hash: txHash,
Expand All @@ -84,9 +88,33 @@ func TestSearch_TransactionHash(t *testing.T) {
}},
}, nil)

// Mock other necessary calls for block and logs search
mockStorage.EXPECT().GetBlocks(mock.Anything).Return(storage.QueryResult[common.Block]{}, nil)
mockStorage.EXPECT().GetLogs(mock.Anything).Return(storage.QueryResult[common.Log]{}, nil)
// 2. 5-30 days (startOffsetDays=30, endOffsetDays=5)
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash &&
filter.FilterParams["block_timestamp_gte"] != "" &&
filter.FilterParams["block_timestamp_lte"] != ""
})).Return(storage.QueryResult[common.Transaction]{}, nil)

// 3. More than 30 days (startOffsetDays=0, endOffsetDays=30)
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash &&
filter.FilterParams["block_timestamp_gte"] == "" &&
filter.FilterParams["block_timestamp_lte"] != ""
})).Return(storage.QueryResult[common.Transaction]{}, nil)

// Mock the GetBlocks call for block hash search
mockStorage.EXPECT().GetBlocks(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash
})).Return(storage.QueryResult[common.Block]{}, nil)

// Mock the GetLogs call for topic_0 search
mockStorage.EXPECT().GetLogs(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.Signature == txHash
})).Return(storage.QueryResult[common.Log]{}, nil)

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/v1/search/1/"+txHash, nil)
Expand All @@ -112,7 +140,7 @@ func TestSearch_Address(t *testing.T) {
router, mockStorage := setupTestRouter()

address := "0x1234567890123456789012345678901234567890"
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.ContractAddress == address
})).Return(storage.QueryResult[common.Transaction]{
Expand All @@ -126,7 +154,7 @@ func TestSearch_Address(t *testing.T) {
}},
}, nil)

mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FromAddress == address
})).Return(storage.QueryResult[common.Transaction]{
Expand Down Expand Up @@ -164,7 +192,7 @@ func TestSearch_Contract(t *testing.T) {
router, mockStorage := setupTestRouter()

address := "0x1234567890123456789012345678901234567890"
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.ContractAddress == address
})).Return(storage.QueryResult[common.Transaction]{
Expand Down Expand Up @@ -204,7 +232,7 @@ func TestSearch_FunctionSignature(t *testing.T) {
router, mockStorage := setupTestRouter()

signature := "0x12345678"
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.Signature == signature
})).Return(storage.QueryResult[common.Transaction]{
Expand Down