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

Function TestMissingNamesContextContext

sqlx_context_test.go:89–203  ·  sqlx_context_test.go::TestMissingNamesContextContext

Test a new backwards compatible feature, that missing scan destinations will silently scan into sql.RawText rather than failing/panicing

(t *testing.T)

Source from the content-addressed store, hash-verified

87// Test a new backwards compatible feature, that missing scan destinations
88// will silently scan into sql.RawText rather than failing/panicing
89func TestMissingNamesContextContext(t *testing.T) {
90 RunWithSchemaContext(context.Background(), defaultSchema, t, func(ctx context.Context, db *DB, t *testing.T) {
91 loadDefaultFixtureContext(ctx, db, t)
92 type PersonPlus struct {
93 FirstName string `db:"first_name"`
94 LastName string `db:"last_name"`
95 Email string
96 // AddedAt time.Time `db:"added_at"`
97 }
98
99 // test Select first
100 pps := []PersonPlus{}
101 // pps lacks added_at destination
102 err := db.SelectContext(ctx, &pps, "SELECT * FROM person")
103 if err == nil {
104 t.Error("Expected missing name from Select to fail, but it did not.")
105 }
106
107 // test Get
108 pp := PersonPlus{}
109 err = db.GetContext(ctx, &pp, "SELECT * FROM person LIMIT 1")
110 if err == nil {
111 t.Error("Expected missing name Get to fail, but it did not.")
112 }
113
114 // test naked StructScan
115 pps = []PersonPlus{}
116 rows, err := db.QueryContext(ctx, "SELECT * FROM person LIMIT 1")
117 if err != nil {
118 t.Fatal(err)
119 }
120 rows.Next()
121 err = StructScan(rows, &pps)
122 if err == nil {
123 t.Error("Expected missing name in StructScan to fail, but it did not.")
124 }
125 rows.Close()
126
127 // now try various things with unsafe set.
128 db = db.Unsafe()
129 pps = []PersonPlus{}
130 err = db.SelectContext(ctx, &pps, "SELECT * FROM person")
131 if err != nil {
132 t.Error(err)
133 }
134
135 // test Get
136 pp = PersonPlus{}
137 err = db.GetContext(ctx, &pp, "SELECT * FROM person LIMIT 1")
138 if err != nil {
139 t.Error(err)
140 }
141
142 // test naked StructScan
143 pps = []PersonPlus{}
144 rowsx, err := db.QueryxContext(ctx, "SELECT * FROM person LIMIT 1")
145 if err != nil {
146 t.Fatal(err)

Callers

nothing calls this directly

Calls 14

RunWithSchemaContextFunction · 0.85
StructScanFunction · 0.85
isUnsafeFunction · 0.85
ErrorMethod · 0.80
NextMethod · 0.80
ErrorfMethod · 0.80
QueryContextMethod · 0.65
CloseMethod · 0.65
QueryxContextMethod · 0.65
SelectContextMethod · 0.45
GetContextMethod · 0.45

Tested by

no test coverage detected