(t *testing.T)
| 970 | } |
| 971 | |
| 972 | func 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(` |
nothing calls this directly
no test coverage detected