76 lines
2.1 KiB
Go
76 lines
2.1 KiB
Go
package batchimport
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNormalizeBatchNoPrefix(t *testing.T) {
|
|
prefix, err := normalizeBatchNoPrefix(" inb ")
|
|
if err != nil {
|
|
t.Fatalf("normalize prefix: %v", err)
|
|
}
|
|
if prefix != "INB" {
|
|
t.Fatalf("unexpected prefix: %s", prefix)
|
|
}
|
|
|
|
if _, err = normalizeBatchNoPrefix("IN"); err == nil {
|
|
t.Fatal("expected error for non-three-character prefix")
|
|
}
|
|
}
|
|
|
|
func TestFormatBatchNo(t *testing.T) {
|
|
now := time.Date(2026, 8, 10, 12, 34, 56, 789000000, time.Local)
|
|
batchNo, err := formatBatchNo("REN", now, 7)
|
|
if err != nil {
|
|
t.Fatalf("format batch no: %v", err)
|
|
}
|
|
if batchNo != "REN20260810123456007" {
|
|
t.Fatalf("unexpected batch no: %s", batchNo)
|
|
}
|
|
if len(batchNo) != 20 {
|
|
t.Fatalf("unexpected batch no length: %d", len(batchNo))
|
|
}
|
|
if strings.Contains(batchNo, "789") {
|
|
t.Fatalf("batch no should not include milliseconds: %s", batchNo)
|
|
}
|
|
}
|
|
|
|
func TestFormatBatchNoRejectsOverflowSequence(t *testing.T) {
|
|
_, err := formatBatchNo("REN", time.Now(), 1000)
|
|
if err == nil {
|
|
t.Fatal("expected overflow sequence error")
|
|
}
|
|
}
|
|
|
|
func TestBatchNoBoundaries(t *testing.T) {
|
|
now := time.Date(2026, 10, 12, 23, 59, 59, 0, time.FixedZone("CST", 8*3600))
|
|
for _, sequence := range []int64{-1, 0, 1000} {
|
|
if _, err := formatBatchNo("REN", now, sequence); err == nil {
|
|
t.Fatalf("sequence %d should be rejected", sequence)
|
|
}
|
|
}
|
|
for _, sequence := range []int64{1, 999} {
|
|
number, err := formatBatchNo("REN", now, sequence)
|
|
if err != nil || len(number) != 20 {
|
|
t.Fatalf("sequence %d: number=%q err=%v", sequence, number, err)
|
|
}
|
|
}
|
|
if batchNoSequenceKey(now) != "iotlcm:batch:no:seq:20261012" || batchNoSequenceKey(now.Add(time.Second)) != "iotlcm:batch:no:seq:20261013" {
|
|
t.Fatal("daily sequence key does not roll over at midnight")
|
|
}
|
|
}
|
|
|
|
func TestNextBatchNoRejectsInvalidPrefixBeforeRedis(t *testing.T) {
|
|
for _, prefix := range []string{"", " ", "RE", "RENEW"} {
|
|
for _, ctx := range []context.Context{nil, context.Background()} {
|
|
number, err := NextBatchNoContext(ctx, prefix)
|
|
if err == nil || number != "" {
|
|
t.Fatalf("prefix %q: number=%q err=%v", prefix, number, err)
|
|
}
|
|
}
|
|
}
|
|
}
|