From bbcc4fc0b8077e021dee828e7623ac70adcece3d Mon Sep 17 00:00:00 2001 From: Evan Jones Date: Mon, 15 May 2023 11:43:34 -0400 Subject: [PATCH] pgtype/hstore_test.go: Add coverage for text protocol The existing test registers pgtype.Hstore in the text map, then uses the query modes that use the binary protocol. The existing test did not use the text parsing code. Add a version of the test that uses pgtype.Hstore as the input and output argument in all query modes, and tests it without registering the codec. --- pgtype/hstore_test.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pgtype/hstore_test.go b/pgtype/hstore_test.go index 9c20ef5cc..5b1b5859c 100644 --- a/pgtype/hstore_test.go +++ b/pgtype/hstore_test.go @@ -181,7 +181,34 @@ func TestHstoreCodec(t *testing.T) { pgxtest.RunValueRoundTripTests(context.Background(), t, ctr, pgxtest.KnownOIDQueryExecModes, "hstore", tests) - // scan empty and NULL: should be different + // run the tests using pgtype.Hstore as input and output types, and test all query modes + for i := range tests { + var h pgtype.Hstore + switch typedParam := tests[i].Param.(type) { + case map[string]*string: + h = pgtype.Hstore(typedParam) + case map[string]string: + if typedParam != nil { + h = pgtype.Hstore{} + for k, v := range typedParam { + h[k] = fs(v) + } + } + } + + tests[i].Param = h + tests[i].Result = &pgtype.Hstore{} + tests[i].Test = func(input any) bool { + return reflect.DeepEqual(input, h) + } + } + pgxtest.RunValueRoundTripTests(context.Background(), t, ctr, pgxtest.AllQueryExecModes, "hstore", tests) + + // run the tests again without the codec registered: uses the text protocol + ctrWithoutCodec := defaultConnTestRunner + pgxtest.RunValueRoundTripTests(context.Background(), t, ctrWithoutCodec, pgxtest.AllQueryExecModes, "hstore", tests) + + // scan empty and NULL: should be different in all query modes pgxtest.RunWithQueryExecModes(context.Background(), t, ctr, pgxtest.AllQueryExecModes, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { h := pgtype.Hstore{"should_be_erased": nil} err := conn.QueryRow(ctx, `select cast(null as hstore)`).Scan(&h)