From 9a0c2444e8f7c7620c539ce0b713f6e942a439cb Mon Sep 17 00:00:00 2001 From: Nathan Seva Date: Wed, 5 Jun 2024 16:59:16 -0500 Subject: [PATCH] test: get the assets amount dynamically --- .../handler/wallet/get_all_assets_test.go | 25 +++++++++- pkg/assets/default_assets.go | 48 ++++++++----------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/internal/handler/wallet/get_all_assets_test.go b/internal/handler/wallet/get_all_assets_test.go index 9f868e2c..ee5b231b 100644 --- a/internal/handler/wallet/get_all_assets_test.go +++ b/internal/handler/wallet/get_all_assets_test.go @@ -4,10 +4,13 @@ import ( "encoding/json" "fmt" "net/http" + "os" "testing" "github.com/massalabs/station-massa-wallet/api/server/models" "github.com/massalabs/station-massa-wallet/api/server/restapi/operations" + "github.com/massalabs/station-massa-wallet/pkg/assets" + "github.com/massalabs/station-massa-wallet/pkg/network" "github.com/stretchr/testify/assert" ) @@ -23,7 +26,8 @@ func TestGetAllAssetsHandler(t *testing.T) { assetsWithBalance := getAssets(t, api, nickname) // Assert that assetsWithBalance contains the expected data - assert.Len(t, assetsWithBalance, 9, "the assets list should have 9 items") + counter := getExpectedAssetsCount(t) + assert.Len(t, assetsWithBalance, counter+1) // +1 for native MAS assert.Equal(t, "1000000", assetsWithBalance[0].Balance) assert.Equal(t, "Massa", assetsWithBalance[0].AssetInfo.Name) @@ -57,3 +61,22 @@ func assertAssetInfoWithBalanceEqual(t *testing.T, actual, expected *models.Asse assert.Equal(t, expected.AssetInfo.Decimals, actual.AssetInfo.Decimals) assert.Equal(t, expected.AssetInfo.ChainID, actual.AssetInfo.ChainID) } + +func getExpectedAssetsCount(t *testing.T) int { + tempDir, err := os.MkdirTemp(os.TempDir(), "*-wallet-dir") + assert.NoError(t, err) + nodeFetcher := network.NewNodeFetcher() + store, err := assets.NewAssetsStore(tempDir, nodeFetcher) + assert.NoError(t, err) + defaultAssets, err := store.Default() + assert.NoError(t, err) + counter := 0 + + for _, asset := range defaultAssets { + if asset.ChainID == 77658377 { + counter += 1 + } + } + + return counter +} diff --git a/pkg/assets/default_assets.go b/pkg/assets/default_assets.go index 150706d1..39534673 100644 --- a/pkg/assets/default_assets.go +++ b/pkg/assets/default_assets.go @@ -70,7 +70,7 @@ func (s *AssetsStore) InitDefault() error { } // if the content is different, overwrite the default assets JSON file - if string(content) != assetsJSON { + if string(content) != AssetsJSON { if err := s.createFileDefault(defaultAssetsJSONPath); err != nil { return err } @@ -87,14 +87,14 @@ func getDefaultJSONPath(assetsJSONDir string) (string, error) { // createFileDefault creates the default assets JSON file with the default assets. func (s *AssetsStore) createFileDefault(path string) error { - if err := os.WriteFile(path, []byte(assetsJSON), permissionUrwGrOr); err != nil { + if err := os.WriteFile(path, []byte(AssetsJSON), permissionUrwGrOr); err != nil { return err } return nil } -const assetsJSON = `[ +const AssetsJSON = `[ { "address": "AS12k8viVmqPtRuXzCm6rKXjLgpQWqbuMjc37YHhB452KSUUb9FgL", "name": "Sepolia USDC", @@ -151,30 +151,6 @@ const assetsJSON = `[ "MEXCSymbol": "ETHUSDT", "ChainID": 77658377 }, - { - "address": "AS133eqPPaPttJ6hJnk3sfoG5cjFFqBDi1VGxdo2wzWkq8AfZnan", - "name": "Purrfect Universe", - "symbol": "PUR", - "decimals": 18, - "MEXCSymbol": "", - "ChainID": 77658377 - }, - { - "address": "", - "name": "Wrapped Ether", - "symbol": "WETH.b", - "decimals": 18, - "MEXCSymbol": "ETHUSDT", - "ChainID": 77658377 - }, - { - "address": "", - "name": "Wrapped Binance USD", - "symbol": "USDT.b", - "decimals": 18, - "MEXCSymbol": "USD", - "ChainID": 77658377 - }, { "address": "AS12RmCXTA9NZaTBUBnRJuH66AGNmtEfEoqXKxLdmrTybS6GFJPFs", "name": "Wrapped Ether", @@ -192,3 +168,21 @@ const assetsJSON = `[ "ChainID": 77658366 } ]` + +// TODO: add the following assets with the addresses +// { +// "address": "", +// "name": "Wrapped Ether", +// "symbol": "WETH.b", +// "decimals": 18, +// "MEXCSymbol": "ETHUSDT", +// "ChainID": 77658377 +// }, +// { +// "address": "", +// "name": "Wrapped Binance USD", +// "symbol": "USDT.b", +// "decimals": 18, +// "MEXCSymbol": "USD", +// "ChainID": 77658377 +// },