From: Chengcheng Zhang <943420582@qq.com> Date: Thu, 18 Jul 2019 10:21:48 +0000 (+0800) Subject: add TestGetAsset X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=128b5c789bcd346972b362501dab8895fb143995;p=bytom%2Fvapor.git add TestGetAsset --- diff --git a/database/wallet_store.go b/database/wallet_store.go index 33c1513f..c0bf9f93 100644 --- a/database/wallet_store.go +++ b/database/wallet_store.go @@ -190,7 +190,7 @@ func (store *WalletStore) DeleteWalletTransactions() { } } -// DeleteWalletUTXOs delete all txs in wallet +// DeleteWalletUTXOs delete all utxos in wallet func (store *WalletStore) DeleteWalletUTXOs() { batch := store.db.NewBatch() if store.batch != nil { diff --git a/database/wallet_store_test.go b/database/wallet_store_test.go index 6f5efdc3..f1a27535 100644 --- a/database/wallet_store_test.go +++ b/database/wallet_store_test.go @@ -2,6 +2,7 @@ package database import ( "bytes" + "encoding/json" "io/ioutil" "os" "reflect" @@ -9,6 +10,7 @@ import ( "testing" acc "github.com/vapor/account" + "github.com/vapor/asset" "github.com/vapor/blockchain/pseudohsm" "github.com/vapor/blockchain/query" "github.com/vapor/crypto/ed25519/chainkd" @@ -525,3 +527,74 @@ func TestGetGlobalTransactionIndex(t *testing.T) { os.RemoveAll("temp") } } + +func TestGetAsset(t *testing.T) { + cases := []struct { + asset *asset.Asset + }{ + { + asset: &asset.Asset{ + AssetID: bc.NewAssetID([32]byte{}), + DefinitionMap: map[string]interface{}{}, + }, + }, + { + asset: &asset.Asset{ + AssetID: bc.NewAssetID([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}), + DefinitionMap: map[string]interface{}{}, + }, + }, + { + asset: &asset.Asset{ + AssetID: bc.NewAssetID([32]byte{}), + DefinitionMap: map[string]interface{}{ + "Name": "assetname", + }, + }, + }, + { + asset: &asset.Asset{ + AssetID: bc.NewAssetID([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}), + DefinitionMap: map[string]interface{}{ + "Name": "assetname", + }, + }, + }, + { + asset: &asset.Asset{ + AssetID: bc.NewAssetID([32]byte{0x02, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}), + DefinitionMap: map[string]interface{}{ + "Name": "assetname", + "Amount": "1000000", + }, + }, + }, + } + + for i, c := range cases { + testDB := dbm.NewDB("testdb", "leveldb", "temp") + walletStore := NewWalletStore(testDB) + ws := walletStore.InitBatch() + definitionByte, err := json.Marshal(c.asset.DefinitionMap) + if err != nil { + t.Fatal(err) + } + + ws.SetAssetDefinition(&c.asset.AssetID, definitionByte) + if err := ws.CommitBatch(); err != nil { + t.Fatal(err) + } + + gotAsset, err := ws.GetAsset(&c.asset.AssetID) + if err != nil { + t.Fatal(err) + } + + if !testutil.DeepEqual(gotAsset.DefinitionMap, c.asset.DefinitionMap) { + t.Errorf("case %v: got asset, got: %v, want: %v.", i, gotAsset.DefinitionMap, c.asset.DefinitionMap) + } + + testDB.Close() + os.RemoveAll("temp") + } +}