-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathmsgpack_test.go
More file actions
141 lines (124 loc) · 4.33 KB
/
msgpack_test.go
File metadata and controls
141 lines (124 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package types
import (
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
pi "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces"
"github.com/CovenantSQL/CovenantSQL/utils"
)
type complexStructure struct {
Tx pi.Transaction
Txs []pi.Transaction
Maps map[string]pi.Transaction
}
func TestEncodeDecodeTransactions(t *testing.T) {
Convey("test encode/decode single transaction", t, func() {
var t pi.Transaction
t = NewBaseAccount(&Account{})
buf, err := utils.EncodeMsgPack(t)
So(err, ShouldBeNil)
var out pi.Transaction
err = utils.DecodeMsgPack(buf.Bytes(), &out)
So(err, ShouldBeNil)
So(out, ShouldNotBeNil)
So(reflect.TypeOf(out).String(), ShouldContainSubstring, "TransactionWrapper")
So(out.GetTransactionType(), ShouldEqual, t.GetTransactionType())
})
Convey("test encode/decode series of transactions", t, func() {
var t []pi.Transaction
t = append(t, NewBaseAccount(&Account{}))
t = append(t, NewTransfer(&TransferHeader{}))
t = append(t, NewCreateDatabase(&CreateDatabaseHeader{}))
buf, err := utils.EncodeMsgPack(t)
So(err, ShouldBeNil)
var out []pi.Transaction
err = utils.DecodeMsgPack(buf.Bytes(), &out)
So(err, ShouldBeNil)
So(out, ShouldNotBeNil)
So(out, ShouldHaveLength, len(t))
for i := range t {
So(out[i], ShouldNotBeNil)
So(out[i].GetTransactionType(), ShouldEqual, t[i].GetTransactionType())
So(reflect.TypeOf(out[i]).String(), ShouldContainSubstring, "TransactionWrapper")
}
})
Convey("test encode/decode complex structure containing transactions", t, func() {
var t complexStructure
t.Tx = NewBaseAccount(&Account{})
t.Txs = append(t.Txs, NewBaseAccount(&Account{}))
t.Txs = append(t.Txs, NewTransfer(&TransferHeader{}))
t.Txs = append(t.Txs, NewCreateDatabase(&CreateDatabaseHeader{}))
t.Maps = make(map[string]pi.Transaction)
t.Maps["BaseAccount"] = NewBaseAccount(&Account{})
t.Maps["Transfer"] = NewTransfer(&TransferHeader{})
t.Maps["CreateDatabase"] = NewCreateDatabase(&CreateDatabaseHeader{})
buf, err := utils.EncodeMsgPack(t)
So(err, ShouldBeNil)
var out complexStructure
err = utils.DecodeMsgPack(buf.Bytes(), &out)
So(err, ShouldBeNil)
So(out.Tx, ShouldNotBeNil)
So(out.Txs, ShouldNotBeNil)
So(out.Txs, ShouldHaveLength, len(t.Txs))
So(out.Maps, ShouldNotBeNil)
So(out.Maps, ShouldHaveLength, len(t.Maps))
So(out.Tx.GetTransactionType(), ShouldEqual, pi.TransactionTypeBaseAccount)
So(reflect.TypeOf(out.Tx).String(), ShouldContainSubstring, "TransactionWrapper")
for i := range out.Txs {
So(out.Txs[i], ShouldNotBeNil)
So(out.Txs[i].GetTransactionType(), ShouldEqual, t.Txs[i].GetTransactionType())
So(reflect.TypeOf(out.Txs[i]).String(), ShouldContainSubstring, "TransactionWrapper")
}
for k, v := range out.Maps {
So(v, ShouldNotBeNil)
So(out.Maps[k].GetTransactionType(), ShouldEqual, t.Maps[k].GetTransactionType())
So(reflect.TypeOf(out.Maps[k]).String(), ShouldContainSubstring, "TransactionWrapper")
}
})
Convey("decode invalid data", t, func() {
var testTypes = []interface{}{
"1",
1,
0.1,
[]int{},
[]int{1, 2},
[]int{1, 2, 3},
struct{ A int }{A: 1},
struct{ TxType int }{TxType: int(pi.TransactionTypeNumber) + 1},
struct{ TxType struct{} }{},
struct{}{},
}
for _, val := range testTypes {
buf, err := utils.EncodeMsgPack(val)
So(err, ShouldBeNil)
var out pi.Transaction
err = utils.DecodeMsgPack(buf.Bytes(), &out)
So(err, ShouldNotBeNil)
}
})
Convey("decode wrapper with nil transaction", t, func() {
var t pi.Transaction
t = pi.WrapTransaction(nil)
buf, err := utils.EncodeMsgPack(t)
So(err, ShouldBeNil)
var out pi.Transaction
err = utils.DecodeMsgPack(buf.Bytes(), &out)
So(err, ShouldBeNil)
So(out, ShouldBeNil)
})
}