Skip to content

Commit

Permalink
move handler_test.go to internal/server package (#284)
Browse files Browse the repository at this point in the history
* moved handler_test.go to internal/server package and renamed routes_test.go, updated docs
  • Loading branch information
eric-jacobson committed Jul 28, 2024
1 parent 539428d commit b5e0e3b
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 38 deletions.
21 changes: 7 additions & 14 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ const (
internalServerPath = "internal/server"
internalDatabasePath = "internal/database"
gitHubActionPath = ".github/workflows"
testHandlerPath = "tests"
)

// ExitCLI checks if the Project has been exited, and closes
Expand Down Expand Up @@ -337,19 +336,6 @@ func (p *Project) CreateMainFile() error {
return err
}

err = p.CreatePath(testHandlerPath, projectPath)
if err != nil {
log.Printf("Error creating path: %s", projectPath)
cobra.CheckErr(err)
return err
}
// inject testhandler template
err = p.CreateFileWithInjection(testHandlerPath, projectPath, "handler_test.go", "tests")
if err != nil {
cobra.CheckErr(err)
return err
}

makeFile, err := os.Create(filepath.Join(projectPath, "Makefile"))
if err != nil {
cobra.CheckErr(err)
Expand Down Expand Up @@ -571,6 +557,13 @@ func (p *Project) CreateMainFile() error {
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(internalServerPath, projectPath, "routes_test.go", "tests")
if err != nil {
cobra.CheckErr(err)
return err
}

err = p.CreateFileWithInjection(internalServerPath, projectPath, "server.go", "server")
if err != nil {
log.Printf("Error injecting server.go file: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/framework/files/makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ docker-down:
# Test the application
test:
@echo "Testing..."
@go test ./tests -v
@go test ./... -v

{{if and (ne .DBDriver "none") (ne .DBDriver "sqlite")}}
# Integrations Tests for the application
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/default-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tests
package server
import (
"io"
"net/http"
"net/http/httptest"
"{{.ProjectName}}/internal/server"
"testing"
)
func TestHandler(t *testing.T) {
s := &server.Server{}
s := &Server{}
server := httptest.NewServer(http.HandlerFunc(s.HelloWorldHandler))
defer server.Close()
resp, err := http.Get(server.URL)
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/echo-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package tests
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"{{.ProjectName}}/internal/server"
"testing"
"github.com/labstack/echo/v4"
)
Expand All @@ -13,7 +12,7 @@ func TestHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()
c := e.NewContext(req, resp)
s := &server.Server{}
s := &Server{}
// Assertions
if err := s.HelloWorldHandler(c); err != nil {
t.Errorf("handler() error = %v", err)
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/fiber-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package tests
package server
import (
"io"
"net/http"
"{{.ProjectName}}/internal/server"
"testing"
"github.com/gofiber/fiber/v2"
)
func TestHandler(t *testing.T) {
// Create a Fiber app for testing
app := fiber.New()
// Inject the Fiber app into the server
s := &server.FiberServer{App: app}
s := &FiberServer{App: app}
// Define a route in the Fiber app
app.Get("/", s.HelloWorldHandler)
// Create a test HTTP request
Expand Down
5 changes: 2 additions & 3 deletions cmd/template/framework/files/tests/gin-test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tests
package server
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"{{.ProjectName}}/internal/server"
)
func TestHelloWorldHandler(t *testing.T) {
s := &server.Server{}
s := &Server{}
r := gin.New()
r.GET("/", s.HelloWorldHandler)
// Create a test HTTP request
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/advanced-flag/tailwind.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ The project tree would look like this:
├── internal/
│ └── server/
│ ├── routes.go
│ ├── routes_test.go
│ └── server.go
├── tests/
│ └── handler_test.go
├── go.mod
├── go.sum
├── Makefile
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/blueprint-core/db-drivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ Integrating a database adds a new layer to the project structure, primarily in t
│ │ └── database.go
│ └── /server
│ ├── routes.go
│ ├── routes_test.go
│ └── server.go
├── /tests
│ └── handler_test.go
├── go.mod
├── go.sum
├── Makefile
Expand Down
5 changes: 2 additions & 3 deletions docs/docs/blueprint-core/frameworks.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ The project is structured with a simple layout, focusing on the cmd, internal, a
├── /internal
│ └── /server
│ ├── routes.go
│ ├── routes_test.go
│ └── server.go
├── /tests
│ └── handler_test.go
├── go.mod
├── go.sum
├── Makefile
└── README.md
```
```
4 changes: 2 additions & 2 deletions docs/docs/creating-project/makefile.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ make docker-down
```

- **Test the Application:**
Executes tests defined in the `./tests` directory.
Executes all tests defined in the project.
```bash
make test
```
Expand All @@ -46,4 +46,4 @@ Monitors file changes and automatically rebuilds and restarts the application us
make watch
```

Makefile simplifies common development tasks, making it easier to build, run, test, and manage dependencies in a Go project. It enhances productivity by providing a standardized approach to project management.
Makefile simplifies common development tasks, making it easier to build, run, test, and manage dependencies in a Go project. It enhances productivity by providing a standardized approach to project management.
3 changes: 1 addition & 2 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ Here's an overview of the project structure created by Go Blueprint when all opt
│ │ └── database.go # File containing functions related to database operations.
│ └── server/
│ ├── routes.go # File defining HTTP routes.
│ ├── routes_test.go # Test file for testing HTTP handlers.
│ └── server.go # Main server logic.
├── tests/
│ └── handler_test.go # Test file for testing HTTP handlers.
├── .air.toml # Configuration file for Air, a live-reload utility.
├── docker-compose.yml # Docker Compose configuration for defining DB config.
├── .env # Environment configuration file.
Expand Down

0 comments on commit b5e0e3b

Please sign in to comment.