From 64256a8d2fd84a8a04bae3a8e0cec029941ac6de Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Tue, 14 Sep 2021 12:36:52 +0300 Subject: [PATCH] tests --- .gitignore | 2 ++ Makefile | 7 +++++ build/.gitkeep | 0 data/repository/author_repository.go | 2 +- data/repository/book_repository.go | 2 +- go.mod | 4 +++ handlers_test.go | 43 ++++++++++++++++++++++++++++ 7 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 build/.gitkeep create mode 100644 handlers_test.go diff --git a/.gitignore b/.gitignore index ecc90a3..660be00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ build/app vendor .idea +coverage.out +test-report.txt diff --git a/Makefile b/Makefile index 6f1be52..3a9ac2f 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,13 @@ deps: @go mod tidy @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 clean: ifneq ($(wildcard $(ROOT_DIR)/vendor/.*),) diff --git a/build/.gitkeep b/build/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/data/repository/author_repository.go b/data/repository/author_repository.go index 8ad0465..a0e6ebf 100644 --- a/data/repository/author_repository.go +++ b/data/repository/author_repository.go @@ -9,7 +9,7 @@ func Authors() (authors []model.Author) { authors = make([]model.Author, 10) for i := 0; i < 10; i++ { authors[i] = model.Author{ - ID: uint64(i), + ID: uint64(i + 1), FirstName: gofakeit.FirstName(), LastName: gofakeit.LastName(), Books: Books(), diff --git a/data/repository/book_repository.go b/data/repository/book_repository.go index fda30fb..8dd73b4 100644 --- a/data/repository/book_repository.go +++ b/data/repository/book_repository.go @@ -12,7 +12,7 @@ func Books() (books []model.Book) { books = make([]model.Book, 10) for i := 0; i < 10; i++ { books[i] = model.Book{ - ID: uint64(i), + ID: uint64(i + 1), Name: gofakeit.Phrase(), PublicationDate: time.Now(), Rate: uint8(rand.Intn(5)), diff --git a/go.mod b/go.mod index fe1794e..6d33340 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,11 @@ go 1.17 require ( github.com/brianvoe/gofakeit/v6 v6.7.1 github.com/gin-gonic/gin v1.7.4 + github.com/stretchr/testify v1.7.0 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.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/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // 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 golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect golang.org/x/text v0.3.6 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/handlers_test.go b/handlers_test.go new file mode 100644 index 0000000..2b5955c --- /dev/null +++ b/handlers_test.go @@ -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) + } + } +}