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

Function TestNilInserts

sqlx_test.go:821–864  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

819}
820
821func TestNilInserts(t *testing.T) {
822 var schema = Schema{
823 create: `
824 CREATE TABLE tt (
825 id integer,
826 value text NULL DEFAULT NULL
827 );`,
828 drop: "drop table tt;",
829 }
830
831 RunWithSchema(schema, t, func(db *DB, t *testing.T, now string) {
832 type TT struct {
833 ID int
834 Value *string
835 }
836 var v, v2 TT
837 r := db.Rebind
838
839 db.MustExec(r(`INSERT INTO tt (id) VALUES (1)`))
840 db.Get(&v, r(`SELECT * FROM tt`))
841 if v.ID != 1 {
842 t.Errorf("Expecting id of 1, got %v", v.ID)
843 }
844 if v.Value != nil {
845 t.Errorf("Expecting NULL to map to nil, got %s", *v.Value)
846 }
847
848 v.ID = 2
849 // NOTE: this incidentally uncovered a bug which was that named queries with
850 // pointer destinations would not work if the passed value here was not addressable,
851 // as reflectx.FieldByIndexes attempts to allocate nil pointer receivers for
852 // writing. This was fixed by creating & using the reflectx.FieldByIndexesReadOnly
853 // function. This next line is important as it provides the only coverage for this.
854 db.NamedExec(`INSERT INTO tt (id, value) VALUES (:id, :value)`, v)
855
856 db.Get(&v2, r(`SELECT * FROM tt WHERE id=2`))
857 if v.ID != v2.ID {
858 t.Errorf("%v != %v", v.ID, v2.ID)
859 }
860 if v2.Value != nil {
861 t.Errorf("Expecting NULL to map to nil, got %s", *v.Value)
862 }
863 })
864}
865
866func TestScanError(t *testing.T) {
867 var schema = Schema{

Callers

nothing calls this directly

Calls 5

RunWithSchemaFunction · 0.85
ErrorfMethod · 0.80
MustExecMethod · 0.45
GetMethod · 0.45
NamedExecMethod · 0.45

Tested by

no test coverage detected