Repository navigation
Expand file tree
/
Copy pathfailures_test.go
More file actions
100 lines (82 loc) · 2.9 KB
/
Copy pathfailures_test.go
File metadata and controls
100 lines (82 loc) · 2.9 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
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
// SPDX-License-Identifier: Apache-2.0
package sqlite_test
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"github.com/gopherium/framework/dbkit"
"github.com/gopherium/framework/dbkit/sqlite/sqlitetest"
"github.com/gopherium/framework/gonsole/auth"
"github.com/gopherium/framework/gonsole/auth/sqlite"
)
// errPlanted is the failure a chosen statement answers with.
var errPlanted = errors.New("planted failure")
// wantUnder fails t unless err matches want under the action the store names.
func wantUnder(t *testing.T, err, want error, action string) {
t.Helper()
if !errors.Is(err, want) || !strings.HasPrefix(err.Error(), "sqlite: "+action+": ") {
t.Errorf("the call answered %v, want %v under %q", err, want, "sqlite: "+action+": ")
}
}
func TestARepeatedRecordIDAnswersDbkitsUniqueClass(t *testing.T) {
t.Parallel()
store := sqlite.NewRecordStore(withRecords(t))
mustInsert(t, store, roleChange())
err := store.Insert(t.Context(), roleChange())
wantUnder(t, err, dbkit.ErrUnique, "insert a record")
}
func TestEveryStatementFailureReachesTheCallerUnderItsAction(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
statement, action string
call func(ctx context.Context, store auth.RecordStore) error
}{
{sqlite.HeldRecords, "check the records", func(ctx context.Context, store auth.RecordStore) error {
_, err := store.Held(ctx)
return err
}},
{sqlite.StoreRecord, "insert a record", func(ctx context.Context, store auth.RecordStore) error {
return store.Insert(ctx, roleChange())
}},
{sqlite.LatestRecords, "list the records", func(ctx context.Context, store auth.RecordStore) error {
_, err := store.Latest(ctx, 10)
return err
}},
} {
t.Run(tc.action, func(t *testing.T) {
t.Parallel()
faults := &sqlitetest.Faults{}
db := template.OpenWithFaults(t, faults)
if err := sqlite.RecordMigration(testMigrate).Apply(t.Context(), "", db); err != nil {
t.Fatalf("applying the records step: %v", err)
}
faults.FailStatement(tc.statement, errPlanted)
err := tc.call(t.Context(), sqlite.NewRecordStore(db))
wantUnder(t, err, errPlanted, tc.action)
})
}
}
func TestARecordThatListsBadlyFailsTheWholeList(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
appliedAt any
args string
}{
{"arguments that are a JSON object", int64(1767225600000000), "{}"},
{"a time that is no integer", "yesterday", "[]"},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
db := withRecords(t)
mustExec(t, db, insertRecord, recordID, tc.appliedAt, "admin@example.com", "account:role", tc.args, "{}")
listed, err := sqlite.NewRecordStore(db).Latest(t.Context(), 10)
if listed != nil || !strings.HasPrefix(fmt.Sprint(err), "sqlite: list the records: ") {
t.Errorf("Latest() over a record with %s = %v, %v, want no list and a failure under the action",
tc.name, listed, err)
}
})
}
}