Skip to content

Commit

Permalink
test(eventindexer): db tests (#14682)
Browse files Browse the repository at this point in the history
Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com>
  • Loading branch information
RogerLamTd and cyberhorsey authored Sep 19, 2023
1 parent 4ebd701 commit 55568df
Show file tree
Hide file tree
Showing 6 changed files with 2,333 additions and 637 deletions.
85 changes: 85 additions & 0 deletions packages/eventindexer/repo/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package repo

import (
"context"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
"github.com/taikoxyz/taiko-mono/packages/eventindexer/db"
)

func Test_NewAccountRepository(t *testing.T) {
tests := []struct {
name string
db eventindexer.DB
wantErr error
}{
{
"success",
&db.DB{},
nil,
},
{
"noDb",
nil,
eventindexer.ErrNoDB,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewAccountRepository(tt.db)
assert.Equal(t, tt.wantErr, err)
})
}
}

func TestIntegration_Account_Save(t *testing.T) {
db, close, err := testMysql(t)
assert.Equal(t, nil, err)

defer close()

accountRepo, err := NewAccountRepository(db)
assert.Equal(t, nil, err)

t1 := time.Now()
tests := []struct {
name string
acct eventindexer.Account
wantErr error
}{
{
"success",
eventindexer.Account{
ID: 0,
Address: "0x1234",
TransactedAt: t1,
},
nil,
},
{
"duplicate",
eventindexer.Account{
ID: 0,
Address: "0x1234",
TransactedAt: t1,
},
nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err = accountRepo.Save(
context.Background(),
common.HexToAddress(tt.acct.Address),
tt.acct.TransactedAt,
)
assert.Equal(t, tt.wantErr, err)
})
}
}
102 changes: 102 additions & 0 deletions packages/eventindexer/repo/block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package repo

import (
"context"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
"github.com/taikoxyz/taiko-mono/packages/eventindexer/db"
)

func Test_NewBlockRepo(t *testing.T) {
tests := []struct {
name string
db eventindexer.DB
wantErr error
}{
{
"success",
&db.DB{},
nil,
},
{
"noDb",
nil,
eventindexer.ErrNoDB,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewBlockRepository(tt.db)
assert.Equal(t, tt.wantErr, err)
})
}
}

func TestIntegration_RawBlock_Save(t *testing.T) {
db, close, err := testMysql(t)
assert.Equal(t, nil, err)

defer close()

blockRepo, err := NewBlockRepository(db)
assert.Equal(t, nil, err)

header := &types.Header{
ParentHash: common.HexToHash("0x3a537c89809712367218bb171b3b1c46aa95df3dee7200ae9dc78f4052024068"),
UncleHash: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000000"),
Root: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
TxHash: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
ReceiptHash: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
Bloom: types.Bloom{},
Difficulty: new(big.Int).SetInt64(2),
Number: new(big.Int).SetInt64(1),
GasLimit: 100000,
GasUsed: 2000,
Time: 1234,
Extra: []byte{0x7f},
MixDigest: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
Nonce: types.BlockNonce{0x13},
BaseFee: big.NewInt(10),
}
b := types.NewBlockWithHeader(header)

genesisHeader := &types.Header{
Time: 0,
}

b2 := types.NewBlockWithHeader(genesisHeader)

tests := []struct {
name string
block *types.Block
chainID *big.Int
wantErr error
}{
{
"success",
b,
big.NewInt(0),
nil,
},
{
"genesis",
b2,
big.NewInt(0),
nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := blockRepo.Save(context.Background(), tt.block, tt.chainID)
assert.Equal(t, tt.wantErr, err)
})
}
}
66 changes: 66 additions & 0 deletions packages/eventindexer/repo/chart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package repo

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/taikoxyz/taiko-mono/packages/eventindexer"
"github.com/taikoxyz/taiko-mono/packages/eventindexer/db"
)

func Test_NewChartRepo(t *testing.T) {
tests := []struct {
name string
db eventindexer.DB
wantErr error
}{
{
"success",
&db.DB{},
nil,
},
{
"noDb",
nil,
eventindexer.ErrNoDB,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewChartRepository(tt.db)
assert.Equal(t, tt.wantErr, err)
})
}
}

func Test_GetDB(t *testing.T) {
db, close, err := testMysql(t)
assert.Equal(t, nil, err)

defer close()

chartRepo, err := NewChartRepository(db)
assert.Equal(t, nil, err)
assert.NotNil(t, chartRepo.getDB())
}

func Test_Integration_FindChart(t *testing.T) {
db, close, err := testMysql(t)
assert.Equal(t, nil, err)

defer close()

chartRepo, err := NewChartRepository(db)
assert.Equal(t, nil, err)

chart, err := chartRepo.Find(
context.Background(),
"test",
"2023-09-08",
"2023-09-09",
)
assert.Equal(t, nil, err)
assert.Equal(t, 0, len(chart.Chart))
}
Loading

0 comments on commit 55568df

Please sign in to comment.