Skip to content

Commit

Permalink
chore: Move test to proper location, add test to cover update without…
Browse files Browse the repository at this point in the history
… qty
  • Loading branch information
carstendietrich committed Oct 16, 2023
1 parent 078b520 commit 6a8253c
Showing 1 changed file with 153 additions and 69 deletions.
222 changes: 153 additions & 69 deletions cart/infrastructure/defaultCartBehaviour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,159 @@ func TestDefaultCartBehaviour_UpdateItems(t *testing.T) {
assert.Equal(t, 0, len(got.Deliveries[0].Cartitems))
assert.Equal(t, 0.0, got.GrandTotal.FloatAmount())
})

t.Run("update without qty still updates other fields", func(t *testing.T) {
t.Parallel()

cob := &DefaultCartBehaviour{}
cob.Inject(
newInMemoryStorage(),
&fake.ProductService{},
flamingo.NullLogger{},
nil,
nil,
nil,
)

cart, err := cob.StoreNewCart(context.Background(), &domaincart.Cart{
ID: "cartID",
Deliveries: []domaincart.Delivery{
{
Cartitems: []domaincart.Item{
{
ID: "firstItem",
Qty: 1,
MarketplaceCode: "fake_fixed_simple_without_discounts",
},
{
ID: "secondItem",
MarketplaceCode: "fake_bundle",
Qty: 1,
BundleConfig: map[domain.Identifier]domain.ChoiceConfiguration{
"identifier1": {
MarketplaceCode: "simple_option1",
Qty: 1,
},
"identifier2": {
MarketplaceCode: "configurable_option2",
VariantMarketplaceCode: "shirt-black-s",
Qty: 1,
},
},
},
},
},
},
})
assert.NoError(t, err)

source := "baz"
got, _, err := cob.UpdateItems(context.Background(), cart, []domaincart.ItemUpdateCommand{
{
ItemID: "firstItem",
AdditionalData: map[string]string{"foo": "bar"},
SourceID: &source,
},
{
ItemID: "secondItem",
BundleConfiguration: map[domain.Identifier]domain.ChoiceConfiguration{
"identifier1": {
MarketplaceCode: "simple_option2",
Qty: 1,
},
"identifier2": {
MarketplaceCode: "configurable_option1",
VariantMarketplaceCode: "shirt-black-l",
Qty: 1,
},
},
},
})

assert.NoError(t, err)

assert.Equal(t, "firstItem", got.Deliveries[0].Cartitems[0].ID)
assert.Equal(t, "fake_fixed_simple_without_discounts", got.Deliveries[0].Cartitems[0].MarketplaceCode)
assert.Equal(t, map[string]string{"foo": "bar"}, got.Deliveries[0].Cartitems[0].AdditionalData)
assert.Equal(t, "baz", got.Deliveries[0].Cartitems[0].SourceID)

assert.Equal(t, "secondItem", got.Deliveries[0].Cartitems[1].ID)
assert.Equal(t, "fake_bundle", got.Deliveries[0].Cartitems[1].MarketplaceCode)
assert.Equal(t, "simple_option2", got.Deliveries[0].Cartitems[1].BundleConfig["identifier1"].MarketplaceCode)
assert.Equal(t, 1, got.Deliveries[0].Cartitems[1].BundleConfig["identifier1"].Qty)
assert.Equal(t, "configurable_option1", got.Deliveries[0].Cartitems[1].BundleConfig["identifier2"].MarketplaceCode)
assert.Equal(t, "shirt-black-l", got.Deliveries[0].Cartitems[1].BundleConfig["identifier2"].VariantMarketplaceCode)
assert.Equal(t, 1, got.Deliveries[0].Cartitems[1].BundleConfig["identifier2"].Qty)
})

