Skip to content

Commit

Permalink
chore: primitive contract tests (#628)
Browse files Browse the repository at this point in the history
* Refactor: streamline test setup and address creation

The commit refactors the test setup by consolidating Ethereum address creation and signer assignment, improving code clarity and reducing redundancy. This change ensures consistent address handling across various test cases while leveraging a new helper function for setting up platforms with specific signers.

* Apply suggestions from code review

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
outerlook and coderabbitai[bot] authored Sep 24, 2024
1 parent bf12535 commit d8ed159
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 124 deletions.
44 changes: 23 additions & 21 deletions internal/contracts/tests/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

"github.com/truflation/tsn-db/internal/contracts/tests/utils/procedure"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -58,7 +60,7 @@ func testMetadataInsertionAndRetrieval(t *testing.T, contractInfo ContractInfo)
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Insert metadata of various types
metadataItems := []struct {
Expand Down Expand Up @@ -126,7 +128,7 @@ func testOnlyOwnerCanInsertMetadata(t *testing.T, contractInfo ContractInfo) kwi
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Change the deployer to a non-owner
nonOwner := util.Unsafe_NewEthereumAddressFromString("0x9999999999999999999999999999999999999999")
Expand Down Expand Up @@ -155,7 +157,7 @@ func testDisableMetadata(t *testing.T, contractInfo ContractInfo) kwilTesting.Te
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Insert metadata
key := "temp_key"
Expand Down Expand Up @@ -204,7 +206,7 @@ func testReadOnlyMetadataCannotBeModified(t *testing.T, contractInfo ContractInf
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Attempt to insert metadata with a read-only key
err := insertMetadata(ctx, platform, contractInfo.Deployer, dbid, "type", "modified", "string")
Expand Down Expand Up @@ -240,7 +242,7 @@ func testOwnershipTransfer(t *testing.T, contractInfo ContractInfo) kwilTesting.
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Transfer ownership
newOwner := "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Expand Down Expand Up @@ -281,7 +283,7 @@ func testInitializationLogic(t *testing.T, contractInfo ContractInfo) kwilTestin
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Attempt to re-initialize the contract
_, err := platform.Engine.Procedure(ctx, platform.DB, &common.ExecutionData{
Expand Down Expand Up @@ -316,7 +318,7 @@ func testVisibilitySettings(t *testing.T, contractInfo ContractInfo) kwilTesting
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Change read_visibility to private (1)
err := insertMetadata(ctx, platform, contractInfo.Deployer, dbid, "read_visibility", "1", "int")
Expand Down Expand Up @@ -353,7 +355,7 @@ func testInvalidEthereumAddressHandling(t *testing.T, contractInfo ContractInfo)
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Attempt to transfer ownership to an invalid address
invalidAddress := "invalid_address"
Expand All @@ -380,7 +382,7 @@ func testPermissionsAfterVisibilityChange(t *testing.T, contractInfo ContractInf
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Initially, anyone should be able to read
nonOwner := util.Unsafe_NewEthereumAddressFromString("0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
Expand All @@ -402,7 +404,7 @@ func testPermissionsAfterVisibilityChange(t *testing.T, contractInfo ContractInf
// Change back to non-owner
platform.Deployer = nonOwner.Bytes()

canRead, err = checkReadPermissions(ctx, platform, contractInfo.Deployer, dbid, nonOwner.Address())
canRead, err = checkReadPermissions(ctx, procedure.WithSigner(platform, nonOwner.Bytes()), contractInfo.Deployer, dbid, nonOwner.Address())
assert.False(t, canRead, "Non-owner should not be able to read when read_visibility is private")
assert.NoError(t, err, "Error should not be returned when checking read permissions")

Expand All @@ -426,7 +428,7 @@ func testIsStreamAllowedToCompose(t *testing.T, contractInfo ContractInfo) kwilT
if err := setupAndInitializeContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)

// Set up a foreign contract (the one attempting to compose)
foreignContractInfo := ContractInfo{
Expand All @@ -441,14 +443,10 @@ func testIsStreamAllowedToCompose(t *testing.T, contractInfo ContractInfo) kwilT
}

// set compose_visibility to private (1)
err := insertMetadata(ctx, platform, contractInfo.Deployer, dbid, "compose_visibility", "1", "int")
if err != nil {
return errors.Wrap(err, "Failed to change compose_visibility")
}

foreignDbid := getDBID(foreignContractInfo, platform)
foreignDbid := getDBID(foreignContractInfo)

canCompose, err := checkComposePermissions(ctx, platform, contractInfo.Deployer, dbid, foreignDbid)
canCompose, err := checkComposePermissions(ctx, platform, dbid, foreignDbid)
assert.False(t, canCompose, "Foreign stream should not be allowed to compose without permission")
assert.Error(t, err, "Expected permission error when composing without permission")

Expand All @@ -461,7 +459,7 @@ func testIsStreamAllowedToCompose(t *testing.T, contractInfo ContractInfo) kwilT
// Attempt to compose again with permission
platform.Deployer = foreignContractInfo.Deployer.Bytes()

canCompose, err = checkComposePermissions(ctx, platform, contractInfo.Deployer, dbid, foreignDbid)
canCompose, err = checkComposePermissions(ctx, platform, dbid, foreignDbid)
assert.True(t, canCompose, "Foreign stream should be allowed to compose after permission is granted")
assert.NoError(t, err, "No error expected when composing with permission")

Expand All @@ -475,7 +473,7 @@ func setupAndInitializeContract(ctx context.Context, platform *kwilTesting.Platf
if err := setupContract(ctx, platform, contractInfo); err != nil {
return err
}
dbid := getDBID(contractInfo, platform)
dbid := getDBID(contractInfo)
return initializeContract(ctx, platform, dbid, contractInfo.Deployer)
}

Expand Down Expand Up @@ -509,7 +507,7 @@ func initializeContract(ctx context.Context, platform *kwilTesting.Platform, dbi
return err
}

func getDBID(contractInfo ContractInfo, platform *kwilTesting.Platform) string {
func getDBID(contractInfo ContractInfo) string {
return utils.GenerateDBID(contractInfo.StreamID.String(), contractInfo.Deployer.Bytes())
}

Expand Down Expand Up @@ -600,7 +598,11 @@ func checkReadPermissions(ctx context.Context, platform *kwilTesting.Platform, d
return result.Rows[0][0].(bool), nil
}

func checkComposePermissions(ctx context.Context, platform *kwilTesting.Platform, deployer util.EthereumAddress, dbid string, foreignCaller string) (bool, error) {
func checkComposePermissions(ctx context.Context, platform *kwilTesting.Platform, dbid string, foreignCaller string) (bool, error) {
deployer, err := util.NewEthereumAddressFromBytes(platform.Deployer)
if err != nil {
return false, err
}
result, err := platform.Engine.Procedure(ctx, platform.DB, &common.ExecutionData{
Procedure: "is_stream_allowed_to_compose",
Dataset: dbid,
Expand Down
7 changes: 3 additions & 4 deletions internal/contracts/tests/complex_composed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ func WithTestSetup(testFn func(ctx context.Context, platform *kwilTesting.Platfo

// Deploy the contracts here
err := setup.SetupComposedFromMarkdown(ctx, setup.MarkdownComposedSetupInput{
Platform: platform,
ComposedStreamName: composedStreamName,
Deployer: complexComposedDeployer,
Height: 1,
Platform: platform,
StreamId: composedStreamId.String(),
Height: 1,
MarkdownData: fmt.Sprintf(`
| date | %s | %s | %s |
| ---------- | ---- | ---- | ---- |
Expand Down
14 changes: 6 additions & 8 deletions internal/contracts/tests/composed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ func testComposedLastAvailable(t *testing.T) func(ctx context.Context, platform

// Setup data for the test
err := setup.SetupComposedFromMarkdown(ctx, setup.MarkdownComposedSetupInput{
Platform: platform,
ComposedStreamName: composedStreamName,
Deployer: composedDeployer,
Height: 1,
Platform: platform,
StreamId: composedStreamId.String(),
Height: 1,
MarkdownData: `
| date | Stream 1 | Stream 2 | Stream 3 |
| ---------- | --------- | --------- | --------- |
Expand Down Expand Up @@ -92,10 +91,9 @@ func testComposedNoPastData(t *testing.T) func(ctx context.Context, platform *kw

// Setup data for the test
err := setup.SetupComposedFromMarkdown(ctx, setup.MarkdownComposedSetupInput{
Platform: platform,
ComposedStreamName: composedStreamName,
Deployer: composedDeployer,
Height: 1,
Platform: platform,
StreamId: composedStreamId.String(),
Height: 1,
MarkdownData: `
| date | Stream 1 | Stream 2 | Stream 3 |
| ---------- | --------- | --------- | --------- |
Expand Down
39 changes: 21 additions & 18 deletions internal/contracts/tests/index_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ func testIndexChange(t *testing.T) func(ctx context.Context, platform *kwilTesti
streamName := "primitive_stream_db_name"
streamId := util.GenerateStreamId(streamName)
dbid := utils.GenerateDBID(streamId.String(), platform.Deployer)
deployer, err := util.NewEthereumAddressFromBytes(platform.Deployer)
if err != nil {
return errors.Wrap(err, "error creating ethereum address")
}

if err := setup.SetupPrimitiveFromMarkdown(ctx, setup.MarkdownPrimitiveSetupInput{
Platform: platform,
Height: 0,
PrimitiveStreamName: streamName,
Deployer: deployer,
Platform: platform,
StreamId: streamId,
Height: 0,

MarkdownData: `
| date | value |
|------------|--------|
Expand All @@ -73,13 +69,19 @@ func testIndexChange(t *testing.T) func(ctx context.Context, platform *kwilTesti
return errors.Wrap(err, "error setting up primitive stream")
}

signer, err := util.NewEthereumAddressFromBytes(platform.Deployer)
if err != nil {
return errors.Wrap(err, "error creating ethereum address")
}

// Get index change for 7 days with 1 day interval
result, err := platform.Engine.Procedure(ctx, platform.DB, &common.ExecutionData{
Procedure: "get_index_change",
Dataset: dbid,
Args: []any{"2023-01-02", "2023-01-08", nil, nil, 1},
TransactionData: common.TransactionData{
Signer: platform.Deployer,
Signer: signer.Bytes(),
Caller: signer.Address(),
TxID: platform.Txid(),
Height: 0,
},
Expand Down Expand Up @@ -125,10 +127,6 @@ func testYoYIndexChange(t *testing.T) func(ctx context.Context, platform *kwilTe
streamName := "primitive_stream_db_name"
streamId := util.GenerateStreamId(streamName)
dbid := utils.GenerateDBID(streamId.String(), platform.Deployer)
deployer, err := util.NewEthereumAddressFromBytes(platform.Deployer)
if err != nil {
return errors.Wrap(err, "error creating ethereum address")
}

/*
Here’s an example calculation for corn inflation for May 22nd 2023:
Expand All @@ -142,10 +140,9 @@ func testYoYIndexChange(t *testing.T) func(ctx context.Context, platform *kwilTe

// Insert test data for two years
if err := setup.SetupPrimitiveFromMarkdown(ctx, setup.MarkdownPrimitiveSetupInput{
Platform: platform,
Height: 0,
PrimitiveStreamName: streamName,
Deployer: deployer,
Platform: platform,
Height: 0,
StreamId: streamId,
MarkdownData: `
| date | value |
|------------|--------|
Expand All @@ -159,13 +156,19 @@ func testYoYIndexChange(t *testing.T) func(ctx context.Context, platform *kwilTe
return errors.Wrap(err, "error setting up primitive stream")
}

signer, err := util.NewEthereumAddressFromBytes(platform.Deployer)
if err != nil {
return errors.Wrap(err, "error creating ethereum address")
}

// Test YoY calculation
result, err := platform.Engine.Procedure(ctx, platform.DB, &common.ExecutionData{
Procedure: "get_index_change",
Dataset: dbid,
Args: []any{"2023-05-22", "2023-05-22", nil, nil, 365}, // 365 days interval for YoY
TransactionData: common.TransactionData{
Signer: platform.Deployer,
Signer: signer.Bytes(),
Caller: signer.Address(),
TxID: platform.Txid(),
Height: 0,
},
Expand Down
Loading

0 comments on commit d8ed159

Please sign in to comment.