Remove manual tagging
A tag could come from two places: rules.toml, or the TUI's t key, which wrote manual_tag with COALESCE(manual_tag, rule_tag) deciding the winner. That split paid for itself in the first invariant of the codebase, in ClearOverrides and the c key, in the * marker on the tag column, and in the one exception to a disposable index -- a tag set by hand was the only thing in index.db that the statements could not reproduce. Now rules.toml decides every tag. The index is derived entirely from the statements plus that file, so deleting it and re-importing gets back exactly what was there, and retag has nothing to be careful of. Tagging a one-off means writing a narrow rule on screen 4, which previews what the glob catches before it is saved. A manual tag in an existing index is dropped along with the column the first time this build opens it, and those rows read as whatever the rules say, or as untagged. TestManualTagSurvivesRetag guarded the invariant that has just been removed; TestRetagRewritesEveryTag replaces it with the one that took its place, and keeps Retag itself covered. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -66,60 +66,45 @@ func byDescription(t *testing.T, db *store.DB, desc string) model.Transaction {
|
||||
return model.Transaction{}
|
||||
}
|
||||
|
||||
// A manual tag must outlive any number of rule changes and re-runs. This is
|
||||
// the property that makes retag safe to run at will.
|
||||
func TestManualTagSurvivesRetag(t *testing.T) {
|
||||
// A tag is derived state: rules.toml is the only thing that decides it, so
|
||||
// retagging rewrites every tag from the current rules however often it runs,
|
||||
// including back to nothing when no rule matches any more.
|
||||
func TestRetagRewritesEveryTag(t *testing.T) {
|
||||
db := testDB(t)
|
||||
seed(t, db, "LIDL SOFIA", "SOME RANDOM SHOP")
|
||||
|
||||
engine := New(&config.Rules{Rule: []config.Rule{
|
||||
{Match: "*LIDL*", Tag: "groceries"},
|
||||
{Match: "*SHOP*", Tag: "shopping"},
|
||||
}})
|
||||
n, err := engine.Retag(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 2 {
|
||||
t.Errorf("retagged %d rows, want 2", n)
|
||||
}
|
||||
if got := byDescription(t, db, "LIDL SOFIA").RuleTag; got != "groceries" {
|
||||
t.Errorf("tag = %q, want groceries", got)
|
||||
}
|
||||
|
||||
// Re-running with the same rules changes nothing and says so.
|
||||
if n, err := engine.Retag(db); err != nil || n != 0 {
|
||||
t.Errorf("second retag: %d rows, %v; want 0 and no error", n, err)
|
||||
}
|
||||
|
||||
// New rules win, and a row no rule matches loses its tag entirely.
|
||||
engine = New(&config.Rules{Rule: []config.Rule{
|
||||
{Match: "*LIDL*", Tag: "food"},
|
||||
}})
|
||||
if _, err := engine.Retag(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The user hand-tags the row the rules did not match, and overrides the
|
||||
// one they did.
|
||||
shop := byDescription(t, db, "SOME RANDOM SHOP")
|
||||
if err := db.SetManualTag(shop.ID, "hobby"); err != nil {
|
||||
t.Fatal(err)
|
||||
if got := byDescription(t, db, "LIDL SOFIA").RuleTag; got != "food" {
|
||||
t.Errorf("tag = %q, want food", got)
|
||||
}
|
||||
lidl := byDescription(t, db, "LIDL SOFIA")
|
||||
if err := db.SetManualTag(lidl.ID, "party supplies"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Rules change completely, and are re-applied twice for good measure.
|
||||
engine = New(&config.Rules{Rule: []config.Rule{
|
||||
{Match: "*LIDL*", Tag: "food"},
|
||||
{Match: "*SHOP*", Tag: "shopping"},
|
||||
}})
|
||||
for range 2 {
|
||||
if _, err := engine.Retag(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
shop = byDescription(t, db, "SOME RANDOM SHOP")
|
||||
if got := shop.Tag(); got != "hobby" {
|
||||
t.Errorf("manual tag = %q, want %q (manual tags must win)", got, "hobby")
|
||||
}
|
||||
if shop.RuleTag != "shopping" {
|
||||
t.Errorf("rule_tag = %q, want %q (rules must still be recorded underneath)", shop.RuleTag, "shopping")
|
||||
}
|
||||
|
||||
lidl = byDescription(t, db, "LIDL SOFIA")
|
||||
if got := lidl.Tag(); got != "party supplies" {
|
||||
t.Errorf("manual override = %q, want %q", got, "party supplies")
|
||||
}
|
||||
|
||||
// Clearing the override falls back to whatever the rules now say.
|
||||
if err := db.ClearOverrides(lidl.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := byDescription(t, db, "LIDL SOFIA").Tag(); got != "food" {
|
||||
t.Errorf("after clearing override, tag = %q, want %q", got, "food")
|
||||
if got := byDescription(t, db, "SOME RANDOM SHOP").RuleTag; got != "" {
|
||||
t.Errorf("tag = %q, want empty once no rule matches", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user