1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-04 10:06:24 -07:00

Add templatetests package and start testing templates

This commit is contained in:
Eggbertx 2024-04-01 17:17:43 -07:00
parent 6191f971c3
commit e44dc78ae5
2 changed files with 78 additions and 2 deletions

View file

@ -0,0 +1,30 @@
package gctemplates_test
import "github.com/gochan-org/gochan/pkg/config"
var (
jsConstsCases = []templateTestCase{
{
desc: "base test",
data: map[string]any{
"styles": []config.Style{
{Name: "Pipes", Filename: "pipes.css"},
{Name: "Yotsuba A", Filename: "yotsuba.css"},
},
"defaultStyle": "pipes.css",
"webroot": "/",
"timezone": -1,
},
expectedOutput: `var styles=[{Name:"Pipes",Filename:"pipes.css"},{Name:"Yotsuba A",Filename:"yotsuba.css"}];var defaultStyle="pipes.css";var webroot="/";var serverTZ=-1;`,
},
{
desc: "empty values",
data: map[string]any{
"defaultStyle": "",
"webroot": "",
"timezone": 0,
},
expectedOutput: `var styles=[];var defaultStyle="";var webroot="";var serverTZ=0;`,
},
}
)

View file

@ -1,6 +1,7 @@
package gctemplates_test
import (
"bytes"
"testing"
"time"
@ -11,6 +12,7 @@ import (
"github.com/gochan-org/gochan/pkg/gctemplates"
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
_ "github.com/gochan-org/gochan/pkg/posting/uploads/inituploads"
"github.com/gochan-org/gochan/pkg/server/serverutil"
"github.com/stretchr/testify/assert"
)
@ -26,7 +28,14 @@ const (
`ORDER BY\s+position ASC,\s*name ASC`
)
func initTemplatesMock(t *testing.T, mock sqlmock.Sqlmock) bool {
type templateTestCase struct {
desc string
data any
expectsError bool
expectedOutput string
}
func initTemplatesMock(t *testing.T, mock sqlmock.Sqlmock, which ...string) bool {
t.Helper()
rows := sqlmock.NewRows([]string{"boards.id", "section_id", "uri", "dir", "navbar_position", "title",
"subtitle", "description", "max_file_size", "max_threads", "default_style", "locked", "created_at",
@ -46,13 +55,50 @@ func initTemplatesMock(t *testing.T, mock sqlmock.Sqlmock) bool {
config.SetTestTemplateDir("templates")
err := gctemplates.InitTemplates()
err := gctemplates.InitTemplates(which...)
if !assert.NoError(t, err) {
return false
}
return assert.NoError(t, mock.ExpectationsWereMet())
}
func TestJsConstsTemplate(t *testing.T) {
_, err := testutil.GoToGochanRoot(t)
if !assert.NoError(t, err) {
return
}
db, mock, err := sqlmock.New()
if !assert.NoError(t, err) {
return
}
config.SetTestDBConfig("mysql", "localhost", "gochan", "gochan", "gochan", "")
if !assert.NoError(t, gcsql.SetTestingDB("mysql", "gochan", "", db)) {
return
}
if !initTemplatesMock(t, mock) {
return
}
serverutil.InitMinifier()
buf := new(bytes.Buffer)
for _, tC := range jsConstsCases {
buf.Reset()
t.Run(tC.desc, func(t *testing.T) {
err := serverutil.MinifyTemplate(gctemplates.JsConsts, tC.data, buf, "text/javascript")
if tC.expectsError {
assert.Error(t, err)
} else {
if !assert.NoError(t, err) {
return
}
}
assert.Equal(t, tC.expectedOutput, buf.String())
})
}
}
func TestTemplateBase(t *testing.T) {
_, err := testutil.GoToGochanRoot(t)
if !assert.NoError(t, err) {