-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(eventindexer): db tests (#14682)
Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com>
- Loading branch information
1 parent
4ebd701
commit 55568df
Showing
6 changed files
with
2,333 additions
and
637 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.