From: Liu-Cheng Xu Date: Fri, 12 Jan 2018 02:12:57 +0000 (+0800) Subject: Add partial test for account and asset (#280) X-Git-Tag: v1.0.5-alpha~555 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=11f85952c581f099470bb1dfa9d65e3529f8e37b;p=bytom%2Fbytom-spv.git Add partial test for account and asset (#280) --- diff --git a/blockchain/account/accounts_test.go b/blockchain/account/accounts_test.go new file mode 100644 index 00000000..d98fbc0a --- /dev/null +++ b/blockchain/account/accounts_test.go @@ -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 index 00000000..3e4549e9 --- /dev/null +++ b/blockchain/account/receivers_test.go @@ -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 index 00000000..c22211af --- /dev/null +++ b/blockchain/asset/asset_test.go @@ -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) +}