package persistence import ( "strings" "testing" "time" ) // TestCore 测试核心功能,不依赖外部模块 func TestTrustlogStatusString(t *testing.T) { if StatusNotTrustlogged != "NOT_TRUSTLOGGED" { t.Error("StatusNotTrustlogged value incorrect") } if StatusTrustlogged != "TRUSTLOGGED" { t.Error("StatusTrustlogged value incorrect") } } func TestRetryStatusString(t *testing.T) { if RetryStatusPending != "PENDING" { t.Error("RetryStatusPending value incorrect") } if RetryStatusRetrying != "RETRYING" { t.Error("RetryStatusRetrying value incorrect") } if RetryStatusDeadLetter != "DEAD_LETTER" { t.Error("RetryStatusDeadLetter value incorrect") } } func TestPersistenceStrategyValues(t *testing.T) { if StrategyDBOnly.String() != "DB_ONLY" { t.Errorf("StrategyDBOnly.String() = %s, want DB_ONLY", StrategyDBOnly.String()) } if StrategyDBAndTrustlog.String() != "DB_AND_TRUSTLOG" { t.Errorf("StrategyDBAndTrustlog.String() = %s, want DB_AND_TRUSTLOG", StrategyDBAndTrustlog.String()) } if StrategyTrustlogOnly.String() != "TRUSTLOG_ONLY" { t.Errorf("StrategyTrustlogOnly.String() = %s, want TRUSTLOG_ONLY", StrategyTrustlogOnly.String()) } } func TestDBConfigDefaults(t *testing.T) { cfg := DefaultDBConfig("postgres", "test-dsn") if cfg.DriverName != "postgres" { t.Error("DriverName not set correctly") } if cfg.DSN != "test-dsn" { t.Error("DSN not set correctly") } if cfg.MaxOpenConns != 25 { t.Error("MaxOpenConns not set to default 25") } if cfg.MaxIdleConns != 5 { t.Error("MaxIdleConns not set to default 5") } if cfg.ConnMaxLifetime != time.Hour { t.Error("ConnMaxLifetime not set to default 1 hour") } if cfg.ConnMaxIdleTime != 10*time.Minute { t.Error("ConnMaxIdleTime not set to default 10 minutes") } } func TestPersistenceConfigDefaults(t *testing.T) { cfg := DefaultPersistenceConfig(StrategyDBAndTrustlog) if cfg.Strategy != StrategyDBAndTrustlog { t.Error("Strategy not set correctly") } if !cfg.EnableRetry { t.Error("EnableRetry should be true by default") } if cfg.MaxRetryCount != 5 { t.Error("MaxRetryCount should be 5 by default") } if cfg.RetryBatchSize != 100 { t.Error("RetryBatchSize should be 100 by default") } } func TestRetryWorkerConfigDefaults(t *testing.T) { cfg := DefaultRetryWorkerConfig() if cfg.RetryInterval != 30*time.Second { t.Error("RetryInterval should be 30s by default") } if cfg.MaxRetryCount != 5 { t.Error("MaxRetryCount should be 5 by default") } if cfg.BatchSize != 100 { t.Error("BatchSize should be 100 by default") } if cfg.BackoffMultiplier != 2.0 { t.Error("BackoffMultiplier should be 2.0 by default") } if cfg.InitialBackoff != 1*time.Minute { t.Error("InitialBackoff should be 1 minute by default") } } func TestDDLContainsRequiredTables(t *testing.T) { // Test operation table if !strings.Contains(OperationTableDDL, "CREATE TABLE") { t.Error("OperationTableDDL should contain CREATE TABLE") } if !strings.Contains(OperationTableDDL, "operation") { t.Error("OperationTableDDL should create operation table") } if !strings.Contains(OperationTableDDL, "client_ip") { t.Error("OperationTableDDL should have client_ip field") } if !strings.Contains(OperationTableDDL, "server_ip") { t.Error("OperationTableDDL should have server_ip field") } if !strings.Contains(OperationTableDDL, "trustlog_status") { t.Error("OperationTableDDL should have trustlog_status field") } // Test cursor table if !strings.Contains(CursorTableDDL, "trustlog_cursor") { t.Error("CursorTableDDL should create trustlog_cursor table") } if !strings.Contains(CursorTableDDL, "cursor_key") { t.Error("CursorTableDDL should have cursor_key field") } if !strings.Contains(CursorTableDDL, "cursor_value") { t.Error("CursorTableDDL should have cursor_value field") } // Test retry table if !strings.Contains(RetryTableDDL, "trustlog_retry") { t.Error("RetryTableDDL should create trustlog_retry table") } if !strings.Contains(RetryTableDDL, "retry_status") { t.Error("RetryTableDDL should have retry_status field") } if !strings.Contains(RetryTableDDL, "retry_count") { t.Error("RetryTableDDL should have retry_count field") } } func TestGetDialectDDLForDifferentDrivers(t *testing.T) { drivers := []string{"postgres", "mysql", "sqlite3", "sqlite", "unknown"} for _, driver := range drivers { opDDL, cursorDDL, retryDDL, err := GetDialectDDL(driver) if err != nil { t.Errorf("GetDialectDDL(%s) returned error: %v", driver, err) } if opDDL == "" { t.Errorf("GetDialectDDL(%s) returned empty operation DDL", driver) } if cursorDDL == "" { t.Errorf("GetDialectDDL(%s) returned empty cursor DDL", driver) } if retryDDL == "" { t.Errorf("GetDialectDDL(%s) returned empty retry DDL", driver) } } } func TestMySQLDDLHasSpecificSyntax(t *testing.T) { opDDL, _, _, _ := GetDialectDDL("mysql") if !strings.Contains(opDDL, "ENGINE=InnoDB") { t.Error("MySQL DDL should contain ENGINE=InnoDB") } if !strings.Contains(opDDL, "CHARSET=utf8mb4") { t.Error("MySQL DDL should contain CHARSET=utf8mb4") } } func TestPostgresDDLHasCorrectTypes(t *testing.T) { opDDL, _, _, _ := GetDialectDDL("postgres") if !strings.Contains(opDDL, "VARCHAR") { t.Error("Postgres DDL should use VARCHAR types") } if !strings.Contains(opDDL, "TIMESTAMP") { t.Error("Postgres DDL should use TIMESTAMP types") } } func TestSQLiteDDLUsesTEXT(t *testing.T) { opDDL, _, _, _ := GetDialectDDL("sqlite3") if !strings.Contains(opDDL, "TEXT") { t.Error("SQLite DDL should use TEXT types") } }