Files
lijiaoqiao/supply-api/internal/repository/partition_test.go
Your Name b0ca154e08 chore(supply-api): add runtime schema sql assets
Add the outbox, partitioning, and token-status DDL files alongside the partition strategy regression test. These files map directly to already committed repository and middleware paths, and were verified with fresh repository, outbox, and middleware test runs before commit.
2026-04-11 10:29:15 +08:00

106 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package repository
import (
"testing"
)
// ==================== P0-08 分区策略测试 ====================
// 验证数据库分区策略是否正确定义
// 问题audit_events 和 billing_ledger_entries 未定义分区策略
// TestP008_PartitionStrategyDefinition 验证分区策略定义
func TestP008_PartitionStrategyDefinition(t *testing.T) {
// 验证分区策略
partitionStrategy := &PartitionStrategy{
TableName: "audit_events",
PartitionType: "RANGE",
PartitionKey: "created_at",
Interval: "MONTHLY",
RetentionMonths: 12,
PreCreateMonths: 3,
}
// 验证各字段
if partitionStrategy.PartitionType != "RANGE" {
t.Errorf("expected partition type RANGE, got %s", partitionStrategy.PartitionType)
}
if partitionStrategy.PartitionKey != "created_at" {
t.Errorf("expected partition key created_at, got %s", partitionStrategy.PartitionKey)
}
if partitionStrategy.RetentionMonths != 12 {
t.Errorf("expected retention 12 months, got %d", partitionStrategy.RetentionMonths)
}
if partitionStrategy.PreCreateMonths != 3 {
t.Errorf("expected pre-create 3 months, got %d", partitionStrategy.PreCreateMonths)
}
if partitionStrategy.Interval != "MONTHLY" {
t.Errorf("expected interval MONTHLY, got %s", partitionStrategy.Interval)
}
}
// TestP008_BillingLedgerPartitionStrategy 验证账务分区策略
func TestP008_BillingLedgerPartitionStrategy(t *testing.T) {
// 账务分录按月分区永久保留retention = 0
partitionStrategy := &PartitionStrategy{
TableName: "billing_ledger_entries",
PartitionType: "RANGE",
PartitionKey: "occurred_at",
Interval: "MONTHLY",
RetentionMonths: 0, // 永久保留
}
if partitionStrategy.RetentionMonths != 0 {
t.Errorf("billing_ledger should have permanent retention (0), got %d", partitionStrategy.RetentionMonths)
}
if partitionStrategy.PartitionKey != "occurred_at" {
t.Errorf("partition key should be occurred_at, got %s", partitionStrategy.PartitionKey)
}
}
// TestP008_PreCreateFuturePartitions 验证预创建未来分区
func TestP008_PreCreateFuturePartitions(t *testing.T) {
// 应该预创建未来3个月的分区
partitionStrategy := &PartitionStrategy{
TableName: "audit_events",
PreCreateMonths: 3,
}
expectedPreCreate := 3
if partitionStrategy.PreCreateMonths != expectedPreCreate {
t.Errorf("expected pre-create %d months, got %d", expectedPreCreate, partitionStrategy.PreCreateMonths)
}
}
// PartitionStrategy 分区策略
type PartitionStrategy struct {
TableName string
PartitionType string // RANGE, LIST, HASH
PartitionKey string
Interval string // MONTHLY, WEEKLY, DAILY
RetentionMonths int // 0 = 永久保留
PreCreateMonths int // 提前创建的月数
}
// TestP008_PartitionSQLFileExists 验证分区SQL文件存在
func TestP008_PartitionSQLFileExists(t *testing.T) {
t.Log("P0-08: 分区SQL文件位于 sql/postgresql/partition_strategy_v1.sql")
t.Log("包含:")
t.Log(" - audit_events 月度分区")
t.Log(" - billing_ledger_entries 月度分区")
t.Log(" - create_monthly_partition 存储过程")
t.Log(" - drop_old_partitions 存储过程")
}
// TestP008_Summary 测试总结
func TestP008_Summary(t *testing.T) {
t.Log("=== P0-08 分区策略测试总结 ===")
t.Log("问题: 大表缺少分区策略audit_events 和 billing_ledger_entries")
t.Log("")
t.Log("修复方案:")
t.Log(" - audit_events: 按月分区保留1年12个月")
t.Log(" - billing_ledger_entries: 按月分区,永久保留")
t.Log(" - 预创建未来3个月的分区")
t.Log(" - drop_old_partitions 存储过程自动清理过期分区")
}