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

Function TestBindStruct

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

Source from the content-addressed store, hash-verified

1605}
1606
1607func TestBindStruct(t *testing.T) {
1608 var err error
1609
1610 q1 := `INSERT INTO foo (a, b, c, d) VALUES (:name, :age, :first, :last)`
1611
1612 type tt struct {
1613 Name string
1614 Age int
1615 First string
1616 Last string
1617 }
1618
1619 type tt2 struct {
1620 Field1 string `db:"field_1"`
1621 Field2 string `db:"field_2"`
1622 }
1623
1624 type tt3 struct {
1625 tt2
1626 Name string
1627 }
1628
1629 am := tt{"Jason Moiron", 30, "Jason", "Moiron"}
1630
1631 bq, args, _ := bindStruct(QUESTION, q1, am, mapper())
1632 expect := `INSERT INTO foo (a, b, c, d) VALUES (?, ?, ?, ?)`
1633 if bq != expect {
1634 t.Errorf("Interpolation of query failed: got `%v`, expected `%v`\n", bq, expect)
1635 }
1636
1637 if args[0].(string) != "Jason Moiron" {
1638 t.Errorf("Expected `Jason Moiron`, got %v\n", args[0])
1639 }
1640
1641 if args[1].(int) != 30 {
1642 t.Errorf("Expected 30, got %v\n", args[1])
1643 }
1644
1645 if args[2].(string) != "Jason" {
1646 t.Errorf("Expected Jason, got %v\n", args[2])
1647 }
1648
1649 if args[3].(string) != "Moiron" {
1650 t.Errorf("Expected Moiron, got %v\n", args[3])
1651 }
1652
1653 am2 := tt2{"Hello", "World"}
1654 bq, args, _ = bindStruct(QUESTION, "INSERT INTO foo (a, b) VALUES (:field_2, :field_1)", am2, mapper())
1655 expect = `INSERT INTO foo (a, b) VALUES (?, ?)`
1656 if bq != expect {
1657 t.Errorf("Interpolation of query failed: got `%v`, expected `%v`\n", bq, expect)
1658 }
1659
1660 if args[0].(string) != "World" {
1661 t.Errorf("Expected 'World', got %s\n", args[0].(string))
1662 }
1663 if args[1].(string) != "Hello" {
1664 t.Errorf("Expected 'Hello', got %s\n", args[1].(string))

Callers

nothing calls this directly

Calls 3

bindStructFunction · 0.85
mapperFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected