OSDN Git Service

Remove /sign-submit-transaction api (#692)
[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 := mergeActions(BuildReq)
73
74                 if len(actions) != c.actionCount {
75                         t.Fatalf("got error count %d, want %d", len(actions), c.actionCount)
76                 }
77
78                 for _, a := range actions {
79                         actionType := a["type"].(string)
80                         assetID := a["asset_id"].(string)
81                         if actionType == "spend_account" {
82                                 amountNumber := a["amount"].(json.Number)
83                                 amount, _ := amountNumber.Int64()
84
85                                 switch assetID {
86                                 case "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff":
87                                         if amount != c.wantBTM {
88                                                 t.Fatalf("index %d, get error amount %v, want %d", i, amount, c.wantBTM)
89                                         }
90                                 case "43c6946d092b2959c1a82e90b282c68fca63e66de289048f6acd6cea9383c79c":
91                                         if amount != c.wantOther {
92                                                 t.Fatalf("index %d, get error amount %v, want %d", i, amount, c.wantOther)
93                                         }
94                                 default:
95                                         t.Fatalf("no %s in test cases", assetID)
96                                 }
97                         }
98                 }
99         }
100 }