fix: move some logic to domain entities
This commit is contained in:
@@ -2,6 +2,8 @@ package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
)
|
||||
|
||||
//Book data
|
||||
@@ -14,3 +16,24 @@ type Book struct {
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
//NewBook create a new book
|
||||
func NewBook(title string, author string, pages int, quantity int) (*Book, error) {
|
||||
b := &Book{
|
||||
ID: NewID(),
|
||||
Title: title,
|
||||
Author: author,
|
||||
Pages: pages,
|
||||
Quantity: quantity,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
//Validate validate book
|
||||
func (b *Book) Validate() error {
|
||||
if b.Title == "" || b.Author == "" || b.Pages <= 0 {
|
||||
return domain.ErrInvalidEntity
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewFixtureBook() *Book {
|
||||
return &Book{
|
||||
ID: NewID(),
|
||||
Title: "I Am Ozzy",
|
||||
Author: "Ozzy Osbourne",
|
||||
Pages: 294,
|
||||
Quantity: 1,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package entity_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewBook(t *testing.T) {
|
||||
b, err := entity.NewBook("American Gods", "Neil Gaiman", 100, 0)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, b.Title, "American Gods")
|
||||
assert.NotNil(t, b.ID)
|
||||
}
|
||||
|
||||
func TestBookValidate(t *testing.T) {
|
||||
type test struct {
|
||||
title string
|
||||
author string
|
||||
pages int
|
||||
quantity int
|
||||
want error
|
||||
}
|
||||
|
||||
tests := []test{
|
||||
{
|
||||
title: "American Gods",
|
||||
author: "Neil Gaiman",
|
||||
pages: 100,
|
||||
quantity: 1,
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
title: "American Gods",
|
||||
author: "Neil Gaiman",
|
||||
pages: 100,
|
||||
quantity: 0,
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
title: "",
|
||||
author: "Neil Gaiman",
|
||||
pages: 100,
|
||||
quantity: 1,
|
||||
want: domain.ErrInvalidEntity,
|
||||
},
|
||||
{
|
||||
title: "American Gods",
|
||||
author: "",
|
||||
pages: 100,
|
||||
quantity: 1,
|
||||
want: domain.ErrInvalidEntity,
|
||||
},
|
||||
{
|
||||
title: "American Gods",
|
||||
author: "Neil Gaiman",
|
||||
pages: 0,
|
||||
quantity: 1,
|
||||
want: domain.ErrInvalidEntity,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
|
||||
b, err := entity.NewBook(tc.title, tc.author, tc.pages, tc.quantity)
|
||||
err = b.Validate()
|
||||
assert.Equal(t, err, tc.want)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
//User data
|
||||
@@ -15,3 +18,67 @@ type User struct {
|
||||
UpdatedAt time.Time
|
||||
Books []ID
|
||||
}
|
||||
|
||||
func NewUser(email, password, firstName, lastName string) (*User, error) {
|
||||
e := &User{
|
||||
ID: NewID(),
|
||||
Email: email,
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
pwd, err := generatePassword(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.Password = pwd
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func (u *User) AddBook(id ID) error {
|
||||
u.Books = append(u.Books, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *User) RemoveBook(id ID) error {
|
||||
for i, j := range u.Books {
|
||||
if j == id {
|
||||
u.Books = append(u.Books[:i], u.Books[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
|
||||
func (u *User) GetBook(id ID) (ID, error) {
|
||||
for _, v := range u.Books {
|
||||
if v == id {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
return id, domain.ErrNotFound
|
||||
}
|
||||
|
||||
func (u *User) Validate() error {
|
||||
if u.Email == "" || u.FirstName == "" || u.LastName == "" || u.Password == "" {
|
||||
return domain.ErrInvalidEntity
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *User) ValidatePassword(p string) error {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(p))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func generatePassword(raw string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(raw), 10)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hash), nil
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewFixtureUser() *User {
|
||||
return &User{
|
||||
ID: NewID(),
|
||||
Email: "[email protected]",
|
||||
Password: "123456",
|
||||
FirstName: "Ozzy",
|
||||
LastName: "Osbourne",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package entity_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewUser(t *testing.T) {
|
||||
u, err := entity.NewUser("[email protected]", "new_password", "Steve", "Jobs")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, u.FirstName, "Steve")
|
||||
assert.NotNil(t, u.ID)
|
||||
assert.NotEqual(t, u.Password, "new_password")
|
||||
}
|
||||
|
||||
func TestValidatePassword(t *testing.T) {
|
||||
u, _ := entity.NewUser("[email protected]", "new_password", "Steve", "Jobs")
|
||||
err := u.ValidatePassword("new_password")
|
||||
assert.Nil(t, err)
|
||||
err = u.ValidatePassword("wrong_password")
|
||||
assert.NotNil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestAddBook(t *testing.T) {
|
||||
u, _ := entity.NewUser("[email protected]", "new_password", "Steve", "Jobs")
|
||||
err := u.AddBook(entity.NewID())
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, len(u.Books))
|
||||
}
|
||||
|
||||
func TestRemoveBook(t *testing.T) {
|
||||
u, _ := entity.NewUser("[email protected]", "new_password", "Steve", "Jobs")
|
||||
err := u.RemoveBook(entity.NewID())
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
bID := entity.NewID()
|
||||
_ = u.AddBook(bID)
|
||||
err = u.RemoveBook(bID)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestGetBook(t *testing.T) {
|
||||
u, _ := entity.NewUser("[email protected]", "new_password", "Steve", "Jobs")
|
||||
bID := entity.NewID()
|
||||
_ = u.AddBook(bID)
|
||||
id, err := u.GetBook(bID)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, id, bID)
|
||||
_, err = u.GetBook(entity.NewID())
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
}
|
||||
|
||||
func TestUserValidate(t *testing.T) {
|
||||
type test struct {
|
||||
email string
|
||||
password string
|
||||
firstName string
|
||||
lastName string
|
||||
want error
|
||||
}
|
||||
|
||||
tests := []test{
|
||||
{
|
||||
email: "[email protected]",
|
||||
password: "new_password",
|
||||
firstName: "Steve",
|
||||
lastName: "Jobs",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
email: "",
|
||||
password: "new_password",
|
||||
firstName: "Steve",
|
||||
lastName: "Jobs",
|
||||
want: domain.ErrInvalidEntity,
|
||||
},
|
||||
{
|
||||
email: "[email protected]",
|
||||
password: "",
|
||||
firstName: "Steve",
|
||||
lastName: "Jobs",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
email: "[email protected]",
|
||||
password: "new_password",
|
||||
firstName: "",
|
||||
lastName: "Jobs",
|
||||
want: domain.ErrInvalidEntity,
|
||||
},
|
||||
{
|
||||
email: "[email protected]",
|
||||
password: "new_password",
|
||||
firstName: "Steve",
|
||||
lastName: "",
|
||||
want: domain.ErrInvalidEntity,
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
|
||||
u, err := entity.NewUser(tc.email, tc.password, tc.firstName, tc.lastName)
|
||||
err = u.Validate()
|
||||
assert.Equal(t, err, tc.want)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,9 @@ import "errors"
|
||||
//ErrNotFound not found
|
||||
var ErrNotFound = errors.New("Not found")
|
||||
|
||||
//ErrInvalidEntity invalid entity
|
||||
var ErrInvalidEntity = errors.New("Invalid entity")
|
||||
|
||||
//ErrCannotBeDeleted cannot be deleted
|
||||
var ErrCannotBeDeleted = errors.New("Cannot Be Deleted")
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ type UseCase interface {
|
||||
GetBook(id entity.ID) (*entity.Book, error)
|
||||
SearchBooks(query string) ([]*entity.Book, error)
|
||||
ListBooks() ([]*entity.Book, error)
|
||||
CreateBook(e *entity.Book) (entity.ID, error)
|
||||
CreateBook(title string, author string, pages int, quantity int) (entity.ID, error)
|
||||
UpdateBook(e *entity.Book) error
|
||||
DeleteBook(id entity.ID) error
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockReader is a mock of Reader interface
|
||||
@@ -324,18 +325,18 @@ func (mr *MockUseCaseMockRecorder) ListBooks() *gomock.Call {
|
||||
}
|
||||
|
||||
// CreateBook mocks base method
|
||||
func (m *MockUseCase) CreateBook(e *entity.Book) (entity.ID, error) {
|
||||
func (m *MockUseCase) CreateBook(title, author string, pages, quantity int) (entity.ID, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "CreateBook", e)
|
||||
ret := m.ctrl.Call(m, "CreateBook", title, author, pages, quantity)
|
||||
ret0, _ := ret[0].(entity.ID)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// CreateBook indicates an expected call of CreateBook
|
||||
func (mr *MockUseCaseMockRecorder) CreateBook(e interface{}) *gomock.Call {
|
||||
func (mr *MockUseCaseMockRecorder) CreateBook(title, author, pages, quantity interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateBook", reflect.TypeOf((*MockUseCase)(nil).CreateBook), e)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateBook", reflect.TypeOf((*MockUseCase)(nil).CreateBook), title, author, pages, quantity)
|
||||
}
|
||||
|
||||
// UpdateBook mocks base method
|
||||
|
||||
@@ -22,10 +22,16 @@ func NewService(r Repository) *Service {
|
||||
}
|
||||
|
||||
//CreateBook create a book
|
||||
func (s *Service) CreateBook(e *entity.Book) (entity.ID, error) {
|
||||
e.ID = entity.NewID()
|
||||
e.CreatedAt = time.Now()
|
||||
return s.repo.Create(e)
|
||||
func (s *Service) CreateBook(title string, author string, pages int, quantity int) (entity.ID, error) {
|
||||
b, err := entity.NewBook(title, author, pages, quantity)
|
||||
if err != nil {
|
||||
return b.ID, err
|
||||
}
|
||||
err = b.Validate()
|
||||
if err != nil {
|
||||
return b.ID, err
|
||||
}
|
||||
return s.repo.Create(b)
|
||||
}
|
||||
|
||||
//GetBook get a book
|
||||
@@ -76,5 +82,10 @@ func (s *Service) DeleteBook(id entity.ID) error {
|
||||
|
||||
//UpdateBook Update a book
|
||||
func (s *Service) UpdateBook(e *entity.Book) error {
|
||||
err := e.Validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.UpdatedAt = time.Now()
|
||||
return s.repo.Update(e)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package book
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
|
||||
@@ -12,25 +13,34 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func newFixtureBook() *entity.Book {
|
||||
return &entity.Book{
|
||||
Title: "I Am Ozzy",
|
||||
Author: "Ozzy Osbourne",
|
||||
Pages: 294,
|
||||
Quantity: 1,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Create(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u := entity.NewFixtureBook()
|
||||
id, err := m.CreateBook(u)
|
||||
u := newFixtureBook()
|
||||
_, err := m.CreateBook(u.Title, u.Author, u.Pages, u.Quantity)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, u.ID, id)
|
||||
assert.False(t, u.CreatedAt.IsZero())
|
||||
}
|
||||
|
||||
func Test_SearchAndFind(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u1 := entity.NewFixtureBook()
|
||||
u2 := entity.NewFixtureBook()
|
||||
u1 := newFixtureBook()
|
||||
u2 := newFixtureBook()
|
||||
u2.Title = "Lemmy: Biography"
|
||||
|
||||
uID, _ := m.CreateBook(u1)
|
||||
_, _ = m.CreateBook(u2)
|
||||
uID, _ := m.CreateBook(u1.Title, u1.Author, u1.Pages, u1.Quantity)
|
||||
_, _ = m.CreateBook(u2.Title, u2.Author, u2.Pages, u2.Quantity)
|
||||
|
||||
t.Run("search", func(t *testing.T) {
|
||||
c, err := m.SearchBooks("ozzy")
|
||||
@@ -58,8 +68,8 @@ func Test_SearchAndFind(t *testing.T) {
|
||||
func Test_Update(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u := entity.NewFixtureBook()
|
||||
id, err := m.CreateBook(u)
|
||||
u := newFixtureBook()
|
||||
id, err := m.CreateBook(u.Title, u.Author, u.Pages, u.Quantity)
|
||||
assert.Nil(t, err)
|
||||
saved, _ := m.GetBook(id)
|
||||
saved.Title = "Lemmy: Biography"
|
||||
@@ -72,9 +82,9 @@ func Test_Update(t *testing.T) {
|
||||
func TestDelete(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u1 := entity.NewFixtureBook()
|
||||
u2 := entity.NewFixtureBook()
|
||||
u2ID, _ := m.CreateBook(u2)
|
||||
u1 := newFixtureBook()
|
||||
u2 := newFixtureBook()
|
||||
u2ID, _ := m.CreateBook(u2.Title, u2.Author, u2.Pages, u2.Quantity)
|
||||
|
||||
err := m.DeleteBook(u1.ID)
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockUseCase is a mock of UseCase interface
|
||||
|
||||
@@ -34,12 +34,15 @@ func (s *Service) Borrow(u *entity.User, b *entity.Book) error {
|
||||
if b.Quantity <= 0 {
|
||||
return domain.ErrNotEnoughBooks
|
||||
}
|
||||
for _, v := range u.Books {
|
||||
if v == b.ID {
|
||||
return domain.ErrBookAlreadyBorrowed
|
||||
}
|
||||
|
||||
_, err = u.GetBook(b.ID)
|
||||
if err == nil {
|
||||
return domain.ErrBookAlreadyBorrowed
|
||||
}
|
||||
err = u.AddBook(b.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.Books = append(u.Books, b.ID)
|
||||
err = s.userService.UpdateUser(u)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -66,13 +69,13 @@ func (s *Service) Return(b *entity.Book) error {
|
||||
borrowed := false
|
||||
var borrowedBy entity.ID
|
||||
for _, u := range all {
|
||||
for _, bookID := range u.Books {
|
||||
if bookID == b.ID {
|
||||
borrowed = true
|
||||
borrowedBy = u.ID
|
||||
break
|
||||
}
|
||||
_, err := u.GetBook(b.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
borrowed = true
|
||||
borrowedBy = u.ID
|
||||
break
|
||||
}
|
||||
if !borrowed {
|
||||
return domain.ErrBookNotBorrowed
|
||||
@@ -81,15 +84,13 @@ func (s *Service) Return(b *entity.Book) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i, j := range u.Books {
|
||||
if j == b.ID {
|
||||
u.Books = append(u.Books[:i], u.Books[i+1:]...)
|
||||
err = s.userService.UpdateUser(u)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
break
|
||||
}
|
||||
err = u.RemoveBook(b.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = s.userService.UpdateUser(u)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.Quantity++
|
||||
err = s.bookService.UpdateBook(b)
|
||||
|
||||
@@ -19,23 +19,35 @@ func Test_Borrow(t *testing.T) {
|
||||
bMock := bmock.NewMockUseCase(controller)
|
||||
uc := NewService(uMock, bMock)
|
||||
t.Run("user not found", func(t *testing.T) {
|
||||
u := entity.NewFixtureUser()
|
||||
b := entity.NewFixtureBook()
|
||||
u := &entity.User{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
uMock.EXPECT().GetUser(u.ID).Return(nil, domain.ErrNotFound)
|
||||
err := uc.Borrow(u, b)
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
})
|
||||
t.Run("book not found", func(t *testing.T) {
|
||||
u := entity.NewFixtureUser()
|
||||
b := entity.NewFixtureBook()
|
||||
u := &entity.User{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||
bMock.EXPECT().GetBook(b.ID).Return(nil, domain.ErrNotFound)
|
||||
err := uc.Borrow(u, b)
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
})
|
||||
t.Run("not enough books to borrow", func(t *testing.T) {
|
||||
u := entity.NewFixtureUser()
|
||||
b := entity.NewFixtureBook()
|
||||
u := &entity.User{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b.Quantity = 0
|
||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||
@@ -43,9 +55,13 @@ func Test_Borrow(t *testing.T) {
|
||||
assert.Equal(t, domain.ErrNotEnoughBooks, err)
|
||||
})
|
||||
t.Run("book already borrowed", func(t *testing.T) {
|
||||
u := entity.NewFixtureUser()
|
||||
b := entity.NewFixtureBook()
|
||||
u.Books = []entity.ID{b.ID}
|
||||
u := &entity.User{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
u.AddBook(b.ID)
|
||||
b.Quantity = 1
|
||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||
@@ -53,8 +69,13 @@ func Test_Borrow(t *testing.T) {
|
||||
assert.Equal(t, domain.ErrBookAlreadyBorrowed, err)
|
||||
})
|
||||
t.Run("sucess", func(t *testing.T) {
|
||||
u := entity.NewFixtureUser()
|
||||
b := entity.NewFixtureBook()
|
||||
u := &entity.User{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
Quantity: 10,
|
||||
}
|
||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||
uMock.EXPECT().UpdateUser(u).Return(nil)
|
||||
@@ -71,23 +92,33 @@ func Test_Return(t *testing.T) {
|
||||
bMock := bmock.NewMockUseCase(controller)
|
||||
uc := NewService(uMock, bMock)
|
||||
t.Run("book not found", func(t *testing.T) {
|
||||
b := entity.NewFixtureBook()
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
bMock.EXPECT().GetBook(b.ID).Return(nil, domain.ErrNotFound)
|
||||
err := uc.Return(b)
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
})
|
||||
t.Run("book not borrowed", func(t *testing.T) {
|
||||
u := entity.NewFixtureUser()
|
||||
b := entity.NewFixtureBook()
|
||||
u := &entity.User{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
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)
|
||||
})
|
||||
t.Run("success", func(t *testing.T) {
|
||||
u := entity.NewFixtureUser()
|
||||
b := entity.NewFixtureBook()
|
||||
u.Books = []entity.ID{b.ID}
|
||||
u := &entity.User{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
b := &entity.Book{
|
||||
ID: entity.NewID(),
|
||||
}
|
||||
u.AddBook(b.ID)
|
||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||
uMock.EXPECT().ListUsers().Return([]*entity.User{u}, nil)
|
||||
|
||||
@@ -29,7 +29,7 @@ type UseCase interface {
|
||||
GetUser(id entity.ID) (*entity.User, error)
|
||||
SearchUsers(query string) ([]*entity.User, error)
|
||||
ListUsers() ([]*entity.User, error)
|
||||
CreateUser(e *entity.User) (entity.ID, error)
|
||||
CreateUser(email, password, firstName, lastName string) (entity.ID, error)
|
||||
UpdateUser(e *entity.User) error
|
||||
DeleteUser(id entity.ID) error
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockReader is a mock of Reader interface
|
||||
@@ -324,18 +325,18 @@ func (mr *MockUseCaseMockRecorder) ListUsers() *gomock.Call {
|
||||
}
|
||||
|
||||
// CreateUser mocks base method
|
||||
func (m *MockUseCase) CreateUser(e *entity.User) (entity.ID, error) {
|
||||
func (m *MockUseCase) CreateUser(email, password, firstName, lastName string) (entity.ID, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "CreateUser", e)
|
||||
ret := m.ctrl.Call(m, "CreateUser", email, password, firstName, lastName)
|
||||
ret0, _ := ret[0].(entity.ID)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// CreateUser indicates an expected call of CreateUser
|
||||
func (mr *MockUseCaseMockRecorder) CreateUser(e interface{}) *gomock.Call {
|
||||
func (mr *MockUseCaseMockRecorder) CreateUser(email, password, firstName, lastName interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUser", reflect.TypeOf((*MockUseCase)(nil).CreateUser), e)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUser", reflect.TypeOf((*MockUseCase)(nil).CreateUser), email, password, firstName, lastName)
|
||||
}
|
||||
|
||||
// UpdateUser mocks base method
|
||||
|
||||
@@ -6,34 +6,31 @@ import (
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/pkg/password"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
)
|
||||
|
||||
//Service interface
|
||||
type Service struct {
|
||||
repo Repository
|
||||
pwd password.Service
|
||||
}
|
||||
|
||||
//NewService create new use case
|
||||
func NewService(r Repository, pwd password.Service) *Service {
|
||||
func NewService(r Repository) *Service {
|
||||
return &Service{
|
||||
repo: r,
|
||||
pwd: pwd,
|
||||
}
|
||||
}
|
||||
|
||||
//CreateUser Create an user
|
||||
func (s *Service) CreateUser(e *entity.User) (entity.ID, error) {
|
||||
e.ID = entity.NewID()
|
||||
e.CreatedAt = time.Now()
|
||||
pwd, err := s.pwd.Generate(e.Password)
|
||||
func (s *Service) CreateUser(email, password, firstName, lastName string) (entity.ID, error) {
|
||||
e, err := entity.NewUser(email, password, firstName, lastName)
|
||||
if err != nil {
|
||||
return e.ID, err
|
||||
return e.ID, domain.ErrInvalidEntity
|
||||
}
|
||||
err = e.Validate()
|
||||
if err != nil {
|
||||
return e.ID, domain.ErrInvalidEntity
|
||||
}
|
||||
e.Password = pwd
|
||||
return s.repo.Create(e)
|
||||
}
|
||||
|
||||
@@ -69,6 +66,10 @@ func (s *Service) DeleteUser(id entity.ID) error {
|
||||
|
||||
//UpdateUser Update an user
|
||||
func (s *Service) UpdateUser(e *entity.User) error {
|
||||
err := e.Validate()
|
||||
if err != nil {
|
||||
return domain.ErrInvalidEntity
|
||||
}
|
||||
e.UpdatedAt = time.Now()
|
||||
return s.repo.Update(e)
|
||||
}
|
||||
|
||||
@@ -2,37 +2,46 @@ package user
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/infra/repository/user"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/pkg/password"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func newFixtureUser() *entity.User {
|
||||
return &entity.User{
|
||||
ID: entity.NewID(),
|
||||
Email: "[email protected]",
|
||||
Password: "123456",
|
||||
FirstName: "Ozzy",
|
||||
LastName: "Osbourne",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Create(t *testing.T) {
|
||||
repo := user.NewInmemRepository()
|
||||
m := NewService(repo, password.NewFakeService())
|
||||
u := entity.NewFixtureUser()
|
||||
id, err := m.CreateUser(u)
|
||||
m := NewService(repo)
|
||||
u := newFixtureUser()
|
||||
_, err := m.CreateUser(u.Email, u.Password, u.FirstName, u.LastName)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, u.ID, id)
|
||||
assert.False(t, u.CreatedAt.IsZero())
|
||||
assert.True(t, u.UpdatedAt.IsZero())
|
||||
}
|
||||
|
||||
func Test_SearchAndFind(t *testing.T) {
|
||||
repo := user.NewInmemRepository()
|
||||
m := NewService(repo, password.NewFakeService())
|
||||
u1 := entity.NewFixtureUser()
|
||||
u2 := entity.NewFixtureUser()
|
||||
m := NewService(repo)
|
||||
u1 := newFixtureUser()
|
||||
u2 := newFixtureUser()
|
||||
u2.FirstName = "Lemmy"
|
||||
|
||||
uID, _ := m.CreateUser(u1)
|
||||
_, _ = m.CreateUser(u2)
|
||||
uID, _ := m.CreateUser(u1.Email, u1.Password, u1.FirstName, u1.LastName)
|
||||
_, _ = m.CreateUser(u2.Email, u2.Password, u2.FirstName, u2.LastName)
|
||||
|
||||
t.Run("search", func(t *testing.T) {
|
||||
c, err := m.SearchUsers("ozzy")
|
||||
@@ -59,9 +68,9 @@ func Test_SearchAndFind(t *testing.T) {
|
||||
|
||||
func Test_Update(t *testing.T) {
|
||||
repo := user.NewInmemRepository()
|
||||
m := NewService(repo, password.NewFakeService())
|
||||
u := entity.NewFixtureUser()
|
||||
id, err := m.CreateUser(u)
|
||||
m := NewService(repo)
|
||||
u := newFixtureUser()
|
||||
id, err := m.CreateUser(u.Email, u.Password, u.FirstName, u.LastName)
|
||||
assert.Nil(t, err)
|
||||
saved, _ := m.GetUser(id)
|
||||
saved.FirstName = "Dio"
|
||||
@@ -70,16 +79,16 @@ func Test_Update(t *testing.T) {
|
||||
updated, err := m.GetUser(id)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "Dio", updated.FirstName)
|
||||
assert.False(t, u.UpdatedAt.IsZero())
|
||||
assert.False(t, updated.UpdatedAt.IsZero())
|
||||
assert.Equal(t, 1, len(updated.Books))
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
repo := user.NewInmemRepository()
|
||||
m := NewService(repo, password.NewFakeService())
|
||||
u1 := entity.NewFixtureUser()
|
||||
u2 := entity.NewFixtureUser()
|
||||
u2ID, _ := m.CreateUser(u2)
|
||||
m := NewService(repo)
|
||||
u1 := newFixtureUser()
|
||||
u2 := newFixtureUser()
|
||||
u2ID, _ := m.CreateUser(u2.Email, u2.Password, u2.FirstName, u2.LastName)
|
||||
|
||||
err := m.DeleteUser(u1.ID)
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
@@ -89,9 +98,11 @@ func TestDelete(t *testing.T) {
|
||||
_, err = m.GetUser(u2ID)
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
|
||||
u3 := entity.NewFixtureUser()
|
||||
u3.Books = []entity.ID{entity.NewID()}
|
||||
_, _ = m.CreateUser(u3)
|
||||
err = m.DeleteUser(u3.ID)
|
||||
u3 := newFixtureUser()
|
||||
id, _ := m.CreateUser(u3.Email, u3.Password, u3.FirstName, u3.LastName)
|
||||
saved, _ := m.GetUser(id)
|
||||
saved.Books = []entity.ID{entity.NewID()}
|
||||
_ = m.UpdateUser(saved)
|
||||
err = m.DeleteUser(id)
|
||||
assert.Equal(t, domain.ErrCannotBeDeleted, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user