🎯 核心变更: - 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集成测试验证 - 分布式并发安全测试通过
351 lines
9.1 KiB
Go
351 lines
9.1 KiB
Go
package model_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.yandata.net/iod/iod/go-trustlog/api/grpc/pb"
|
|
"go.yandata.net/iod/iod/go-trustlog/api/model"
|
|
)
|
|
|
|
func TestProofFromProtobuf_Nil(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
result := model.ProofFromProtobuf(nil)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestProofFromProtobuf_Empty(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Empty(t, result.Sign)
|
|
assert.Empty(t, result.Version)
|
|
assert.Nil(t, result.ColItems)
|
|
assert.Nil(t, result.RawItems)
|
|
assert.Nil(t, result.ColRootItem)
|
|
assert.Nil(t, result.RawRootItem)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithSign(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
Sign: "test-signature",
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "test-signature", result.Sign)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithVersion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
Version: "v1.0.0",
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "v1.0.0", result.Version)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithColItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
ColItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "hash1", Left: true},
|
|
{Floor: 2, Hash: "hash2", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.ColItems, 2)
|
|
assert.Equal(t, uint32(1), result.ColItems[0].Floor)
|
|
assert.Equal(t, "hash1", result.ColItems[0].Hash)
|
|
assert.True(t, result.ColItems[0].Left)
|
|
assert.Equal(t, uint32(2), result.ColItems[1].Floor)
|
|
assert.Equal(t, "hash2", result.ColItems[1].Hash)
|
|
assert.False(t, result.ColItems[1].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithRawItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
RawItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "hash3", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.RawItems, 1)
|
|
assert.Equal(t, uint32(3), result.RawItems[0].Floor)
|
|
assert.Equal(t, "hash3", result.RawItems[0].Hash)
|
|
assert.True(t, result.RawItems[0].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithColRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
ColRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "hash4", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.ColRootItem, 1)
|
|
assert.Equal(t, uint32(4), result.ColRootItem[0].Floor)
|
|
assert.Equal(t, "hash4", result.ColRootItem[0].Hash)
|
|
assert.False(t, result.ColRootItem[0].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_WithRawRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
RawRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 5, Hash: "hash5", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.RawRootItem, 1)
|
|
assert.Equal(t, uint32(5), result.RawRootItem[0].Floor)
|
|
assert.Equal(t, "hash5", result.RawRootItem[0].Hash)
|
|
assert.True(t, result.RawRootItem[0].Left)
|
|
}
|
|
|
|
func TestProofFromProtobuf_Full(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
pbProof := &pb.Proof{
|
|
Sign: "full-signature",
|
|
Version: "v1.0.0",
|
|
ColItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "col1", Left: true},
|
|
},
|
|
RawItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 2, Hash: "raw1", Left: false},
|
|
},
|
|
ColRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "colroot1", Left: true},
|
|
},
|
|
RawRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "rawroot1", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofFromProtobuf(pbProof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "full-signature", result.Sign)
|
|
assert.Equal(t, "v1.0.0", result.Version)
|
|
assert.Len(t, result.ColItems, 1)
|
|
assert.Len(t, result.RawItems, 1)
|
|
assert.Len(t, result.ColRootItem, 1)
|
|
assert.Len(t, result.RawRootItem, 1)
|
|
}
|
|
|
|
func TestProofToProtobuf_Nil(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
result := model.ProofToProtobuf(nil)
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestProofToProtobuf_Empty(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Empty(t, result.GetSign())
|
|
assert.Empty(t, result.GetVersion())
|
|
assert.Nil(t, result.GetColItems())
|
|
assert.Nil(t, result.GetRawItems())
|
|
assert.Nil(t, result.GetColRootItem())
|
|
assert.Nil(t, result.GetRawRootItem())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithSign(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
Sign: "test-signature",
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "test-signature", result.GetSign())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithVersion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
Version: "v1.0.0",
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "v1.0.0", result.GetVersion())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithColItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
ColItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "hash1", Left: true},
|
|
{Floor: 2, Hash: "hash2", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetColItems(), 2)
|
|
assert.Equal(t, uint32(1), result.GetColItems()[0].GetFloor())
|
|
assert.Equal(t, "hash1", result.GetColItems()[0].GetHash())
|
|
assert.True(t, result.GetColItems()[0].GetLeft())
|
|
assert.Equal(t, uint32(2), result.GetColItems()[1].GetFloor())
|
|
assert.Equal(t, "hash2", result.GetColItems()[1].GetHash())
|
|
assert.False(t, result.GetColItems()[1].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithRawItems(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
RawItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "hash3", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetRawItems(), 1)
|
|
assert.Equal(t, uint32(3), result.GetRawItems()[0].GetFloor())
|
|
assert.Equal(t, "hash3", result.GetRawItems()[0].GetHash())
|
|
assert.True(t, result.GetRawItems()[0].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithColRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
ColRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "hash4", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetColRootItem(), 1)
|
|
assert.Equal(t, uint32(4), result.GetColRootItem()[0].GetFloor())
|
|
assert.Equal(t, "hash4", result.GetColRootItem()[0].GetHash())
|
|
assert.False(t, result.GetColRootItem()[0].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_WithRawRootItem(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
RawRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 5, Hash: "hash5", Left: true},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
require.Len(t, result.GetRawRootItem(), 1)
|
|
assert.Equal(t, uint32(5), result.GetRawRootItem()[0].GetFloor())
|
|
assert.Equal(t, "hash5", result.GetRawRootItem()[0].GetHash())
|
|
assert.True(t, result.GetRawRootItem()[0].GetLeft())
|
|
}
|
|
|
|
func TestProofToProtobuf_Full(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
proof := &model.Proof{
|
|
Sign: "full-signature",
|
|
Version: "v1.0.0",
|
|
ColItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "col1", Left: true},
|
|
},
|
|
RawItems: []*model.MerkleTreeProofItem{
|
|
{Floor: 2, Hash: "raw1", Left: false},
|
|
},
|
|
ColRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "colroot1", Left: true},
|
|
},
|
|
RawRootItem: []*model.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "rawroot1", Left: false},
|
|
},
|
|
}
|
|
result := model.ProofToProtobuf(proof)
|
|
|
|
require.NotNil(t, result)
|
|
assert.Equal(t, "full-signature", result.GetSign())
|
|
assert.Equal(t, "v1.0.0", result.GetVersion())
|
|
assert.Len(t, result.GetColItems(), 1)
|
|
assert.Len(t, result.GetRawItems(), 1)
|
|
assert.Len(t, result.GetColRootItem(), 1)
|
|
assert.Len(t, result.GetRawRootItem(), 1)
|
|
}
|
|
|
|
func TestProofRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
original := &pb.Proof{
|
|
Sign: "round-trip-signature",
|
|
Version: "v1.0.0",
|
|
ColItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 1, Hash: "col1", Left: true},
|
|
{Floor: 2, Hash: "col2", Left: false},
|
|
},
|
|
RawItems: []*pb.MerkleTreeProofItem{
|
|
{Floor: 3, Hash: "raw1", Left: true},
|
|
},
|
|
ColRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 4, Hash: "colroot1", Left: false},
|
|
},
|
|
RawRootItem: []*pb.MerkleTreeProofItem{
|
|
{Floor: 5, Hash: "rawroot1", Left: true},
|
|
},
|
|
}
|
|
|
|
// Convert to model
|
|
modelProof := model.ProofFromProtobuf(original)
|
|
require.NotNil(t, modelProof)
|
|
|
|
// Convert back to protobuf
|
|
pbProof := model.ProofToProtobuf(modelProof)
|
|
require.NotNil(t, pbProof)
|
|
|
|
// Verify round trip
|
|
assert.Equal(t, original.GetSign(), pbProof.GetSign())
|
|
assert.Equal(t, original.GetVersion(), pbProof.GetVersion())
|
|
assert.Len(t, pbProof.GetColItems(), 2)
|
|
assert.Len(t, pbProof.GetRawItems(), 1)
|
|
assert.Len(t, pbProof.GetColRootItem(), 1)
|
|
assert.Len(t, pbProof.GetRawRootItem(), 1)
|
|
|
|
assert.Equal(t, original.GetColItems()[0].GetFloor(), pbProof.GetColItems()[0].GetFloor())
|
|
assert.Equal(t, original.GetColItems()[0].GetHash(), pbProof.GetColItems()[0].GetHash())
|
|
assert.Equal(t, original.GetColItems()[0].GetLeft(), pbProof.GetColItems()[0].GetLeft())
|
|
}
|
|
|