flamingo.me/flamingo-commerce/v3@v3.11.0/sourcing/domain/restrictor/restrictor_test.go (about) 1 package restrictors 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "flamingo.me/flamingo-commerce/v3/cart/domain/cart" 9 "flamingo.me/flamingo-commerce/v3/cart/domain/validation" 10 "flamingo.me/flamingo-commerce/v3/product/domain" 11 sourcingApplication "flamingo.me/flamingo-commerce/v3/sourcing/application" 12 sourcingDomain "flamingo.me/flamingo-commerce/v3/sourcing/domain" 13 14 "flamingo.me/flamingo/v3/framework/flamingo" 15 "flamingo.me/flamingo/v3/framework/web" 16 17 "github.com/stretchr/testify/assert" 18 ) 19 20 type ( 21 sourcingServiceMock struct { 22 AvailableSources sourcingDomain.AvailableSourcesPerProduct 23 AvailableSourcesError error 24 DeductedAvailableSources sourcingDomain.AvailableSourcesPerProduct 25 DeductedAvailableSourcesError error 26 } 27 ) 28 29 func (s *sourcingServiceMock) GetAvailableSourcesDeductedByCurrentCart(_ context.Context, _ *web.Session, _ domain.BasicProduct, _ string) (sourcingDomain.AvailableSourcesPerProduct, error) { 30 return s.DeductedAvailableSources, s.DeductedAvailableSourcesError 31 } 32 33 func (s *sourcingServiceMock) GetAvailableSources(_ context.Context, _ *web.Session, _ domain.BasicProduct, _ string) (sourcingDomain.AvailableSourcesPerProduct, error) { 34 return s.AvailableSources, s.AvailableSourcesError 35 } 36 37 var ( 38 _ sourcingApplication.SourcingApplication = new(sourcingServiceMock) 39 ) 40 41 func TestRestrictor_Restrict(t *testing.T) { 42 fixtureProduct := domain.SimpleProduct{Identifier: "productid"} 43 44 fixtureCart := &cart.Cart{} 45 46 t.Run("error handing on error fetching available sources", func(t *testing.T) { 47 want := &validation.RestrictionResult{ 48 IsRestricted: false, 49 MaxAllowed: 0, 50 RemainingDifference: 0, 51 RestrictorName: "SourceAvailableRestrictor", 52 } 53 54 restrictor := &Restrictor{} 55 restrictor.Inject( 56 flamingo.NullLogger{}, 57 &sourcingServiceMock{ 58 AvailableSources: nil, 59 DeductedAvailableSources: nil, 60 AvailableSourcesError: errors.New("mocked available sources error"), 61 }, 62 ) 63 64 got := restrictor.Restrict( 65 context.Background(), 66 web.EmptySession(), 67 fixtureProduct, 68 fixtureCart, 69 "test", 70 ) 71 72 assert.Equal(t, got, want) 73 }) 74 75 t.Run("available sources were fetched but deduction failed, returning full source stock", func(t *testing.T) { 76 want := &validation.RestrictionResult{ 77 IsRestricted: true, 78 MaxAllowed: 3, 79 RemainingDifference: 3, 80 RestrictorName: "SourceAvailableRestrictor", 81 } 82 83 restrictor := &Restrictor{} 84 restrictor.Inject( 85 flamingo.NullLogger{}, 86 &sourcingServiceMock{ 87 AvailableSources: sourcingDomain.AvailableSourcesPerProduct{ 88 "productid": sourcingDomain.AvailableSources{ 89 sourcingDomain.Source{ 90 LocationCode: "testCode1", 91 ExternalLocationCode: "testExternalLocation1", 92 }: 3, 93 }, 94 }, 95 DeductedAvailableSources: nil, 96 AvailableSourcesError: nil, 97 DeductedAvailableSourcesError: errors.New("mocked available sources error"), 98 }, 99 ) 100 101 got := restrictor.Restrict( 102 context.Background(), 103 web.EmptySession(), 104 fixtureProduct, 105 fixtureCart, 106 "test", 107 ) 108 109 assert.Equal(t, got, want) 110 }) 111 112 t.Run("returning deducted source normally", func(t *testing.T) { 113 want := &validation.RestrictionResult{ 114 IsRestricted: true, 115 MaxAllowed: 5, 116 RemainingDifference: 3, 117 RestrictorName: "SourceAvailableRestrictor", 118 } 119 120 restrictor := &Restrictor{} 121 restrictor.Inject( 122 flamingo.NullLogger{}, 123 &sourcingServiceMock{ 124 AvailableSources: sourcingDomain.AvailableSourcesPerProduct{ 125 "productid": sourcingDomain.AvailableSources{ 126 sourcingDomain.Source{ 127 LocationCode: "testCode1", 128 ExternalLocationCode: "testExternalLocation1", 129 }: 3, 130 sourcingDomain.Source{ 131 LocationCode: "testCode2", 132 ExternalLocationCode: "testExternalLocation1", 133 }: 2, 134 }, 135 }, 136 DeductedAvailableSources: sourcingDomain.AvailableSourcesPerProduct{ 137 "productid": sourcingDomain.AvailableSources{ 138 sourcingDomain.Source{ 139 LocationCode: "testCode1", 140 ExternalLocationCode: "testExternalLocation", 141 }: 2, 142 sourcingDomain.Source{ 143 LocationCode: "testCode2", 144 ExternalLocationCode: "testExternalLocation", 145 }: 1, 146 }, 147 }, 148 AvailableSourcesError: nil, 149 DeductedAvailableSourcesError: nil, 150 }, 151 ) 152 153 got := restrictor.Restrict( 154 context.Background(), 155 web.EmptySession(), 156 fixtureProduct, 157 fixtureCart, 158 "test", 159 ) 160 161 assert.Equal(t, got, want) 162 }) 163 }