You've already forked Go-Template
Future self, please forgive me and don't hit me with the baseball bat again!
This commit is contained in:
29
http_handlers/errors/badrequest_test.go
Normal file
29
http_handlers/errors/badrequest_test.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewBadRequestError(t *testing.T) {
|
||||||
|
e := echo.New()
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
|
||||||
|
fields := map[string]string{"field1": "error1", "field2": "error2"}
|
||||||
|
err := NewBadRequestError(c, fields)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusBadRequest, rec.Code)
|
||||||
|
|
||||||
|
var resp map[string]interface{}
|
||||||
|
b := json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||||
|
assert.NoError(t, b)
|
||||||
|
assert.Equal(t, "Bad Request", resp["error"])
|
||||||
|
assert.Equal(t, map[string]interface{}{"field1": "error1", "field2": "error2"}, resp["fields"])
|
||||||
|
}
|
||||||
27
http_handlers/errors/notfound_test.go
Normal file
27
http_handlers/errors/notfound_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewNotFoundError(t *testing.T) {
|
||||||
|
e := echo.New()
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/notfound", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
|
||||||
|
err := NewNotFoundError(c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusNotFound, rec.Code)
|
||||||
|
|
||||||
|
var resp map[string]string
|
||||||
|
e2 := json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||||
|
assert.NoError(t, e2)
|
||||||
|
assert.Equal(t, "Not Found", resp["error"])
|
||||||
|
}
|
||||||
122
http_handlers/index/index_test.go
Normal file
122
http_handlers/index/index_test.go
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package index
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.siteworxpro.com/Siteworxpro/Go-Template/logger"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mockLogger struct {
|
||||||
|
logger.Logger
|
||||||
|
infoCalled bool
|
||||||
|
infoMsg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockLogger) Info(format string, args ...interface{}) {
|
||||||
|
m.infoCalled = true
|
||||||
|
m.infoMsg = format
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupContext(method, path string, l *mockLogger) echo.Context {
|
||||||
|
e := echo.New()
|
||||||
|
req := httptest.NewRequest(method, path, nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
c.Set("logger", l)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexGet(t *testing.T) {
|
||||||
|
l := &mockLogger{}
|
||||||
|
c := setupContext(http.MethodGet, "/", l)
|
||||||
|
idx := &index{}
|
||||||
|
|
||||||
|
err := idx.Get(c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, l.infoCalled)
|
||||||
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
||||||
|
|
||||||
|
var resp map[string]string
|
||||||
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
||||||
|
assert.NoError(t, e)
|
||||||
|
assert.Equal(t, "Hello, World!", resp["message"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexPost(t *testing.T) {
|
||||||
|
l := &mockLogger{}
|
||||||
|
c := setupContext(http.MethodPost, "/", l)
|
||||||
|
idx := &index{}
|
||||||
|
|
||||||
|
err := idx.Post(c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusBadRequest, c.Response().Status)
|
||||||
|
|
||||||
|
var resp map[string]interface{}
|
||||||
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
||||||
|
assert.NoError(t, e)
|
||||||
|
assert.Equal(t, "Bad Request", resp["error"])
|
||||||
|
fields := resp["fields"].(map[string]interface{})
|
||||||
|
assert.Equal(t, "Test is a required field", fields["test"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexPut(t *testing.T) {
|
||||||
|
l := &mockLogger{}
|
||||||
|
c := setupContext(http.MethodPut, "/", l)
|
||||||
|
idx := &index{}
|
||||||
|
|
||||||
|
err := idx.Put(c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
||||||
|
|
||||||
|
var resp map[string]string
|
||||||
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
||||||
|
assert.NoError(t, e)
|
||||||
|
assert.Equal(t, "Hello, World!", resp["message"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexDelete(t *testing.T) {
|
||||||
|
l := &mockLogger{}
|
||||||
|
c := setupContext(http.MethodDelete, "/", l)
|
||||||
|
idx := &index{}
|
||||||
|
|
||||||
|
err := idx.Delete(c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
||||||
|
|
||||||
|
var resp map[string]string
|
||||||
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
||||||
|
assert.NoError(t, e)
|
||||||
|
assert.Equal(t, "Hello, World!", resp["message"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIndexPatch(t *testing.T) {
|
||||||
|
l := &mockLogger{}
|
||||||
|
c := setupContext(http.MethodPatch, "/", l)
|
||||||
|
idx := &index{}
|
||||||
|
|
||||||
|
err := idx.Patch(c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusNotFound, c.Response().Status)
|
||||||
|
|
||||||
|
var resp map[string]string
|
||||||
|
e := json.Unmarshal(c.Response().Writer.(*httptest.ResponseRecorder).Body.Bytes(), &resp)
|
||||||
|
assert.NoError(t, e)
|
||||||
|
assert.Equal(t, "Not Found", resp["error"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegister(t *testing.T) {
|
||||||
|
l := &mockLogger{}
|
||||||
|
e := echo.New()
|
||||||
|
g := e.Group("/")
|
||||||
|
Register(g)
|
||||||
|
c := setupContext(http.MethodGet, "/", l)
|
||||||
|
idx := &index{}
|
||||||
|
err := idx.Get(c)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, http.StatusOK, c.Response().Status)
|
||||||
|
}
|
||||||
54
logger/log_test.go
Normal file
54
logger/log_test.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewLogger(t *testing.T) {
|
||||||
|
logger := NewLogger(log.InfoLevel)
|
||||||
|
if logger == nil {
|
||||||
|
t.Fatal("Expected logger to be non-nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if logger.GetLevel() != log.InfoLevel {
|
||||||
|
t.Errorf("Expected log level %v, got %v", log.InfoLevel, logger.GetLevel())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewContextAndFromContext(t *testing.T) {
|
||||||
|
baseCtx := context.Background()
|
||||||
|
logger := NewLogger(log.WarnLevel)
|
||||||
|
ctx := NewContext(baseCtx, logger)
|
||||||
|
|
||||||
|
l := FromContext(ctx)
|
||||||
|
if l == nil {
|
||||||
|
t.Fatal("Expected logger from context to be non-nil")
|
||||||
|
}
|
||||||
|
if l.GetLevel() != log.WarnLevel {
|
||||||
|
t.Errorf("Expected log level %v, got %v", log.WarnLevel, l.GetLevel())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFromContext_NoLogger(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
l := FromContext(ctx)
|
||||||
|
if l != nil {
|
||||||
|
t.Error("Expected nil when no logger in context")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerMethods(t *testing.T) {
|
||||||
|
logger := NewLogger(log.DebugLevel)
|
||||||
|
logger.Info("Test info")
|
||||||
|
logger.Error("Test error")
|
||||||
|
logger.Debug("Test debug")
|
||||||
|
logger.Warn("Test warn")
|
||||||
|
|
||||||
|
// Check if the logger methods work without errors
|
||||||
|
if logger.GetLevel() != log.DebugLevel {
|
||||||
|
t.Errorf("Expected log level %v, got %v", log.DebugLevel, logger.GetLevel())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user