-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
159 additions
and
2 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
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 |
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 @@ | ||
.idea |
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
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
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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
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,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 | ||
) |