mirror of https://github.com/jackc/pgx.git
cleanup and add test for json codec
parent
6e9fa42fef
commit
2190a8e0d1
|
@ -1,15 +0,0 @@
|
||||||
{
|
|
||||||
// Use IntelliSense to learn about possible attributes.
|
|
||||||
// Hover to view descriptions of existing attributes.
|
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"name": "Launch Package",
|
|
||||||
"type": "go",
|
|
||||||
"request": "launch",
|
|
||||||
"mode": "debug",
|
|
||||||
"program": "${fileDirname}"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"go.testEnvVars": {
|
|
||||||
"PGX_TEST_DATABASE":"host=127.0.0.1 user=gamerhound password=gamerhound dbname=gamerhound"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -48,6 +48,7 @@ func TestJSONCodec(t *testing.T) {
|
||||||
Age int `json:"age"`
|
Age int `json:"age"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var str string
|
||||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "json", []pgxtest.ValueRoundTripTest{
|
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "json", []pgxtest.ValueRoundTripTest{
|
||||||
{nil, new(*jsonStruct), isExpectedEq((*jsonStruct)(nil))},
|
{nil, new(*jsonStruct), isExpectedEq((*jsonStruct)(nil))},
|
||||||
{map[string]any(nil), new(*string), isExpectedEq((*string)(nil))},
|
{map[string]any(nil), new(*string), isExpectedEq((*string)(nil))},
|
||||||
|
@ -65,6 +66,14 @@ func TestJSONCodec(t *testing.T) {
|
||||||
{Issue1805(7), new(Issue1805), isExpectedEq(Issue1805(7))},
|
{Issue1805(7), new(Issue1805), isExpectedEq(Issue1805(7))},
|
||||||
// Test driver.Scanner is used before json.Unmarshaler (https://github.com/jackc/pgx/issues/2146)
|
// Test driver.Scanner is used before json.Unmarshaler (https://github.com/jackc/pgx/issues/2146)
|
||||||
{Issue2146(7), new(*Issue2146), isPtrExpectedEq(Issue2146(7))},
|
{Issue2146(7), new(*Issue2146), isPtrExpectedEq(Issue2146(7))},
|
||||||
|
|
||||||
|
// Test driver.Scanner without pointer receiver (https://github.com/jackc/pgx/issues/2204)
|
||||||
|
{NonPointerJSONScanner{V: stringPtr("{}")}, NonPointerJSONScanner{V: &str}, func(a any) bool {
|
||||||
|
if n, is := a.(NonPointerJSONScanner); is {
|
||||||
|
return *n.V == "{}"
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}},
|
||||||
})
|
})
|
||||||
|
|
||||||
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, pgxtest.KnownOIDQueryExecModes, "json", []pgxtest.ValueRoundTripTest{
|
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, pgxtest.KnownOIDQueryExecModes, "json", []pgxtest.ValueRoundTripTest{
|
||||||
|
@ -136,6 +145,27 @@ func (i Issue2146) Value() (driver.Value, error) {
|
||||||
return string(b), err
|
return string(b), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NonPointerJSONScanner struct {
|
||||||
|
V *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i NonPointerJSONScanner) Scan(src any) error {
|
||||||
|
switch c := src.(type) {
|
||||||
|
case string:
|
||||||
|
*i.V = c
|
||||||
|
case []byte:
|
||||||
|
*i.V = string(c)
|
||||||
|
default:
|
||||||
|
return errors.New("unknown source type")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i NonPointerJSONScanner) Value() (driver.Value, error) {
|
||||||
|
return i.V, nil
|
||||||
|
}
|
||||||
|
|
||||||
// https://github.com/jackc/pgx/issues/1273#issuecomment-1221414648
|
// https://github.com/jackc/pgx/issues/1273#issuecomment-1221414648
|
||||||
func TestJSONCodecUnmarshalSQLNull(t *testing.T) {
|
func TestJSONCodecUnmarshalSQLNull(t *testing.T) {
|
||||||
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
|
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
|
||||||
|
|
80
tete/main.go
80
tete/main.go
|
@ -1,80 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"reflect"
|
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
pool, err := pgxpool.New(context.Background(), "postgres://gamerhound:gamerhound@localhost:5432/gamerhound")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer pool.Close()
|
|
||||||
|
|
||||||
// Create the enum type.
|
|
||||||
_, err = pool.Exec(context.Background(), `DROP TYPE IF EXISTS test_enum_type`)
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = pool.Exec(context.Background(), `CREATE TYPE test_enum_type AS ENUM ('a', 'b')`)
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = testQuery(pool, "SELECT 'a'", "a")
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("test TEXT error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = testQuery(pool, "SELECT 'a'::test_enum_type", "a")
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("test ENUM error: %s\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = testQuery(pool, "SELECT '{}'::jsonb", "{}")
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("test JSONB error: %s\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// T implements the sql.Scanner interface.
|
|
||||||
type T struct {
|
|
||||||
v *any
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t T) Scan(v any) error {
|
|
||||||
*t.v = v
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// testQuery executes the query and checks if the scanned value matches
|
|
||||||
// the expected result.
|
|
||||||
func testQuery(pool *pgxpool.Pool, query string, expected any) error {
|
|
||||||
rows, err := pool.Query(context.Background(), query)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// defer rows.Close()
|
|
||||||
|
|
||||||
var got any
|
|
||||||
t := T{v: &got}
|
|
||||||
for rows.Next() {
|
|
||||||
if err := rows.Scan(t); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = rows.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(got, expected) {
|
|
||||||
return fmt.Errorf("expected %#v; got %#v", expected, got)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue