-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: integration test for database (#1)
* Tests: integration test for database * Fix: remove unused test * Fix: close fixtures database connection * Fix: close database connection * Upgrade libraries and change ORM
- Loading branch information
1 parent
18c3654
commit 34b7497
Showing
18 changed files
with
593 additions
and
369 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
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
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
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
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
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
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
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,8 @@ | ||
- id: 1 | ||
hash: 0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7 | ||
- id: 2 | ||
hash: 0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8 | ||
- id: 3 | ||
hash: 0x04eafcdde3136a2db2aec1de26e43ac9e04b11d6a9e879c92cb2f9ef1f0be815 | ||
- id: 4 | ||
hash: 0x046bfa580e4fa55a38eaa7f51a3469f86b336eed59a6136a07b7adcd095b0eb2 |
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,4 @@ | ||
- id: 1 | ||
name: test | ||
last_height: 100 | ||
last_time: '2023-08-15T11:29:06+00:00' |
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,44 @@ | ||
- id: 1 | ||
created_at: 1691750161 | ||
updated_at: 1691750161 | ||
update_id: 10 | ||
contract_id: 1 | ||
token_id: 0 | ||
type: erc20 | ||
status: new | ||
attempts: 0 | ||
- id: 2 | ||
created_at: 1691750161 | ||
updated_at: 1691750161 | ||
update_id: 11 | ||
contract_id: 2 | ||
token_id: 0 | ||
type: erc20 | ||
status: filled | ||
uri: token_2_uri | ||
attempts: 1 | ||
- id: 3 | ||
created_at: 1691750161 | ||
updated_at: 1691750161 | ||
update_id: 12 | ||
contract_id: 3 | ||
token_id: 1 | ||
type: erc721 | ||
status: success | ||
uri: token_2_uri | ||
metadata: | ||
name: Token 1 | ||
symbol: TKN | ||
decimals: 18 | ||
attempts: 1 | ||
- id: 4 | ||
created_at: 1691750161 | ||
updated_at: 1691750161 | ||
update_id: 13 | ||
contract_id: 4 | ||
token_id: 14 | ||
type: erc1155 | ||
status: failed | ||
uri: token_3_uri | ||
attempts: 5 | ||
error: invalid URI |
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
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,126 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
||
"github.com/dipdup-io/starknet-metadata/internal/storage" | ||
"github.com/dipdup-net/go-lib/config" | ||
"github.com/dipdup-net/go-lib/database" | ||
"github.com/go-testfixtures/testfixtures/v3" | ||
_ "github.com/lib/pq" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type TestSuite struct { | ||
suite.Suite | ||
psqlContainer *database.PostgreSQLContainer | ||
storage Storage | ||
} | ||
|
||
// SetupSuite - | ||
func (s *TestSuite) SetupSuite() { | ||
ctx, ctxCancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer ctxCancel() | ||
|
||
psqlContainer, err := database.NewPostgreSQLContainer(ctx, database.PostgreSQLContainerConfig{ | ||
User: "user", | ||
Password: "password", | ||
Database: "db_test", | ||
Port: 5432, | ||
Image: "postgres:15", | ||
}) | ||
s.Require().NoError(err) | ||
s.psqlContainer = psqlContainer | ||
|
||
s.storage, err = Create(ctx, config.Database{ | ||
Kind: config.DBKindPostgres, | ||
User: s.psqlContainer.Config.User, | ||
Database: s.psqlContainer.Config.Database, | ||
Password: s.psqlContainer.Config.Password, | ||
Host: s.psqlContainer.Config.Host, | ||
Port: s.psqlContainer.MappedPort().Int(), | ||
}) | ||
s.Require().NoError(err) | ||
|
||
db, err := sql.Open("postgres", s.psqlContainer.GetDSN()) | ||
s.Require().NoError(err) | ||
|
||
fixtures, err := testfixtures.New( | ||
testfixtures.Database(db), | ||
testfixtures.Dialect("postgres"), | ||
testfixtures.Directory("fixtures"), | ||
) | ||
s.Require().NoError(err) | ||
s.Require().NoError(fixtures.Load()) | ||
s.Require().NoError(db.Close()) | ||
} | ||
|
||
func (s *TestSuite) TearDownSuite() { | ||
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer ctxCancel() | ||
|
||
s.Require().NoError(s.storage.Close()) | ||
s.Require().NoError(s.psqlContainer.Terminate(ctx)) | ||
} | ||
|
||
func (s *TestSuite) TestTokenMetadataGetByStatus() { | ||
for _, status := range []storage.Status{ | ||
storage.StatusFailed, | ||
storage.StatusFilled, | ||
storage.StatusNew, | ||
storage.StatusSuccess, | ||
} { | ||
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer ctxCancel() | ||
response, err := s.storage.TokenMetadata.GetByStatus(ctx, status, 1, 0, 0, 0) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Len(response, 1) | ||
s.Require().Equal(response[0].Status, status) | ||
s.Require().Len(response[0].Contract.Hash, 32) | ||
} | ||
} | ||
|
||
func (s *TestSuite) TestStateByName() { | ||
|
||
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer ctxCancel() | ||
|
||
response, err := s.storage.State.ByName(ctx, "test") | ||
s.Require().NoError(err) | ||
s.Require().EqualValues(1, response.ID) | ||
s.Require().EqualValues(100, response.LastHeight) | ||
|
||
_, err = s.storage.State.ByName(ctx, "unknown") | ||
s.Require().Error(err) | ||
} | ||
|
||
func (s *TestSuite) TestTxSaveAddress() { | ||
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer ctxCancel() | ||
|
||
tx, err := BeginTransaction(ctx, s.storage.Transactable) | ||
s.Require().NoError(err) | ||
defer tx.Close(ctx) | ||
|
||
err = tx.SaveAddress(ctx, &storage.Address{ | ||
ID: 100, | ||
Hash: []byte{0, 1, 2, 3, 4}, | ||
}) | ||
s.Require().NoError(err) | ||
|
||
err = tx.Flush(ctx) | ||
s.Require().NoError(err) | ||
|
||
response, err := s.storage.Address.GetByID(ctx, 100) | ||
s.Require().NoError(err) | ||
s.Require().EqualValues(100, response.ID) | ||
s.Require().Equal([]byte{0, 1, 2, 3, 4}, response.Hash) | ||
} | ||
|
||
func TestSuite_Run(t *testing.T) { | ||
suite.Run(t, new(TestSuite)) | ||
} |
Oops, something went wrong.