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

Function TestJoinQueryNamedPointerStructs

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

Source from the content-addressed store, hash-verified

503}
504
505func TestJoinQueryNamedPointerStructs(t *testing.T) {
506 type Employee struct {
507 Name string
508 ID int64
509 // BossID is an id into the employee table
510 BossID sql.NullInt64 `db:"boss_id"`
511 }
512 type Boss Employee
513
514 RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
515 loadDefaultFixture(db, t)
516
517 var employees []struct {
518 Emp1 *Employee `db:"emp1"`
519 Emp2 *Employee `db:"emp2"`
520 *Boss `db:"boss"`
521 }
522
523 err := db.Select(
524 &employees,
525 `SELECT emp.name "emp1.name", emp.id "emp1.id", emp.boss_id "emp1.boss_id",
526 emp.name "emp2.name", emp.id "emp2.id", emp.boss_id "emp2.boss_id",
527 boss.id "boss.id", boss.name "boss.name" FROM employees AS emp
528 JOIN employees AS boss ON emp.boss_id = boss.id
529 `)
530 if err != nil {
531 t.Fatal(err)
532 }
533
534 for _, em := range employees {
535 if len(em.Emp1.Name) == 0 || len(em.Emp2.Name) == 0 {
536 t.Errorf("Expected non zero lengthed name.")
537 }
538 if em.Emp1.BossID.Int64 != em.Boss.ID || em.Emp2.BossID.Int64 != em.Boss.ID {
539 t.Errorf("Expected boss ids to match")
540 }
541 }
542 })
543}
544
545func TestSelectSliceMapTime(t *testing.T) {
546 RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {

Callers

nothing calls this directly

Calls 4

RunWithSchemaFunction · 0.85
loadDefaultFixtureFunction · 0.85
ErrorfMethod · 0.80
SelectMethod · 0.45

Tested by

no test coverage detected