MCPcopy
hub / github.com/jmoiron/sqlx / TestNilInsertsContext

Function TestNilInsertsContext

sqlx_context_test.go:649–692  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

647}
648
649func TestNilInsertsContext(t *testing.T) {
650 var schema = Schema{
651 create: `
652 CREATE TABLE tt (
653 id integer,
654 value text NULL DEFAULT NULL
655 );`,
656 drop: "drop table tt;",
657 }
658
659 RunWithSchemaContext(context.Background(), schema, t, func(ctx context.Context, db *DB, t *testing.T) {
660 type TT struct {
661 ID int
662 Value *string
663 }
664 var v, v2 TT
665 r := db.Rebind
666
667 db.MustExecContext(ctx, r(`INSERT INTO tt (id) VALUES (1)`))
668 db.GetContext(ctx, &v, r(`SELECT * FROM tt`))
669 if v.ID != 1 {
670 t.Errorf("Expecting id of 1, got %v", v.ID)
671 }
672 if v.Value != nil {
673 t.Errorf("Expecting NULL to map to nil, got %s", *v.Value)
674 }
675
676 v.ID = 2
677 // NOTE: this incidentally uncovered a bug which was that named queries with
678 // pointer destinations would not work if the passed value here was not addressable,
679 // as reflectx.FieldByIndexes attempts to allocate nil pointer receivers for
680 // writing. This was fixed by creating & using the reflectx.FieldByIndexesReadOnly
681 // function. This next line is important as it provides the only coverage for this.
682 db.NamedExecContext(ctx, `INSERT INTO tt (id, value) VALUES (:id, :value)`, v)
683
684 db.GetContext(ctx, &v2, r(`SELECT * FROM tt WHERE id=2`))
685 if v.ID != v2.ID {
686 t.Errorf("%v != %v", v.ID, v2.ID)
687 }
688 if v2.Value != nil {
689 t.Errorf("Expecting NULL to map to nil, got %s", *v.Value)
690 }
691 })
692}
693
694func TestScanErrorContext(t *testing.T) {
695 var schema = Schema{

Callers

nothing calls this directly

Calls 5

RunWithSchemaContextFunction · 0.85
ErrorfMethod · 0.80
MustExecContextMethod · 0.45
GetContextMethod · 0.45
NamedExecContextMethod · 0.45

Tested by

no test coverage detected