-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjson_text_test.go
More file actions
76 lines (65 loc) · 1.96 KB
/
json_text_test.go
File metadata and controls
76 lines (65 loc) · 1.96 KB
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
package pq_types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
. "gopkg.in/check.v1"
)
func (s *TypesSuite) TestJSONText(c *C) {
type testData struct {
j JSONText
b []byte
}
for _, d := range []testData{
{JSONText(nil), []byte(nil)},
{JSONText(`null`), []byte(`null`)},
{JSONText(`{}`), []byte(`{}`)},
{JSONText(`[]`), []byte(`[]`)},
{JSONText(`[{"b": true, "n": 123}, {"s": "foo", "obj": {"f1": 456, "f2": false}}, [null]]`),
[]byte(`[{"b": true, "n": 123}, {"s": "foo", "obj": {"f1": 456, "f2": false}}, [null]]`)},
} {
b1, err := json.Marshal(d.j)
c.Check(err, IsNil)
b := bytes.Replace(d.b, []byte(` `), nil, -1)
if d.j == nil {
// special case
c.Check(b1, DeepEquals, []byte(`null`))
} else {
c.Check(b1, DeepEquals, b, Commentf("\nb1 = %#q\nb = %#q", b1, b))
}
for _, col := range []string{"jsontext_varchar", "jsontext_json", "jsontext_jsonb"} {
if strings.HasSuffix(col, "json") && s.skipJSON {
continue
}
if strings.HasSuffix(col, "jsonb") && s.skipJSONB {
continue
}
s.SetUpTest(c)
_, err = s.db.Exec(fmt.Sprintf("INSERT INTO pq_types (%s) VALUES($1)", col), d.j)
c.Assert(err, IsNil)
b1 := []byte(`{"foo": "bar"}`)
j1 := JSONText(`{"foo": "bar"}`)
err = s.db.QueryRow(fmt.Sprintf("SELECT %s, %s FROM pq_types", col, col)).Scan(&b1, &j1)
c.Check(err, IsNil)
c.Check(b1, DeepEquals, d.b, Commentf("\nb1 = %#q\nd.b = %#q", b1, d.b))
c.Check(j1, DeepEquals, d.j)
}
}
for _, j := range []JSONText{
JSONText{},
} {
for _, col := range []string{"jsontext_varchar", "jsontext_json", "jsontext_jsonb"} {
if strings.HasSuffix(col, "json") && s.skipJSON {
continue
}
if strings.HasSuffix(col, "jsonb") && s.skipJSONB {
continue
}
s.SetUpTest(c)
_, err := s.db.Exec(fmt.Sprintf("INSERT INTO pq_types (%s) VALUES($1)", col), j)
c.Check(err, DeepEquals, errors.New(`sql: converting Exec argument #0's type: unexpected end of JSON input`))
}
}
}