forked from lukashes/pg
/
encoder_test.go
138 lines (111 loc) · 3.08 KB
/
encoder_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
package pg_test
import (
"math"
. "gopkg.in/check.v1"
"github.com/lukashes/pg"
)
type EncoderTest struct{}
var _ = Suite(&EncoderTest{})
func (t *EncoderTest) TestTooManyParamsError(c *C) {
_, err := pg.FormatQ("", "foo", "bar")
c.Assert(err.Error(), Equals, "pg: expected 0 parameters, got 2")
}
func (t *EncoderTest) TestTooFewParamsError(c *C) {
_, err := pg.FormatQ("? ? ?", "foo", "bar")
c.Assert(err.Error(), Equals, "pg: expected at least 3 parameters, got 2")
}
func (t *EncoderTest) TestNoParams(c *C) {
q, err := pg.FormatQ("?")
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "?")
}
// TODO: check for overflow?
func (t *EncoderTest) TestUint64(c *C) {
q, err := pg.FormatQ("?", uint64(math.MaxUint64))
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "-1")
}
func (t *EncoderTest) TestTypeAlias(c *C) {
type mystr string
q, err := pg.FormatQ("?", mystr("hello world"))
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "'hello world'")
}
func (t *EncoderTest) TestPointers(c *C) {
var x *int
q, err := pg.FormatQ("?", x)
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "NULL")
y := new(int)
*y = 42
q, err = pg.FormatQ("?", y)
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "42")
q, err = pg.FormatQ("?", nil)
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "NULL")
}
type structFormatter struct {
Foo string
}
func (structFormatter) Meth() string {
return "value"
}
func (structFormatter) MethWithArgs(string) string {
return "value"
}
func (structFormatter) MethWithCompositeReturn() (string, string) {
return "value1", "value2"
}
type embeddedStructFormatter struct {
*structFormatter
}
func (embeddedStructFormatter) Meth2() string {
return "value2"
}
func (t *EncoderTest) TestStruct(c *C) {
{
src := struct{ Foo string }{"bar"}
q, err := pg.FormatQ("?bar", src)
c.Assert(err.Error(), Equals, `pg: cannot map "bar"`)
c.Assert(string(q), Equals, "")
}
{
src := struct{ S1, S2 string }{"value1", "value2"}
q, err := pg.FormatQ("? ?s1 ? ?s2 ?", "one", "two", "three", src)
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "'one' 'value1' 'two' 'value2' 'three'")
}
{
src := &structFormatter{}
_, err := pg.FormatQ("?MethWithArgs", src)
c.Assert(err.Error(), Equals, `pg: cannot map "MethWithArgs"`)
}
{
src := &structFormatter{}
_, err := pg.FormatQ("?MethWithCompositeReturn", src)
c.Assert(err.Error(), Equals, `pg: cannot map "MethWithCompositeReturn"`)
}
{
src := &structFormatter{"bar"}
q, err := pg.FormatQ("?foo ?Meth", src)
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "'bar' 'value'")
}
{
src := &embeddedStructFormatter{&structFormatter{"bar"}}
q, err := pg.FormatQ("?foo ?Meth ?Meth2", src)
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "'bar' 'value' 'value2'")
}
}
func (t *EncoderTest) TestFormatInts(c *C) {
q, err := pg.FormatQ("?", pg.Ints{1, 2, 3})
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "1,2,3")
}
func (t *EncoderTest) TestFormatStrings(c *C) {
q, err := pg.FormatQ("?", pg.Strings{"hello", "world"})
c.Assert(err, IsNil)
c.Assert(string(q), Equals, "'hello','world'")
}