Giter VIP home page Giter VIP logo

golang-unit-tests's Introduction

golang-unit-tests

ejemplos de test unitarios en go usando bun router

Requisitos

  • Instalar go y configurar las rutas necesarias como variables de entorno
  • Instalar vs studio code o editor de preferencia
  • Instalar go tools y el plugin de go
  • Iniciar repositorio

Paso a paso

  1. Iniciar modulo

       go mod init "nombre del modulo"
    
  2. Para crear nuestro go.sum(archivo que registrará los requerimientos y dependencias) go mod tidy

  3. Crear arhivo main.go

   package main

   func main() {

   }
  1. Vamos a descargar el enrutador http que usaremos

       go get github.com/uptrace/bunrouter
    
  2. Agregamos el import al main

import "github.com/uptrace/bunrouter"
  1. Creamos la instancia al router que nos proporciona esta librería
router := bunrouter.New()
  1. Ver documentación oficial Creando un go router

  2. Según la documentación oficial nos recomiendan ordenar los endpoint en grupos

router.WithGroup("/api/categories", func(group *bunrouter.Group) {
    // /api/categories/:category/items
    group.WithGroup("/:category/items", func(group *bunrouter.Group) {})

    // /api/categories/archive
    group.WithGroup("/archive", func(group *bunrouter.Group) {})
})
  1. Agregaremos el archivo handler.go
package main

import (
	"net/http"

	"github.com/uptrace/bunrouter"

)

type handler struct {
}

func (h handler)GetById(w http.ResponseWriter, req bunrouter.Request) error {

	return nil
}

func (h handler)Create(w http.ResponseWriter, req bunrouter.Request) error {

	return nil
}

func (h handler)Delete(w http.ResponseWriter, req bunrouter.Request) error {

	return nil
}

func (h handler)Update(w http.ResponseWriter, req bunrouter.Request) error {

	return nil
}
  1. Actualizamos el main para incluir el import del handler y especificar los endpoint - El método, la ruta y los parametros
package main

import (
	"go-tests/handler"

	"github.com/uptrace/bunrouter"
)
func main() {

	h:= &handler.Controlador{}

	router := bunrouter.New()
	router.WithGroup("/api/categories", func(group *bunrouter.Group) {
		// /api/categories/:category/items
		group.WithGroup("/:category/items", func(group *bunrouter.Group) {

			    group.GET("/:id", h.GetById)
					group.POST("", h.Create)
					group.PATCH("/:id", h.Update)
					group.DELETE("/:id", h.Delete)
		})

		// /api/categories/archive
		group.WithGroup("/archive", func(group *bunrouter.Group) {})
	})
}
  1. En el handler del metodo getbyid agregamos para imprimir en consola detalles de la request
    fmt.Println(req.Method, req.Route(), req.Params().Map())
  1. Podemos usar curl o armar un postman ((o cliente http a elección))

curl --location 'http://localhost:9999/api/categories/category/items/1'

  1. Agregar la línea en main.go donde levantaremos el servidor
log.Println(http.ListenAndServe(":9999", router))
  1. Ejecutar con go run main.go y realizar la request

  2. Debiesemos ver en consola

GET /api/categories/:category/items/:id map[category:category id:1]

  1. Vamos a modificar el handler agregando un slice de categorias
type Category struct{
	Id int
	Name string
}

var	categories = []Category{Category{
		Id: 1,
		Name: "News",
}}

func (h Controlador)GetById(w http.ResponseWriter, req bunrouter.Request) error {
 // req embeds *http.Request and has all the same fields and methods
    fmt.Println(req.Method, req.Route(), req.Params().Map())

		id := req.Params.ByName("id")
		cat :=""
		for i := range categories {
			if fmt.Sprint(categories[i].Id) ==id {
						// Found!
						cat = categories[i].Name
				}
		}
		if cat ==""{
			w.WriteHeader(404)
			return nil
		}
			//caso feliz
	w.WriteHeader(200)
	return nil
}
  1. Agregamos el archivo handler_test.go
package handler_test

import "testing"

func TestHandlerGetById(t *testing.T) {

}
func TestHandlerCreate(t *testing.T) {

}
func TestHandlerGetDelete(t *testing.T) {

}
func TestHandlerGetUpdate(t *testing.T) {

}
  1. Agregar libreria para asserts

go get "github.com/stretchr/testify/assert"

  1. Actualizamos con los test
var router = bunrouter.New()

func TestHandlerGetById(t *testing.T) {
c :=&handler.Controlador{}

router.GET("/api/categories/category/items/:id", c.GetById)

	t.Run("Happy Path 200 status code", func(t *testing.T) {
		w := httptest.NewRecorder()
		baseURL, _ := url.Parse("/api/categories/category/items/1")
    params := url.Values{}
    params.Add("id", "1")
    baseURL.RawQuery = params.Encode()

		req, _ := http.NewRequest(http.MethodGet, baseURL.String(), nil)
		err := router.ServeHTTPError(w, req)
	  assert.Equal(t, 200, w.Code)
		require.NoError(t, err)

	})

	t.Run("UnHappy Path 404 status code", func(t *testing.T) {
		w := httptest.NewRecorder()
		baseURL, _ := url.Parse("/api/categories/category/items/2")
    params := url.Values{}
    params.Add("id", "2")
    baseURL.RawQuery = params.Encode()

		req, _ := http.NewRequest(http.MethodGet, baseURL.String(), nil)
		err := router.ServeHTTPError(w, req)
	  assert.Equal(t, 404, w.Code)
		require.NoError(t, err)

	})
}

golang-unit-tests's People

Contributors

millukii avatar

Stargazers

Hector Oliveros avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.