ids-svc-product/internal/repository/product_repository_test.go

102 lines
3.2 KiB
Go

package repository
import (
"strings"
"testing"
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
)
func TestEscapeLikeLiteral(t *testing.T) {
input := `100%_off\\bundle`
if got, want := escapeLikeLiteral(input), `100\%\_off\\\\bundle`; got != want {
t.Fatalf("escapeLikeLiteral(%q) = %q, want %q", input, got, want)
}
}
func TestAggregateInventoryAdjustments(t *testing.T) {
got, err := aggregateInventoryAdjustments([]InventoryAdjustment{
{SKUID: "1002", Quantity: 3},
{SKUID: "1001", Quantity: 1},
{SKUID: "1001", Quantity: 2},
})
if err != nil {
t.Fatal(err)
}
if len(got) != 2 {
t.Fatalf("len = %d, want 2", len(got))
}
if got[0] != (InventoryAdjustment{SKUID: "1001", Quantity: 3}) {
t.Fatalf("first adjustment = %#v", got[0])
}
if got[1] != (InventoryAdjustment{SKUID: "1002", Quantity: 3}) {
t.Fatalf("second adjustment = %#v", got[1])
}
}
func TestAggregateInventoryAdjustmentsRejectsInvalidSKUID(t *testing.T) {
_, err := aggregateInventoryAdjustments([]InventoryAdjustment{
{SKUID: "bad-sku", Quantity: 1},
})
if err == nil {
t.Fatal("expected invalid sku id error")
}
}
func TestReplaceProductLeafCategoryBindingsRequiresStoreScope(t *testing.T) {
err := replaceProductLeafCategoryBindings(nil, nil, "1001", []string{"2001"}, []string{"3001"}, StoreScope{})
if err != ErrStoreScopeRequired {
t.Fatalf("err = %v, want %v", err, ErrStoreScopeRequired)
}
}
func TestReplaceProductGroupBindingsRequiresStoreScope(t *testing.T) {
err := replaceProductGroupBindings(nil, nil, "1001", []string{"2001"}, []string{"3001"}, StoreScope{})
if err != ErrStoreScopeRequired {
t.Fatalf("err = %v, want %v", err, ErrStoreScopeRequired)
}
}
func TestProductListWhereUsesBusinessProductCategoryTree(t *testing.T) {
where, args := productListWhere(ProductListFilter{ProductCategoryID: "2001"}, StoreScope{StoreID: 1001})
for _, expected := range []string{
"WITH RECURSIVE category_descendants",
"product.product_category c",
"product.product_category_bindings pcb",
"pcb.product_category_id",
} {
if !strings.Contains(where, expected) {
t.Fatalf("category predicate missing %q:\n%s", expected, where)
}
}
if strings.Contains(where, "product.product_catalog_bindings") {
t.Fatalf("category predicate must not query storefront catalog bindings:\n%s", where)
}
if len(args) != 2 || args[0] != int64(1001) || args[1] != "2001" {
t.Fatalf("args = %#v, want [1001 2001]", args)
}
}
func TestProductListWhereFiltersStatus(t *testing.T) {
productStatus := productv1.ProductStatus_PRODUCT_STATUS_INVENTORY
where, args := productListWhere(ProductListFilter{Status: &productStatus}, StoreScope{StoreID: 1001})
if !strings.Contains(where, "p.status = $2") {
t.Fatalf("status predicate missing:\n%s", where)
}
if len(args) != 2 || args[1] != int16(1) {
t.Fatalf("args = %#v, want [1001 1]", args)
}
}
func TestProductListWhereIgnoresSoftDeletedGroupBindings(t *testing.T) {
where, _ := productListWhere(ProductListFilter{GroupID: "2001"}, StoreScope{StoreID: 1001})
for _, expected := range []string{
"pcb.deleted_at IS NULL",
"pc.deleted_at IS NULL",
} {
if !strings.Contains(where, expected) {
t.Fatalf("group predicate missing %q:\n%s", expected, where)
}
}
}