Skip to content

Commit aec78dc

Browse files
committed
feature: reading tests that were erased
1 parent 9160b34 commit aec78dc

File tree

4 files changed

+202
-9
lines changed

4 files changed

+202
-9
lines changed

internal/core/cart/cart.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package cart
22

3-
import "github.com/google/uuid"
3+
import (
4+
"github.com/google/uuid"
5+
)
46

57
type Cart struct {
68
ID uuid.UUID `json:"id"`
79
ClientID uuid.UUID `json:"client_id"`
810
Products []*Product `json:"products"`
911
}
1012

13+
type ICartService interface {
14+
GetFullCart(clientID uuid.UUID) (*Cart, error)
15+
Cleanup(clientID uuid.UUID) error
16+
GetProductByID(id uuid.UUID) (*Product, error)
17+
}
18+
1119
type Product struct {
1220
ProductID uuid.UUID `json:"product_id"`
1321
Quantity int `json:"quantity"`

internal/core/order/order_service.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
type Service struct {
1313
OrderRepository IOrderRepository
1414
OrderProductRepository IOrderProductRepository
15-
CartService cart.Service
15+
CartService cart.ICartService
1616
}
1717

18-
func NewOrderService(repo IOrderRepository, orderProductRepository IOrderProductRepository, cartService *cart.Service) IOrderService {
18+
func NewOrderService(repo IOrderRepository, orderProductRepository IOrderProductRepository, cartService cart.ICartService) IOrderService {
1919
return &Service{
2020
OrderRepository: repo,
2121
OrderProductRepository: orderProductRepository,
22-
CartService: *cartService,
22+
CartService: cartService,
2323
}
2424
}
2525

@@ -71,8 +71,8 @@ func (s *Service) Update(order *Order) (*Order, error) {
7171
}
7272

7373
func (s *Service) Create(clientID uuid.UUID) (*Order, error) {
74-
7574
c, err := s.CartService.GetFullCart(clientID)
75+
7676
if err != nil {
7777
return nil, err
7878
}
@@ -85,8 +85,6 @@ func (s *Service) Create(clientID uuid.UUID) (*Order, error) {
8585
return nil, fmt.Errorf("empty cart")
8686
}
8787

88-
fmt.Println(c)
89-
9088
order := &Order{
9189
ClientID: clientID,
9290
Status: StatusCreated,
@@ -107,8 +105,6 @@ func (s *Service) Create(clientID uuid.UUID) (*Order, error) {
107105

108106
productTotal := stockProduct.Price * float64(p.Quantity)
109107

110-
fmt.Println(productTotal)
111-
112108
orderProduct := &Product{
113109
ClientID: clientID,
114110
ProductID: p.ProductID,

internal/core/order/order_service_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package order_test
33
import (
44
"errors"
55
"github.com/google/uuid"
6+
"github.com/pangolin-do-golang/tech-challenge-order-api/internal/core/cart"
67
"github.com/pangolin-do-golang/tech-challenge-order-api/internal/core/order"
78
"github.com/pangolin-do-golang/tech-challenge-order-api/internal/errutil"
89
"github.com/pangolin-do-golang/tech-challenge-order-api/mocks"
10+
"github.com/stretchr/testify/assert"
911
"github.com/stretchr/testify/mock"
1012
"reflect"
1113
"testing"
@@ -536,3 +538,83 @@ func TestService_Update(t *testing.T) {
536538
})
537539
}
538540
}
541+
542+
func TestCreate(t *testing.T) {
543+
clientID := uuid.New()
544+
545+
mockCartService := new(mocks.ICartService)
546+
547+
mockCartService.On("GetFullCart", clientID).Return(&cart.Cart{
548+
Products: []*cart.Product{
549+
{ProductID: uuid.New(), Quantity: 1, Comments: "Test product"},
550+
},
551+
}, nil)
552+
mockCartService.On("GetProductByID", mock.Anything).Return(&cart.Product{Price: 100.0}, nil)
553+
mockCartService.On("Cleanup", clientID).Return(nil)
554+
555+
mockOrderRepo := new(mocks.IOrderRepository)
556+
mockOrderRepo.On("Create", mock.Anything).Return(&order.Order{
557+
ID: uuid.New(),
558+
ClientID: clientID,
559+
Status: order.StatusCreated,
560+
}, nil)
561+
mockOrderRepo.On("Update", mock.Anything).Return(nil)
562+
563+
mockOrderProductRepo := new(mocks.IOrderProductRepository)
564+
mockOrderProductRepo.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(nil)
565+
566+
service := &order.Service{
567+
CartService: mockCartService,
568+
OrderRepository: mockOrderRepo,
569+
OrderProductRepository: mockOrderProductRepo,
570+
}
571+
572+
createdOrder, err := service.Create(clientID)
573+
574+
assert.NoError(t, err)
575+
assert.NotNil(t, createdOrder)
576+
assert.Equal(t, order.StatusPending, createdOrder.Status)
577+
assert.Equal(t, clientID, createdOrder.ClientID)
578+
579+
mockCartService.AssertExpectations(t)
580+
mockOrderRepo.AssertExpectations(t)
581+
mockOrderProductRepo.AssertExpectations(t)
582+
}
583+
584+
func TestCreate_CartNotFound(t *testing.T) {
585+
clientID := uuid.New()
586+
587+
mockCartService := new(mocks.ICartService)
588+
mockCartService.On("GetFullCart", clientID).Return(nil, errors.New("cart not found"))
589+
590+
service := &order.Service{
591+
CartService: mockCartService,
592+
}
593+
594+
order, err := service.Create(clientID)
595+
596+
assert.Error(t, err)
597+
assert.Nil(t, order)
598+
assert.Equal(t, "cart not found", err.Error())
599+
600+
mockCartService.AssertExpectations(t)
601+
}
602+
603+
func TestCreate_EmptyCart(t *testing.T) {
604+
clientID := uuid.New()
605+
606+
mockCartService := new(mocks.ICartService)
607+
mockCartService.On("GetFullCart", clientID).Return(&cart.Cart{Products: []*cart.Product{}}, nil)
608+
609+
service := &order.Service{
610+
CartService: mockCartService,
611+
}
612+
613+
o, err := service.Create(clientID)
614+
615+
assert.Error(t, err)
616+
assert.Nil(t, o)
617+
assert.Equal(t, "empty cart", err.Error())
618+
619+
mockCartService.AssertExpectations(t)
620+
}

mocks/ICartService.go

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)