ids-svc-product/internal/repository/store_scope_test.go
robinj0726 604dfeb06c feat: implement store scope validation and enforce it in product repository
- Added StoreScope struct with TenantID and StoreID fields.
- Introduced ErrStoreScopeRequired to handle missing store scope.
- Implemented Valid method to check if both TenantID and StoreID are present.
- Created requireWriteScope function to validate store scope before write operations.
- Updated product repository methods to require store scope for various operations.
- Added tests for StoreScope validation and requirement enforcement.
2026-09-13 00:12:44 +08:00

34 lines
923 B
Go

package repository
import (
"testing"
"github.com/jackc/pgx/v5/pgconn"
)
func TestStoreScopeValid(t *testing.T) {
if (StoreScope{}).Valid() {
t.Fatal("empty scope must be invalid")
}
if !(StoreScope{TenantID: 1, StoreID: 2}).Valid() {
t.Fatal("complete scope must be valid")
}
}
func TestRequireWriteScope(t *testing.T) {
if err := requireWriteScope(StoreScope{}); err != ErrStoreScopeRequired {
t.Fatalf("empty scope err = %v", err)
}
}
func TestDuplicateSPUNoConstraintNames(t *testing.T) {
old := &pgconn.PgError{Code: "23505", ConstraintName: "uq_products_spu_no"}
if !isPostgresConstraintError(old, "23505", "uq_products_spu_no") {
t.Fatal("old spu unique name must match")
}
next := &pgconn.PgError{Code: "23505", ConstraintName: "uq_products_store_spu_no"}
if !isPostgresConstraintError(next, "23505", "uq_products_store_spu_no") {
t.Fatal("store-scoped spu unique name must match")
}
}