-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
299 additions
and
207 deletions.
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
cmd/fuego/templates/controller/controller_controller_test.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-fuego/fuego" | ||
) | ||
|
||
type MyContext struct { | ||
fuego.BaseContext | ||
} | ||
|
||
// Controllers tests for /NewController | ||
// Does not test the actual HTTP requests, only the controller logic. | ||
// Beware of the Context, it is not exactly the same as the one used in the actual HTTP requests. | ||
|
||
func TestNewControllerRessources_Routes(t *testing.T) { | ||
s := fuego.NewServer() | ||
|
||
rs := NewControllerRessources{ | ||
// Dependency Injection | ||
NewControllerService: NewControllerServiceMock{ | ||
getAllNewControllerLength: 2, | ||
}, | ||
} | ||
|
||
rs.Routes(s) | ||
|
||
t.Run("GET /newController", func(t *testing.T) { | ||
t.Run("ok", func(t *testing.T) { | ||
r := httptest.NewRequest("GET", "/newController", nil) | ||
|
||
newControllers, err := rs.getAllNewController(fuego.BaseContext{ | ||
Req: r, | ||
}) | ||
if err != nil { | ||
t.Errorf("error: " + err.Error()) | ||
} | ||
if len(newControllers) != 2 { | ||
t.Errorf("error: newControllers is of wrong length, expected %d", 2) | ||
} | ||
}) | ||
}) | ||
|
||
t.Run("getNewController", func(t *testing.T) { | ||
r := httptest.NewRequest("GET", "/newController/123", nil) | ||
w := httptest.NewRecorder() | ||
|
||
_, err := rs.getNewController(fuego.BaseContext{Req: r, Res: w}) | ||
if err != nil { | ||
t.Errorf("error: " + err.Error()) | ||
} | ||
}) | ||
|
||
t.Run("POST /newController", func(t *testing.T) { | ||
body := `{"name": "hi"}` | ||
r := httptest.NewRequest("GET", "/newController/123", strings.NewReader(body)) | ||
w := httptest.NewRecorder() | ||
|
||
_, err := rs.postNewController(&fuego.BaseContextWithBody[NewControllerCreate]{ | ||
BaseContext: fuego.BaseContext{Req: r, Res: w}, | ||
}) | ||
if err != nil { | ||
t.Errorf("error: " + err.Error()) | ||
} | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-fuego/fuego" | ||
) | ||
|
||
// HTTP tests for /NewController | ||
func TestNewControllerRessources_HttpRoutes(t *testing.T) { | ||
s := fuego.NewServer() | ||
|
||
rs := NewControllerRessources{ | ||
// Dependency Injection | ||
NewControllerService: NewControllerServiceMock{ | ||
getAllNewControllerLength: 2, | ||
}, | ||
} | ||
|
||
rs.Routes(s) | ||
|
||
t.Run("404", func(t *testing.T) { | ||
r := httptest.NewRequest("GET", "/non-existing", nil) | ||
w := httptest.NewRecorder() | ||
|
||
s.Mux.ServeHTTP(w, r) | ||
|
||
if w.Code != 404 { | ||
t.Errorf("Expected status code 404, got %d", w.Code) | ||
} | ||
}) | ||
|
||
t.Run("GET /newController", func(t *testing.T) { | ||
r := httptest.NewRequest("GET", "/newController/", nil) | ||
w := httptest.NewRecorder() | ||
|
||
s.Mux.ServeHTTP(w, r) | ||
|
||
if w.Code != 200 { | ||
t.Errorf("Expected status code 200, got %d", w.Code) | ||
} | ||
expectedBody := `[{"id":"","name":""},{"id":"","name":""}]` | ||
actualBody := strings.TrimSpace(w.Body.String()) | ||
if expectedBody != actualBody { | ||
t.Errorf("Expected body %s, got %s", expectedBody, actualBody) | ||
} | ||
}) | ||
|
||
t.Run("GET /newController/{id}", func(t *testing.T) { | ||
r := httptest.NewRequest("GET", "/newController/123", nil) | ||
w := httptest.NewRecorder() | ||
|
||
s.Mux.ServeHTTP(w, r) | ||
|
||
if w.Code != 200 { | ||
t.Errorf("Expected status code 200, got %d", w.Code) | ||
} | ||
expectedBody := `{"id":"","name":""}` | ||
actualBody := strings.TrimSpace(w.Body.String()) | ||
if expectedBody != actualBody { | ||
t.Errorf("Expected body %s, got %s", expectedBody, actualBody) | ||
} | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package controller | ||
|
||
import "github.com/go-fuego/fuego" | ||
|
||
type NewControllerServiceMock struct { | ||
getAllNewControllerLength int | ||
} | ||
|
||
var _ NewControllerService = NewControllerServiceMock{} | ||
|
||
// CreateNewController implements NewControllerService. | ||
func (NewControllerServiceMock) CreateNewController(body NewControllerCreate) (NewController, error) { | ||
return NewController{ | ||
ID: "randomID", | ||
Name: body.Name, | ||
}, nil | ||
} | ||
|
||
// DeleteNewController implements NewControllerService. | ||
func (NewControllerServiceMock) DeleteNewController(id string) (any, error) { | ||
return nil, nil | ||
} | ||
|
||
// GetNewController implements NewControllerService. | ||
func (NewControllerServiceMock) GetNewController(id string) (NewController, error) { | ||
if id == "404" { | ||
return NewController{}, fuego.HTTPError{ | ||
Message: "not found", | ||
StatusCode: 404, | ||
} | ||
} | ||
return NewController{}, nil | ||
} | ||
|
||
// UpdateNewController implements NewControllerService. | ||
func (NewControllerServiceMock) UpdateNewController(id string, input NewControllerUpdate) (NewController, error) { | ||
return NewController{}, nil | ||
} | ||
|
||
func (m NewControllerServiceMock) GetAllNewController() ([]NewController, error) { | ||
allNewController := make([]NewController, m.getAllNewControllerLength) | ||
return allNewController, nil | ||
} |
Oops, something went wrong.