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

Function TestJoinQueryContext

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

Source from the content-addressed store, hash-verified

295}
296
297func TestJoinQueryContext(t *testing.T) {
298 type Employee struct {
299 Name string
300 ID int64
301 // BossID is an id into the employee table
302 BossID sql.NullInt64 `db:"boss_id"`
303 }
304 type Boss Employee
305
306 RunWithSchemaContext(context.Background(), defaultSchema, t, func(ctx context.Context, db *DB, t *testing.T) {
307 loadDefaultFixtureContext(ctx, db, t)
308
309 var employees []struct {
310 Employee
311 Boss `db:"boss"`
312 }
313
314 err := db.SelectContext(ctx,
315 &employees,
316 `SELECT employees.*, boss.id "boss.id", boss.name "boss.name" FROM employees
317 JOIN employees AS boss ON employees.boss_id = boss.id`)
318 if err != nil {
319 t.Fatal(err)
320 }
321
322 for _, em := range employees {
323 if len(em.Employee.Name) == 0 {
324 t.Errorf("Expected non zero lengthed name.")
325 }
326 if em.Employee.BossID.Int64 != em.Boss.ID {
327 t.Errorf("Expected boss ids to match")
328 }
329 }
330 })
331}
332
333func TestJoinQueryNamedPointerStructsContext(t *testing.T) {
334 type Employee struct {

Callers

nothing calls this directly

Calls 4

RunWithSchemaContextFunction · 0.85
ErrorfMethod · 0.80
SelectContextMethod · 0.45

Tested by

no test coverage detected