mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-06 20:06:23 -07:00
72 lines
2.8 KiB
Go
72 lines
2.8 KiB
Go
|
package gctemplates_test
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/DATA-DOG/go-sqlmock"
|
||
|
"github.com/gochan-org/gochan/pkg/config"
|
||
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
||
|
_ "github.com/gochan-org/gochan/pkg/gcsql/initsql"
|
||
|
"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/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
selectBoardsQueryExpectation = `SELECT\s+boards\.id, section_id,\s*uri,\s*dir,\s*navbar_position,\s*title,\s*` +
|
||
|
`subtitle,\s*description,\s*max_file_size,\s*max_threads,\s*default_style,\s*locked,\s*created_at,\s*` +
|
||
|
`anonymous_name,\s*force_anonymous,\s*autosage_after,\s*no_images_after,\s*max_message_length,\s*` +
|
||
|
`min_message_length,\s*allow_embeds,\s*redirect_to_thread,\s*require_file,\s*enable_catalog\s+` +
|
||
|
`FROM boards\s+INNER JOIN\s*\(\s*SELECT id,\s*hidden\s+FROM sections\s*\)\s+s\s+ON ` +
|
||
|
`boards\.section_id = s\.id\s+WHERE s\.hidden = FALSE ORDER BY navbar_position ASC, boards\.id ASC`
|
||
|
|
||
|
selectSectionsQueryExpectation = `SELECT\s+id,\s*name,\s*abbreviation,\s*position,\s*hidden\s+FROM\s+sections\s+WHERE\s+hidden\s*=\s*FALSE\s+` +
|
||
|
`ORDER BY\s+position ASC,\s*name ASC`
|
||
|
)
|
||
|
|
||
|
func initTemplatesMock(t *testing.T, mock sqlmock.Sqlmock) 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",
|
||
|
"anonymous_name", "force_anonymous", "autosage_after", "no_images_after", "max_message_length",
|
||
|
"min_message_length", "allow_embeds", "redirect_to_thread", "require_file", "enable_catalog"}).
|
||
|
AddRow(1, 1, "test", "test", 1, "Testing board", "Board for testing", "Board for testing", 500, 500,
|
||
|
"pipes.css", false, time.Now(), "Anonymous", false, 200, 500, 1500, 0, false, false, false, true)
|
||
|
|
||
|
mock.ExpectPrepare(selectBoardsQueryExpectation).
|
||
|
ExpectQuery().WithoutArgs().WillReturnRows(rows)
|
||
|
|
||
|
rows = sqlmock.NewRows([]string{"id", "name", "abbreviation", "position", "hidden"}).
|
||
|
AddRow(1, "Main", "main", 1, false)
|
||
|
|
||
|
mock.ExpectPrepare(selectSectionsQueryExpectation).
|
||
|
ExpectQuery().WithoutArgs().WillReturnRows(rows)
|
||
|
|
||
|
config.SetTestTemplateDir("templates")
|
||
|
|
||
|
err := gctemplates.InitTemplates()
|
||
|
if !assert.NoError(t, err) {
|
||
|
return false
|
||
|
}
|
||
|
return assert.NoError(t, mock.ExpectationsWereMet())
|
||
|
}
|
||
|
|
||
|
func TestTemplateBase(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
|
||
|
}
|
||
|
|
||
|
initTemplatesMock(t, mock)
|
||
|
}
|