ids-svc-product/internal/idgen/snowflake_test.go
robinj0726 517f629d76 feat: integrate Snowflake ID generation for product and SKU creation
- Added a new ID generator using the Snowflake algorithm to create unique identifiers for products and SKUs.
- Updated the product server to utilize the ID generator for assigning IDs during product and SKU creation.
- Refactored the product repository to accommodate the new ID handling logic.
- Introduced unit tests for the ID generator to ensure correct functionality and error handling.
2026-07-10 15:56:42 +08:00

29 lines
567 B
Go

package idgen
import (
"strconv"
"testing"
)
func TestGeneratorNextString(t *testing.T) {
t.Setenv("SNOWFLAKE_WORKER_ID", "1")
g, err := NewFromEnv()
if err != nil {
t.Fatalf("NewFromEnv() error = %v", err)
}
id := g.NextString()
if _, err := strconv.ParseInt(id, 10, 64); err != nil {
t.Fatalf("NextString() = %q, want decimal int64: %v", id, err)
}
}
func TestNewFromEnvRejectsInvalidWorkerID(t *testing.T) {
t.Setenv("SNOWFLAKE_WORKER_ID", "1024")
if _, err := NewFromEnv(); err == nil {
t.Fatal("NewFromEnv() error = nil, want error")
}
}