t.Run("update bundle configuration for a cart item", func(t *testing.T) {
t.Parallel()

cob := &DefaultCartBehaviour{}
cob.Inject(
newInMemoryStorage(),
&fake.ProductService{},
flamingo.NullLogger{},
nil,
nil,
nil,
)

cart, err := cob.StoreNewCart(context.Background(), &domaincart.Cart{
ID: "1234",
Deliveries: []domaincart.Delivery{
{
DeliveryInfo: domaincart.DeliveryInfo{
Code: "delivery",
},
Cartitems: []domaincart.Item{
{
ID: "1234",
MarketplaceCode: "fake_bundle",
Qty: 1,
BundleConfig: map[domain.Identifier]domain.ChoiceConfiguration{
"identifier1": {
MarketplaceCode: "simple_option1",
Qty: 1,
},
"identifier2": {
MarketplaceCode: "configurable_option2",
VariantMarketplaceCode: "shirt-black-s",
Qty: 1,
},
},
},
},
},
},
})
assert.NoError(t, err)

qty := 1
got, _, err := cob.UpdateItems(context.Background(), cart, []domaincart.ItemUpdateCommand{{
ItemID: "1234",
Qty: &qty,
BundleConfiguration: map[domain.Identifier]domain.ChoiceConfiguration{
"identifier1": {
MarketplaceCode: "simple_option2",
Qty: 2,
},
"identifier2": {
MarketplaceCode: "configurable_option1",
VariantMarketplaceCode: "shirt-black-l",
Qty: 2,
},
},
}})

assert.NoError(t, err)
assert.Equal(t, "fake_bundle", got.Deliveries[0].Cartitems[0].MarketplaceCode)
assert.Equal(t, "simple_option2", got.Deliveries[0].Cartitems[0].BundleConfig["identifier1"].MarketplaceCode)
assert.Equal(t, 2, got.Deliveries[0].Cartitems[0].BundleConfig["identifier1"].Qty)
assert.Equal(t, "configurable_option1", got.Deliveries[0].Cartitems[0].BundleConfig["identifier2"].MarketplaceCode)
assert.Equal(t, "shirt-black-l", got.Deliveries[0].Cartitems[0].BundleConfig["identifier2"].VariantMarketplaceCode)
assert.Equal(t, 2, got.Deliveries[0].Cartitems[0].BundleConfig["identifier2"].Qty)
})
}

func TestDefaultCartBehaviour_AddToCart(t *testing.T) {
Expand Down Expand Up @@ -952,75 +1105,6 @@ func TestDefaultCartBehaviour_AddToCart(t *testing.T) {
assert.Equal(t, "fake_bundle", got.Deliveries[0].Cartitems[1].MarketplaceCode)
assert.Equal(t, "simple_option2", got.Deliveries[0].Cartitems[1].BundleConfig["identifier1"].MarketplaceCode)
})

t.Run("update bundle configuration for a cart item", func(t *testing.T) {
t.Parallel()

cob := &DefaultCartBehaviour{}
cob.Inject(
newInMemoryStorage(),
&fake.ProductService{},
flamingo.NullLogger{},
nil,
nil,
nil,
)

cart, err := cob.StoreNewCart(context.Background(), &domaincart.Cart{
ID: "1234",
Deliveries: []domaincart.Delivery{
{
DeliveryInfo: domaincart.DeliveryInfo{
Code: "delivery",
},
Cartitems: []domaincart.Item{
{
ID: "1234",
MarketplaceCode: "fake_bundle",
Qty: 1,
BundleConfig: map[domain.Identifier]domain.ChoiceConfiguration{
"identifier1": {
MarketplaceCode: "simple_option1",
Qty: 1,
},
"identifier2": {
MarketplaceCode: "configurable_option2",
VariantMarketplaceCode: "shirt-black-s",
Qty: 1,
},
},
},
},
},
},
})
assert.NoError(t, err)

qty := 1
got, _, err := cob.UpdateItem(context.Background(), cart, domaincart.ItemUpdateCommand{
ItemID: "1234",
Qty: &qty,
BundleConfiguration: map[domain.Identifier]domain.ChoiceConfiguration{
"identifier1": {
MarketplaceCode: "simple_option2",
Qty: 2,
},
"identifier2": {
MarketplaceCode: "configurable_option1",
VariantMarketplaceCode: "shirt-black-l",
Qty: 2,
},
},
})

assert.NoError(t, err)
assert.Equal(t, "fake_bundle", got.Deliveries[0].Cartitems[0].MarketplaceCode)
assert.Equal(t, "simple_option2", got.Deliveries[0].Cartitems[0].BundleConfig["identifier1"].MarketplaceCode)
assert.Equal(t, 2, got.Deliveries[0].Cartitems[0].BundleConfig["identifier1"].Qty)
assert.Equal(t, "configurable_option1", got.Deliveries[0].Cartitems[0].BundleConfig["identifier2"].MarketplaceCode)
assert.Equal(t, "shirt-black-l", got.Deliveries[0].Cartitems[0].BundleConfig["identifier2"].VariantMarketplaceCode)
assert.Equal(t, 2, got.Deliveries[0].Cartitems[0].BundleConfig["identifier2"].Qty)
})
}

func TestDefaultCartBehaviour_UpdatePurchaser(t *testing.T) {
Expand Down

0 comments on commit 6a8253c

Please sign in to comment.