🎯 核心变更: - OpType (string) → OpCode (int32) - 20+ OpCode枚举常量 (基于DOIP/IRP标准) - 类型安全 + 性能优化 📊 影响范围: - 核心模型: Operation结构体、CBOR序列化 - 数据库: schema.go + SQL DDL (PostgreSQL/MySQL/SQLite) - 持久化: repository.go查询、cursor_worker.go - API接口: Protobuf定义 + gRPC客户端 - 测试代码: 60+ 测试文件更新 ✅ 测试结果: - 通过率: 100% (所有87个测试用例) - 总体覆盖率: 53.7% - 核心包覆盖率: logger(100%), highclient(95.3%), model(79.1%) 📝 文档: - 精简README (1056行→489行,减少54%) - 完整的OpCode枚举说明 - 三种持久化策略示例 - 数据库表结构和架构图 🔧 技术细节: - 类型转换: string(OpCode) → int32(OpCode) - SQL参数: 字符串值 → 整数值 - Protobuf: op_type string → op_code int32 - 测试断言: 字符串比较 → 常量比较 🎉 质量保证: - 零编译错误 - 100%测试通过 - PostgreSQL/Pulsar集成测试验证 - 分布式并发安全测试通过
58 lines
1.6 KiB
Go
58 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,
|
|
OpCode: model.OpCodeCreateID,
|
|
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(),
|
|
"纳秒部分应该相等")
|
|
}
|
|
|