-
-
Save calvinmclean/16fcc97d8e9f2fe30b8d0f7c44243a24 to your computer and use it in GitHub Desktop.
babyapi TDD - 4 - Generated test boilerplate with placeholders for JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"testing" | |
"github.com/calvinmclean/babyapi" | |
babytest "github.com/calvinmclean/babyapi/test" | |
) | |
func TestAPI(t *testing.T) { | |
// Create API here | |
api := babyapi.NewAPI( | |
"MyAPI", "/", | |
func() *babyapi.NilResource { return &babyapi.NilResource{} }, | |
) | |
babytest.RunTableTest(t, api, []babytest.TestCase[*babyapi.AnyResource]{ | |
{ | |
Name: "Create", | |
Test: babytest.RequestTest[*babyapi.AnyResource]{ | |
Method: http.MethodPost, | |
Body: `TODO: JSON here`, | |
}, | |
ExpectedResponse: babytest.ExpectedResponse{ | |
Status: http.StatusCreated, | |
// Example of regexp for ID | |
// BodyRegexp: `{"id":"[0-9a-v]{20}",...}`, | |
BodyRegexp: `TODO: JSON here`, | |
}, | |
}, | |
{ | |
Name: "Get", | |
Test: babytest.RequestTest[*babyapi.AnyResource]{ | |
Method: http.MethodGet, | |
IDFunc: func(getResponse babytest.PreviousResponseGetter) string { | |
// Get the ID from Create test in order to get the resource | |
return getResponse("Create").Data.GetID() | |
}, | |
}, | |
ExpectedResponse: babytest.ExpectedResponse{ | |
Status: http.StatusOK, | |
BodyRegexp: `TODO: JSON here`, | |
}, | |
}, | |
{ | |
Name: "List", | |
Test: babytest.RequestTest[*babyapi.AnyResource]{ | |
Method: babyapi.MethodGetAll, | |
}, | |
ExpectedResponse: babytest.ExpectedResponse{ | |
Status: http.StatusOK, | |
BodyRegexp: `TODO: JSON here`, | |
}, | |
}, | |
{ | |
Name: "UpdateWithPut", | |
Test: babytest.RequestTest[*babyapi.AnyResource]{ | |
Method: http.MethodPut, | |
IDFunc: func(getResponse babytest.PreviousResponseGetter) string { | |
return getResponse("Create").Data.GetID() | |
}, | |
BodyFunc: func(getResponse babytest.PreviousResponseGetter) string { | |
id := getResponse("Create").Data.GetID() | |
return fmt.Sprintf(`"id": "%s", TODO: JSON here`, id) | |
}, | |
}, | |
ExpectedResponse: babytest.ExpectedResponse{ | |
Status: http.StatusOK, | |
BodyRegexp: `TODO: JSON here`, | |
}, | |
}, | |
{ | |
Name: "Delete", | |
Test: babytest.RequestTest[*babyapi.AnyResource]{ | |
Method: http.MethodDelete, | |
IDFunc: func(getResponse babytest.PreviousResponseGetter) string { | |
return getResponse("Create").Data.GetID() | |
}, | |
}, | |
ExpectedResponse: babytest.ExpectedResponse{ | |
Status: http.StatusNoContent, | |
}, | |
}, | |
{ | |
Name: "NotFoundAfterDelete", | |
Test: babytest.RequestTest[*babyapi.AnyResource]{ | |
Method: http.MethodGet, | |
IDFunc: func(getResponse babytest.PreviousResponseGetter) string { | |
return getResponse("Create").Data.GetID() | |
}, | |
}, | |
ExpectedResponse: babytest.ExpectedResponse{ | |
Status: http.StatusNotFound, | |
Error: "error getting resource: unexpected response with text: Resource not found.", | |
Body: `{"status":"Resource not found."}`, | |
}, | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment