Skip to content

Commit

Permalink
unit tests for lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Nov 21, 2024
1 parent a7b8b8b commit 7332617
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241112140826-0e2daed34ef6
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
github.com/stretchr/testify v1.9.0
github.com/test-go/testify v1.1.4
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/sync v0.8.0
Expand Down
137 changes: 137 additions & 0 deletions pkg/solana/chainwriter/lookups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package chainwriter_test

import (
"testing"

"github.com/gagliardetto/solana-go"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/chainwriter"
"github.com/test-go/testify/require"
)

type TestArgs struct {
Inner []InnerArgs
}

type InnerArgs struct {
Address []byte
}

func TestAccountContant(t *testing.T) {

t.Run("AccountConstant resolves valid address", func(t *testing.T) {
expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M"
expectedMeta := []*solana.AccountMeta{
{
PublicKey: solana.MustPublicKeyFromBase58(expectedAddr),
IsSigner: true,
IsWritable: true,
},
}
constantConfig := chainwriter.AccountConstant{
Name: "TestAccount",
Address: expectedAddr,
IsSigner: true,
IsWritable: true,
}
result, err := constantConfig.Resolve(nil, nil, nil, "")
require.NoError(t, err)
require.Equal(t, expectedMeta, result)
})
}
func TestAccountLookups(t *testing.T) {
t.Run("AccountLookup resolves valid address with just one address", func(t *testing.T) {
expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M"
testArgs := TestArgs{
Inner: []InnerArgs{
{Address: solana.MustPublicKeyFromBase58(expectedAddr).Bytes()},
},
}
expectedMeta := []*solana.AccountMeta{
{
PublicKey: solana.MustPublicKeyFromBase58(expectedAddr),
IsSigner: true,
IsWritable: true,
},
}

lookupConfig := chainwriter.AccountLookup{
Name: "TestAccount",
Location: "Inner.Address",
IsSigner: true,
IsWritable: true,
}
result, err := lookupConfig.Resolve(nil, testArgs, nil, "")
require.NoError(t, err)
require.Equal(t, expectedMeta, result)
})

t.Run("AccountLookup resolves valid address with just multiple addresses", func(t *testing.T) {
expectedAddr1 := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M"
expectedAddr2 := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6N"
testArgs := TestArgs{
Inner: []InnerArgs{
{Address: solana.MustPublicKeyFromBase58(expectedAddr1).Bytes()},
{Address: solana.MustPublicKeyFromBase58(expectedAddr2).Bytes()},
},
}
expectedMeta := []*solana.AccountMeta{
{
PublicKey: solana.MustPublicKeyFromBase58(expectedAddr1),
IsSigner: true,
IsWritable: true,
},
{
PublicKey: solana.MustPublicKeyFromBase58(expectedAddr2),
IsSigner: true,
IsWritable: true,
},
}

lookupConfig := chainwriter.AccountLookup{
Name: "TestAccount",
Location: "Inner.Address",
IsSigner: true,
IsWritable: true,
}
result, err := lookupConfig.Resolve(nil, testArgs, nil, "")
require.NoError(t, err)
for i, meta := range result {
require.Equal(t, expectedMeta[i], meta)
}
})

t.Run("AccountLookup fails when address isn't in args", func(t *testing.T) {
expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M"
testArgs := TestArgs{
Inner: []InnerArgs{
{Address: solana.MustPublicKeyFromBase58(expectedAddr).Bytes()},
},
}
lookupConfig := chainwriter.AccountLookup{
Name: "InvalidAccount",
Location: "Invalid.Directory",
IsSigner: true,
IsWritable: true,
}
_, err := lookupConfig.Resolve(nil, testArgs, nil, "")
require.Error(t, err)
})
}

func TestPDALookups(t *testing.T) {
t.Run("PDALookup resolves valid address", func(t *testing.T) {
expectedAddr := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6M"
expectedMeta := []*solana.AccountMeta{
{
PublicKey: solana.MustPublicKeyFromBase58(expectedAddr),
IsSigner: true,
IsWritable: true,
},
}
lookupConfig := chainwriter.PDALookups{
Name: "TestAccount",
PublicKey:
}

Check failure on line 134 in pkg/solana/chainwriter/lookups_test.go

View workflow job for this annotation

GitHub Actions / Relay Run Unit Tests

expected operand, found '}'

})
}

0 comments on commit 7332617

Please sign in to comment.