This commit is contained in:
Pavel 2021-09-14 12:36:52 +03:00
parent 611a4a4f13
commit 64256a8d2f
7 changed files with 58 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
build/app build/app
vendor vendor
.idea .idea
coverage.out
test-report.txt

View File

@ -26,6 +26,13 @@ deps:
@go mod tidy @go mod tidy
@go mod vendor @go mod vendor
test: clean build
@echo "==> Running tests"
@go test ./... -v -cpu 2 -race -timeout=30s -coverpkg=./... -coverprofile=$(ROOT_DIR)/coverage.out | tee $(ROOT_DIR)/test-report.txt
@cd / && go install github.com/axw/gocov/gocov@v1.0.0
@gocov convert $(ROOT_DIR)/coverage.out | gocov report
@go mod tidy
.PHONY: clean .PHONY: clean
clean: clean:
ifneq ($(wildcard $(ROOT_DIR)/vendor/.*),) ifneq ($(wildcard $(ROOT_DIR)/vendor/.*),)

0
build/.gitkeep Normal file
View File

View File

@ -9,7 +9,7 @@ func Authors() (authors []model.Author) {
authors = make([]model.Author, 10) authors = make([]model.Author, 10)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
authors[i] = model.Author{ authors[i] = model.Author{
ID: uint64(i), ID: uint64(i + 1),
FirstName: gofakeit.FirstName(), FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(), LastName: gofakeit.LastName(),
Books: Books(), Books: Books(),

View File

@ -12,7 +12,7 @@ func Books() (books []model.Book) {
books = make([]model.Book, 10) books = make([]model.Book, 10)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
books[i] = model.Book{ books[i] = model.Book{
ID: uint64(i), ID: uint64(i + 1),
Name: gofakeit.Phrase(), Name: gofakeit.Phrase(),
PublicationDate: time.Now(), PublicationDate: time.Now(),
Rate: uint8(rand.Intn(5)), Rate: uint8(rand.Intn(5)),

4
go.mod
View File

@ -5,9 +5,11 @@ go 1.17
require ( require (
github.com/brianvoe/gofakeit/v6 v6.7.1 github.com/brianvoe/gofakeit/v6 v6.7.1
github.com/gin-gonic/gin v1.7.4 github.com/gin-gonic/gin v1.7.4
github.com/stretchr/testify v1.7.0
) )
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect
@ -18,10 +20,12 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect github.com/ugorji/go/codec v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/text v0.3.6 // indirect golang.org/x/text v0.3.6 // indirect
google.golang.org/protobuf v1.27.1 // indirect google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
) )

43
handlers_test.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/Neur0toxine/demo-web-service-go/data/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthorsHandler(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/authors", nil)
require.NoError(t, err)
r := router()
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
data, err := io.ReadAll(rec.Body)
require.NoError(t, err)
var authors []model.Author
require.NoError(t, json.Unmarshal(data, &authors))
assert.Len(t, authors, 10)
for _, author := range authors {
assert.NotEmpty(t, author.ID)
assert.NotEmpty(t, author.FirstName)
assert.NotEmpty(t, author.LastName)
assert.Len(t, author.Books, 10)
for _, book := range author.Books {
assert.NotEmpty(t, book.ID)
assert.NotEmpty(t, book.Name)
assert.NotEmpty(t, book.PublicationDate)
}
}
}