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

Function TestEmbeddedStructsContextContext

sqlx_context_test.go:205–295  ·  sqlx_context_test.go::TestEmbeddedStructsContextContext
(t *testing.T)

Source from the content-addressed store, hash-verified

203}
204
205func TestEmbeddedStructsContextContext(t *testing.T) {
206 type Loop1 struct{ Person }
207 type Loop2 struct{ Loop1 }
208 type Loop3 struct{ Loop2 }
209
210 RunWithSchemaContext(context.Background(), defaultSchema, t, func(ctx context.Context, db *DB, t *testing.T) {
211 loadDefaultFixtureContext(ctx, db, t)
212 peopleAndPlaces := []PersonPlace{}
213 err := db.SelectContext(
214 ctx,
215 &peopleAndPlaces,
216 `SELECT person.*, place.* FROM
217 person natural join place`)
218 if err != nil {
219 t.Fatal(err)
220 }
221 for _, pp := range peopleAndPlaces {
222 if len(pp.Person.FirstName) == 0 {
223 t.Errorf("Expected non zero lengthed first name.")
224 }
225 if len(pp.Place.Country) == 0 {
226 t.Errorf("Expected non zero lengthed country.")
227 }
228 }
229
230 // test embedded structs with StructScan
231 rows, err := db.QueryxContext(
232 ctx,
233 `SELECT person.*, place.* FROM
234 person natural join place`)
235 if err != nil {
236 t.Error(err)
237 }
238
239 perp := PersonPlace{}
240 rows.Next()
241 err = rows.StructScan(&perp)
242 if err != nil {
243 t.Error(err)
244 }
245
246 if len(perp.Person.FirstName) == 0 {
247 t.Errorf("Expected non zero lengthed first name.")
248 }
249 if len(perp.Place.Country) == 0 {
250 t.Errorf("Expected non zero lengthed country.")
251 }
252
253 rows.Close()
254
255 // test the same for embedded pointer structs
256 peopleAndPlacesPtrs := []PersonPlacePtr{}
257 err = db.SelectContext(
258 ctx,
259 &peopleAndPlacesPtrs,
260 `SELECT person.*, place.* FROM
261 person natural join place`)
262 if err != nil {

Callers

nothing calls this directly

Calls 9

RunWithSchemaContextFunction · 0.85
ErrorfMethod · 0.80
ErrorMethod · 0.80
NextMethod · 0.80
QueryxContextMethod · 0.65
CloseMethod · 0.65
SelectContextMethod · 0.45
StructScanMethod · 0.45

Tested by

no test coverage detected