TestData-Factory [WIP]

This commit is contained in:
Mike Schwörer 2022-12-09 00:40:50 +01:00
parent edfcdd1135
commit 06788c3e12
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
8 changed files with 142 additions and 28 deletions

View File

@ -5,6 +5,8 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var SuppressGinLogs = false
func NewEngine(cfg scn.Config) *gin.Engine { func NewEngine(cfg scn.Config) *gin.Engine {
engine := gin.New() engine := gin.New()
@ -14,7 +16,13 @@ func NewEngine(cfg scn.Config) *gin.Engine {
engine.Use(CorsMiddleware()) engine.Use(CorsMiddleware())
if cfg.GinDebug { if cfg.GinDebug {
engine.Use(gin.Logger()) ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
if SuppressGinLogs {
return
}
ginlogger(context)
})
} }
return engine return engine

View File

@ -56,7 +56,7 @@ var configLocHost = func() Config {
DBFile: ".run-data/db.sqlite3", DBFile: ".run-data/db.sqlite3",
DBJournal: "WAL", DBJournal: "WAL",
DBTimeout: 5 * time.Second, DBTimeout: 5 * time.Second,
DBCheckForeignKeys: true, DBCheckForeignKeys: false,
DBMaxOpenConns: 5, DBMaxOpenConns: 5,
DBMaxIdleConns: 5, DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute, DBConnMaxLifetime: 60 * time.Minute,
@ -90,7 +90,7 @@ var configLocDocker = func() Config {
DBFile: "/data/scn_docker.sqlite3", DBFile: "/data/scn_docker.sqlite3",
DBJournal: "WAL", DBJournal: "WAL",
DBTimeout: 5 * time.Second, DBTimeout: 5 * time.Second,
DBCheckForeignKeys: true, DBCheckForeignKeys: false,
DBMaxOpenConns: 5, DBMaxOpenConns: 5,
DBMaxIdleConns: 5, DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute, DBConnMaxLifetime: 60 * time.Minute,
@ -124,7 +124,7 @@ var configDev = func() Config {
DBFile: "/data/scn.sqlite3", DBFile: "/data/scn.sqlite3",
DBJournal: "WAL", DBJournal: "WAL",
DBTimeout: 5 * time.Second, DBTimeout: 5 * time.Second,
DBCheckForeignKeys: true, DBCheckForeignKeys: false,
DBMaxOpenConns: 5, DBMaxOpenConns: 5,
DBMaxIdleConns: 5, DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute, DBConnMaxLifetime: 60 * time.Minute,
@ -158,7 +158,7 @@ var configStag = func() Config {
DBFile: "/data/scn.sqlite3", DBFile: "/data/scn.sqlite3",
DBJournal: "WAL", DBJournal: "WAL",
DBTimeout: 5 * time.Second, DBTimeout: 5 * time.Second,
DBCheckForeignKeys: true, DBCheckForeignKeys: false,
DBMaxOpenConns: 5, DBMaxOpenConns: 5,
DBMaxIdleConns: 5, DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute, DBConnMaxLifetime: 60 * time.Minute,
@ -192,7 +192,7 @@ var configProd = func() Config {
DBFile: "/data/scn.sqlite3", DBFile: "/data/scn.sqlite3",
DBJournal: "WAL", DBJournal: "WAL",
DBTimeout: 5 * time.Second, DBTimeout: 5 * time.Second,
DBCheckForeignKeys: true, DBCheckForeignKeys: false,
DBMaxOpenConns: 5, DBMaxOpenConns: 5,
DBMaxIdleConns: 5, DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute, DBConnMaxLifetime: 60 * time.Minute,

View File

@ -4,6 +4,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/logic"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/timeext" "gogs.mikescher.com/BlackForestBytes/goext/timeext"
"gopkg.in/loremipsum.v1" "gopkg.in/loremipsum.v1"
"testing" "testing"
@ -266,6 +267,17 @@ var messageExamples = []msgex{
} }
func InitDefaultData(t *testing.T, ws *logic.Application) { func InitDefaultData(t *testing.T, ws *logic.Application) {
// set logger to buffer, only output if error occured
success := false
SetBufLogger()
defer func() {
ClearBufLogger(!success)
if success {
log.Info().Msgf("Succesfully initialized default data (%d messages, %d users)", len(messageExamples), len(userExamples))
}
}()
baseUrl := "http://127.0.0.1:" + ws.Port baseUrl := "http://127.0.0.1:" + ws.Port
users := make([]userdat, 0, len(userExamples)) users := make([]userdat, 0, len(userExamples))
@ -347,6 +359,8 @@ func InitDefaultData(t *testing.T, ws *logic.Application) {
RequestPost[gin.H](t, baseUrl, "/", body) RequestPost[gin.H](t, baseUrl, "/", body)
} }
success = true
} }
func lipsum(seed int64, paracount int) string { func lipsum(seed int64, paracount int) string {

View File

@ -4,24 +4,32 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"io"
"os" "os"
) )
func InitTests() { func InitTests() {
cw := zerolog.ConsoleWriter{ log.Logger = createLogger(createConsoleWriter())
gin.SetMode(gin.TestMode)
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
func createConsoleWriter() *zerolog.ConsoleWriter {
return &zerolog.ConsoleWriter{
Out: os.Stdout, Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05.000 Z07:00", TimeFormat: "2006-01-02 15:04:05.000 Z07:00",
} }
}
func createLogger(cw io.Writer) zerolog.Logger {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
multi := zerolog.MultiLevelWriter(cw) multi := zerolog.MultiLevelWriter(cw)
logger := zerolog.New(multi).With(). logger := zerolog.New(multi).With().
Timestamp(). Timestamp().
Caller(). Caller().
Logger() Logger()
log.Logger = logger return logger
gin.SetMode(gin.TestMode)
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }

47
server/test/util/log.go Normal file
View File

@ -0,0 +1,47 @@
package util
import (
"blackforestbytes.com/simplecloudnotifier/common/ginext"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
var buflogger *BufferWriter = nil
func SetBufLogger() {
buflogger = &BufferWriter{cw: createConsoleWriter()}
log.Logger = createLogger(buflogger)
gin.SetMode(gin.ReleaseMode)
ginext.SuppressGinLogs = true
}
func ClearBufLogger(dump bool) {
size := len(buflogger.buffer)
if dump {
buflogger.Dump()
}
log.Logger = createLogger(createConsoleWriter())
buflogger = nil
gin.SetMode(gin.TestMode)
ginext.SuppressGinLogs = false
if !dump {
log.Info().Msgf("Suppressed %d logmessages / printf-statements", size)
}
}
func TPrintf(format string, a ...any) {
if buflogger != nil {
buflogger.Printf(format, a...)
} else {
fmt.Printf(format, a...)
}
}
func TPrintln(a ...any) {
if buflogger != nil {
buflogger.Println(a...)
} else {
fmt.Println(a...)
}
}

View File

@ -0,0 +1,38 @@
package util
import (
"fmt"
"github.com/rs/zerolog"
)
type BufferWriter struct {
cw *zerolog.ConsoleWriter
buffer []func(cw *zerolog.ConsoleWriter)
}
func (b *BufferWriter) Write(p []byte) (n int, err error) {
b.buffer = append(b.buffer, func(cw *zerolog.ConsoleWriter) {
_, _ = cw.Write(p)
})
return len(p), nil
}
func (b *BufferWriter) Dump() {
for _, v := range b.buffer {
v(b.cw)
}
b.buffer = nil
}
func (b *BufferWriter) Println(a ...any) {
b.buffer = append(b.buffer, func(cw *zerolog.ConsoleWriter) {
fmt.Println(a...)
})
}
func (b *BufferWriter) Printf(format string, a ...any) {
b.buffer = append(b.buffer, func(cw *zerolog.ConsoleWriter) {
fmt.Printf(format, a...)
})
}

View File

@ -89,7 +89,7 @@ func RequestAuthDeleteShouldFail(t *testing.T, akey string, baseURL string, urlS
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any) TResult { func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any) TResult {
client := http.Client{} client := http.Client{}
fmt.Printf("[-> REQUEST] (%s) %s%s [%s] [%s]\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), langext.Conditional(body == nil, "NO BODY", "BODY")) TPrintf("[-> REQUEST] (%s) %s%s [%s] [%s]\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), langext.Conditional(body == nil, "NO BODY", "BODY"))
bytesbody := make([]byte, 0) bytesbody := make([]byte, 0)
contentType := "" contentType := ""
@ -144,12 +144,12 @@ func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL s
TestFailErr(t, err) TestFailErr(t, err)
} }
fmt.Println("") TPrintln("")
fmt.Printf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode) TPrintf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode)
fmt.Println(langext.TryPrettyPrintJson(string(respBodyBin))) TPrintln(langext.TryPrettyPrintJson(string(respBodyBin)))
TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "") TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "")
fmt.Println("---------------- -------- ----------------") TPrintln("---------------- -------- ----------------")
fmt.Println("") TPrintln("")
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
TestFail(t, "Statuscode != 200") TestFail(t, "Statuscode != 200")
@ -166,7 +166,7 @@ func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL s
func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) { func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
client := http.Client{} client := http.Client{}
fmt.Printf("[-> REQUEST] (%s) %s%s [%s] (should-fail with %d/%d)\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), statusCode, errcode) TPrintf("[-> REQUEST] (%s) %s%s [%s] (should-fail with %d/%d)\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), statusCode, errcode)
bytesbody := make([]byte, 0) bytesbody := make([]byte, 0)
contentType := "" contentType := ""
@ -221,14 +221,14 @@ func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL
TestFailErr(t, err) TestFailErr(t, err)
} }
fmt.Println("") TPrintln("")
fmt.Printf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode) TPrintf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode)
fmt.Println(langext.TryPrettyPrintJson(string(respBodyBin))) TPrintln(langext.TryPrettyPrintJson(string(respBodyBin)))
if (statusCode != 0 && resp.StatusCode != statusCode) || (statusCode == 0 && resp.StatusCode == 200) { if (statusCode != 0 && resp.StatusCode != statusCode) || (statusCode == 0 && resp.StatusCode == 200) {
TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "") TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "")
} }
fmt.Println("---------------- -------- ----------------") TPrintln("---------------- -------- ----------------")
fmt.Println("") TPrintln("")
if statusCode != 0 && resp.StatusCode != statusCode { if statusCode != 0 && resp.StatusCode != statusCode {
TestFailFmt(t, "Statuscode != %d (expected failure)", statusCode) TestFailFmt(t, "Statuscode != %d (expected failure)", statusCode)
@ -267,13 +267,13 @@ func TryPrintTraceObj(prefix string, body []byte, suffix string) {
if v2, ok := v1["traceObj"]; ok { if v2, ok := v1["traceObj"]; ok {
if v3, ok := v2.(string); ok { if v3, ok := v2.(string); ok {
if prefix != "" { if prefix != "" {
fmt.Println(prefix) TPrintln(prefix)
} }
fmt.Println(strings.TrimSpace(v3)) TPrintln(strings.TrimSpace(v3))
if suffix != "" { if suffix != "" {
fmt.Println(suffix) TPrintln(suffix)
} }
} }
} }

View File

@ -9,7 +9,6 @@ import (
"blackforestbytes.com/simplecloudnotifier/jobs" "blackforestbytes.com/simplecloudnotifier/jobs"
"blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/push" "blackforestbytes.com/simplecloudnotifier/push"
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"os" "os"
"path/filepath" "path/filepath"
@ -45,7 +44,7 @@ func StartSimpleWebserver(t *testing.T) (*logic.Application, func()) {
TestFailErr(t, err) TestFailErr(t, err)
} }
fmt.Println("DatabaseFile: " + dbfile) TPrintln("DatabaseFile: " + dbfile)
conf := scn.Config{ conf := scn.Config{
Namespace: "test", Namespace: "test",