forked from eaigner/jet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
expand_test.go
59 lines (49 loc) · 1.42 KB
/
expand_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
package jet
import (
"testing"
)
func TestQueryMarkSubstitution(t *testing.T) {
query := `INSERT INTO "fruits" ( "name", "attrs", "origin" ) VALUES ( $1, hstore(ARRAY[ $2 ]), $3 )`
args := []interface{}{"banana", map[string]interface{}{
"color": "yellow",
"price": 2,
}, "cuba"}
// Maps
newquery, newargs := substituteMapAndArrayMarks(query, args...)
if newquery != `INSERT INTO "fruits" ( "name", "attrs", "origin" ) VALUES ( $1, hstore(ARRAY[ $2, $3, $4, $5 ]), $6 )` {
t.Fatal(newquery)
}
if x := len(newargs); x != 6 {
t.Fatal(x)
}
// Slice
query2 := `INSERT INTO "fruits" ( "name", "attrs", "origin" ) VALUES ( $1, ARRAY[ $2 ], $3 )`
args2 := []interface{}{"banana", []interface{}{"a", "b", "c"}, "cuba"}
t.Log(query)
t.Log(args2)
newquery, newargs = substituteMapAndArrayMarks(query2, args2...)
if newquery != `INSERT INTO "fruits" ( "name", "attrs", "origin" ) VALUES ( $1, ARRAY[ $2, $3, $4 ], $5 )` {
t.Fatal(newquery)
}
if x := len(newargs); x != 5 {
t.Fatal(x)
}
}
func TestHstoreColumnParse(t *testing.T) {
h := parseHstoreColumn(`"a\"b\"c"=>"d\"e\"f", "g\"h\"i"=>"j\"k\"l", "m"=>NULL, "n"=>"NULL"`)
if x := len(h); x != 4 {
t.Fatal(h)
}
t.Log(h)
if x := h[`a"b"c`]; x != `d"e"f` {
t.Fatal(x)
}
var exp interface{} = nil
if x := h["m"]; x != exp {
t.Fatalf("Error: %v != %v", x, exp)
}
exp = "NULL"
if x, ok := h["n"]; x != exp || ok != true {
t.Fatalf("Error: %v != %v", x, exp)
}
}