-
Notifications
You must be signed in to change notification settings - Fork 6
/
json_test.go
63 lines (52 loc) · 1020 Bytes
/
json_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
package pgsql_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/acoshift/pgsql"
)
func TestJSON(t *testing.T) {
t.Parallel()
db := open(t)
defer db.Close()
_, err := db.Exec(`
drop table if exists test_pgsql_json;
create table test_pgsql_json (
id int primary key,
value json
);
`)
if !assert.NoError(t, err) {
return
}
defer db.Exec(`drop table test_pgsql_json`)
var obj struct {
A string
B int
}
obj.A = "test"
obj.B = 7
var ok bool
err = db.QueryRow(`
insert into test_pgsql_json (id, value)
values (1, $1)
returning value is not null
`, pgsql.JSON(&obj)).Scan(&ok)
assert.NoError(t, err)
assert.True(t, ok)
obj.A = ""
obj.B = 0
err = db.QueryRow(`
select value
from test_pgsql_json
where id = 1
`).Scan(pgsql.JSON(&obj))
if !assert.NoError(t, err) {
return
}
assert.Equal(t, "test", obj.A)
assert.Equal(t, 7, obj.B)
obj.A = ""
obj.B = 0
err = db.QueryRow(`select null`).Scan(pgsql.JSON(&obj))
assert.NoError(t, err)
}