mirror of https://github.com/jackc/pgx.git
Renable json tests
parent
92cff1d961
commit
6f0ec4c470
258
values_test.go
258
values_test.go
|
@ -6,6 +6,8 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDateTranscode(t *testing.T) {
|
func TestDateTranscode(t *testing.T) {
|
||||||
|
@ -77,159 +79,151 @@ func TestTimestampTzTranscode(t *testing.T) {
|
||||||
|
|
||||||
// TODO - move these tests to pgtype
|
// TODO - move these tests to pgtype
|
||||||
|
|
||||||
// func TestJSONAndJSONBTranscode(t *testing.T) {
|
func TestJSONAndJSONBTranscode(t *testing.T) {
|
||||||
// t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// conn := mustConnect(t, *defaultConnConfig)
|
conn := mustConnect(t, *defaultConnConfig)
|
||||||
// defer closeConn(t, conn)
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
// for _, oid := range []pgtype.Oid{pgx.JsonOid, pgx.JsonbOid} {
|
for _, typename := range []string{"json", "jsonb"} {
|
||||||
// if _, ok := conn.ConnInfo.DataTypeForOid(oid); !ok {
|
if _, ok := conn.ConnInfo.DataTypeForName(typename); !ok {
|
||||||
// return // No JSON/JSONB type -- must be running against old PostgreSQL
|
continue // No JSON/JSONB type -- must be running against old PostgreSQL
|
||||||
// }
|
}
|
||||||
|
|
||||||
// for _, format := range []int16{pgx.TextFormatCode, pgx.BinaryFormatCode} {
|
testJSONString(t, conn, typename)
|
||||||
// pgtype := conn.PgTypes[oid]
|
testJSONStringPointer(t, conn, typename)
|
||||||
// pgtype.DefaultFormat = format
|
testJSONSingleLevelStringMap(t, conn, typename)
|
||||||
// conn.PgTypes[oid] = pgtype
|
testJSONNestedMap(t, conn, typename)
|
||||||
|
testJSONStringArray(t, conn, typename)
|
||||||
|
testJSONInt64Array(t, conn, typename)
|
||||||
|
testJSONInt16ArrayFailureDueToOverflow(t, conn, typename)
|
||||||
|
testJSONStruct(t, conn, typename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// typename := conn.PgTypes[oid].Name
|
func testJSONString(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
|
input := `{"key": "value"}`
|
||||||
|
expectedOutput := map[string]string{"key": "value"}
|
||||||
|
var output map[string]string
|
||||||
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// testJSONString(t, conn, typename, format)
|
if !reflect.DeepEqual(expectedOutput, output) {
|
||||||
// testJSONStringPointer(t, conn, typename, format)
|
t.Errorf("%s: Did not transcode map[string]string successfully: %v is not %v", typename, expectedOutput, output)
|
||||||
// testJSONSingleLevelStringMap(t, conn, typename, format)
|
return
|
||||||
// testJSONNestedMap(t, conn, typename, format)
|
}
|
||||||
// testJSONStringArray(t, conn, typename, format)
|
}
|
||||||
// testJSONInt64Array(t, conn, typename, format)
|
|
||||||
// testJSONInt16ArrayFailureDueToOverflow(t, conn, typename, format)
|
|
||||||
// testJSONStruct(t, conn, typename, format)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func testJSONString(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
func testJSONStringPointer(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
// input := `{"key": "value"}`
|
input := `{"key": "value"}`
|
||||||
// expectedOutput := map[string]string{"key": "value"}
|
expectedOutput := map[string]string{"key": "value"}
|
||||||
// var output map[string]string
|
var output map[string]string
|
||||||
// err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
err := conn.QueryRow("select $1::"+typename, &input).Scan(&output)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// t.Errorf("%s %d: QueryRow Scan failed: %v", typename, format, err)
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if !reflect.DeepEqual(expectedOutput, output) {
|
if !reflect.DeepEqual(expectedOutput, output) {
|
||||||
// t.Errorf("%s %d: Did not transcode map[string]string successfully: %v is not %v", typename, format, expectedOutput, output)
|
t.Errorf("%s: Did not transcode map[string]string successfully: %v is not %v", typename, expectedOutput, output)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func testJSONStringPointer(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
func testJSONSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
// input := `{"key": "value"}`
|
input := map[string]string{"key": "value"}
|
||||||
// expectedOutput := map[string]string{"key": "value"}
|
var output map[string]string
|
||||||
// var output map[string]string
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
// err := conn.QueryRow("select $1::"+typename, &input).Scan(&output)
|
if err != nil {
|
||||||
// if err != nil {
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
// t.Errorf("%s %d: QueryRow Scan failed: %v", typename, format, err)
|
return
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// if !reflect.DeepEqual(expectedOutput, output) {
|
if !reflect.DeepEqual(input, output) {
|
||||||
// t.Errorf("%s %d: Did not transcode map[string]string successfully: %v is not %v", typename, format, expectedOutput, output)
|
t.Errorf("%s: Did not transcode map[string]string successfully: %v is not %v", typename, input, output)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func testJSONSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
func testJSONNestedMap(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
// input := map[string]string{"key": "value"}
|
input := map[string]interface{}{
|
||||||
// var output map[string]string
|
"name": "Uncanny",
|
||||||
// err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
"stats": map[string]interface{}{"hp": float64(107), "maxhp": float64(150)},
|
||||||
// if err != nil {
|
"inventory": []interface{}{"phone", "key"},
|
||||||
// t.Errorf("%s %d: QueryRow Scan failed: %v", typename, format, err)
|
}
|
||||||
// return
|
var output map[string]interface{}
|
||||||
// }
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// if !reflect.DeepEqual(input, output) {
|
if !reflect.DeepEqual(input, output) {
|
||||||
// t.Errorf("%s %d: Did not transcode map[string]string successfully: %v is not %v", typename, format, input, output)
|
t.Errorf("%s: Did not transcode map[string]interface{} successfully: %v is not %v", typename, input, output)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func testJSONNestedMap(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
func testJSONStringArray(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
// input := map[string]interface{}{
|
input := []string{"foo", "bar", "baz"}
|
||||||
// "name": "Uncanny",
|
var output []string
|
||||||
// "stats": map[string]interface{}{"hp": float64(107), "maxhp": float64(150)},
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
// "inventory": []interface{}{"phone", "key"},
|
if err != nil {
|
||||||
// }
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
// var output map[string]interface{}
|
}
|
||||||
// err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
|
||||||
// if err != nil {
|
|
||||||
// t.Errorf("%s %d: QueryRow Scan failed: %v", typename, format, err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if !reflect.DeepEqual(input, output) {
|
if !reflect.DeepEqual(input, output) {
|
||||||
// t.Errorf("%s %d: Did not transcode map[string]interface{} successfully: %v is not %v", typename, format, input, output)
|
t.Errorf("%s: Did not transcode []string successfully: %v is not %v", typename, input, output)
|
||||||
// return
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// func testJSONStringArray(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
func testJSONInt64Array(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
// input := []string{"foo", "bar", "baz"}
|
input := []int64{1, 2, 234432}
|
||||||
// var output []string
|
var output []int64
|
||||||
// err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// t.Errorf("%s %d: QueryRow Scan failed: %v", typename, format, err)
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if !reflect.DeepEqual(input, output) {
|
if !reflect.DeepEqual(input, output) {
|
||||||
// t.Errorf("%s %d: Did not transcode []string successfully: %v is not %v", typename, format, input, output)
|
t.Errorf("%s: Did not transcode []int64 successfully: %v is not %v", typename, input, output)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// func testJSONInt64Array(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
func testJSONInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
// input := []int64{1, 2, 234432}
|
input := []int{1, 2, 234432}
|
||||||
// var output []int64
|
var output []int16
|
||||||
// err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
// if err != nil {
|
if err == nil || err.Error() != "can't scan into dest[0]: json: cannot unmarshal number 234432 into Go value of type int16" {
|
||||||
// t.Errorf("%s %d: QueryRow Scan failed: %v", typename, format, err)
|
t.Errorf("%s: Expected *json.UnmarkalTypeError, but got %v", typename, err)
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if !reflect.DeepEqual(input, output) {
|
func testJSONStruct(t *testing.T, conn *pgx.Conn, typename string) {
|
||||||
// t.Errorf("%s %d: Did not transcode []int64 successfully: %v is not %v", typename, format, input, output)
|
type person struct {
|
||||||
// }
|
Name string `json:"name"`
|
||||||
// }
|
Age int `json:"age"`
|
||||||
|
}
|
||||||
|
|
||||||
// func testJSONInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
input := person{
|
||||||
// input := []int{1, 2, 234432}
|
Name: "John",
|
||||||
// var output []int16
|
Age: 42,
|
||||||
// err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
}
|
||||||
// if err == nil || err.Error() != "can't scan into dest[0]: json: cannot unmarshal number 234432 into Go value of type int16" {
|
|
||||||
// t.Errorf("%s %d: Expected *json.UnmarkalTypeError, but got %v", typename, format, err)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func testJSONStruct(t *testing.T, conn *pgx.Conn, typename string, format int16) {
|
var output person
|
||||||
// type person struct {
|
|
||||||
// Name string `json:"name"`
|
|
||||||
// Age int `json:"age"`
|
|
||||||
// }
|
|
||||||
|
|
||||||
// input := person{
|
err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
||||||
// Name: "John",
|
if err != nil {
|
||||||
// Age: 42,
|
t.Errorf("%s: QueryRow Scan failed: %v", typename, err)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// var output person
|
if !reflect.DeepEqual(input, output) {
|
||||||
|
t.Errorf("%s: Did not transcode struct successfully: %v is not %v", typename, input, output)
|
||||||
// err := conn.QueryRow("select $1::"+typename, input).Scan(&output)
|
}
|
||||||
// if err != nil {
|
}
|
||||||
// t.Errorf("%s %d: QueryRow Scan failed: %v", typename, format, err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if !reflect.DeepEqual(input, output) {
|
|
||||||
// t.Errorf("%s %d: Did not transcode struct successfully: %v is not %v", typename, format, input, output)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
func mustParseCidr(t *testing.T, s string) *net.IPNet {
|
func mustParseCidr(t *testing.T, s string) *net.IPNet {
|
||||||
_, ipnet, err := net.ParseCIDR(s)
|
_, ipnet, err := net.ParseCIDR(s)
|
||||||
|
|
Loading…
Reference in New Issue