Skip to content

Commit

Permalink
optimize test
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Sep 4, 2024
1 parent 1f830ca commit e9fee1d
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 270 deletions.
62 changes: 0 additions & 62 deletions tests/controllers/lang_controller_test.go

This file was deleted.

20 changes: 0 additions & 20 deletions tests/controllers/main_test.go

This file was deleted.

66 changes: 0 additions & 66 deletions tests/controllers/validation_controller_test.go

This file was deleted.

File renamed without changes.
File renamed without changes.
109 changes: 101 additions & 8 deletions tests/feature/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@ package feature
import (
"fmt"
"net/http"
"strings"
"testing"

"github.com/go-resty/resty/v2"
"github.com/goravel/framework/facades"
"github.com/stretchr/testify/suite"

"goravel/app/models"
"goravel/tests"
"goravel/tests/utils"
)

type RouteTestSuite struct {
suite.Suite
tests.TestCase
http *resty.Request
}

func TestRouteTestSuite(t *testing.T) {
suite.Run(t, &RouteTestSuite{})
suite.Run(t, &RouteTestSuite{
http: resty.New().
SetBaseURL(fmt.Sprintf("http://%s:%s",
facades.Config().GetString("APP_HOST"),
facades.Config().GetString("APP_PORT"))).
SetHeader("Content-Type", "application/json").R(),
})
}

// SetupTest will run before each test in the suite.
Expand All @@ -36,7 +45,7 @@ func (s *RouteTestSuite) TestUsers() {
User models.User
}

resp, err := utils.Http().SetResult(&createdUser).SetBody(map[string]string{
resp, err := s.http.SetResult(&createdUser).SetBody(map[string]string{
"name": "Goravel",
"avatar": "https://goravel.dev/avatar.png",
}).Post("users")
Expand All @@ -51,7 +60,7 @@ func (s *RouteTestSuite) TestUsers() {
var users struct {
Users []models.User
}
resp, err = utils.Http().SetResult(&users).Get("users")
resp, err = s.http.SetResult(&users).Get("users")

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
Expand All @@ -65,7 +74,7 @@ func (s *RouteTestSuite) TestUsers() {
User models.User
}

resp, err = utils.Http().SetResult(&updatedUser).SetBody(map[string]string{
resp, err = s.http.SetResult(&updatedUser).SetBody(map[string]string{
"name": "Framework",
}).Put(fmt.Sprintf("users/%d", createdUser.User.ID))

Expand All @@ -79,7 +88,7 @@ func (s *RouteTestSuite) TestUsers() {
var user struct {
User models.User
}
resp, err = utils.Http().SetResult(&user).Get(fmt.Sprintf("users/%d", createdUser.User.ID))
resp, err = s.http.SetResult(&user).Get(fmt.Sprintf("users/%d", createdUser.User.ID))

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
Expand All @@ -88,16 +97,100 @@ func (s *RouteTestSuite) TestUsers() {
s.Equal("https://goravel.dev/avatar.png", user.User.Avatar)

// Delete the User
resp, err = utils.Http().Delete(fmt.Sprintf("users/%d", createdUser.User.ID))
resp, err = s.http.Delete(fmt.Sprintf("users/%d", createdUser.User.ID))

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"rows_affected\":1}", resp.String())

// Get Users
resp, err = utils.Http().Get("users")
resp, err = s.http.Get("users")

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"users\":[]}", resp.String())
}

func (s *RouteTestSuite) TestLang() {
tests := []struct {
name string
lang string
expectResponse string
}{
{
name: "use default lang",
expectResponse: "{\"current_locale\":\"en\",\"fallback\":\"Goravel 是一个基于 Go 语言的 Web 开发框架\",\"name\":\"Goravel Framework\"}",
},
{
name: "lang is cn",
lang: "cn",
expectResponse: "{\"current_locale\":\"cn\",\"fallback\":\"Goravel 是一个基于 Go 语言的 Web 开发框架\",\"name\":\"Goravel 框架\"}",
},
}

for _, test := range tests {
s.Run(test.name, func() {
resp, err := s.http.Get(fmt.Sprintf("/lang?lang=%s", test.lang))

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal(test.expectResponse, resp.String())
})
}
}

func (s *RouteTestSuite) TestValidationJson() {
payload := strings.NewReader(`{
"name": "Goravel",
"date": "2024-07-08 18:33:32"
}`)

resp, err := s.http.SetBody(payload).Post("/validation/json")

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"date\":\"2024-07-08 18:33:32\",\"name\":\"Goravel\"}", resp.String())
}

func (s *RouteTestSuite) TestValidationRequest() {
payload := strings.NewReader(`{
"name": "Goravel",
"date": "2024-07-08 18:33:32",
"tags": ["tag1", "tag2"],
"scores": [1, 2]
}`)

resp, err := s.http.SetBody(payload).Post("/validation/request")

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"date\":\"2024-07-08 18:33:32\",\"name\":\"Goravel\",\"scores\":[1,2],\"tags\":[\"tag1\",\"tag2\"]}", resp.String())
}

func (s *RouteTestSuite) TestThrottle() {
tests := []struct {
name string
expectStatusCode int
}{
{
name: "no throttle",
expectStatusCode: 200,
},
{
name: "throttle",
expectStatusCode: 429,
},
}

for _, test := range tests {
s.Run(test.name, func() {
var resp *resty.Response
var err error
for i := 0; i < 5; i++ {
resp, err = s.http.Get("/jwt/login")
s.Require().NoError(err)
}
s.Equal(test.expectStatusCode, resp.StatusCode())
})
}
}
36 changes: 0 additions & 36 deletions tests/middlewares/main_test.go

This file was deleted.

Loading

0 comments on commit e9fee1d

Please sign in to comment.