MCPcopy
hub / github.com/livebud/bud / TestResourceContext

Function TestResourceContext

framework/controller/controller_test.go:1667–1753  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1665}
1666
1667func TestResourceContext(t *testing.T) {
1668 is := is.New(t)
1669 ctx := context.Background()
1670 dir := t.TempDir()
1671 td := testdir.New(dir)
1672 td.Files["controller/users/users.go"] = `
1673 package users
1674 import contexts "context"
1675 type User struct {
1676 ID int ` + "`" + `json:"id"` + "`" + `
1677 Name string ` + "`" + `json:"name"` + "`" + `
1678 }
1679 type Controller struct {}
1680 func (c *Controller) Index(ctx contexts.Context) []*User {
1681 return []*User{{1, "a"}, {2, "b"}}
1682 }
1683 func (c *Controller) Create(ctx contexts.Context, name string) *User {
1684 return &User{1, name}
1685 }
1686 func (c *Controller) Show(ctx contexts.Context, id int) *User {
1687 return &User{id, "a"}
1688 }
1689 func (c *Controller) Edit(ctx contexts.Context, id int) *User {
1690 return &User{id, "a"}
1691 }
1692 func (c *Controller) Update(ctx contexts.Context, id int, name string) (*User, error) {
1693 return &User{id, name}, nil
1694 }
1695 func (c *Controller) Delete(ctx contexts.Context, id int) (*User, error) {
1696 return &User{id, "a"}, nil
1697 }
1698 `
1699 is.NoErr(td.Write(ctx))
1700 cli := testcli.New(dir)
1701 app, err := cli.Start(ctx, "run")
1702 is.NoErr(err)
1703 defer app.Close()
1704 res, err := app.GetJSON("/users")
1705 is.NoErr(err)
1706 is.NoErr(res.Diff(`
1707 HTTP/1.1 200 OK
1708 Content-Type: application/json
1709
1710 [{"id":1,"name":"a"},{"id":2,"name":"b"}]
1711 `))
1712 res, err = app.PostJSON("/users", bytes.NewBufferString(`{"name":"b"}`))
1713 is.NoErr(err)
1714 is.NoErr(res.Diff(`
1715 HTTP/1.1 200 OK
1716 Content-Type: application/json
1717
1718 {"id":1,"name":"b"}
1719 `))
1720 res, err = app.GetJSON("/users/2")
1721 is.NoErr(err)
1722 is.NoErr(res.Diff(`
1723 HTTP/1.1 200 OK
1724 Content-Type: application/json

Callers

nothing calls this directly

Calls 12

NewFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
NoErrMethod · 0.80
GetJSONMethod · 0.80
DiffMethod · 0.80
PostJSONMethod · 0.80
PatchJSONMethod · 0.80
DeleteJSONMethod · 0.80
CloseMethod · 0.65
WriteMethod · 0.45
StartMethod · 0.45

Tested by

no test coverage detected