Skip to content

Commit

Permalink
Add automatic tests
Browse files Browse the repository at this point in the history
Add pipeline CI for testing webhook
  • Loading branch information
rafaelgfirmino committed Sep 1, 2023
1 parent 5d36860 commit 8010516
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run unit tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21.x'

- name: Install dependencies
run: go mod tidy

- name: Build
run: make build

- name: Test with the Go CLI
run: make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ module github.com/firmino/webhook

go 1.21.0

require github.com/gofiber/fiber/v2 v2.49.0
require (
github.com/gofiber/fiber/v2 v2.49.0
github.com/google/uuid v1.3.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.48.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v2 v2.49.0 h1:xBVG2c66GDcWfww56xHvMn52Q0XX7UrSvjj6MD8/5EE=
github.com/gofiber/fiber/v2 v2.49.0/go.mod h1:oxpt7wQaEYgdDmq7nMxCGhilYicBLFnZ+jQSJcQDlSE=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
Expand All @@ -13,8 +15,12 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.48.0 h1:oJWvHb9BIZToTQS3MuQ2R3bJZiNSa2KiNdeI8A+79Tc=
Expand All @@ -25,3 +31,7 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 31 additions & 0 deletions internal/domain/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package domain

import (
"github.com/firmino/webhook/pkg"
"github.com/google/uuid"
)

const ErrorEventNameIsRequired = "Field Name is required"
const ErrorEventDescriptionIsRequired = "Field Description is required"

type Event struct {
Id uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
}

func NewEvent(name string, description string) (Event, error) {
e := Event{Name: name, Description: description}
err := e.validate()
return e, err
}

func (e *Event) validate() error {
if e.Name == "" {
return pkg.NewCustomError(pkg.PreconditionFailed, ErrorEventNameIsRequired)
}
if e.Description == "" {
return pkg.NewCustomError(pkg.PreconditionFailed, ErrorEventDescriptionIsRequired)
}
return nil
}
45 changes: 45 additions & 0 deletions internal/domain/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package domain

import (
"testing"

"github.com/firmino/webhook/pkg"
"github.com/stretchr/testify/assert"
)

func TestNewEvent(t *testing.T) {
tests := []struct {
name string
input Event
expected Event
expectedError error
}{
{
name: "Invalid Name",
input: Event{Name: "", Description: "Teste"},
expected: Event{Name: "", Description: "Teste"},
expectedError: pkg.NewCustomError(pkg.PreconditionFailed, ErrorEventNameIsRequired),
},
{
name: "Invalid Description",
input: Event{Name: "Teste", Description: ""},
expected: Event{Name: "Teste", Description: ""},
expectedError: pkg.NewCustomError(pkg.PreconditionFailed, ErrorEventDescriptionIsRequired),
},
{
name: "Valid Event",
input: Event{Name: "Teste", Description: "Teste"},
expected: Event{Name: "Teste", Description: "Teste"},
expectedError: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
event, err := NewEvent(test.input.Name, test.input.Description)
assert.Equal(t, test.expected, event)
if test.expectedError != nil {
assert.Equal(t, err, test.expectedError)
}
})
}
}
10 changes: 10 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ up:

down:
@docker-compose down

test:
@go test -v -cover -short ./...

godoc:
@godoc -http=:6060 | echo "godoc is running on http://localhost:6060"



.PHONY: up-build up down test godoc
27 changes: 27 additions & 0 deletions pkg/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pkg

type GenericError struct {
Code ErrorCode
Message string
}

type CustomError struct {
GenericError
}

func NewCustomError(code ErrorCode, msg string) *CustomError {
return &CustomError{GenericError{
Code: code,
Message: msg,
}}
}

func (e *CustomError) Error() string {
return e.Message
}

type ErrorCode uint8

const (
PreconditionFailed ErrorCode = iota
)

0 comments on commit 8010516

Please sign in to comment.