Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 9246655

Browse files
committed
update clickhouse_test.go
1 parent c1d423c commit 9246655

File tree

4 files changed

+122
-8
lines changed

4 files changed

+122
-8
lines changed

clickhouse_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ func Test_Insert(t *testing.T) {
9191
date Date,
9292
datetime DateTime,
9393
ipv4 IPv4,
94-
ipv6 IPv6
94+
ipv6 IPv6,
95+
ipv4str FixedString(16),
96+
ipv6str FixedString(16)
9597
) Engine=Memory
9698
`
9799
dml = `
@@ -111,7 +113,9 @@ func Test_Insert(t *testing.T) {
111113
date,
112114
datetime,
113115
ipv4,
114-
ipv6
116+
ipv6,
117+
ipv4str,
118+
ipv6str
115119
) VALUES (
116120
?,
117121
?,
@@ -128,6 +132,8 @@ func Test_Insert(t *testing.T) {
128132
?,
129133
?,
130134
?,
135+
?,
136+
?,
131137
?
132138
)
133139
`
@@ -148,7 +154,9 @@ func Test_Insert(t *testing.T) {
148154
date,
149155
datetime,
150156
ipv4,
151-
ipv6
157+
ipv6,
158+
ipv4str,
159+
ipv6str
152160
FROM clickhouse_test_insert
153161
`
154162
)
@@ -162,12 +170,14 @@ func Test_Insert(t *testing.T) {
162170
-1*i, -2*i, -4*i, -8*i, // int
163171
uint8(1*i), uint16(2*i), uint32(4*i), uint64(8*i), // uint
164172
1.32*float32(i), 1.64*float64(i), //float
165-
fmt.Sprintf("string %d", i), // string
166-
"RU", //fixedstring,
167-
time.Now(), //date
168-
time.Now(), //datetime
169-
"1.2.3.4", // ipv4
173+
fmt.Sprintf("string %d", i), // string
174+
"RU", //fixedstring,
175+
time.Now(), //date
176+
time.Now(), //datetime
177+
"1.2.3.4", // ipv4
170178
"2001:0db8:85a3:0000:0000:8a2e:0370:7334", //ipv6
179+
column.IP(net.ParseIP("127.0.0.1").To4()),
180+
column.IP(net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")),
171181
)
172182
if !assert.NoError(t, err) {
173183
return
@@ -194,6 +204,8 @@ func Test_Insert(t *testing.T) {
194204
DateTime time.Time
195205
Ipv6 column.IP
196206
Ipv4 column.IP
207+
Ipv4str column.IP
208+
Ipv6str column.IP
197209
}
198210
if rows, err := connect.Query(query); assert.NoError(t, err) {
199211
var count int
@@ -216,6 +228,8 @@ func Test_Insert(t *testing.T) {
216228
&item.DateTime,
217229
&item.Ipv4,
218230
&item.Ipv6,
231+
&item.Ipv4str,
232+
&item.Ipv6str,
219233
)
220234
if !assert.NoError(t, err) {
221235
return

lib/column/ip_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package column
22

33
import (
44
"fmt"
5+
"github.com/kshvakov/clickhouse/lib/binary"
6+
"io/ioutil"
57
"net"
68
"strings"
79
"testing"
@@ -47,5 +49,85 @@ func Test_IPConverter(t *testing.T) {
4749
assert.Equal(t, errInvalidScanValue, ip.Scan(""))
4850
assert.Equal(t, errInvalidScanValue, ip.Scan([]byte{'1', '2', '3'}))
4951
assert.Equal(t, errInvalidScanType, ip.Scan(1))
52+
}
53+
54+
func TestIPv4_Write(t *testing.T) {
55+
ip := net.ParseIP("0.0.0.0")
56+
ipv4 := IPv4{}
5057

58+
cases := []struct {
59+
Key interface{}
60+
Error interface{}
61+
}{
62+
{
63+
net.ParseIP("0.0.0.0"),
64+
nil,
65+
},
66+
{
67+
&ip,
68+
nil,
69+
},
70+
{
71+
"0.0.0.0",
72+
nil,
73+
},
74+
{
75+
"",
76+
&ErrUnexpectedType{
77+
&ipv4,
78+
"",
79+
},
80+
},
81+
{
82+
"2001:0db8::0001",
83+
&ErrUnexpectedType{
84+
&ipv4,
85+
"2001:0db8::0001",
86+
},
87+
},
88+
}
89+
for _, Case := range cases {
90+
91+
buffer := binary.NewEncoder(ioutil.Discard)
92+
assert.Equal(t, ipv4.Write(buffer, Case.Key), Case.Error)
93+
}
5194
}
95+
96+
func TestIPv6_Write(t *testing.T) {
97+
ip := net.ParseIP("2001:0db8::0001")
98+
ipv6 := IPv6{}
99+
100+
cases := []struct {
101+
Key interface{}
102+
Error interface{}
103+
}{
104+
{
105+
net.ParseIP("2001:0db8::0001"),
106+
nil,
107+
},
108+
{
109+
&ip,
110+
nil,
111+
},
112+
{
113+
"2001:0db8::0001",
114+
nil,
115+
},
116+
{
117+
"",
118+
&ErrUnexpectedType{
119+
&ipv6,
120+
"",
121+
},
122+
},
123+
{
124+
"0.0.0.0",
125+
nil,
126+
},
127+
}
128+
for _, Case := range cases {
129+
130+
buffer := binary.NewEncoder(ioutil.Discard)
131+
assert.Equal(t, ipv6.Write(buffer, Case.Key), Case.Error)
132+
}
133+
}

lib/column/ipv4.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ func (ip *IPv4) Write(encoder *binary.Encoder, v interface{}) error {
3434
}
3535
}
3636

37+
if netIP == nil {
38+
return &ErrUnexpectedType{
39+
T: v,
40+
Column: ip,
41+
}
42+
}
3743
ip4 := netIP.To4()
44+
if ip4 == nil {
45+
return &ErrUnexpectedType{
46+
T: v,
47+
Column: ip,
48+
}
49+
}
3850
if _, err := encoder.Write([]byte{ip4[3], ip4[2], ip4[1], ip4[0]}); err != nil {
3951
return err
4052
}

lib/column/ipv6.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ func (ip *IPv6) Write(encoder *binary.Encoder, v interface{}) error {
3434
}
3535
}
3636

37+
if netIP == nil {
38+
return &ErrUnexpectedType{
39+
T: v,
40+
Column: ip,
41+
}
42+
}
3743
if _, err := encoder.Write([]byte(netIP.To16())); err != nil {
3844
return err
3945
}

0 commit comments

Comments
 (0)