-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestconfig.go
159 lines (139 loc) · 3.59 KB
/
testconfig.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package testconfig
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/interline-io/transitland-dbutil/testutil"
"github.com/interline-io/transitland-lib/rt"
"github.com/interline-io/transitland-lib/tldb"
"github.com/interline-io/transitland-mw/auth/authz"
"github.com/interline-io/transitland-mw/auth/azcheck"
"github.com/interline-io/transitland-mw/jobs"
"github.com/interline-io/transitland-server/finders/dbfinder"
"github.com/interline-io/transitland-server/finders/gbfsfinder"
"github.com/interline-io/transitland-server/finders/rtfinder"
"github.com/interline-io/transitland-server/internal/clock"
"github.com/interline-io/transitland-server/model"
"github.com/interline-io/transitland-server/testdata"
"github.com/jmoiron/sqlx"
"google.golang.org/protobuf/proto"
)
// Test helpers
type Options struct {
When string
Storage string
RTStorage string
RTJsons []RTJsonFile
FGAEndpoint string
FGAModelFile string
FGAModelTuples []authz.TupleKey
}
func Config(t testing.TB, opts Options) model.Config {
db := testutil.MustOpenTestDB(t)
return newTestConfig(t, &tldb.QueryLogger{Ext: db}, opts)
}
func ConfigTx(t testing.TB, opts Options, cb func(model.Config) error) {
// Start Txn
db := testutil.MustOpenTestDB(t)
tx := db.MustBeginTx(context.Background(), nil)
defer tx.Rollback()
// Get finders
testEnv := newTestConfig(t, &tldb.QueryLogger{Ext: tx}, opts)
// Commit or rollback
if err := cb(testEnv); err != nil {
//tx.Rollback()
} else {
tx.Commit()
}
}
func ConfigTxRollback(t testing.TB, opts Options, cb func(model.Config)) {
ConfigTx(t, opts, func(c model.Config) error {
cb(c)
return errors.New("rollback")
})
}
type RTJsonFile struct {
Feed string
Ftype string
Fname string
}
func DefaultRTJson() []RTJsonFile {
return []RTJsonFile{
{"BA", "realtime_trip_updates", "BA.json"},
{"BA", "realtime_alerts", "BA-alerts.json"},
{"CT", "realtime_trip_updates", "CT.json"},
}
}
func newTestConfig(t testing.TB, db sqlx.Ext, opts Options) model.Config {
// Default time
if opts.When == "" {
opts.When = "2022-09-01T00:00:00"
}
when, err := time.Parse("2006-01-02T15:04:05", opts.When)
if err != nil {
t.Fatal(err)
}
cl := &clock.Mock{T: when}
// Setup Checker
var checker model.Checker
var whoami model.Whoami
if opts.FGAEndpoint != "" {
checkerCfg := azcheck.CheckerConfig{
FGAEndpoint: opts.FGAEndpoint,
FGALoadModelFile: opts.FGAModelFile,
FGALoadTestData: opts.FGAModelTuples,
}
ch, err := azcheck.NewCheckerFromConfig(checkerCfg, db)
if err != nil {
t.Fatal(err)
}
checker = ch
whoami = ch
} else {
whoami = &azcheck.Whoami{}
}
// Setup DB
dbf := dbfinder.NewFinder(db)
dbf.Clock = cl
// Setup RT
rtf := rtfinder.NewFinder(rtfinder.NewLocalCache(), db)
rtf.Clock = cl
for _, rtj := range opts.RTJsons {
fn := testdata.Path("rt", rtj.Fname)
msg, err := rt.ReadFile(fn)
if err != nil {
t.Fatal(err)
}
key := fmt.Sprintf("rtdata:%s:%s", rtj.Feed, rtj.Ftype)
rtdata, err := proto.Marshal(msg)
if err != nil {
t.Fatal(err)
}
if err := rtf.AddData(key, rtdata); err != nil {
t.Fatal(err)
}
}
// Setup GBFS
gbf := gbfsfinder.NewFinder(nil)
if opts.Storage == "" {
opts.Storage = t.TempDir()
}
if opts.RTStorage == "" {
opts.RTStorage = t.TempDir()
}
// Initialize job queue - do not start
jobQueue := jobs.NewLocalJobs()
return model.Config{
Finder: dbf,
RTFinder: rtf,
GbfsFinder: gbf,
Whoami: whoami,
Checker: checker,
JobQueue: jobQueue,
Clock: cl,
Storage: opts.Storage,
RTStorage: opts.RTStorage,
}
}