flamingo.me/flamingo-commerce/v3@v3.11.0/cart/domain/decorator/discount_test.go (about) 1 package decorator_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "flamingo.me/flamingo-commerce/v3/cart/domain/cart" 8 "flamingo.me/flamingo-commerce/v3/cart/domain/decorator" 9 "flamingo.me/flamingo-commerce/v3/cart/domain/testutils" 10 "flamingo.me/flamingo-commerce/v3/price/domain" 11 ) 12 13 func TestDecoratedItem_MergeDiscounts(t *testing.T) { 14 tests := []struct { 15 name string 16 item *decorator.DecoratedCartItem 17 want cart.AppliedDiscounts 18 }{ 19 { 20 name: "no discounts on item", 21 item: &decorator.DecoratedCartItem{ 22 Item: cart.Item{}, 23 }, 24 want: nil, 25 }, 26 { 27 name: "multiple discounts on item", 28 item: &decorator.DecoratedCartItem{ 29 Item: *testutils.BuildItemWithDiscounts(t), 30 }, 31 want: cart.AppliedDiscounts{ 32 { 33 CampaignCode: "code-2", 34 Label: "title-2", 35 Type: "type-1", 36 Applied: domain.NewFromFloat(-15.0, "$"), 37 SortOrder: 2, 38 }, 39 { 40 CampaignCode: "code-1", 41 Label: "title-1", 42 Type: "type-1", 43 Applied: domain.NewFromFloat(-10.0, "$"), 44 SortOrder: 3, 45 }, 46 { 47 CampaignCode: "code-3", 48 Label: "title-1", 49 Type: "type-2", 50 Applied: domain.NewFromFloat(-5.0, "$"), 51 SortOrder: 4, 52 }, 53 }, 54 }, 55 } 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 got := tt.item.MergeDiscounts() 59 if !reflect.DeepEqual(got, tt.want) { 60 t.Errorf("DecoratedItem.MergeDiscounts() = %v, want %v", got, tt.want) 61 } 62 }) 63 } 64 } 65 66 func TestDecoratedItem_HasDiscounts(t *testing.T) { 67 tests := []struct { 68 name string 69 item *decorator.DecoratedCartItem 70 want bool 71 }{ 72 { 73 name: "no discounts on item", 74 item: &decorator.DecoratedCartItem{ 75 Item: cart.Item{}, 76 }, 77 want: false, 78 }, 79 { 80 name: "multiple discounts on item", 81 item: func() *decorator.DecoratedCartItem { 82 item := cart.Item{ID: "item-1", AppliedDiscounts: []cart.AppliedDiscount{{ 83 CampaignCode: "code-1", 84 Label: "title-1", 85 Type: "type-1", 86 }}} 87 decorated := decorator.DecoratedCartItem{ 88 Item: item, 89 } 90 return &decorated 91 }(), 92 want: true, 93 }, 94 } 95 for _, tt := range tests { 96 t.Run(tt.name, func(t *testing.T) { 97 if got := tt.item.HasAppliedDiscounts(); got != tt.want { 98 t.Errorf("DecoratedItem.HasDiscounts() = %v, want %v", got, tt.want) 99 } 100 }) 101 } 102 } 103 104 func TestDecoratedDelivery_MergeDiscounts(t *testing.T) { 105 tests := []struct { 106 name string 107 delivery *decorator.DecoratedDelivery 108 want cart.AppliedDiscounts 109 }{ 110 { 111 name: "empty delivery", 112 delivery: &decorator.DecoratedDelivery{ 113 Delivery: cart.Delivery{}, 114 }, 115 want: cart.AppliedDiscounts{}, 116 }, 117 { 118 name: "delivery with items but without discounts", 119 delivery: func() *decorator.DecoratedDelivery { 120 delivery := cart.Delivery{DeliveryInfo: cart.DeliveryInfo{Code: "code"}, Cartitems: []cart.Item{{}, {}}} 121 decorated := decorator.DecoratedDelivery{ 122 Delivery: delivery, 123 } 124 return &decorated 125 }(), 126 want: cart.AppliedDiscounts{}, 127 }, 128 { 129 name: "delivery with items with discounts", 130 delivery: &decorator.DecoratedDelivery{ 131 Delivery: *testutils.BuildDeliveryWithDiscounts(t), 132 }, 133 want: cart.AppliedDiscounts{ 134 { 135 CampaignCode: "code-2", 136 Label: "title-2", 137 Type: "type-1", 138 Applied: domain.NewFromFloat(-30.0, "$"), 139 SortOrder: 2, 140 }, 141 { 142 CampaignCode: "code-1", 143 Label: "title-1", 144 Type: "type-1", 145 Applied: domain.NewFromFloat(-20.0, "$"), 146 SortOrder: 3, 147 }, 148 { 149 CampaignCode: "code-3", 150 Label: "title-1", 151 Type: "type-2", 152 Applied: domain.NewFromFloat(-10.0, "$"), 153 SortOrder: 4, 154 }, 155 }, 156 }, 157 } 158 for _, tt := range tests { 159 t.Run(tt.name, func(t *testing.T) { 160 got := tt.delivery.MergeDiscounts() 161 if !reflect.DeepEqual(got, tt.want) { 162 t.Errorf("DecoratedDelivery.MergeDiscounts() = %v, want %v", got, tt.want) 163 } 164 }) 165 } 166 } 167 168 func TestDecoratedDelivery_HasDiscounts(t *testing.T) { 169 tests := []struct { 170 name string 171 delivery *decorator.DecoratedDelivery 172 want bool 173 }{ 174 { 175 name: "empty delivery", 176 delivery: &decorator.DecoratedDelivery{ 177 Delivery: cart.Delivery{}, 178 }, 179 want: false, 180 }, 181 { 182 name: "delivery with items but without discounts", 183 delivery: &decorator.DecoratedDelivery{ 184 Delivery: *testutils.BuildDeliveryWithoutDiscounts(t), 185 }, 186 want: false, 187 }, 188 { 189 name: "delivery with items with discounts", 190 delivery: &decorator.DecoratedDelivery{ 191 Delivery: *testutils.BuildDeliveryWithDiscounts(t), 192 }, 193 want: true, 194 }, 195 } 196 for _, tt := range tests { 197 t.Run(tt.name, func(t *testing.T) { 198 if got := tt.delivery.HasAppliedDiscounts(); got != tt.want { 199 t.Errorf("DecoratedDelivery.HasDiscounts() = %v, want %v", got, tt.want) 200 } 201 }) 202 } 203 } 204 205 func TestDecoratedCart_MergeDiscounts(t *testing.T) { 206 tests := []struct { 207 name string 208 cart *decorator.DecoratedCart 209 want cart.AppliedDiscounts 210 }{ 211 { 212 name: "empty cart", 213 cart: &decorator.DecoratedCart{ 214 Cart: cart.Cart{}, 215 }, 216 want: cart.AppliedDiscounts{}, 217 }, 218 { 219 name: "cart with deliveries with items but without discounts", 220 cart: &decorator.DecoratedCart{ 221 Cart: cart.Cart{ 222 Deliveries: func() []cart.Delivery { 223 result := make([]cart.Delivery, 0) 224 delivery := cart.Delivery{DeliveryInfo: cart.DeliveryInfo{Code: "code-1"}} 225 result = append(result, delivery) 226 return result 227 }(), 228 }, 229 }, 230 want: cart.AppliedDiscounts{}, 231 }, 232 { 233 name: "cart with deliveries with items with discounts", 234 cart: &decorator.DecoratedCart{ 235 Cart: cart.Cart{ 236 Deliveries: func() []cart.Delivery { 237 result := make([]cart.Delivery, 0) 238 delivery := testutils.BuildDeliveryWithDiscounts(t) 239 result = append(result, *delivery) 240 delivery = testutils.BuildDeliveryWithDiscounts(t) 241 result = append(result, *delivery) 242 return result 243 }(), 244 }, 245 }, 246 want: cart.AppliedDiscounts{ 247 { 248 CampaignCode: "code-2", 249 Label: "title-2", 250 Type: "type-1", 251 Applied: domain.NewFromFloat(-60.0, "$"), 252 SortOrder: 2, 253 }, 254 { 255 CampaignCode: "code-1", 256 Label: "title-1", 257 Type: "type-1", 258 Applied: domain.NewFromFloat(-40.0, "$"), 259 SortOrder: 3, 260 }, 261 { 262 CampaignCode: "code-3", 263 Label: "title-1", 264 Type: "type-2", 265 Applied: domain.NewFromFloat(-20.0, "$"), 266 SortOrder: 4, 267 }, 268 }, 269 }, 270 } 271 for _, tt := range tests { 272 t.Run(tt.name, func(t *testing.T) { 273 got := tt.cart.MergeDiscounts() 274 if !reflect.DeepEqual(got, tt.want) { 275 t.Errorf("DecoratedCart.MergeDiscounts() = %v, want %v", got, tt.want) 276 } 277 }) 278 } 279 } 280 281 func TestDecoratedCart_HasDiscounts(t *testing.T) { 282 tests := []struct { 283 name string 284 cart *decorator.DecoratedCart 285 want bool 286 }{ 287 { 288 name: "empty cart", 289 cart: &decorator.DecoratedCart{ 290 Cart: cart.Cart{}, 291 }, 292 want: false, 293 }, 294 { 295 name: "cart with deliveries with items with discounts", 296 cart: &decorator.DecoratedCart{ 297 Cart: cart.Cart{ 298 Deliveries: func() []cart.Delivery { 299 result := make([]cart.Delivery, 0) 300 delivery := testutils.BuildDeliveryWithDiscounts(t) 301 result = append(result, *delivery) 302 delivery = testutils.BuildDeliveryWithDiscounts(t) 303 result = append(result, *delivery) 304 return result 305 }(), 306 }, 307 }, 308 want: true, 309 }, 310 } 311 for _, tt := range tests { 312 t.Run(tt.name, func(t *testing.T) { 313 if got := tt.cart.HasAppliedDiscounts(); got != tt.want { 314 t.Errorf("DecoratedCart.HasDiscounts() = %v, want %v", got, tt.want) 315 } 316 }) 317 } 318 }