OSDN Git Service

Add partial test for account and asset (#280)
authorLiu-Cheng Xu <xuliuchengxlc@gmail.com>
Fri, 12 Jan 2018 02:12:57 +0000 (10:12 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 12 Jan 2018 02:12:57 +0000 (10:12 +0800)
blockchain/account/accounts_test.go [new file with mode: 0644]
blockchain/account/receivers_test.go [new file with mode: 0644]
blockchain/asset/asset_test.go [new file with mode: 0644]

diff --git a/blockchain/account/accounts_test.go b/blockchain/account/accounts_test.go
new file mode 100644 (file)
index 0000000..d98fbc0
--- /dev/null
@@ -0,0 +1,106 @@
+package account
+
+import (
+       "context"
+       "io/ioutil"
+       "os"
+       "testing"
+
+       dbm "github.com/tendermint/tmlibs/db"
+
+       "github.com/bytom/blockchain/txdb"
+       "github.com/bytom/crypto/ed25519/chainkd"
+       "github.com/bytom/errors"
+       "github.com/bytom/protocol"
+       "github.com/bytom/protocol/bc"
+       "github.com/bytom/testutil"
+)
+
+func TestCreateAccount(t *testing.T) {
+       m := mockAccountManager(t)
+       ctx := context.Background()
+
+       account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil, "")
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       found, err := m.findByID(ctx, account.ID)
+       if err != nil {
+               t.Errorf("unexpected error %v", err)
+       }
+       if !testutil.DeepEqual(account, found) {
+               t.Errorf("expected account %s to be recorded as %s", account, found)
+       }
+}
+
+func TestCreateAccountReusedAlias(t *testing.T) {
+       m := mockAccountManager(t)
+       ctx := context.Background()
+       m.createTestAccount(ctx, t, "test-alias", nil)
+
+       _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil, "")
+       if errors.Root(err) != ErrDuplicateAlias {
+               t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
+       }
+}
+
+func TestFindByID(t *testing.T) {
+       m := mockAccountManager(t)
+       ctx := context.Background()
+       account := m.createTestAccount(ctx, t, "", nil)
+
+       found, err := m.findByID(ctx, account.ID)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       if !testutil.DeepEqual(account, found) {
+               t.Errorf("expected found account to be %v, instead found %v", account, found)
+       }
+}
+
+func TestFindByAlias(t *testing.T) {
+       m := mockAccountManager(t)
+       ctx := context.Background()
+       account := m.createTestAccount(ctx, t, "some-alias", nil)
+
+       found, err := m.FindByAlias(ctx, "some-alias")
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       if !testutil.DeepEqual(account, found) {
+               t.Errorf("expected found account to be %v, instead found %v", account, found)
+       }
+}
+
+func mockAccountManager(t *testing.T) *Manager {
+       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)
+       }
+
+       return NewManager(testDB, chain)
+}
+
+func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
+       account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias, tags, "")
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       return account
+
+}
diff --git a/blockchain/account/receivers_test.go b/blockchain/account/receivers_test.go
new file mode 100644 (file)
index 0000000..3e4549e
--- /dev/null
@@ -0,0 +1,49 @@
+package account
+
+import (
+       "context"
+       "testing"
+
+       "github.com/bytom/crypto/ed25519/chainkd"
+       "github.com/bytom/testutil"
+)
+
+func TestCreateReceiver(t *testing.T) {
+       m := mockAccountManager(t)
+       ctx := context.Background()
+
+       account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil, "")
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       _, err = m.CreateReceiver(ctx, account.ID)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       _, err = m.CreateReceiver(ctx, account.Alias)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+}
+
+func TestCreateAddressReceiver(t *testing.T) {
+       m := mockAccountManager(t)
+       ctx := context.Background()
+
+       account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil, "")
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       _, err = m.CreateAddressReceiver(ctx, account.ID)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       _, err = m.CreateAddressReceiver(ctx, account.Alias)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+}
diff --git a/blockchain/asset/asset_test.go b/blockchain/asset/asset_test.go
new file mode 100644 (file)
index 0000000..c22211a
--- /dev/null
@@ -0,0 +1,82 @@
+package asset
+
+import (
+       "context"
+       "io/ioutil"
+       "os"
+       "testing"
+
+       dbm "github.com/tendermint/tmlibs/db"
+
+       "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 TestDefineAsset(t *testing.T) {
+       ctx := context.Background()
+       reg := mockNewRegistry(t)
+       asset, err := reg.Define(ctx, []chainkd.XPub{testutil.TestXPub}, 1, nil, "asset-alias", nil, "")
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       found, err := reg.findByID(ctx, &asset.AssetID)
+       if err != nil {
+               t.Errorf("unexpected error %v", err)
+       }
+
+       if !testutil.DeepEqual(asset, found) {
+               t.Errorf("expected asset %s to be recorded as %s", asset, found)
+       }
+}
+
+func TestFindAssetByID(t *testing.T) {
+       ctx := context.Background()
+       reg := mockNewRegistry(t)
+       keys := []chainkd.XPub{testutil.TestXPub}
+       asset, err := reg.Define(ctx, keys, 1, nil, "", nil, "")
+       if err != nil {
+               testutil.FatalErr(t, err)
+
+       }
+       found, err := reg.findByID(ctx, &asset.AssetID)
+       if err != nil {
+               testutil.FatalErr(t, err)
+       }
+
+       if !testutil.DeepEqual(asset, found) {
+               t.Errorf("expected %v and %v to match", asset, found)
+       }
+
+}
+
+func mockChain(testDB dbm.DB) (*protocol.Chain, error) {
+       store := txdb.NewStore(testDB)
+       txPool := protocol.NewTxPool()
+       chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
+       if err != nil {
+               return nil, err
+       }
+       return chain, nil
+}
+
+func mockNewRegistry(t *testing.T) *Registry {
+       dirPath, err := ioutil.TempDir(".", "")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(dirPath)
+
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer os.RemoveAll("temp")
+
+       chain, err := mockChain(testDB)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       return NewRegistry(testDB, chain)
+}