OSDN Git Service

Add blockchain/accounts,assets test (#308)
authorLiu-Cheng Xu <xuliuchengxlc@gmail.com>
Mon, 22 Jan 2018 02:19:51 +0000 (10:19 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 22 Jan 2018 02:19:51 +0000 (10:19 +0800)
blockchain/accounts_test.go [new file with mode: 0644]
blockchain/assets_test.go [new file with mode: 0644]

diff --git a/blockchain/accounts_test.go b/blockchain/accounts_test.go
new file mode 100644 (file)
index 0000000..426b44f
--- /dev/null
@@ -0,0 +1,85 @@
+package blockchain
+
+import (
+       "context"
+       "io/ioutil"
+       "os"
+       "reflect"
+       "testing"
+
+       dbm "github.com/tendermint/tmlibs/db"
+
+       "github.com/bytom/blockchain/account"
+       "github.com/bytom/blockchain/txdb"
+       "github.com/bytom/crypto/ed25519/chainkd"
+       "github.com/bytom/protocol"
+       "github.com/bytom/protocol/bc"
+       "github.com/bytom/testutil"
+)
+
+func TestUpdateAccountTags(t *testing.T) {
+       dirPath, err := ioutil.TempDir(".", "")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(dirPath)
+
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer os.RemoveAll("temp")
+
+       store := txdb.NewStore(testDB)
+       txPool := protocol.NewTxPool()
+       chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       m := account.NewManager(testDB, chain)
+       ctx := context.Background()
+
+       account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "account-alias",
+               map[string]interface{}{
+                       "test_tag": "v0",
+               })
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       // Update by ID
+       wantTags := map[string]interface{}{
+               "test_tag": "v1",
+       }
+
+       if m.UpdateTags(ctx, account.ID, wantTags) != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       account1, err := m.FindByAlias(ctx, account.Alias)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       gotTags := account1.Tags
+       if !reflect.DeepEqual(gotTags, wantTags) {
+               t.Fatalf("tags:\ngot:  %v\nwant: %v", gotTags, wantTags)
+       }
+
+       // Update by alias
+       wantTags = map[string]interface{}{
+               "test_tag": "v2",
+       }
+
+       if m.UpdateTags(ctx, account.Alias, wantTags) != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       account2, err := m.FindByAlias(ctx, account.Alias)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       gotTags = account2.Tags
+       if !reflect.DeepEqual(gotTags, wantTags) {
+               t.Fatalf("tags:\ngot:  %v\nwant: %v", gotTags, wantTags)
+       }
+}
diff --git a/blockchain/assets_test.go b/blockchain/assets_test.go
new file mode 100644 (file)
index 0000000..bb9cb90
--- /dev/null
@@ -0,0 +1,85 @@
+package blockchain
+
+import (
+       "context"
+       "io/ioutil"
+       "os"
+       "reflect"
+       "testing"
+
+       dbm "github.com/tendermint/tmlibs/db"
+
+       "github.com/bytom/blockchain/asset"
+       "github.com/bytom/blockchain/txdb"
+       "github.com/bytom/crypto/ed25519/chainkd"
+       "github.com/bytom/protocol"
+       "github.com/bytom/protocol/bc"
+       "github.com/bytom/testutil"
+)
+
+func TestUpdateAssetTags(t *testing.T) {
+       dirPath, err := ioutil.TempDir(".", "")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(dirPath)
+
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer os.RemoveAll("temp")
+
+       store := txdb.NewStore(testDB)
+       txPool := protocol.NewTxPool()
+       chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       reg := asset.NewRegistry(testDB, chain)
+       ctx := context.Background()
+
+       asset, err := reg.Define([]chainkd.XPub{testutil.TestXPub}, 1, nil, "asset-alias",
+               map[string]interface{}{
+                       "test_tag": "v0",
+               })
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       // Update by ID
+       wantTags := map[string]interface{}{
+               "test_tag": "v1",
+       }
+
+       if reg.UpdateTags(ctx, asset.AssetID.String(), wantTags) != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       asset1, err := reg.FindByAlias(ctx, *asset.Alias)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       gotTags := asset1.Tags
+       if !reflect.DeepEqual(gotTags, wantTags) {
+               t.Fatalf("tags:\ngot:  %v\nwant: %v", gotTags, wantTags)
+       }
+
+       // Update by alias
+       wantTags = map[string]interface{}{
+               "test_tag": "v2",
+       }
+
+       if reg.UpdateTags(ctx, *asset.Alias, wantTags) != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       asset2, err := reg.FindByAlias(ctx, *asset.Alias)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       gotTags = asset2.Tags
+       if !reflect.DeepEqual(gotTags, wantTags) {
+               t.Fatalf("tags:\ngot:  %v\nwant: %v", gotTags, wantTags)
+       }
+}