OSDN Git Service

add unit test
authormars <mars@bytom.io>
Tue, 23 Jul 2019 01:33:14 +0000 (09:33 +0800)
committermars <mars@bytom.io>
Tue, 23 Jul 2019 01:33:14 +0000 (09:33 +0800)
toolbar/api_node/node_test.go [new file with mode: 0644]

diff --git a/toolbar/api_node/node_test.go b/toolbar/api_node/node_test.go
new file mode 100644 (file)
index 0000000..e7e2ff5
--- /dev/null
@@ -0,0 +1,70 @@
+package api_node
+
+import (
+       "encoding/json"
+       "testing"
+
+       "github.com/vapor/consensus"
+       "github.com/vapor/errors"
+       "github.com/vapor/protocol/bc"
+)
+
+func buildTxRequest(accountID string, outputs map[string]uint64) ([]byte, error) {
+       totalBTM := uint64(10000000)
+       actions := []interface{}{}
+       for address, amount := range outputs {
+               actions = append(actions, &ControlAddressAction{
+                       Address:     address,
+                       AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: amount},
+               })
+               totalBTM += amount
+       }
+
+       actions = append(actions, &SpendAccountAction{
+               AccountID:   accountID,
+               AssetAmount: &bc.AssetAmount{AssetId: consensus.BTMAssetID, Amount: totalBTM},
+       })
+       payload, err := json.Marshal(&buildTxReq{Actions: actions})
+       if err != nil {
+               return nil, errors.Wrap(err, "Marshal spend request")
+       }
+
+       return payload, nil
+}
+
+type args struct {
+       accountID string
+       outputs   map[string]uint64
+}
+
+func TestBuildTxRequest(t *testing.T) {
+       cases := []struct {
+               args args
+               want string
+       }{
+               {
+                       args: args{
+                               accountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
+                               outputs:   map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100},
+                       },
+                       want: `{"actions":[{"type":"control_address","address":"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":10000100}]}`,
+               },
+               {
+                       args: args{
+                               accountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
+                               outputs:   map[string]uint64{"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf": 100, "sp1qcgtxkhfzytul4lfttwex3skfqhm0tg6ms9da28": 200},
+                       },
+                       want: `{"actions":[{"type":"control_address","address":"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"control_address","address":"sp1qcgtxkhfzytul4lfttwex3skfqhm0tg6ms9da28","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":200},{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":10000300}]}`,
+               },
+       }
+
+       for i, c := range cases {
+               tx, err := buildTxRequest(c.args.accountID, c.args.outputs)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if string(tx) != string(c.want) {
+                       t.Fatal(i, string(tx))
+               }
+       }
+}