OSDN Git Service

add asset unit test: TestUpdateAssetAlias and TestListAssets (#509)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Sat, 31 Mar 2018 08:27:29 +0000 (16:27 +0800)
committerYongfeng LI <wliyongfeng@gmail.com>
Sat, 31 Mar 2018 08:27:29 +0000 (16:27 +0800)
asset/asset_test.go

index e0a41da..065e317 100644 (file)
@@ -5,13 +5,13 @@ import (
        "io/ioutil"
        "os"
        "reflect"
-       "testing"
        "strings"
+       "testing"
 
        dbm "github.com/tendermint/tmlibs/db"
 
-       "github.com/bytom/crypto/ed25519/chainkd"
        "github.com/bytom/consensus"
+       "github.com/bytom/crypto/ed25519/chainkd"
        "github.com/bytom/database/leveldb"
        "github.com/bytom/protocol"
        "github.com/bytom/protocol/bc"
@@ -154,6 +154,61 @@ func TestUpdateAssetTags(t *testing.T) {
        }
 }
 
+func TestUpdateAssetAlias(t *testing.T) {
+       ctx := context.Background()
+       reg := mockNewRegistry(t)
+
+       oldAlias := "OLD_ALIAS"
+       newAlias := "NEW_ALIAS"
+
+       _, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, oldAlias, nil)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       if reg.UpdateAssetAlias(oldAlias, newAlias) != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       asset1, err := reg.FindByAlias(ctx, newAlias)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       gotAlias := *asset1.Alias
+       if !reflect.DeepEqual(gotAlias, newAlias) {
+               t.Fatalf("alias:\ngot:  %v\nwant: %v", gotAlias, newAlias)
+       }
+}
+
+func TestListAssets(t *testing.T) {
+       reg := mockNewRegistry(t)
+
+       firstAlias := "FIRST_ALIAS"
+       secondAlias := "SECOND_ALIAS"
+
+       firstAsset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, firstAlias, nil)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       secondAsset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, secondAlias, nil)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       wantAssets := []*Asset{DefaultNativeAsset, secondAsset, firstAsset}
+
+       gotAssets, err := reg.ListAssets("")
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       if !testutil.DeepEqual(gotAssets, wantAssets) {
+               t.Fatalf("got:\ngot:  %v\nwant: %v", gotAssets, wantAssets)
+       }
+}
+
 func mockChain(testDB dbm.DB) (*protocol.Chain, error) {
        store := leveldb.NewStore(testDB)
        txPool := protocol.NewTxPool()