MCPcopy Index your code
hub / github.com/livebud/bud / TestDeepNestedResource

Function TestDeepNestedResource

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

Source from the content-addressed store, hash-verified

970}
971
972func TestDeepNestedResource(t *testing.T) {
973 is := is.New(t)
974 ctx := context.Background()
975 dir := t.TempDir()
976 td := testdir.New(dir)
977 td.Files["controller/posts/comments/comments.go"] = `
978 package comments
979 type DB struct {}
980 type Controller struct {
981 DB *DB
982 }
983 type Comment struct {
984 ID int ` + "`" + `json:"id,omitempty"` + "`" + `
985 PostID int ` + "`" + `json:"post_id,omitempty"` + "`" + `
986 Title string ` + "`" + `json:"title,omitempty"` + "`" + `
987 }
988 func (c *Controller) Index(postID int) ([]*Comment, error) {
989 return []*Comment{{2, postID, "a"}, {3, postID, "b"}}, nil
990 }
991 func (c *Controller) New(postID int) {}
992 func (c *Controller) Create(postID int, title string) (*Comment, error) {
993 return &Comment{1, postID, title}, nil
994 }
995 func (c *Controller) Show(postID, id int) (*Comment, error) {
996 return &Comment{id, postID, "a"}, nil
997 }
998 func (c *Controller) Edit(postID, id int) {}
999 func (c *Controller) Update(postID, id int, title *string) (*Comment, error) {
1000 if title == nil {
1001 return &Comment{postID, id, ""}, nil
1002 }
1003 return &Comment{postID, id, *title}, nil
1004 }
1005 func (c *Controller) Delete(postID, id int) (*Comment, error) {
1006 return &Comment{postID, id, ""}, nil
1007 }
1008 `
1009 is.NoErr(td.Write(ctx))
1010 cli := testcli.New(dir)
1011 app, err := cli.Start(ctx, "run")
1012 is.NoErr(err)
1013 defer app.Close()
1014 res, err := app.GetJSON("/posts/1/comments")
1015 is.NoErr(err)
1016 is.NoErr(res.Diff(`
1017 HTTP/1.1 200 OK
1018 Content-Type: application/json
1019
1020 [{"id":2,"post_id":1,"title":"a"},{"id":3,"post_id":1,"title":"b"}]
1021 `))
1022 res, err = app.GetJSON("/posts/1/comments/new")
1023 is.NoErr(err)
1024 is.NoErr(res.Diff(`
1025 HTTP/1.1 204 No Content
1026 `))
1027 res, err = app.PostJSON("/posts/1/comments", bytes.NewBufferString(`{"title":"1st"}`))
1028 is.NoErr(err)
1029 is.NoErr(res.Diff(`

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