主要变更: - Operation.OpType: Type → string - NewFullOperation 参数: opType Type → opType string - IsValidOpType 参数: opType Type → opType string - operationMeta.OpType: *Type → *string - queryclient.ListRequest.OpType: model.Type → string 优点: - 更灵活,支持动态扩展操作类型 - 不再受限于预定义的枚举常量 - 简化类型转换逻辑 兼容性: - Type 常量定义保持不变 (OpTypeCreate, OpTypeUpdate 等) - 使用时需要 string() 转换: string(model.OpTypeCreate) - 所有单元测试已更新并通过 (100%) 测试结果: ✅ api/adapter - PASS ✅ api/highclient - PASS ✅ api/logger - PASS ✅ api/model - PASS ✅ api/persistence - PASS ✅ api/queryclient - PASS ✅ internal/* - PASS
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package model_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.yandata.net/iod/iod/go-trustlog/api/model"
|
|
)
|
|
|
|
// TestOperation_TimestampNanosecondPrecision 验证 Operation 的时间戳在 CBOR 序列化/反序列化后能保留纳秒精度
|
|
func TestOperation_TimestampNanosecondPrecision(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// 创建一个包含纳秒精度的时间戳
|
|
timestamp := time.Date(2024, 1, 1, 12, 30, 45, 123456789, time.UTC)
|
|
|
|
original := &model.Operation{
|
|
OpID: "op-nanosecond-test",
|
|
Timestamp: timestamp,
|
|
OpSource: model.OpSourceIRP,
|
|
OpType: string(model.OpTypeOCCreateHandle),
|
|
DoPrefix: "test",
|
|
DoRepository: "repo",
|
|
Doid: "test/repo/123",
|
|
ProducerID: "producer-1",
|
|
OpActor: "actor-1",
|
|
}
|
|
|
|
err := original.CheckAndInit()
|
|
require.NoError(t, err)
|
|
|
|
t.Logf("Original timestamp: %v", original.Timestamp)
|
|
t.Logf("Original nanoseconds: %d", original.Timestamp.Nanosecond())
|
|
|
|
// 序列化
|
|
data, err := original.MarshalBinary()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, data)
|
|
|
|
// 反序列化
|
|
result := &model.Operation{}
|
|
err = result.UnmarshalBinary(data)
|
|
require.NoError(t, err)
|
|
|
|
t.Logf("Decoded timestamp: %v", result.Timestamp)
|
|
t.Logf("Decoded nanoseconds: %d", result.Timestamp.Nanosecond())
|
|
|
|
// 验证纳秒精度被完整保留
|
|
assert.Equal(t, original.Timestamp.UnixNano(), result.Timestamp.UnixNano(),
|
|
"时间戳的纳秒精度应该被完整保留")
|
|
assert.Equal(t, original.Timestamp.Nanosecond(), result.Timestamp.Nanosecond(),
|
|
"纳秒部分应该相等")
|
|
}
|