package postgres import ( "fmt" "strings" "testing" "time" "gitea.maximumdirect.net/ejr/feedkit/config" ) func TestRegisterPostgresSchemasNilConfig(t *testing.T) { err := RegisterPostgresSchemas(nil) if err == nil { t.Fatalf("RegisterPostgresSchemas(nil) expected error") } if !strings.Contains(err.Error(), "config is nil") { t.Fatalf("error = %q, want config is nil", err) } } func TestRegisterPostgresSchemasNonPostgresNoOp(t *testing.T) { cfg := &config.Config{ Sinks: []config.SinkConfig{ {Name: "stdout_only", Driver: "stdout"}, {Name: "nats_only", Driver: "nats"}, }, } if err := RegisterPostgresSchemas(cfg); err != nil { t.Fatalf("RegisterPostgresSchemas(non-postgres) error = %v", err) } } func TestRegisterPostgresSchemasDuplicateRegistrationFails(t *testing.T) { sinkName := uniqueSinkName("pg_test") cfg := &config.Config{ Sinks: []config.SinkConfig{ {Name: sinkName, Driver: "postgres"}, }, } if err := RegisterPostgresSchemas(cfg); err != nil { t.Fatalf("first RegisterPostgresSchemas() error = %v", err) } err := RegisterPostgresSchemas(cfg) if err == nil { t.Fatalf("second RegisterPostgresSchemas() expected duplicate error") } if !strings.Contains(err.Error(), "already registered") { t.Fatalf("error = %q, want already registered", err) } } func TestWeatherPostgresSchemaShape(t *testing.T) { s := weatherPostgresSchema() if s.MapEvent == nil { t.Fatalf("weatherPostgresSchema().MapEvent is nil") } wantTables := map[string]bool{ tableObservations: true, tableObservationPresentWeather: true, tableForecasts: true, tableForecastPeriods: true, tableAlertRuns: true, tableAlerts: true, tableAlertReferences: true, } if len(s.Tables) != len(wantTables) { t.Fatalf("weatherPostgresSchema().Tables len = %d, want %d", len(s.Tables), len(wantTables)) } seenIndexes := map[string]bool{} for _, tbl := range s.Tables { if !wantTables[tbl.Name] { t.Fatalf("unexpected table %q in schema", tbl.Name) } if tbl.PruneColumn == "" { t.Fatalf("table %q missing prune column", tbl.Name) } for _, idx := range tbl.Indexes { if seenIndexes[idx.Name] { t.Fatalf("duplicate index name %q", idx.Name) } seenIndexes[idx.Name] = true } } } func uniqueSinkName(prefix string) string { return fmt.Sprintf("%s_%d", prefix, time.Now().UnixNano()) }