OSDN Git Service

92f92a5b3dbc2e8b40841b06bf7d7514b9280fb4
[bytom/vapor.git] / toolbar / reward / service / node_test.go
1 package service
2
3 import (
4         "encoding/json"
5         "testing"
6
7         "github.com/vapor/consensus"
8         "github.com/vapor/protocol/bc"
9 )
10
11 func TestBuildRequest(t *testing.T) {
12         cases := []struct {
13                 input   InputAction
14                 outputs []OutputAction
15                 want    string
16                 err     error
17         }{
18                 {
19                         input: InputAction{
20                                 Type:      "spend_account",
21                                 AccountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
22                                 AssetAmount: bc.AssetAmount{
23                                         AssetId: consensus.BTMAssetID,
24                                         Amount:  100,
25                                 },
26                         },
27                         outputs: []OutputAction{
28                                 OutputAction{
29                                         Type:    "control_address",
30                                         Address: "sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf",
31                                         AssetAmount: bc.AssetAmount{
32                                                 AssetId: consensus.BTMAssetID,
33                                                 Amount:  100,
34                                         },
35                                 },
36                         },
37                         want: `{"actions":[{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"control_address","address":"sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100}]}`,
38                 },
39                 {
40                         input: InputAction{
41                                 Type:      "spend_account",
42                                 AccountID: "9bb77612-350e-4d53-81e2-525b28247ba5",
43                                 AssetAmount: bc.AssetAmount{
44                                         AssetId: consensus.BTMAssetID,
45                                         Amount:  100,
46                                 },
47                         },
48                         outputs: []OutputAction{
49                                 OutputAction{
50                                         Type:    "control_address",
51                                         Address: "sp1qlryy65a5apylphqp6axvhx7nd6y2zlexuvn7gf",
52                                         AssetAmount: bc.AssetAmount{
53                                                 AssetId: consensus.BTMAssetID,
54                                                 Amount:  50,
55                                         },
56                                 },
57                                 OutputAction{
58                                         Type:    "control_address",
59                                         Address: "sp1qklmexrd32ch8yc8xhkpkdx05wye75pvzuy2gch",
60                                         AssetAmount: bc.AssetAmount{
61                                                 AssetId: consensus.BTMAssetID,
62                                                 Amount:  50,
63                                         },
64                                 },
65                         },
66                         want: `{"actions":[{"type":"spend_account","account_id":"9bb77612-350e-4d53-81e2-525b28247ba5","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":100},{"type":"control_address","address":"sp1qklmexrd32ch8yc8xhkpkdx05wye75pvzuy2gch","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":50},{"type":"control_address","address":"sp1qklmexrd32ch8yc8xhkpkdx05wye75pvzuy2gch","asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount":50}]}`,
67                 },
68         }
69
70         for i, c := range cases {
71                 n := &Node{}
72                 req := &buildSpendReq{}
73                 if err := n.buildRequest(c.input, c.outputs, req); err != nil {
74                         t.Fatal(err)
75                 }
76
77                 buildReq, err := json.Marshal(req)
78                 if err != nil {
79                         t.Fatal(err)
80                 }
81
82                 if string(buildReq) != string(c.want) {
83                         t.Fatal(i, string(buildReq))
84                 }
85
86         }
87 }