Future self, please forgive me and don't hit me with the baseball bat again!

This commit is contained in:
2025-05-22 16:57:34 -04:00
parent 71ff41d387
commit f0e84818b8
4 changed files with 232 additions and 0 deletions

View 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"])
}

View 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"])
}

View 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)
}