package dbmanager import ( "strings" "testing" ) func TestParseTSV(t *testing.T) { got := parseTSV("Uptime\t120\nQuestions\t240\nQcache_hits\t12\n") if got["Uptime"] != "120" || got["Questions"] != "240" || got["Qcache_hits"] != "12" { t.Fatalf("unexpected map: %v", got) } } func TestEstimateMaxMemory(t *testing.T) { p := Performance{ InnoDBPoolMB: 64, KeyBufferMB: 32, QueryCacheMB: 32, TmpTableMB: 32, InnoDBLogBufferMB: 16, MaxConnections: 100, SortBufferKB: 256, ReadBufferKB: 256, ReadRndBufferKB: 256, JoinBufferKB: 512, ThreadStackKB: 256, BinlogCacheKB: 64, } est, detail := estimateMaxMemory(p) // 176 MB fixed + 100 * 1600KB (156.25MB) = 332.25 MB, the same estimate the // reference panel shows for its 1-2GB profile. if est < 331 || est > 334 { t.Fatalf("estimate = %.2f, want ~332.25\n%s", est, detail) } } func TestRenderMysqldQueryCache(t *testing.T) { p := Performance{ QueryCacheMB: 32, TableOpenCache: 128, ThreadCacheSize: 64, KeyBufferMB: 32, TmpTableMB: 32, InnoDBPoolMB: 64, InnoDBLogBufferMB: 16, MaxConnections: 100, SortBufferKB: 256, ReadBufferKB: 256, ReadRndBufferKB: 256, JoinBufferKB: 512, ThreadStackKB: 256, BinlogCacheKB: 64, } with := renderMysqld(p, true) if !strings.Contains(with, "query_cache_size = 32M") { t.Fatalf("query cache missing:\n%s", with) } without := renderMysqld(p, false) if strings.Contains(without, "query_cache") { t.Fatalf("query cache must be omitted on MySQL 8:\n%s", without) } } func TestUpsertMysqldPreservesOtherSections(t *testing.T) { raw := `[client] port = 3306 [mysqld] port = 3306 max_connections = 50 # keep this comment [mysqldump] quick ` body := "\n[mysqld]\nmax_connections = 200\nsort_buffer_size = 512K\n" out := upsertMysqld(raw, body) for _, want := range []string{ "[client]\nport = 3306", "max_connections = 200", "sort_buffer_size = 512K", "[mysqldump]\nquick", "# keep this comment", } { if !strings.Contains(out, want) { t.Errorf("missing %q\n%s", want, out) } } if strings.Contains(out, "max_connections = 50") { t.Errorf("old value not replaced:\n%s", out) } } func TestUpsertMysqldAppendsSection(t *testing.T) { out := upsertMysqld("[client]\nport=3306\n", "\n[mysqld]\nmax_connections = 100\n") if !strings.Contains(out, "[client]") || !strings.Contains(out, "[mysqld]") { t.Fatalf("section not appended:\n%s", out) } }