-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeanut_test.go
167 lines (147 loc) · 3.79 KB
/
peanut_test.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package peanut_test
import (
"github.com/jimsmart/peanut"
. "github.com/onsi/gomega"
)
type Foo struct {
StringField string `peanut:"foo_string,pk"`
IntField int `peanut:"foo_int"`
}
type Bar struct {
IntField int `peanut:"bar_int,pk"`
StringField string `peanut:"bar_string,pk"`
}
type Baz struct {
StringField string `peanut:"baz_string,pk"`
BoolField bool `peanut:"baz_bool"`
Float32Field float64 `peanut:"baz_float32"`
Float64Field float64 `peanut:"baz_float64"`
IntField int `peanut:"baz_int"`
Int8Field int8 `peanut:"baz_int8"`
Int16Field int16 `peanut:"baz_int16"`
Int32Field int32 `peanut:"baz_int32"`
Int64Field int64 `peanut:"baz_int64"`
UintField uint `peanut:"baz_uint"`
Uint8Field uint8 `peanut:"baz_uint8"`
Uint16Field uint16 `peanut:"baz_uint16"`
Uint32Field uint32 `peanut:"baz_uint32"`
Uint64Field uint64 `peanut:"baz_uint64"`
IgnoredField int // No tag.
privateField int `peanut:"baz_private"`
}
type Qux struct {
StringField string
IntField int
}
type BadUnsupported struct {
BytesField []byte `peanut:"bytes_field"`
}
var testOutputFoo = []*Foo{
{StringField: "test 1", IntField: 1},
{StringField: "test 2", IntField: 2},
{StringField: "test 3", IntField: 3},
}
var testOutputBar = []*Bar{
{IntField: 1, StringField: "test 1"},
{IntField: 2, StringField: "test 2"},
{IntField: 3, StringField: "test 3"},
}
var testOutputBaz = []Baz{
{
StringField: "test 1",
BoolField: true,
Float32Field: 1.234,
Float64Field: 9.876,
IntField: -12345,
Int8Field: -8,
Int16Field: -16,
Int32Field: -32,
Int64Field: -64,
UintField: 12345,
Uint8Field: 8,
Uint16Field: 16,
Uint32Field: 32,
Uint64Field: 64,
},
}
var testOutputQux = []*Qux{
{IntField: 1, StringField: "test 1"},
{IntField: 2, StringField: "test 2"},
{IntField: 3, StringField: "test 3"},
}
func testWritesAndCloseSequential(w peanut.Writer) {
var err error
for i := range testOutputFoo {
err = w.Write(testOutputFoo[i])
Expect(err).To(BeNil())
}
for i := range testOutputBar {
err = w.Write(testOutputBar[i])
Expect(err).To(BeNil())
}
for i := range testOutputBaz {
err = w.Write(testOutputBaz[i])
Expect(err).To(BeNil())
}
for i := range testOutputQux {
err = w.Write(testOutputQux[i])
Expect(err).To(BeNil())
}
err = w.Close()
Expect(err).To(BeNil())
// Calling Cancel after Close should be a no-op.
err = w.Cancel()
Expect(err).To(BeNil())
}
func testWritesAndCloseInterleaved(w peanut.Writer) {
var err error
for i := range testOutputFoo {
err = w.Write(testOutputFoo[i])
Expect(err).To(BeNil())
err = w.Write(testOutputBar[i])
Expect(err).To(BeNil())
err = w.Write(testOutputQux[i])
Expect(err).To(BeNil())
}
for i := range testOutputBaz {
err = w.Write(testOutputBaz[i])
Expect(err).To(BeNil())
}
err = w.Close()
Expect(err).To(BeNil())
}
func testWritesAndCancel(w peanut.Writer) {
var err error
err = w.Write(testOutputFoo[0])
Expect(err).To(BeNil())
err = w.Write(testOutputBar[0])
Expect(err).To(BeNil())
err = w.Cancel()
Expect(err).To(BeNil())
// Calling Close after Cancel should be a no-op.
err = w.Close()
Expect(err).To(BeNil())
}
func testWriteBadType(w peanut.Writer) {
defer func() {
err1 := w.Cancel()
err2 := w.Close()
Expect(err1).To(BeNil())
Expect(err2).To(BeNil())
}()
err := w.Write(BadUnsupported{})
Expect(err).ToNot(BeNil())
// Expect error message to be informative.
Expect(err.Error()).To(SatisfyAll(
MatchRegexp(`slice`), // type
MatchRegexp("BytesField"), // field name
MatchRegexp("BadUnsupported"), // struct name
))
}
func testWriteAfterClose(w peanut.Writer) {
var err error
err = w.Close()
Expect(err).To(BeNil())
err = w.Write(testOutputFoo[0])
Expect(err).To(Equal(peanut.ErrClosedWriter))
}