|
| 1 | +package sourcify |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | +) |
| 11 | + |
| 12 | +// SourcifySuite is a struct that embeds suite.Suite and contains fields necessary for |
| 13 | +// executing tests. This includes a Client to interact with the sourcify service, a list of |
| 14 | +// addresses to check, a list of chain IDs to use in testing, and specific instances |
| 15 | +// of an address and a chain ID. |
| 16 | +type SourcifySuite struct { |
| 17 | + suite.Suite |
| 18 | + client *Client |
| 19 | + Addresses []string |
| 20 | + ChainIDs []int |
| 21 | + SpecificChainID int |
| 22 | + SpecificAddress common.Address |
| 23 | +} |
| 24 | + |
| 25 | +// SetupTest initializes the test suite before each test. It creates a new Client with |
| 26 | +// specified options and initializes the addresses and chain IDs to be used in the tests. |
| 27 | +func (suite *SourcifySuite) SetupTest() { |
| 28 | + suite.client = NewClient( |
| 29 | + WithBaseURL("https://sourcify.dev/server"), |
| 30 | + WithRetryOptions( |
| 31 | + WithMaxRetries(3), |
| 32 | + WithDelay(2*time.Second), |
| 33 | + ), |
| 34 | + //WithRateLimit(20, 1*time.Second), |
| 35 | + ) |
| 36 | + suite.Addresses = []string{"0x054B2223509D430269a31De4AE2f335890be5C8F"} |
| 37 | + suite.ChainIDs = []int{1, 56, 137} |
| 38 | + suite.SpecificChainID = 56 // Add the specific chain ID for the two methods |
| 39 | + suite.SpecificAddress = common.HexToAddress("0x054B2223509D430269a31De4AE2f335890be5C8F") // Add the specific address for the two methods |
| 40 | +} |
| 41 | + |
| 42 | +// TestGetContractMetadata tests the GetContractMetadata function. It asserts that no error |
| 43 | +// is returned and the metadata is not nil. |
| 44 | +func (suite *SourcifySuite) TestGetContractMetadata() { |
| 45 | + // Act |
| 46 | + metadata, err := GetContractMetadata(suite.client, suite.SpecificChainID, suite.SpecificAddress, MethodMatchTypeFull) |
| 47 | + |
| 48 | + // Assert |
| 49 | + assert := assert.New(suite.T()) |
| 50 | + assert.NoError(err, "GetContractMetadata should not return an error") |
| 51 | + assert.NotNil(metadata, "metadata should not be nil") |
| 52 | +} |
| 53 | + |
| 54 | +// TestGetContractSourceCode tests the GetContractSourceCode function. It asserts that no error |
| 55 | +// is returned, the source code is not nil, and the length of the source code is 2. |
| 56 | +func (suite *SourcifySuite) TestGetContractSourceCode() { |
| 57 | + sourceCode, err := GetContractSourceCode(suite.client, suite.SpecificChainID, suite.SpecificAddress, MethodMatchTypeFull) |
| 58 | + |
| 59 | + assert := assert.New(suite.T()) |
| 60 | + assert.NoError(err, "Expected GetContractSourceCode to run without error") |
| 61 | + assert.NotNil(sourceCode, "source code should not be nil") |
| 62 | + assert.Equal(len(sourceCode.Code), 2, "Expected source code to have 2 files") |
| 63 | +} |
| 64 | + |
| 65 | +// TestGetChains tests the GetChains function. It asserts that no error is returned, the chains |
| 66 | +// are not nil, and the length of the chains is at least 98. |
| 67 | +func (suite *SourcifySuite) TestGetChains() { |
| 68 | + chains, err := GetChains(suite.client) |
| 69 | + |
| 70 | + assert := assert.New(suite.T()) |
| 71 | + assert.NoError(err, "Expected GetChains to run without error") |
| 72 | + assert.NotNil(chains, "source code should not be nil") |
| 73 | + assert.GreaterOrEqual(len(chains), 98, "Expected source code to have at least 98 chains") |
| 74 | +} |
| 75 | + |
| 76 | +// TestGetAvailableContractAddresses tests the GetAvailableContractAddresses function. It asserts |
| 77 | +// that no error is returned, the addresses are not nil, and the length of the full and partial addresses |
| 78 | +// are at least 1000 each. |
| 79 | +func (suite *SourcifySuite) TestGetAvailableContractAddresses() { |
| 80 | + // Act |
| 81 | + addresses, err := GetAvailableContractAddresses(suite.client, suite.SpecificChainID) |
| 82 | + |
| 83 | + // Assert |
| 84 | + assert := assert.New(suite.T()) |
| 85 | + assert.NoError(err, "GetAvailableContractAddresses should not return an error") |
| 86 | + assert.NotNil(addresses, "addresses should not be nil") |
| 87 | + assert.GreaterOrEqual(len(addresses.Full), 1000, "Expected source code to have at least 1000 addresses") |
| 88 | + assert.GreaterOrEqual(len(addresses.Partial), 1000, "Expected source code to have at least 1000 addresses") |
| 89 | +} |
| 90 | + |
| 91 | +// TestCheckContractByAddresses tests the CheckContractByAddresses function. It asserts that no error |
| 92 | +// is returned and the checks are not nil. |
| 93 | +func (suite *SourcifySuite) TestCheckContractByAddresses() { |
| 94 | + // Act |
| 95 | + checks, err := CheckContractByAddresses(suite.client, suite.Addresses, suite.ChainIDs, MethodMatchTypeFull) |
| 96 | + |
| 97 | + // Assert |
| 98 | + assert := assert.New(suite.T()) |
| 99 | + assert.NoError(err, "CheckContractByAddresses should not return an error") |
| 100 | + assert.NotNil(checks, "checks should not be nil") |
| 101 | +} |
| 102 | + |
| 103 | +// TestGetHealth tests the GetHealth function. It asserts that no error is returned and the status |
| 104 | +// returned is true. |
| 105 | +func (suite *SourcifySuite) TestGetHealth() { |
| 106 | + // Act |
| 107 | + status, err := GetHealth(suite.client) |
| 108 | + |
| 109 | + // Assert |
| 110 | + assert := assert.New(suite.T()) |
| 111 | + assert.NoError(err, "GetHealth should not return an error") |
| 112 | + assert.True(status, "status should be true") |
| 113 | +} |
| 114 | + |
| 115 | +// TestGetContractFiles tests the GetContractFiles function. It asserts that no error is returned, |
| 116 | +// the tree is not nil, and the length of the files is 2. |
| 117 | +func (suite *SourcifySuite) TestGetContractFiles() { |
| 118 | + tree, err := GetContractFiles(suite.client, suite.SpecificChainID, suite.SpecificAddress, MethodMatchTypeFull) |
| 119 | + |
| 120 | + assert := assert.New(suite.T()) |
| 121 | + assert.NoError(err, "Expected GetContractSourceCode to run without error") |
| 122 | + assert.NotNil(tree, "tree code should not be nil") |
| 123 | + assert.Equal(len(tree.Files), 2, "Expected tree to have 2 files") |
| 124 | +} |
| 125 | + |
| 126 | +// TestGetContractFiles tests the GetContractFiles function. It asserts that no error is returned, |
| 127 | +// the tree is not nil, and the length of the files is 2. |
| 128 | +func TestSourcifySuite(t *testing.T) { |
| 129 | + suite.Run(t, new(SourcifySuite)) |
| 130 | +} |
0 commit comments