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

Function TestNestedResource

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

Source from the content-addressed store, hash-verified

884}
885
886func TestNestedResource(t *testing.T) {
887 is := is.New(t)
888 ctx := context.Background()
889 dir := t.TempDir()
890 td := testdir.New(dir)
891 td.Files["controller/users/users.go"] = `
892 package users
893 type DB struct {}
894 type Controller struct {
895 DB *DB
896 }
897 type User struct {
898 ID int ` + "`" + `json:"id"` + "`" + `
899 Name string ` + "`" + `json:"name"` + "`" + `
900 Age int ` + "`" + `json:"age"` + "`" + `
901 }
902 func (c *Controller) Index() ([]*User, error) {
903 return []*User{{1, "a", 2}, {2, "b", 3}}, nil
904 }
905 func (c *Controller) New() {}
906 func (c *Controller) Create(name string, age int) (*User, error) {
907 return &User{3, name, age}, nil
908 }
909 func (c *Controller) Show(id int) (*User, error) {
910 return &User{id, "d", 5}, nil
911 }
912 func (c *Controller) Edit(id int) {}
913 func (c *Controller) Update(id int, name *string, age *int) error {
914 return nil
915 }
916 func (c *Controller) Delete(id int) error {
917 return nil
918 }
919 `
920 is.NoErr(td.Write(ctx))
921 cli := testcli.New(dir)
922 app, err := cli.Start(ctx, "run")
923 is.NoErr(err)
924 defer app.Close()
925 res, err := app.GetJSON("/users")
926 is.NoErr(err)
927 is.NoErr(res.Diff(`
928 HTTP/1.1 200 OK
929 Content-Type: application/json
930
931 [{"id":1,"name":"a","age":2},{"id":2,"name":"b","age":3}]
932 `))
933 res, err = app.GetJSON("/users/new")
934 is.NoErr(err)
935 is.NoErr(res.Diff(`
936 HTTP/1.1 204 No Content
937 `))
938 res, err = app.PostJSON("/users?name=matt&age=10", nil)
939 is.NoErr(err)
940 is.NoErr(res.Diff(`
941 HTTP/1.1 200 OK
942 Content-Type: application/json
943

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