feat: move entity and usecase to root folder

This commit is contained in:
Elton Minetto
2020-10-12 07:35:29 -03:00
parent 2c0f46aff1
commit 3e4eb2e616
33 changed files with 109 additions and 138 deletions
+3 -3
View File
@@ -32,9 +32,9 @@ ci: dependencies test
build-mocks:
@go get github.com/golang/mock/gomock
@go install github.com/golang/mock/mockgen
@~/go/bin/mockgen -source=domain/usecase/book/interface.go -destination=domain/usecase/book/mock/book.go -package=mock
@~/go/bin/mockgen -source=domain/usecase/user/interface.go -destination=domain/usecase/user/mock/user.go -package=mock
@~/go/bin/mockgen -source=domain/usecase/loan/interface.go -destination=domain/usecase/loan/mock/loan.go -package=mock
@~/go/bin/mockgen -source=usecase/book/interface.go -destination=usecase/book/mock/book.go -package=mock
@~/go/bin/mockgen -source=usecase/user/interface.go -destination=usecase/user/mock/user.go -package=mock
@~/go/bin/mockgen -source=usecase/loan/interface.go -destination=usecase/loan/mock/loan.go -package=mock
test:
go test -tags testing ./...
+4 -6
View File
@@ -5,14 +5,12 @@ import (
"log"
"net/http"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
"github.com/codegangsta/negroni"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/gorilla/mux"
)
@@ -29,7 +27,7 @@ func listBooks(service book.UseCase) http.Handler {
data, err = service.SearchBooks(title)
}
w.Header().Set("Content-Type", "application/json")
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
@@ -108,7 +106,7 @@ func getBook(service book.UseCase) http.Handler {
return
}
data, err := service.GetBook(id)
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
+3 -4
View File
@@ -8,11 +8,10 @@ import (
"strings"
"testing"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/codegangsta/negroni"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book/mock"
"github.com/eminetto/clean-architecture-go-v2/usecase/book/mock"
"github.com/golang/mock/gomock"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
@@ -49,7 +48,7 @@ func Test_listBooks_NotFound(t *testing.T) {
defer ts.Close()
service.EXPECT().
SearchBooks("book of books").
Return(nil, domain.ErrNotFound)
Return(nil, entity.ErrNotFound)
res, err := http.Get(ts.URL + "?title=book+of+books")
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
+8 -10
View File
@@ -4,15 +4,13 @@ import (
"fmt"
"net/http"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/loan"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/usecase/loan"
"github.com/codegangsta/negroni"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/gorilla/mux"
)
@@ -27,7 +25,7 @@ func borrowBook(bookService book.UseCase, userService user.UseCase, loanService
return
}
b, err := bookService.GetBook(bID)
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
@@ -44,7 +42,7 @@ func borrowBook(bookService book.UseCase, userService user.UseCase, loanService
return
}
u, err := userService.GetUser(uID)
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
@@ -76,7 +74,7 @@ func returnBook(bookService book.UseCase, loanService loan.UseCase) http.Handler
return
}
b, err := bookService.GetBook(bID)
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
@@ -87,7 +85,7 @@ func returnBook(bookService book.UseCase, loanService loan.UseCase) http.Handler
return
}
err = loanService.Return(b)
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
+7 -8
View File
@@ -6,13 +6,12 @@ import (
"net/http/httptest"
"testing"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/codegangsta/negroni"
bmock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/book/mock"
lmock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/loan/mock"
umock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/user/mock"
bmock "github.com/eminetto/clean-architecture-go-v2/usecase/book/mock"
lmock "github.com/eminetto/clean-architecture-go-v2/usecase/loan/mock"
umock "github.com/eminetto/clean-architecture-go-v2/usecase/user/mock"
"github.com/golang/mock/gomock"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
@@ -35,7 +34,7 @@ func Test_borrowBook(t *testing.T) {
t.Run("book not found", func(t *testing.T) {
bID := entity.NewID()
uID := entity.NewID()
bMock.EXPECT().GetBook(bID).Return(nil, domain.ErrNotFound)
bMock.EXPECT().GetBook(bID).Return(nil, entity.ErrNotFound)
ts := httptest.NewServer(r)
defer ts.Close()
res, err := http.Get(fmt.Sprintf("%s/v1/loan/borrow/%s/%s", ts.URL, bID.String(), uID.String()))
@@ -48,7 +47,7 @@ func Test_borrowBook(t *testing.T) {
}
uID := entity.NewID()
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
uMock.EXPECT().GetUser(uID).Return(nil, domain.ErrNotFound)
uMock.EXPECT().GetUser(uID).Return(nil, entity.ErrNotFound)
ts := httptest.NewServer(r)
defer ts.Close()
res, err := http.Get(fmt.Sprintf("%s/v1/loan/borrow/%s/%s", ts.URL, b.ID.String(), uID.String()))
@@ -89,7 +88,7 @@ func Test_returnBook(t *testing.T) {
r.Handle("/v1/loan/return/{book_id}", handler)
t.Run("book not found", func(t *testing.T) {
bID := entity.NewID()
bMock.EXPECT().GetBook(bID).Return(nil, domain.ErrNotFound)
bMock.EXPECT().GetBook(bID).Return(nil, entity.ErrNotFound)
ts := httptest.NewServer(r)
defer ts.Close()
res, err := http.Get(fmt.Sprintf("%s/v1/loan/return/%s", ts.URL, bID.String()))
+4 -6
View File
@@ -5,13 +5,11 @@ import (
"log"
"net/http"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
@@ -30,7 +28,7 @@ func listUsers(service user.UseCase) http.Handler {
data, err = service.SearchUsers(name)
}
w.Header().Set("Content-Type", "application/json")
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
@@ -108,7 +106,7 @@ func getUser(service user.UseCase) http.Handler {
}
data, err := service.GetUser(id)
w.Header().Set("Content-Type", "application/json")
if err != nil && err != domain.ErrNotFound {
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
+3 -4
View File
@@ -10,9 +10,8 @@ import (
"github.com/codegangsta/negroni"
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user/mock"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/eminetto/clean-architecture-go-v2/usecase/user/mock"
"github.com/golang/mock/gomock"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
@@ -49,7 +48,7 @@ func Test_listUsers_NotFound(t *testing.T) {
defer ts.Close()
m.EXPECT().
SearchUsers("dio").
Return(nil, domain.ErrNotFound)
Return(nil, entity.ErrNotFound)
res, err := http.Get(ts.URL + "?name=dio")
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
+3 -3
View File
@@ -9,11 +9,11 @@ import (
"strconv"
"time"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/loan"
"github.com/eminetto/clean-architecture-go-v2/usecase/loan"
"github.com/prometheus/client_golang/prometheus/promhttp"
+1 -1
View File
@@ -1,7 +1,7 @@
package presenter
import (
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//Book data
+1 -1
View File
@@ -1,7 +1,7 @@
package presenter
import (
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//User data
+1 -1
View File
@@ -7,8 +7,8 @@ import (
"log"
"os"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/config"
_ "github.com/go-sql-driver/mysql"
+2 -4
View File
@@ -2,8 +2,6 @@ package entity
import (
"time"
"github.com/eminetto/clean-architecture-go-v2/domain"
)
//Book data
@@ -29,7 +27,7 @@ func NewBook(title string, author string, pages int, quantity int) (*Book, error
}
err := b.Validate()
if err != nil {
return nil, domain.ErrInvalidEntity
return nil, ErrInvalidEntity
}
return b, nil
}
@@ -37,7 +35,7 @@ func NewBook(title string, author string, pages int, quantity int) (*Book, error
//Validate validate book
func (b *Book) Validate() error {
if b.Title == "" || b.Author == "" || b.Pages <= 0 || b.Quantity <= 0 {
return domain.ErrInvalidEntity
return ErrInvalidEntity
}
return nil
}
@@ -3,8 +3,7 @@ package entity_test
import (
"testing"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/stretchr/testify/assert"
)
@@ -37,28 +36,28 @@ func TestBookValidate(t *testing.T) {
author: "Neil Gaiman",
pages: 100,
quantity: 0,
want: domain.ErrInvalidEntity,
want: entity.ErrInvalidEntity,
},
{
title: "",
author: "Neil Gaiman",
pages: 100,
quantity: 1,
want: domain.ErrInvalidEntity,
want: entity.ErrInvalidEntity,
},
{
title: "American Gods",
author: "",
pages: 100,
quantity: 1,
want: domain.ErrInvalidEntity,
want: entity.ErrInvalidEntity,
},
{
title: "American Gods",
author: "Neil Gaiman",
pages: 0,
quantity: 1,
want: domain.ErrInvalidEntity,
want: entity.ErrInvalidEntity,
},
}
for _, tc := range tests {
+1 -1
View File
@@ -1,4 +1,4 @@
package domain
package entity
import "errors"
+5 -6
View File
@@ -3,7 +3,6 @@ package entity
import (
"time"
"github.com/eminetto/clean-architecture-go-v2/domain"
"golang.org/x/crypto/bcrypt"
)
@@ -35,7 +34,7 @@ func NewUser(email, password, firstName, lastName string) (*User, error) {
u.Password = pwd
err = u.Validate()
if err != nil {
return nil, domain.ErrInvalidEntity
return nil, ErrInvalidEntity
}
return u, nil
}
@@ -44,7 +43,7 @@ func NewUser(email, password, firstName, lastName string) (*User, error) {
func (u *User) AddBook(id ID) error {
_, err := u.GetBook(id)
if err == nil {
return domain.ErrBookAlreadyBorrowed
return ErrBookAlreadyBorrowed
}
u.Books = append(u.Books, id)
return nil
@@ -58,7 +57,7 @@ func (u *User) RemoveBook(id ID) error {
return nil
}
}
return domain.ErrNotFound
return ErrNotFound
}
//GetBook get a book
@@ -68,13 +67,13 @@ func (u *User) GetBook(id ID) (ID, error) {
return id, nil
}
}
return id, domain.ErrNotFound
return id, ErrNotFound
}
//Validate validate data
func (u *User) Validate() error {
if u.Email == "" || u.FirstName == "" || u.LastName == "" || u.Password == "" {
return domain.ErrInvalidEntity
return ErrInvalidEntity
}
return nil
@@ -3,8 +3,7 @@ package entity_test
import (
"testing"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/stretchr/testify/assert"
)
@@ -32,14 +31,14 @@ func TestAddBook(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, len(u.Books))
err = u.AddBook(bID)
assert.Equal(t, domain.ErrBookAlreadyBorrowed, err)
assert.Equal(t, entity.ErrBookAlreadyBorrowed, err)
}
func TestRemoveBook(t *testing.T) {
u, _ := entity.NewUser("sjobs@apple.com", "new_password", "Steve", "Jobs")
err := u.RemoveBook(entity.NewID())
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
bID := entity.NewID()
_ = u.AddBook(bID)
err = u.RemoveBook(bID)
@@ -54,7 +53,7 @@ func TestGetBook(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, id, bID)
_, err = u.GetBook(entity.NewID())
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
}
func TestUserValidate(t *testing.T) {
@@ -79,7 +78,7 @@ func TestUserValidate(t *testing.T) {
password: "new_password",
firstName: "Steve",
lastName: "Jobs",
want: domain.ErrInvalidEntity,
want: entity.ErrInvalidEntity,
},
{
email: "sjobs@apple.com",
@@ -93,14 +92,14 @@ func TestUserValidate(t *testing.T) {
password: "new_password",
firstName: "",
lastName: "Jobs",
want: domain.ErrInvalidEntity,
want: entity.ErrInvalidEntity,
},
{
email: "sjobs@apple.com",
password: "new_password",
firstName: "Steve",
lastName: "",
want: domain.ErrInvalidEntity,
want: entity.ErrInvalidEntity,
},
}
for _, tc := range tests {
+3 -6
View File
@@ -3,8 +3,7 @@ package repository
import (
"strings"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//BookInmem in memory repo
@@ -29,8 +28,7 @@ func (r *BookInmem) Create(e *entity.Book) (entity.ID, error) {
//Get a book
func (r *BookInmem) Get(id entity.ID) (*entity.Book, error) {
if r.m[id] == nil {
// return nil, fmt.Errorf("not found")
return nil, domain.ErrNotFound
return nil, entity.ErrNotFound
}
return r.m[id], nil
}
@@ -68,8 +66,7 @@ func (r *BookInmem) List() ([]*entity.Book, error) {
//Delete a book
func (r *BookInmem) Delete(id entity.ID) error {
if r.m[id] == nil {
// return fmt.Errorf("not found")
return domain.ErrNotFound
return entity.ErrNotFound
}
r.m[id] = nil
return nil
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"database/sql"
"time"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//BookMySQL mysql repo
+3 -6
View File
@@ -4,8 +4,7 @@ import (
"fmt"
"strings"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//UserInmem in memory repo
@@ -30,8 +29,7 @@ func (r *UserInmem) Create(e *entity.User) (entity.ID, error) {
//Get an user
func (r *UserInmem) Get(id entity.ID) (*entity.User, error) {
if r.m[id] == nil {
// return nil, fmt.Errorf("not found")
return nil, domain.ErrNotFound
return nil, entity.ErrNotFound
}
return r.m[id], nil
}
@@ -55,8 +53,7 @@ func (r *UserInmem) Search(query string) ([]*entity.User, error) {
}
}
if len(d) == 0 {
// return nil, fmt.Errorf("not found")
return nil, domain.ErrNotFound
return nil, entity.ErrNotFound
}
return d, nil
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"time"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//UserMySQL mysql repo
@@ -1,7 +1,7 @@
package book
import (
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//Reader interface
@@ -1,11 +1,11 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: domain/usecase/book/interface.go
// Source: usecase/book/interface.go
// Package mock is a generated GoMock package.
package mock
import (
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
entity "github.com/eminetto/clean-architecture-go-v2/entity"
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
@@ -4,9 +4,7 @@ import (
"strings"
"time"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//Service book usecase
@@ -34,7 +32,7 @@ func (s *Service) CreateBook(title string, author string, pages int, quantity in
func (s *Service) GetBook(id entity.ID) (*entity.Book, error) {
b, err := s.repo.Get(id)
if b == nil {
return nil, domain.ErrNotFound
return nil, entity.ErrNotFound
}
if err != nil {
return nil, err
@@ -50,7 +48,7 @@ func (s *Service) SearchBooks(query string) ([]*entity.Book, error) {
return nil, err
}
if len(books) == 0 {
return nil, domain.ErrNotFound
return nil, entity.ErrNotFound
}
return books, nil
}
@@ -62,7 +60,7 @@ func (s *Service) ListBooks() ([]*entity.Book, error) {
return nil, err
}
if len(books) == 0 {
return nil, domain.ErrNotFound
return nil, entity.ErrNotFound
}
return books, nil
}
@@ -4,11 +4,9 @@ import (
"testing"
"time"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/stretchr/testify/assert"
)
@@ -48,7 +46,7 @@ func Test_SearchAndFind(t *testing.T) {
assert.Equal(t, "I Am Ozzy", c[0].Title)
c, err = m.SearchBooks("dio")
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
assert.Nil(t, c)
})
t.Run("list all", func(t *testing.T) {
@@ -86,10 +84,10 @@ func TestDelete(t *testing.T) {
u2ID, _ := m.CreateBook(u2.Title, u2.Author, u2.Pages, u2.Quantity)
err := m.DeleteBook(u1.ID)
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
err = m.DeleteBook(u2ID)
assert.Nil(t, err)
_, err = m.GetBook(u2ID)
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
}
@@ -1,7 +1,7 @@
package loan
import (
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//UseCase use case interface
@@ -1,11 +1,11 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: domain/usecase/loan/interface.go
// Source: usecase/loan/interface.go
// Package mock is a generated GoMock package.
package mock
import (
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
entity "github.com/eminetto/clean-architecture-go-v2/entity"
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
@@ -1,10 +1,9 @@
package loan
import (
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
)
//Service loan usecase
@@ -32,7 +31,7 @@ func (s *Service) Borrow(u *entity.User, b *entity.Book) error {
return err
}
if b.Quantity <= 0 {
return domain.ErrNotEnoughBooks
return entity.ErrNotEnoughBooks
}
err = u.AddBook(b.ID)
@@ -74,7 +73,7 @@ func (s *Service) Return(b *entity.Book) error {
break
}
if !borrowed {
return domain.ErrBookNotBorrowed
return entity.ErrBookNotBorrowed
}
u, err := s.userService.GetUser(borrowedBy)
if err != nil {
@@ -3,11 +3,10 @@ package loan
import (
"testing"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/eminetto/clean-architecture-go-v2/domain"
bmock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/book/mock"
umock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/user/mock"
bmock "github.com/eminetto/clean-architecture-go-v2/usecase/book/mock"
umock "github.com/eminetto/clean-architecture-go-v2/usecase/user/mock"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
@@ -25,9 +24,9 @@ func Test_Borrow(t *testing.T) {
b := &entity.Book{
ID: entity.NewID(),
}
uMock.EXPECT().GetUser(u.ID).Return(nil, domain.ErrNotFound)
uMock.EXPECT().GetUser(u.ID).Return(nil, entity.ErrNotFound)
err := uc.Borrow(u, b)
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
})
t.Run("book not found", func(t *testing.T) {
u := &entity.User{
@@ -37,9 +36,9 @@ func Test_Borrow(t *testing.T) {
ID: entity.NewID(),
}
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
bMock.EXPECT().GetBook(b.ID).Return(nil, domain.ErrNotFound)
bMock.EXPECT().GetBook(b.ID).Return(nil, entity.ErrNotFound)
err := uc.Borrow(u, b)
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
})
t.Run("not enough books to borrow", func(t *testing.T) {
u := &entity.User{
@@ -52,7 +51,7 @@ func Test_Borrow(t *testing.T) {
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
err := uc.Borrow(u, b)
assert.Equal(t, domain.ErrNotEnoughBooks, err)
assert.Equal(t, entity.ErrNotEnoughBooks, err)
})
t.Run("book already borrowed", func(t *testing.T) {
u := &entity.User{
@@ -66,7 +65,7 @@ func Test_Borrow(t *testing.T) {
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
err := uc.Borrow(u, b)
assert.Equal(t, domain.ErrBookAlreadyBorrowed, err)
assert.Equal(t, entity.ErrBookAlreadyBorrowed, err)
})
t.Run("sucess", func(t *testing.T) {
u := &entity.User{
@@ -95,9 +94,9 @@ func Test_Return(t *testing.T) {
b := &entity.Book{
ID: entity.NewID(),
}
bMock.EXPECT().GetBook(b.ID).Return(nil, domain.ErrNotFound)
bMock.EXPECT().GetBook(b.ID).Return(nil, entity.ErrNotFound)
err := uc.Return(b)
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
})
t.Run("book not borrowed", func(t *testing.T) {
u := &entity.User{
@@ -109,7 +108,7 @@ func Test_Return(t *testing.T) {
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
uMock.EXPECT().ListUsers().Return([]*entity.User{u}, nil)
err := uc.Return(b)
assert.Equal(t, domain.ErrBookNotBorrowed, err)
assert.Equal(t, entity.ErrBookNotBorrowed, err)
})
t.Run("success", func(t *testing.T) {
u := &entity.User{
@@ -1,7 +1,7 @@
package user
import (
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//Reader interface
@@ -1,11 +1,11 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: domain/usecase/user/interface.go
// Source: usecase/user/interface.go
// Package mock is a generated GoMock package.
package mock
import (
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
entity "github.com/eminetto/clean-architecture-go-v2/entity"
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
@@ -4,9 +4,7 @@ import (
"strings"
"time"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
)
//Service interface
@@ -49,13 +47,13 @@ func (s *Service) ListUsers() ([]*entity.User, error) {
func (s *Service) DeleteUser(id entity.ID) error {
u, err := s.GetUser(id)
if u == nil {
return domain.ErrNotFound
return entity.ErrNotFound
}
if err != nil {
return err
}
if len(u.Books) > 0 {
return domain.ErrCannotBeDeleted
return entity.ErrCannotBeDeleted
}
return s.repo.Delete(id)
}
@@ -64,7 +62,7 @@ func (s *Service) DeleteUser(id entity.ID) error {
func (s *Service) UpdateUser(e *entity.User) error {
err := e.Validate()
if err != nil {
return domain.ErrInvalidEntity
return entity.ErrInvalidEntity
}
e.UpdatedAt = time.Now()
return s.repo.Update(e)
@@ -6,8 +6,7 @@ import (
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
"github.com/eminetto/clean-architecture-go-v2/domain"
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
"github.com/eminetto/clean-architecture-go-v2/entity"
"github.com/stretchr/testify/assert"
)
@@ -50,7 +49,7 @@ func Test_SearchAndFind(t *testing.T) {
assert.Equal(t, "Osbourne", c[0].LastName)
c, err = m.SearchUsers("dio")
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
assert.Nil(t, c)
})
t.Run("list all", func(t *testing.T) {
@@ -91,12 +90,12 @@ func TestDelete(t *testing.T) {
u2ID, _ := m.CreateUser(u2.Email, u2.Password, u2.FirstName, u2.LastName)
err := m.DeleteUser(u1.ID)
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
err = m.DeleteUser(u2ID)
assert.Nil(t, err)
_, err = m.GetUser(u2ID)
assert.Equal(t, domain.ErrNotFound, err)
assert.Equal(t, entity.ErrNotFound, err)
u3 := newFixtureUser()
id, _ := m.CreateUser(u3.Email, u3.Password, u3.FirstName, u3.LastName)
@@ -104,5 +103,5 @@ func TestDelete(t *testing.T) {
saved.Books = []entity.ID{entity.NewID()}
_ = m.UpdateUser(saved)
err = m.DeleteUser(id)
assert.Equal(t, domain.ErrCannotBeDeleted, err)
assert.Equal(t, entity.ErrCannotBeDeleted, err)
}