-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
76 lines (61 loc) · 1.72 KB
/
app_test.go
File metadata and controls
76 lines (61 loc) · 1.72 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
// Copyright 2023 Bill Nixon. All rights reserved.
// Use of this source code is governed by the license found in the LICENSE file.
package main
import (
"html/template"
"sync"
"testing"
"github.com/bnixon67/webapp/webapp"
"github.com/bnixon67/webapp/webauth"
"github.com/bnixon67/webapp/weblog"
"github.com/bnixon67/webapp/webutil"
_ "github.com/go-sql-driver/mysql"
)
// globals to provide a singleton bidApp.
var (
bidApp *BidApp //nolint
once sync.Once
)
func initBidAppForTest(t *testing.T) *BidApp {
var err error
// Read config.
cfg, err := webauth.LoadConfigFromJSON("testdata/config.json")
if err != nil {
t.Fatalf("Failed to get config: %v", err)
}
// Initialize logging.
err = weblog.Init(cfg.Log)
if err != nil {
t.Fatalf("cannot init logging: %v", err)
}
// Define the custom functions.
funcMap := template.FuncMap{
"ToTimeZone": webutil.ToTimeZone,
}
// Initialize templates.
tmpl, err := webutil.TemplatesWithFuncs(cfg.App.TmplPattern, funcMap)
if err != nil {
t.Fatalf("Error initializing templates: %v", err)
}
// Initialize db.
db, err := webauth.InitDB(cfg.SQL.DriverName, cfg.SQL.DataSourceName)
if err != nil {
t.Fatalf("cannot init db: %v", err)
}
// Create the web login app.
app, err := webauth.NewApp(webapp.WithName(cfg.App.Name), webapp.WithTemplate(tmpl), webauth.WithConfig(*cfg), webauth.WithDB(db))
if err != nil {
t.Fatalf("cannot create app: %v", err)
}
// Embed web login app into BidApp.
bidApp = &BidApp{AuthApp: app, BidDB: &BidDB{}}
bidApp.BidDB.sqlDB = app.DB
return bidApp
}
// AppForTest is a helper function that returns an App used for testing.
func AppForTest(t *testing.T) *BidApp {
once.Do(func() {
bidApp = initBidAppForTest(t)
})
return bidApp
}