app: add routes_test.go
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
3439ec7c2a
commit
61ec8bfea1
134
app/routes_test.go
Normal file
134
app/routes_test.go
Normal file
@ -0,0 +1,134 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
_ "github.com/xiaoqidun/entps"
|
||||
|
||||
"git.dotya.ml/mirre-mt/pcmt/config"
|
||||
"git.dotya.ml/mirre-mt/pcmt/ent"
|
||||
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
||||
)
|
||||
|
||||
var conf = &config.Config{
|
||||
Port: 3005,
|
||||
AppName: "pcmt-test",
|
||||
LiveMode: true,
|
||||
DevelMode: false,
|
||||
SessionCookieName: "sessionz",
|
||||
SessionCookieSecret: "secret",
|
||||
}
|
||||
|
||||
func TestStaticRoute(t *testing.T) {
|
||||
a := &App{
|
||||
templatesPath: "../templates",
|
||||
assetsPath: "../assets",
|
||||
}
|
||||
db := supplyDBClient()
|
||||
log := slogging.GetLogger()
|
||||
tstRoute := "/static/"
|
||||
|
||||
defer db.Close()
|
||||
|
||||
err := a.Init("test", log, conf, db)
|
||||
if err != nil {
|
||||
t.Errorf("failed to initialise app: %v", a)
|
||||
}
|
||||
|
||||
a.SetupRoutes()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, tstRoute, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
a.E().ServeHTTP(rec, req)
|
||||
|
||||
want := http.StatusMovedPermanently
|
||||
got := rec.Code
|
||||
|
||||
if want != got {
|
||||
t.Fatalf("unexpected status code when testing %s, want: %d, got %d",
|
||||
tstRoute, want, got,
|
||||
)
|
||||
}
|
||||
|
||||
uri, err := rec.Result().Location()
|
||||
if err != nil {
|
||||
t.Fatal("error getting response location")
|
||||
}
|
||||
|
||||
if uri.Path != "/assets/" {
|
||||
t.Errorf("unexpected location, want: %s, got: %s", "/assets/", uri.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStatic(b *testing.B) {
|
||||
a := &App{
|
||||
templatesPath: "../templates",
|
||||
assetsPath: "../assets",
|
||||
}
|
||||
db := supplyDBClient()
|
||||
log := slogging.GetLogger()
|
||||
|
||||
defer db.Close()
|
||||
|
||||
err := a.Init("test", log, conf, db)
|
||||
if err != nil {
|
||||
b.Errorf("failed to initialise app: %v", a)
|
||||
}
|
||||
|
||||
a.SetupRoutes()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "/static/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
a.E().ServeHTTP(rec, req)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStatic2(b *testing.B) {
|
||||
a := &App{
|
||||
templatesPath: "../templates",
|
||||
assetsPath: "../assets",
|
||||
}
|
||||
log := slogging.GetLogger()
|
||||
db := supplyDBClient()
|
||||
|
||||
defer db.Close()
|
||||
|
||||
err := a.Init("test", log, conf, db)
|
||||
if err != nil {
|
||||
b.Errorf("failed to initialise app: %v", a)
|
||||
}
|
||||
|
||||
a.SetupRoutes()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "/static2/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
a.E().ServeHTTP(rec, req)
|
||||
}
|
||||
}
|
||||
|
||||
func supplyDBClient() *ent.Client {
|
||||
connstr := "file:ent_tests?mode=memory&cache=shared&_fk=1"
|
||||
|
||||
log := slogging.Init(false)
|
||||
|
||||
db, err := ent.Open("sqlite3", connstr)
|
||||
if err != nil {
|
||||
log.Infof("failed to open a connection to sqlite %q\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = db.Schema.Create(context.Background()); err != nil {
|
||||
log.Infof("failed creating schema resources: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
Loading…
Reference in New Issue
Block a user