-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfsb_test.go
109 lines (98 loc) · 2.8 KB
/
lfsb_test.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package lfsb_test
import (
"flag"
"fmt"
"testing"
lfsb "github.com/stephen/litefs-backup"
"github.com/superfly/ltx"
)
var _ = flag.Bool("integration", false, "run integration tests")
func TestErrorCode(t *testing.T) {
for _, tt := range []struct {
err error
want string
}{
// Root error is lfsb.Error
{lfsb.Errorf(lfsb.ErrorTypeValidation, "EXYZ", "test"), "EXYZ"},
// Nested lfsb.Error
{fmt.Errorf("test: %w", lfsb.Errorf(lfsb.ErrorTypeValidation, "EXYZ", "...")), "EXYZ"},
// Non-lfsb.Error
{fmt.Errorf("test"), "EINTERNAL"},
// Nil errors
{nil, ""},
{fmt.Errorf("test: %w", nil), "EINTERNAL"},
} {
t.Run("", func(t *testing.T) {
if got, want := lfsb.ErrorCode(tt.err), tt.want; got != want {
t.Fatalf("code=%q, want %q", got, want)
}
})
}
}
func TestValidateClusterName(t *testing.T) {
t.Run("OK", func(t *testing.T) {
if err := lfsb.ValidateClusterName("cluster"); err != nil {
t.Fatal(err)
}
})
t.Run("ErrClusterRequired", func(t *testing.T) {
if err := lfsb.ValidateClusterName(""); err != lfsb.ErrClusterRequired {
t.Fatal(err)
}
})
t.Run("ErrClusterInvalid", func(t *testing.T) {
if err := lfsb.ValidateClusterName("foo bar"); err != lfsb.ErrClusterInvalid {
t.Fatal(err)
}
})
}
func TestValidateDatabase(t *testing.T) {
t.Run("OK", func(t *testing.T) {
if err := lfsb.ValidateDatabase("database"); err != nil {
t.Fatal(err)
}
})
t.Run("ErrDatabaseRequired", func(t *testing.T) {
if err := lfsb.ValidateDatabase(""); err != lfsb.ErrDatabaseRequired {
t.Fatal(err)
}
})
t.Run("ErrDatabaseInvalid", func(t *testing.T) {
if err := lfsb.ValidateDatabase("foo bar"); lfsb.ErrorCode(err) != "EBADDB" {
t.Fatalf("unexpected error: %s", err)
}
})
}
func TestParseLTXFilename(t *testing.T) {
t.Run("OK", func(t *testing.T) {
for i, tt := range []struct {
name string
min, max ltx.TXID
}{
{name: "00000000000003e8-00000000000007d0.ltx", min: 1000, max: 2000},
{name: "0000000000000001-0000000000000001.ltx", min: 1, max: 1},
} {
if min, max, err := lfsb.ParseLTXFilename(tt.name); err != nil {
t.Error(i, err)
} else if min != tt.min {
t.Errorf("%d. min=%x, want %x", i, min, tt.min)
} else if max != tt.max {
t.Errorf("%d. max=%x, want %x", i, max, tt.max)
}
}
})
t.Run("ErrInvalidLTXFilename", func(t *testing.T) {
for i, tt := range []struct {
name string
}{
{name: "0000000000000001.ltx"}, // too short
{name: "0000000000000001-0000000000000001.xyz"}, // invalid extension
{name: "000000000000000X-0000000000000001.ltx"}, // invalid min txid
{name: "0000000000000001-000000000000000X.ltx"}, // invalid max txid
} {
if _, _, err := lfsb.ParseLTXFilename(tt.name); err != lfsb.ErrInvalidLTXFilename {
t.Errorf("%d. unexpected error: %s", i, err)
}
}
})
}