variants as a single product

This commit is contained in:
2026-09-03 10:43:05 +05:30
parent 2401158190
commit da0e9d987e
7 changed files with 205 additions and 4 deletions

View File

@@ -667,3 +667,51 @@ func TestFilteringGroupsDoesNotDisturbTheCallersSlice(t *testing.T) {
t.Errorf("the caller's slice was modified: %d products left", len(original[0].Products))
}
}
/*
One product, three sizes.
R mart stocks Aachi Baby Fryums as three product rows — 100g, 500g and 1kg —
each with its own price and its own shelf. In the app that read as three
near-identical listings a shopper had to tell apart by a suffix. Grouped, it is
one listing with a size picker.
*/
func TestASizeOfAnotherProductIsNotListedOnItsOwn(t *testing.T) {
groups := []models.Tenantproducts{{Products: []models.Products{
{Productid: 7112}, // 100g — the parent
{Productid: 7113}, // 1kg — a size of it
{Productid: 7114}, // 500g — a size of it
}}}
got := WithoutVariantChildren(groups, map[int]bool{7113: true, 7114: true})
if len(got[0].Products) != 1 || got[0].Products[0].Productid != 7112 {
t.Fatalf("expected only the parent, got %v", got[0].Products)
}
}
func TestAShopWithNoGroupsIsUntouched(t *testing.T) {
// The common case by far, and it must not pay for the feature.
groups := []models.Tenantproducts{{Products: []models.Products{{Productid: 1}, {Productid: 2}}}}
got := WithoutVariantChildren(groups, map[int]bool{})
if len(got[0].Products) != 2 {
t.Errorf("an ungrouped shop lost products: %v", got[0].Products)
}
}
func TestTheShopSurvivesEvenIfEveryProductIsASize(t *testing.T) {
// Should not happen — a group always has a parent — but a listing is the
// wrong place to discover bad data, and an empty list beats a crash.
groups := []models.Tenantproducts{{Products: []models.Products{{Productid: 5}}}}
got := WithoutVariantChildren(groups, map[int]bool{5: true})
if len(got) != 1 || len(got[0].Products) != 0 {
t.Errorf("expected the shop to survive with nothing listed, got %v", got)
}
}
func TestFilteringChildrenDoesNotDisturbTheCallersSlice(t *testing.T) {
original := []models.Tenantproducts{{Products: []models.Products{{Productid: 1}, {Productid: 2}}}}
_ = WithoutVariantChildren(original, map[int]bool{2: true})
if len(original[0].Products) != 2 {
t.Errorf("the caller's slice was modified")
}
}