Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
56 lines (50 loc) · 1.57 KB
/
Copy pathmain_test.go
File metadata and controls
56 lines (50 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"errors"
"testing"
"codeswitch/services"
_ "modernc.org/sqlite"
)
func TestRunMaintenanceCommand(t *testing.T) {
t.Run("no command starts service", func(t *testing.T) {
called := false
handled, err := runMaintenanceCommand(nil, func() error {
called = true
return nil
})
if err != nil || handled || called {
t.Fatalf("handled=%t called=%t err=%v, want false false nil", handled, called, err)
}
})
t.Run("migration command runs once", func(t *testing.T) {
calls := 0
handled, err := runMaintenanceCommand([]string{services.RequestLogIndexMigrationCommand}, func() error {
calls++
return nil
})
if err != nil || !handled || calls != 1 {
t.Fatalf("handled=%t calls=%d err=%v, want true 1 nil", handled, calls, err)
}
})
t.Run("migration error is propagated", func(t *testing.T) {
wantErr := errors.New("migration failed")
handled, err := runMaintenanceCommand([]string{services.RequestLogIndexMigrationCommand}, func() error {
return wantErr
})
if !handled || !errors.Is(err, wantErr) {
t.Fatalf("handled=%t err=%v, want handled migration error", handled, err)
}
})
for _, args := range [][]string{{"unknown"}, {services.RequestLogIndexMigrationCommand, "extra"}} {
t.Run("rejects invalid arguments", func(t *testing.T) {
called := false
handled, err := runMaintenanceCommand(args, func() error {
called = true
return nil
})
if !handled || err == nil || called {
t.Fatalf("args=%v handled=%t called=%t err=%v, want rejected without migration", args, handled, called, err)
}
})
}
}