Skip to content

Commit

Permalink
pgtype/hstore_test.go: Add coverage for text protocol
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evanj authored and jackc committed May 15, 2023
1 parent cead918 commit bbcc4fc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pgtype/hstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bbcc4fc

Please sign in to comment.