47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestUploadOperationSurvivesRestartAndBlocksNewKey(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "state.db")
|
|
s, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
op := UploadOperation{ProductID: "fictional-internal-id", ShopeeID: "123", IdempotencyKey: "fictional-key-1", FilePath: "fictional.mp4", FileSize: 10, UpdatedAt: "2026-09-30T00:00:00Z"}
|
|
if err := s.BeginUploadOperation(op); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
got, found, err := s.GetUploadOperation(op.ProductID)
|
|
if err != nil || !found || got.IdempotencyKey != op.IdempotencyKey {
|
|
t.Fatalf("operation lost: %+v %v", got, err)
|
|
}
|
|
op.IdempotencyKey = "fictional-key-2"
|
|
if err := s.BeginUploadOperation(op); !errors.Is(err, ErrUploadOperationPending) {
|
|
t.Fatalf("pending operation overwritten: %v", err)
|
|
}
|
|
got.Status = "succeeded"
|
|
if err := s.UpdateUploadOperation(got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.BeginUploadOperation(op); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _, err = s.GetUploadOperation(op.ProductID)
|
|
if err != nil || got.IdempotencyKey != op.IdempotencyKey {
|
|
t.Fatalf("terminal operation not replaced: %+v %v", got, err)
|
|
}
|
|
}
|