@@ -3,9 +3,11 @@ package order_test
3
3
import (
4
4
"errors"
5
5
"github.com/google/uuid"
6
+ "github.com/pangolin-do-golang/tech-challenge-order-api/internal/core/cart"
6
7
"github.com/pangolin-do-golang/tech-challenge-order-api/internal/core/order"
7
8
"github.com/pangolin-do-golang/tech-challenge-order-api/internal/errutil"
8
9
"github.com/pangolin-do-golang/tech-challenge-order-api/mocks"
10
+ "github.com/stretchr/testify/assert"
9
11
"github.com/stretchr/testify/mock"
10
12
"reflect"
11
13
"testing"
@@ -536,3 +538,83 @@ func TestService_Update(t *testing.T) {
536
538
})
537
539
}
538
540
}
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
+ }
0 commit comments