test(channelserver): add comprehensive GuildRepository tests

Add 34 tests covering all previously-untested GuildRepository methods:
invitations/scouts, guild posts, alliances, adventures, treasure
hunts, meals, kill tracking, and edge cases like disband with
alliance cleanup.

Fix test schema setup to apply the 9.2 update schema after init.sql,
which bridges v9.1.0 to v9.2.0 and resolves 9 pre-existing test
failures caused by missing columns (rp_today, created_at, etc.).

Make patch schemas 14 and 19 idempotent so they no longer fail when
applied against a schema that already has the target state (e.g.
festival_color type already renamed, legacy column names).
This commit is contained in:
Houmgaor
2026-02-20 22:53:16 +01:00
parent 93f28c721a
commit 7436ac0870
4 changed files with 980 additions and 18 deletions

View File

@@ -128,10 +128,46 @@ func ApplyTestSchema(t *testing.T, db *sqlx.DB) {
}
}
// Apply the 9.2 update schema (init.sql bootstraps to 9.1.0)
applyUpdateSchema(t, db, projectRoot)
// Apply patch schemas in order
applyPatchSchemas(t, db, projectRoot)
}
// applyUpdateSchema applies the 9.2 update schema that bridges init.sql (v9.1.0) to v9.2.0.
// It runs each statement individually to tolerate partial failures (e.g. role references).
func applyUpdateSchema(t *testing.T, db *sqlx.DB, projectRoot string) {
t.Helper()
updatePath := filepath.Join(projectRoot, "schemas", "update-schema", "9.2-update.sql")
updateSQL, err := os.ReadFile(updatePath)
if err != nil {
t.Logf("Warning: Could not read 9.2 update schema: %v", err)
return
}
// Strip the outer BEGIN/END transaction wrapper so we can run statements individually.
content := string(updateSQL)
content = strings.Replace(content, "BEGIN;", "", 1)
// Remove trailing END; (last occurrence)
if idx := strings.LastIndex(content, "END;"); idx >= 0 {
content = content[:idx] + content[idx+4:]
}
// Split on semicolons and execute each statement, tolerating errors from
// role references or already-applied changes.
for _, stmt := range strings.Split(content, ";") {
stmt = strings.TrimSpace(stmt)
if stmt == "" {
continue
}
if _, err := db.Exec(stmt); err != nil {
// Silently ignore — these are expected for role mismatches, already-applied changes, etc.
}
}
}
// applyPatchSchemas applies all patch schema files in numeric order
func applyPatchSchemas(t *testing.T, db *sqlx.DB, projectRoot string) {
t.Helper()