This commit is contained in:
2026-09-02 15:37:59 +05:30
parent 68871cb043
commit 09efc5403f
6 changed files with 289 additions and 46 deletions

View File

@@ -606,3 +606,64 @@ func TestImportPublishesAfterTheRowsExist(t *testing.T) {
t.Fatalf("want CreateProductLocation before PublishPricedLocations, got %v", repo.calls)
}
}
/*
The grouped shape, which is what `getallproducts` answers with.
Measured 2026-09-02 on R mart: the app was offering four products with a zero
balance — Cadbury Bournvita 500g, Amul 90g, Amul 150g and a Dabur chewing gum.
A shopper could pick any of them and only find out at checkout, where the stock
check answers 409.
*/
func groupOf(stocks ...int) []models.Tenantproducts {
products := make([]models.Products, 0, len(stocks))
for i, s := range stocks {
products = append(products, models.Products{Productid: 100 + i, Productstock: s})
}
return []models.Tenantproducts{{Products: products}}
}
func TestAnEmptyShelfIsNotOfferedToShoppers(t *testing.T) {
got := InStockOnlyGrouped(groupOf(5, 0, 3, 0))
if len(got) != 1 {
t.Fatalf("expected the tenant group to survive, got %d groups", len(got))
}
if len(got[0].Products) != 2 {
t.Fatalf("expected 2 sellable products, got %d", len(got[0].Products))
}
for _, p := range got[0].Products {
if p.Productstock <= 0 {
t.Errorf("product %d has no stock and was still offered", p.Productid)
}
}
}
func TestAShopWithNothingOnTheShelfStillExists(t *testing.T) {
// The group is kept with an empty list. Dropping it would tell the app the
// shop is not there, which is a different and wrong statement: it is open,
// it just has nothing to sell today.
got := InStockOnlyGrouped(groupOf(0, 0))
if len(got) != 1 {
t.Fatalf("the shop disappeared: got %d groups", len(got))
}
if len(got[0].Products) != 0 {
t.Errorf("expected nothing sellable, got %d", len(got[0].Products))
}
}
func TestANegativeBalanceIsNotStockWhenGrouped(t *testing.T) {
// A ledger can go negative through correction. Negative is not "some".
got := InStockOnlyGrouped(groupOf(-4))
if len(got[0].Products) != 0 {
t.Errorf("a negative balance was treated as stock")
}
}
func TestFilteringGroupsDoesNotDisturbTheCallersSlice(t *testing.T) {
original := groupOf(1, 0)
_ = InStockOnlyGrouped(original)
if len(original[0].Products) != 2 {
t.Errorf("the caller's slice was modified: %d products left", len(original[0].Products))
}
}