Skip to content

Commit

Permalink
Golden file testing
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Jul 22, 2024
1 parent c6ac637 commit 0ed93c2
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 28 deletions.
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ default: ci

ci: fmt lint cover

ci-full: ci dependencies-analyze bench
ci-full: ci dependencies-analyze openapi-check test-all-modules lint-markdown bench

test:
go test ./...

test-all-modules:
./test.sh

cover:
go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
Expand All @@ -30,14 +33,26 @@ lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@latest run

lint-markdown:
markdownlint --dot .
markdownlint --ignore documentation/node_modules --dot .

# Update golden files
golden-update:
(cd examples/petstore && go test -update)

# Check OpenAPI spec generated for the Petstore example. Uses https://github.com/daveshanley/vacuum
openapi-check:
vacuum lint -d examples/petstore/testdata/doc/openapi.json

# Examples
example:
( cd examples/full-app-gourmet && go run . -debug )

example-watch:
( cd examples/full-app-gourmet && air -- -debug )

petstore:
( cd examples/petstore && go run . -debug )

# Documentation website
docs:
go run golang.org/x/pkgsite/cmd/pkgsite@latest -http localhost:8084
Expand All @@ -46,4 +61,5 @@ docs-open:
go run golang.org/x/pkgsite/cmd/pkgsite@latest -http localhost:8084 -open

.PHONY: docs-open docs example-watch example lint lint-markdown fmt ci ci-full
.PHONY: dependencies-analyze build bench cover-web cover test
.PHONY: dependencies-analyze build bench cover-web cover test petstore test-all-modules
.PHONY: golden-update openapi-check
89 changes: 89 additions & 0 deletions examples/petstore/controllers/pets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//coverage:ignore
package controller

import (
"github.com/go-fuego/fuego"
)

type PetsRessources struct {
PetsService PetsService
}

type Pets struct {
ID string `json:"id"`
Name string `json:"name" example:"Napoleon"`
Age int `json:"age" example:"18"`
}

type PetsCreate struct {
Name string `json:"name" validate:"required,min=1,max=100" example:"Napoleon"`
Age int `json:"age" validate:"max=100" example:"18"`
}

type PetsUpdate struct {
Name string `json:"name" validate:"min=1,max=100" example:"Napoleon"`
Age int `json:"age" validate:"max=100" example:"18"`
}

func (rs PetsRessources) Routes(s *fuego.Server) {
petsGroup := fuego.Group(s, "/pets")

fuego.Get(petsGroup, "/", rs.getAllPets)
fuego.Post(petsGroup, "/", rs.postPets)

fuego.Get(petsGroup, "/{id}", rs.getPets)
fuego.Put(petsGroup, "/{id}", rs.putPets)
fuego.Delete(petsGroup, "/{id}", rs.deletePets)
}

func (rs PetsRessources) getAllPets(c fuego.ContextNoBody) ([]Pets, error) {
return rs.PetsService.GetAllPets()
}

func (rs PetsRessources) postPets(c *fuego.ContextWithBody[PetsCreate]) (Pets, error) {
body, err := c.Body()
if err != nil {
return Pets{}, err
}

new, err := rs.PetsService.CreatePets(body)
if err != nil {
return Pets{}, err
}

return new, nil
}

func (rs PetsRessources) getPets(c fuego.ContextNoBody) (Pets, error) {
id := c.PathParam("id")

return rs.PetsService.GetPets(id)
}

func (rs PetsRessources) putPets(c *fuego.ContextWithBody[PetsUpdate]) (Pets, error) {
id := c.PathParam("id")

body, err := c.Body()
if err != nil {
return Pets{}, err
}

new, err := rs.PetsService.UpdatePets(id, body)
if err != nil {
return Pets{}, err
}

return new, nil
}

func (rs PetsRessources) deletePets(c *fuego.ContextNoBody) (any, error) {
return rs.PetsService.DeletePets(c.PathParam("id"))
}

type PetsService interface {
GetPets(id string) (Pets, error)
CreatePets(PetsCreate) (Pets, error)
GetAllPets() ([]Pets, error)
UpdatePets(id string, input PetsUpdate) (Pets, error)
DeletePets(id string) (any, error)
}
21 changes: 21 additions & 0 deletions examples/petstore/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/go-fuego/fuego"
controller "github.com/go-fuego/fuego/examples/petstore/controllers"
)

func newPetStoreServer(options ...func(*fuego.Server)) *fuego.Server {
s := fuego.NewServer(options...)

petsRessources := controller.PetsRessources{
PetsService: nil, // Dependency injection: we can pass a service here (for example a database service)
}
petsRessources.Routes(s)

return s
}

func main() {
newPetStoreServer().Run()
}
30 changes: 30 additions & 0 deletions examples/petstore/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"context"
"os"
"testing"

"github.com/go-fuego/fuego"
"github.com/stretchr/testify/require"
"gotest.tools/v3/golden"
)

func TestPetstoreOpenAPIGeneration(t *testing.T) {
server := newPetStoreServer(
fuego.WithoutStartupMessages(),
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{
JsonFilePath: "testdata/doc/openapi.json",
PrettyFormatJson: true,
}),
)

server.OutputOpenAPISpec()
err := server.OpenApiSpec.Validate(context.Background())
require.NoError(t, err)

generatedSpec, err := os.ReadFile("testdata/doc/openapi.json")
require.NoError(t, err)

golden.Assert(t, string(generatedSpec), "doc/openapi.golden.json")
}
Loading

0 comments on commit 0ed93c2

Please sign in to comment.