diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-04 08:22:16 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-04 08:22:16 +0200 |
| commit | a499218f4f4c33b18bb821adfdbe8c0c25f8ca91 (patch) | |
| tree | 7d698a7599ce7eb2021445a271adcf0687b5c17a | |
| parent | 1c930570550c629171f5d7eebff9021495a4d4bb (diff) | |
worktime tests: convert validations to table-driven
| -rw-r--r-- | internal/worktime/entries_test.go | 85 | ||||
| -rw-r--r-- | internal/worktime/report_test.go | 39 |
2 files changed, 92 insertions, 32 deletions
diff --git a/internal/worktime/entries_test.go b/internal/worktime/entries_test.go index c0caf4a..3b3296e 100644 --- a/internal/worktime/entries_test.go +++ b/internal/worktime/entries_test.go @@ -119,17 +119,47 @@ func TestDurationValidation(t *testing.T) { dbDir := t.TempDir() host := "host-a" - if _, err := Add(dbDir, host, "work", 0, time.Unix(100, 0), ""); err == nil { - t.Fatal("Add() accepted zero duration") - } - if _, err := Add(dbDir, host, "work", -time.Minute, time.Unix(100, 0), ""); err == nil { - t.Fatal("Add() accepted negative duration") - } - if _, err := Sub(dbDir, host, "work", 0, time.Unix(100, 0), ""); err == nil { - t.Fatal("Sub() accepted zero duration") - } - if _, err := UseBuffer(dbDir, host, 0, time.Unix(100, 0), ""); err == nil { - t.Fatal("UseBuffer() accepted zero duration") + tests := []struct { + name string + run func() error + }{ + { + name: "add zero duration", + run: func() error { + _, err := Add(dbDir, host, "work", 0, time.Unix(100, 0), "") + return err + }, + }, + { + name: "add negative duration", + run: func() error { + _, err := Add(dbDir, host, "work", -time.Minute, time.Unix(100, 0), "") + return err + }, + }, + { + name: "sub zero duration", + run: func() error { + _, err := Sub(dbDir, host, "work", 0, time.Unix(100, 0), "") + return err + }, + }, + { + name: "use buffer zero duration", + run: func() error { + _, err := UseBuffer(dbDir, host, 0, time.Unix(100, 0), "") + return err + }, + }, + } + + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + if err := test.run(); err == nil { + t.Fatalf("%s: error = nil, want validation error", test.name) + } + }) } } @@ -198,14 +228,31 @@ func TestEditEntryValidation(t *testing.T) { t.Fatalf("Add(seed) error = %v", err) } - if _, err := EditEntry(dbDir, host, 0, Entry{Action: "bad", Epoch: 1}); err == nil { - t.Fatal("EditEntry() accepted unsupported action") - } - if _, err := EditEntry(dbDir, host, 0, Entry{Action: "add", Epoch: 0}); err == nil { - t.Fatal("EditEntry() accepted non-positive epoch") - } - if _, err := EditEntry(dbDir, host, 0, Entry{Action: "add", Epoch: 1, Source: "other-host"}); err == nil { - t.Fatal("EditEntry() accepted mismatched source") + tests := []struct { + name string + replacement Entry + }{ + { + name: "unsupported action", + replacement: Entry{Action: "bad", Epoch: 1}, + }, + { + name: "non-positive epoch", + replacement: Entry{Action: "add", Epoch: 0}, + }, + { + name: "mismatched source", + replacement: Entry{Action: "add", Epoch: 1, Source: "other-host"}, + }, + } + + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + if _, err := EditEntry(dbDir, host, 0, test.replacement); err == nil { + t.Fatalf("EditEntry() accepted invalid replacement: %s", test.name) + } + }) } } diff --git a/internal/worktime/report_test.go b/internal/worktime/report_test.go index 95ab0c2..6bed630 100644 --- a/internal/worktime/report_test.go +++ b/internal/worktime/report_test.go @@ -90,19 +90,32 @@ func TestBuildReportTracksBufferTotals(t *testing.T) { func TestBuildReportRejectsInvalidLoginSequences(t *testing.T) { cfg := config.Default() - _, err := BuildReport([]Entry{ - {Action: "logout", What: "work", Epoch: localEpoch(2026, 1, 5, 10, 0, 0)}, - }, cfg) - if err == nil { - t.Fatal("BuildReport() accepted logout without login") - } - - _, err = BuildReport([]Entry{ - {Action: "login", What: "work", Epoch: localEpoch(2026, 1, 5, 9, 0, 0)}, - {Action: "login", What: "work", Epoch: localEpoch(2026, 1, 5, 10, 0, 0)}, - }, cfg) - if err == nil { - t.Fatal("BuildReport() accepted double login") + tests := []struct { + name string + entries []Entry + }{ + { + name: "logout without login", + entries: []Entry{ + {Action: "logout", What: "work", Epoch: localEpoch(2026, 1, 5, 10, 0, 0)}, + }, + }, + { + name: "double login", + entries: []Entry{ + {Action: "login", What: "work", Epoch: localEpoch(2026, 1, 5, 9, 0, 0)}, + {Action: "login", What: "work", Epoch: localEpoch(2026, 1, 5, 10, 0, 0)}, + }, + }, + } + + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + if _, err := BuildReport(test.entries, cfg); err == nil { + t.Fatalf("BuildReport() accepted invalid sequence %q", test.name) + } + }) } } |
