This repository has been archived by the owner on Nov 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
converter_test.go
111 lines (104 loc) · 2.93 KB
/
converter_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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package monetdb
import (
"bytes"
"database/sql/driver"
"testing"
"time"
)
func TestConvertToMonet(t *testing.T) {
type tc struct {
v driver.Value
e string
}
var tcs = []tc{
tc{1, "1"},
tc{"string", "'string'"},
tc{"quoted 'string'", "'quoted \\'string\\''"},
tc{"quoted \"string\"", "'quoted \"string\"'"},
tc{"back\\slashed", "'back\\\\slashed'"},
tc{"quoted \\'string\\'", "'quoted \\\\\\'string\\\\\\''"},
tc{int8(8), "8"},
tc{int16(16), "16"},
tc{int32(32), "32"},
tc{int64(64), "64"},
tc{float32(3.2), "3.2"},
tc{float64(6.4), "6.4"},
tc{true, "true"},
tc{false, "false"},
tc{nil, "NULL"},
tc{[]byte{1, 2, 3}, "'" + string([]byte{1, 2, 3}) + "'"},
tc{Time{10, 20, 30}, "'10:20:30'"},
tc{Date{2001, time.January, 2}, "'2001-01-02'"},
tc{time.Date(2001, time.January, 2, 10, 20, 30, 0, time.FixedZone("CET", 3600)),
"'2001-01-02 10:20:30 +0100 CET'"},
}
for _, c := range tcs {
s, err := convertToMonet(c.v)
if err != nil {
t.Errorf("Error converting value: %v -> %v", c.v, err)
} else if s != c.e {
t.Errorf("Invalid value: %s, expected: %s", s, c.e)
}
}
}
func TestConvertToGo(t *testing.T) {
type tc struct {
v string
t string
e driver.Value
}
var tcs = []tc{
tc{"8", "tinyint", int8(8)},
tc{"16", "smallint", int16(16)},
tc{"16", "shortint", int16(16)},
tc{"32", "int", int32(32)},
tc{"32", "mediumint", int32(32)},
tc{"64", "bigint", int64(64)},
tc{"64", "longint", int64(64)},
tc{"64", "hugeint", int64(64)},
tc{"64", "serial", int64(64)},
tc{"3.2", "float", float32(3.2)},
tc{"3.2", "real", float32(3.2)},
tc{"6.4", "double", float64(6.4)},
tc{"6.4", "decimal", float64(6.4)},
tc{"true", "boolean", true},
tc{"false", "boolean", false},
tc{"10:20:30", "time", Time{10, 20, 30}},
tc{"2001-01-02", "date", Date{2001, time.January, 2}},
tc{"'string'", "char", "string"},
tc{"'string'", "varchar", "string"},
tc{"'quoted \"string\"'", "char", "quoted \"string\""},
tc{"'quoted \\'string\\''", "char", "quoted 'string'"},
tc{"'quoted \\\\\\'string\\\\\\''", "char", "quoted \\'string\\'"},
tc{"'back\\\\slashed'", "char", "back\\slashed"},
tc{"'ABC'", "blob", []uint8{0x41, 0x42, 0x43}},
}
for _, c := range tcs {
v, err := convertToGo(c.v, c.t)
if err != nil {
t.Errorf("Error converting value: %v (%s) -> %v", c.v, c.t, err)
} else {
ok := true
switch val := v.(type) {
case []byte:
ok = compareByteArray(t, val, c.e)
default:
ok = v == c.e
}
if !ok {
t.Errorf("Invalid value: %v (%v - %s), expected: %v", v, c.v, c.t, c.e)
}
}
}
}
func compareByteArray(t *testing.T, val []byte, e driver.Value) bool {
switch exp := e.(type) {
case []byte:
return bytes.Compare(val, exp) == 0
default:
return false
}
}