1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 15:06:23 -07:00

Add public function to gcsql for SQL mock access

This commit is contained in:
Eggbertx 2024-05-31 14:43:35 -07:00
parent 8be391ba5c
commit 9fe6df52ee
5 changed files with 105 additions and 59 deletions

View file

@ -12,7 +12,6 @@ import (
"github.com/Eggbertx/durationutil" "github.com/Eggbertx/durationutil"
"github.com/gochan-org/gochan/pkg/gcutil" "github.com/gochan-org/gochan/pkg/gcutil"
"github.com/gochan-org/gochan/pkg/gcutil/testutil"
"github.com/gochan-org/gochan/pkg/posting/geoip" "github.com/gochan-org/gochan/pkg/posting/geoip"
) )
@ -401,40 +400,3 @@ func SetVerbose(verbose bool) {
func GetVersion() *GochanVersion { func GetVersion() *GochanVersion {
return cfg.Version return cfg.Version
} }
// SetVersion should (in most cases) only be used for tests, where a config file wouldn't be loaded
func SetVersion(version string) {
if cfg == nil {
cfg = defaultGochanConfig
}
cfg.Version = ParseVersion(version)
}
// SetTestTemplateDir sets the directory for templates, used only in testing. If it is not run via `go test`, it will panic.
func SetTestTemplateDir(dir string) {
testutil.PanicIfNotTest()
if cfg == nil {
cfg = defaultGochanConfig
}
cfg.TemplateDir = dir
}
// SetTestDBConfig sets up the database configuration for a testing environment. If it is not run via `go test`, it will panic
func SetTestDBConfig(dbType string, dbHost string, dbName string, dbUsername string, dbPassword string, dbPrefix string) {
testutil.PanicIfNotTest()
if cfg == nil {
cfg = defaultGochanConfig
}
cfg.DBtype = dbType
cfg.DBhost = dbHost
cfg.DBname = dbName
cfg.DBusername = dbUsername
cfg.DBpassword = dbPassword
cfg.DBprefix = dbPrefix
}
// SetRandomSeed is usd to set a deterministic seed to make testing easier. If it is not run via `go test`, it will panic
func SetRandomSeed(seed string) {
testutil.PanicIfNotTest()
cfg.RandomSeed = seed
}

72
pkg/config/testing.go Normal file
View file

@ -0,0 +1,72 @@
package config
import "github.com/gochan-org/gochan/pkg/gcutil/testutil"
func setDefaultCfgIfNotSet() {
if cfg == nil {
cfg = defaultGochanConfig
}
}
// SetVersion should only be used for tests, where a config file wouldn't be loaded
func SetVersion(version string) {
testutil.PanicIfNotTest()
setDefaultCfgIfNotSet()
cfg.Version = ParseVersion(version)
}
// SetTestTemplateDir sets the directory for templates, used only in testing. If it is not run via `go test`, it will panic.
func SetTestTemplateDir(dir string) {
testutil.PanicIfNotTest()
setDefaultCfgIfNotSet()
cfg.TemplateDir = dir
}
// SetTestDBConfig sets up the database configuration for a testing environment. If it is not run via `go test`, it will panic
func SetTestDBConfig(dbType string, dbHost string, dbName string, dbUsername string, dbPassword string, dbPrefix string) {
testutil.PanicIfNotTest()
setDefaultCfgIfNotSet()
cfg.DBtype = dbType
cfg.DBhost = dbHost
cfg.DBname = dbName
cfg.DBusername = dbUsername
cfg.DBpassword = dbPassword
cfg.DBprefix = dbPrefix
}
// SetRandomSeed is usd to set a deterministic seed to make testing easier. If it is not run via `go test`, it will panic
func SetRandomSeed(seed string) {
testutil.PanicIfNotTest()
setDefaultCfgIfNotSet()
cfg.RandomSeed = seed
}
// SetSystemCriticalConfig sets system critical configuration values in testing. It will panic if it is not run in a
// test environment
func SetSystemCriticalConfig(systemCritical *SystemCriticalConfig) {
testutil.PanicIfNotTest()
setDefaultCfgIfNotSet()
cfg.SystemCriticalConfig = *systemCritical
}
// SetSiteConfig sets the site configuration values in testing. It will panic if it is not run in a test environment
func SetSiteConfig(siteConfig *SiteConfig) {
testutil.PanicIfNotTest()
setDefaultCfgIfNotSet()
cfg.SiteConfig = *siteConfig
}
// SetBoardConfig applies the configuration to the given board. It will panic if it is not run in a test environment
func SetBoardConfig(board string, boardCfg *BoardConfig) {
testutil.PanicIfNotTest()
setDefaultCfgIfNotSet()
if board == "" {
cfg.BoardConfig = *boardCfg
} else {
boardConfigs[board] = *boardCfg
}
}

View file

@ -8,6 +8,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/DATA-DOG/go-sqlmock"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -388,6 +389,33 @@ func setupDBConn(cfg *config.SQLConfig) (db *GCDB, err error) {
return db, nil return db, nil
} }
func setupSqlTestConfig(dbDriver string, dbName string, dbPrefix string) *config.SQLConfig {
return &config.SQLConfig{
DBtype: dbDriver,
DBhost: "localhost",
DBname: dbName,
DBusername: "gochan",
DBpassword: "gochan",
DBprefix: dbPrefix,
DBTimeoutSeconds: config.DefaultSQLTimeout,
DBMaxOpenConnections: config.DefaultSQLMaxConns,
DBMaxIdleConnections: config.DefaultSQLMaxConns,
DBConnMaxLifetimeMin: config.DefaultSQLConnMaxLifetimeMin,
}
}
func SetupMockDB(driver string) (sqlmock.Sqlmock, error) {
var err error
gcdb, err = setupDBConn(setupSqlTestConfig(driver, "gochan", ""))
if err != nil {
return nil, err
}
var mock sqlmock.Sqlmock
gcdb.db, mock, err = sqlmock.New()
return mock, err
}
// Open opens and returns a new gochan database connection with the provided host, driver, DB name, // Open opens and returns a new gochan database connection with the provided host, driver, DB name,
// username, password, and table prefix // username, password, and table prefix
func Open(cfg *config.SQLConfig) (db *GCDB, err error) { func Open(cfg *config.SQLConfig) (db *GCDB, err error) {

View file

@ -4,7 +4,6 @@ import (
"testing" "testing"
"github.com/DATA-DOG/go-sqlmock" "github.com/DATA-DOG/go-sqlmock"
"github.com/gochan-org/gochan/pkg/config"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -19,24 +18,9 @@ func closeMock(t *testing.T, mock sqlmock.Sqlmock) {
assert.NoError(t, err) assert.NoError(t, err)
} }
func setupSQLConfig(dbDriver string, dbName string, dbPrefix string) *config.SQLConfig {
return &config.SQLConfig{
DBtype: dbDriver,
DBhost: "localhost",
DBname: dbName,
DBusername: "gochan",
DBpassword: "gochan",
DBprefix: dbPrefix,
DBTimeoutSeconds: config.DefaultSQLTimeout,
DBMaxOpenConnections: config.DefaultSQLMaxConns,
DBMaxIdleConnections: config.DefaultSQLMaxConns,
DBConnMaxLifetimeMin: config.DefaultSQLConnMaxLifetimeMin,
}
}
func TestOpenMySQL(t *testing.T) { func TestOpenMySQL(t *testing.T) {
var err error var err error
gcdb, err = setupDBConn(setupSQLConfig("mysql", "gochan", "")) gcdb, err = setupDBConn(setupSqlTestConfig("mysql", "gochan", ""))
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
} }
@ -49,7 +33,7 @@ func TestOpenMySQL(t *testing.T) {
func TestOpenPostgres(t *testing.T) { func TestOpenPostgres(t *testing.T) {
var err error var err error
gcdb, err = setupDBConn(setupSQLConfig("postgres", "gochan", "")) gcdb, err = setupDBConn(setupSqlTestConfig("postgres", "gochan", ""))
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
} }
@ -62,7 +46,7 @@ func TestOpenPostgres(t *testing.T) {
func TestOpenSQLite3(t *testing.T) { func TestOpenSQLite3(t *testing.T) {
var err error var err error
gcdb, err = setupDBConn(setupSQLConfig("sqlite3", "gochan", "")) gcdb, err = setupDBConn(setupSqlTestConfig("sqlite3", "gochan", ""))
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
} }
@ -75,6 +59,6 @@ func TestOpenSQLite3(t *testing.T) {
func TestOpenUnrecognizedDriver(t *testing.T) { func TestOpenUnrecognizedDriver(t *testing.T) {
assert.NoError(t, Close()) assert.NoError(t, Close())
_, err := setupDBConn(setupSQLConfig("wat", "gochan", "")) _, err := setupDBConn(setupSqlTestConfig("wat", "gochan", ""))
assert.Error(t, err) assert.Error(t, err)
} }

View file

@ -25,7 +25,7 @@ func TestProvision(t *testing.T) {
t.Run(driver, func(t *testing.T) { t.Run(driver, func(t *testing.T) {
config.SetTestDBConfig(driver, "localhost", "gochan", "gochan", "gochan", "") config.SetTestDBConfig(driver, "localhost", "gochan", "gochan", "gochan", "")
gcdb, err = setupDBConn(setupSQLConfig(driver, "gochan", "")) gcdb, err = setupDBConn(setupSqlTestConfig(driver, "gochan", ""))
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
return return
} }