Tests[CreateUser]
This commit is contained in:
+80
-37
@@ -20,12 +20,15 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Void = struct{}
|
||||
|
||||
func NewSimpleWebserver() (*logic.Application, func()) {
|
||||
func StartSimpleWebserver(t *testing.T) (*logic.Application, func()) {
|
||||
cw := zerolog.ConsoleWriter{
|
||||
Out: os.Stdout,
|
||||
TimeFormat: "2006-01-02 15:04:05 Z07:00",
|
||||
@@ -43,28 +46,27 @@ func NewSimpleWebserver() (*logic.Application, func()) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
|
||||
uuid1, _ := langext.NewHexUUID()
|
||||
uuid2, _ := langext.NewHexUUID()
|
||||
dbdir := filepath.Join(os.TempDir(), uuid1)
|
||||
dbdir := t.TempDir()
|
||||
dbfile := filepath.Join(dbdir, uuid2+".sqlite3")
|
||||
|
||||
err := os.MkdirAll(dbdir, os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
f, err := os.Create(dbfile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
err = os.Chmod(dbfile, 0777)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
fmt.Println("DatabaseFile: " + dbfile)
|
||||
@@ -82,13 +84,13 @@ func NewSimpleWebserver() (*logic.Application, func()) {
|
||||
|
||||
sqlite, err := db.NewDatabase(dbfile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
app := logic.NewApp(sqlite)
|
||||
|
||||
if err := app.Migrate(); err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
ginengine := ginext.NewEngine(conf)
|
||||
@@ -104,64 +106,67 @@ func NewSimpleWebserver() (*logic.Application, func()) {
|
||||
|
||||
router.Init(ginengine)
|
||||
|
||||
return app, func() { app.Stop(); _ = os.Remove(dbfile) }
|
||||
stop := func() { app.Stop(); _ = os.Remove(dbfile) }
|
||||
go func() { app.Run() }()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return app, stop
|
||||
}
|
||||
|
||||
func requestGet[TResult any](baseURL string, prefix string) TResult {
|
||||
return requestAny[TResult]("", "GET", baseURL, prefix, nil)
|
||||
func requestGet[TResult any](t *testing.T, baseURL string, prefix string) TResult {
|
||||
return requestAny[TResult](t, "", "GET", baseURL, prefix, nil)
|
||||
}
|
||||
|
||||
func requestAuthGet[TResult any](akey string, baseURL string, prefix string) TResult {
|
||||
return requestAny[TResult](akey, "GET", baseURL, prefix, nil)
|
||||
func requestAuthGet[TResult any](t *testing.T, akey string, baseURL string, prefix string) TResult {
|
||||
return requestAny[TResult](t, akey, "GET", baseURL, prefix, nil)
|
||||
}
|
||||
|
||||
func requestPost[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "POST", baseURL, prefix, body)
|
||||
func requestPost[TResult any](t *testing.T, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, "", "POST", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthPost[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "POST", baseURL, prefix, body)
|
||||
func requestAuthPost[TResult any](t *testing.T, akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, akey, "POST", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestPut[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "PUT", baseURL, prefix, body)
|
||||
func requestPut[TResult any](t *testing.T, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, "", "PUT", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthPUT[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "PUT", baseURL, prefix, body)
|
||||
func requestAuthPUT[TResult any](t *testing.T, akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, akey, "PUT", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestPatch[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "PATCH", baseURL, prefix, body)
|
||||
func requestPatch[TResult any](t *testing.T, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, "", "PATCH", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthPatch[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "PATCH", baseURL, prefix, body)
|
||||
func requestAuthPatch[TResult any](t *testing.T, akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, akey, "PATCH", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestDelete[TResult any](baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult]("", "DELETE", baseURL, prefix, body)
|
||||
func requestDelete[TResult any](t *testing.T, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, "", "DELETE", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAuthDelete[TResult any](akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](akey, "DELETE", baseURL, prefix, body)
|
||||
func requestAuthDelete[TResult any](t *testing.T, akey string, baseURL string, prefix string, body any) TResult {
|
||||
return requestAny[TResult](t, akey, "DELETE", baseURL, prefix, body)
|
||||
}
|
||||
|
||||
func requestAny[TResult any](akey string, method string, baseURL string, prefix string, body any) TResult {
|
||||
func requestAny[TResult any](t *testing.T, akey string, method string, baseURL string, prefix string, body any) TResult {
|
||||
client := http.Client{}
|
||||
|
||||
bytesbody := make([]byte, 0)
|
||||
if body != nil {
|
||||
bjson, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
bytesbody = bjson
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, baseURL+prefix, bytes.NewReader(bytesbody))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
if body != nil {
|
||||
@@ -174,25 +179,63 @@ func requestAny[TResult any](akey string, method string, baseURL string, prefix
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
respBodyBin, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
fmt.Println("Request: " + method + " :: " + baseURL + prefix)
|
||||
fmt.Println(string(respBodyBin))
|
||||
panic("Statuscode != 200")
|
||||
testFail(t, "Statuscode != 200")
|
||||
}
|
||||
|
||||
var data TResult
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
panic(err)
|
||||
testFailErr(t, err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func assertEqual(t *testing.T, key string, expected any, actual any) {
|
||||
if expected != actual {
|
||||
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
str1 := fmt.Sprintf("%v", expected)
|
||||
str2 := fmt.Sprintf("%v", actual)
|
||||
|
||||
if strings.Contains(str1, "\n") {
|
||||
t.Errorf("Actual :\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", expected)
|
||||
} else {
|
||||
t.Errorf("Actual : \"%v\"\n", expected)
|
||||
}
|
||||
|
||||
if strings.Contains(str2, "\n") {
|
||||
t.Errorf("Expected :\n~~~~~~~~~~~~~~~~\n%v\n~~~~~~~~~~~~~~~~\n\n", actual)
|
||||
} else {
|
||||
t.Errorf("Expected : \"%v\"\n", actual)
|
||||
}
|
||||
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func testFail(t *testing.T, msg string) {
|
||||
t.Error(msg)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func testFailFmt(t *testing.T, format string, args ...any) {
|
||||
t.Errorf(format, args...)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func testFailErr(t *testing.T, e error) {
|
||||
t.Error(fmt.Sprintf("Failed with error:\n%s\n\nError:\n%+v\n\nTrace:\n%s", e.Error(), e, string(debug.Stack())))
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user