OSDN Git Service

modify result
[bytom/bytom.git] / api / transact_test.go
1 package api
2
3 import (
4         "encoding/json"
5         "fmt"
6         "testing"
7 )
8
9 func TestMergeActions(t *testing.T) {
10         cases := []struct {
11                 buildStr    string
12                 actionCount int
13                 wantBTM     int64
14                 wantOther   int64
15         }{
16                 {
17                         `{"actions": [
18                         {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":100, "account_id": "123"},
19                                 {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount": 200,"account_id": "123"},
20                                 {"type": "control_receiver", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount": 200, "receiver":{"control_program": "program","expires_at":"2017"}}
21                         ]}`,
22                         2,
23                         300,
24                         0,
25                 },
26                 {
27                         `{"actions": [
28                         {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":100, "account_id": "123"},
29                                 {"type": "spend_account", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c","amount": 200,"account_id": "123"},
30                                 {"type": "control_receiver", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c", "amount": 200, "receiver":{"control_program": "program","expires_at":"2017"}}
31                         ]}`,
32                         3,
33                         100,
34                         200,
35                 }, {
36                         `{"actions": [
37                         {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":100, "account_id": "123"},
38                                 {"type": "spend_account", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c","amount": 200,"account_id": "123"},
39                                 {"type": "spend_account", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c","amount": 300,"account_id": "123"},
40                                 {"type": "control_receiver", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c", "amount": 500, "receiver":{"control_program": "program","expires_at":"2017"}}
41                         ]}`,
42                         3,
43                         100,
44                         500,
45                 },
46                 {
47                         `{"actions": [
48                         {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":100, "account_id": "123"},
49                                 {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","amount": 200,"account_id": "123"},
50                                 {"type": "spend_account", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c","amount": 200,"account_id": "123"},
51                                 {"type": "spend_account", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c","amount": 300,"account_id": "123"},
52                                 {"type": "control_receiver", "asset_id": "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c", "amount": 500, "receiver":{"control_program": "program","expires_at":"2017"}}
53                         ]}`,
54                         3,
55                         300,
56                         500,
57                 },
58         }
59
60         for i, c := range cases {
61                 BuildReq := &BuildRequest{}
62
63                 if err := json.Unmarshal([]byte(c.buildStr), BuildReq); err != nil {
64                         t.Fatal(err)
65                 }
66
67                 for _, m := range BuildReq.Actions {
68                         amount := m["amount"].(float64)
69                         m["amount"] = json.Number(fmt.Sprintf("%v", amount))
70                 }
71
72                 actions, err := mergeActions(BuildReq)
73                 if err != nil {
74                         t.Fatal(err)
75                 }
76
77                 if len(actions) != c.actionCount {
78                         t.Fatalf("got error count %d, want %d", len(actions), c.actionCount)
79                 }
80
81                 for _, a := range actions {
82                         actionType := a["type"].(string)
83                         assetID := a["asset_id"].(string)
84                         if actionType == "spend_account" {
85                                 amountNumber := a["amount"].(json.Number)
86                                 amount, _ := amountNumber.Int64()
87
88                                 switch assetID {
89                                 case "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff":
90                                         if amount != c.wantBTM {
91                                                 t.Fatalf("index %d, get error amount %v, want %d", i, amount, c.wantBTM)
92                                         }
93                                 case "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c":
94                                         if amount != c.wantOther {
95                                                 t.Fatalf("index %d, get error amount %v, want %d", i, amount, c.wantOther)
96                                         }
97                                 default:
98                                         t.Fatalf("no %s in test cases", assetID)
99                                 }
100                         }
101                 }
102         }
103 }