Files
backend_fiesta/repositories/productVariantLink_test.go
2026-09-02 13:11:01 +05:30

68 lines
2.3 KiB
Go

package repositories
import (
"errors"
"testing"
"nearle/models"
)
/*
Variants are a relationship between two products, and every test here is about
a shape that would break the ordering screen rather than merely store bad data.
The bug this replaces: `productvariants` had `productid` and `variantproductid`
all along, neither was mapped, so a variant could never be attached to a
product. The app fell back to `products.variants` — a group number that is 0 on
every product — and asked for "everything in group 0", which matched the
tenant's whole ungrouped catalogue. Six unrelated products came back as each
other's sizes and the order could not be placed.
*/
func TestAVariantWithoutAParentIsRefused(t *testing.T) {
r := &productRepository{}
_, err := r.AddProductVariant(models.Productvariant{Tenantid: 1, Variantproductid: 7})
if !errors.Is(err, ErrVariantParentMissing) {
t.Fatalf("expected a missing-parent refusal, got %v", err)
}
}
func TestAVariantThatOrdersNothingIsRefused(t *testing.T) {
// Without `variantproductid` there is no product to put in the basket, so
// the size would be pickable and unbuyable.
r := &productRepository{}
_, err := r.AddProductVariant(models.Productvariant{Tenantid: 1, Productid: 7})
if !errors.Is(err, ErrVariantProductMissing) {
t.Fatalf("expected a missing-product refusal, got %v", err)
}
}
func TestAProductCannotBeItsOwnVariant(t *testing.T) {
// The picker would offer the thing already tapped, and choosing it loops.
r := &productRepository{}
_, err := r.AddProductVariant(models.Productvariant{Tenantid: 1, Productid: 7, Variantproductid: 7})
if !errors.Is(err, ErrVariantSelfReference) {
t.Fatalf("expected a self-reference refusal, got %v", err)
}
}
func TestNoProductsMeansNoQueryAndNoVariants(t *testing.T) {
// Guards the N+1: the batch loader must not run a query for an empty page.
// `r.db` is nil here, so reaching the database at all would panic.
r := &productRepository{}
got, err := r.VariantsForProducts(1, 1, nil)
if err != nil {
t.Fatalf("VariantsForProducts: %v", err)
}
if len(got) != 0 {
t.Errorf("expected no variants, got %d", len(got))
}
}
func TestAnUnknownTenantAsksTheDatabaseNothing(t *testing.T) {
r := &productRepository{}
if _, err := r.VariantsForProducts(0, 1, []int{7, 8}); err != nil {
t.Fatalf("VariantsForProducts: %v", err)
}
}