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

Function TestJoinQuery

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

Source from the content-addressed store, hash-verified

467}
468
469func TestJoinQuery(t *testing.T) {
470 type Employee struct {
471 Name string
472 ID int64
473 // BossID is an id into the employee table
474 BossID sql.NullInt64 `db:"boss_id"`
475 }
476 type Boss Employee
477
478 RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
479 loadDefaultFixture(db, t)
480
481 var employees []struct {
482 Employee
483 Boss `db:"boss"`
484 }
485
486 err := db.Select(
487 &employees,
488 `SELECT employees.*, boss.id "boss.id", boss.name "boss.name" FROM employees
489 JOIN employees AS boss ON employees.boss_id = boss.id`)
490 if err != nil {
491 t.Fatal(err)
492 }
493
494 for _, em := range employees {
495 if len(em.Employee.Name) == 0 {
496 t.Errorf("Expected non zero lengthed name.")
497 }
498 if em.Employee.BossID.Int64 != em.Boss.ID {
499 t.Errorf("Expected boss ids to match")
500 }
501 }
502 })
503}
504
505func TestJoinQueryNamedPointerStructs(t *testing.T) {
506 type Employee struct {

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