adding tests

This commit is contained in:
SwiftyOS
2024-09-12 17:11:42 +02:00
parent ae1d410b65
commit 33ee2f2ee5
4 changed files with 247 additions and 20 deletions

View File

@@ -2,29 +2,52 @@ package handlers
import (
"context"
"go.uber.org/zap"
"strconv"
)
const pageKey string = "page"
func getPageFromContext(ctx context.Context) int {
if pageValue, ok := ctx.Value(pageKey).(int); ok {
if pageValue < 1 {
zap.L().Error("Invalid page value", zap.Int("page", pageValue))
return 1
}
return pageValue
defaultPage := 1
if ctx == nil {
return defaultPage
}
return 1
pageValue := ctx.Value(pageKey)
if pageValue == nil {
return defaultPage
}
// Type assertion to check if the value is an int
if page, ok := pageValue.(int); ok {
if page < 1 {
return defaultPage
}
return page
}
// If it's not an int, try to convert from string
if pageStr, ok := pageValue.(string); ok {
page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
return defaultPage
}
return page
}
return defaultPage
}
const pageSizeKey string = "page_size"
func getPageSizeFromContext(ctx context.Context) int {
if pageSizeValue, ok := ctx.Value(pageSizeKey).(int); ok {
pageSizeValue := ctx.Value(pageSizeKey)
if pageSizeValue == nil {
return 10
}
if pageSizeValue, ok := pageSizeValue.(int); ok {
if pageSizeValue < 1 {
zap.L().Error("Invalid page size value", zap.Int("page_size", pageSizeValue))
return 10
}
return pageSizeValue
@@ -35,32 +58,38 @@ func getPageSizeFromContext(ctx context.Context) int {
const nameKey string = "name"
func getNameFromContext(ctx context.Context) *string {
if nameValue, ok := ctx.Value(nameKey).(string); ok {
zap.L().Debug("Retrieved name from context", zap.String("name", nameValue))
nameValue := ctx.Value(nameKey)
if nameValue == nil {
return nil
}
if nameValue, ok := nameValue.(string); ok {
return &nameValue
}
zap.L().Debug("No name found in context")
return nil
}
const keywordsKey string = "keywords"
func getKeywordsFromContext(ctx context.Context) *string {
if keywordsValue, ok := ctx.Value(keywordsKey).(string); ok {
zap.L().Debug("Retrieved keywords from context", zap.String("keywords", keywordsValue))
keywordsValue := ctx.Value(keywordsKey)
if keywordsValue == nil {
return nil
}
if keywordsValue, ok := keywordsValue.(string); ok {
return &keywordsValue
}
zap.L().Debug("No keywords found in context")
return nil
}
const categoriesKey string = "categories"
func getCategoriesFromContext(ctx context.Context) *string {
if categoriesValue, ok := ctx.Value(categoriesKey).(string); ok {
zap.L().Debug("Retrieved categories from context", zap.String("categories", categoriesValue))
categoriesValue := ctx.Value(categoriesKey)
if categoriesValue == nil {
return nil
}
if categoriesValue, ok := categoriesValue.(string); ok {
return &categoriesValue
}
zap.L().Debug("No categories found in context")
return nil
}

View File

@@ -0,0 +1,153 @@
package handlers
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)
func TestGetPageFromContext_ValidPage(t *testing.T) {
ctx := context.WithValue(context.Background(), pageKey, 5)
result := getPageFromContext(ctx)
assert.Equal(t, 5, result)
}
func TestGetPageFromContext_InvalidPageZero(t *testing.T) {
ctx := context.WithValue(context.Background(), pageKey, 0)
result := getPageFromContext(ctx)
assert.Equal(t, 1, result)
}
func TestGetPageFromContext_NoPageValue(t *testing.T) {
ctx := context.Background()
result := getPageFromContext(ctx)
assert.Equal(t, 1, result)
}
func TestGetPageFromContext_InvalidPageNegative(t *testing.T) {
ctx := context.WithValue(context.Background(), pageKey, -1)
result := getPageFromContext(ctx)
assert.Equal(t, 1, result)
}
func TestGetPageFromContext_InvalidType(t *testing.T) {
ctx := context.WithValue(context.Background(), pageKey, "not an int")
result := getPageFromContext(ctx)
assert.Equal(t, 1, result)
}
func TestGetPageSizeFromContext_ValidPageSize(t *testing.T) {
ctx := context.WithValue(context.Background(), pageSizeKey, 20)
result := getPageSizeFromContext(ctx)
assert.Equal(t, 20, result)
}
func TestGetPageSizeFromContext_InvalidPageSizeNegative(t *testing.T) {
ctx := context.WithValue(context.Background(), pageSizeKey, -1)
result := getPageSizeFromContext(ctx)
assert.Equal(t, 10, result)
}
func TestGetPageSizeFromContext_InvalidPageSizeZero(t *testing.T) {
ctx := context.WithValue(context.Background(), pageSizeKey, 0)
result := getPageSizeFromContext(ctx)
assert.Equal(t, 10, result)
}
func TestGetPageSizeFromContext_NoPageSizeValue(t *testing.T) {
ctx := context.Background()
result := getPageSizeFromContext(ctx)
assert.Equal(t, 10, result)
}
func TestGetPageSizeFromContext_InvalidType(t *testing.T) {
ctx := context.WithValue(context.Background(), pageSizeKey, "not an int")
result := getPageSizeFromContext(ctx)
assert.Equal(t, 10, result)
}
func TestGetNameFromContext_ValidName(t *testing.T) {
ctx := context.WithValue(context.Background(), nameKey, "Test Name")
result := getNameFromContext(ctx)
assert.Equal(t, strPtr("Test Name"), result)
}
func TestGetNameFromContext_EmptyString(t *testing.T) {
ctx := context.WithValue(context.Background(), nameKey, "")
result := getNameFromContext(ctx)
assert.Equal(t, strPtr(""), result)
}
func TestGetNameFromContext_NoNameValue(t *testing.T) {
ctx := context.Background()
result := getNameFromContext(ctx)
assert.Nil(t, result)
}
func TestGetNameFromContext_InvalidType(t *testing.T) {
ctx := context.WithValue(context.Background(), nameKey, 123)
result := getNameFromContext(ctx)
assert.Nil(t, result)
}
func TestGetKeywordsFromContext_ValidKeywords(t *testing.T) {
ctx := context.WithValue(context.Background(), keywordsKey, "keyword1,keyword2")
result := getKeywordsFromContext(ctx)
assert.Equal(t, strPtr("keyword1,keyword2"), result)
}
func TestGetKeywordsFromContext_EmptyString(t *testing.T) {
ctx := context.WithValue(context.Background(), keywordsKey, "")
result := getKeywordsFromContext(ctx)
assert.Equal(t, strPtr(""), result)
}
func TestGetKeywordsFromContext_NoKeywordsValue(t *testing.T) {
ctx := context.Background()
result := getKeywordsFromContext(ctx)
assert.Nil(t, result)
}
func TestGetKeywordsFromContext_InvalidType(t *testing.T) {
ctx := context.WithValue(context.Background(), keywordsKey, 123)
result := getKeywordsFromContext(ctx)
assert.Nil(t, result)
}
func TestGetCategoriesFromContext_ValidCategories(t *testing.T) {
ctx := context.WithValue(context.Background(), categoriesKey, "category1,category2")
result := getCategoriesFromContext(ctx)
assert.Equal(t, strPtr("category1,category2"), result)
}
func TestGetCategoriesFromContext_EmptyString(t *testing.T) {
ctx := context.WithValue(context.Background(), categoriesKey, "")
result := getCategoriesFromContext(ctx)
assert.Equal(t, strPtr(""), result)
}
func TestGetCategoriesFromContext_NoCategoriesValue(t *testing.T) {
ctx := context.Background()
result := getCategoriesFromContext(ctx)
assert.Nil(t, result)
}
func TestGetCategoriesFromContext_InvalidType(t *testing.T) {
ctx := context.WithValue(context.Background(), categoriesKey, 123)
result := getCategoriesFromContext(ctx)
assert.Nil(t, result)
}
func strPtr(s string) *string {
return &s
}
func init() {
logger := zaptest.NewLogger(nil)
zap.ReplaceGlobals(logger)
}

View File

@@ -0,0 +1,45 @@
package handlers
import (
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/swiftyos/market/models"
)
func TestGetUserFromContext(t *testing.T) {
t.Run("User exists in context", func(t *testing.T) {
// Create a new gin context
c, _ := gin.CreateTestContext(nil)
// Create a test user
testUser := models.User{
UserID: "123",
Role: "admin",
Email: "test@example.com",
}
// Set the user in the context
c.Set("user", testUser)
// Call the function
user, exists := GetUserFromContext(c)
// Assert the results
assert.True(t, exists)
assert.Equal(t, testUser, user)
})
t.Run("User does not exist in context", func(t *testing.T) {
// Create a new gin context
c, _ := gin.CreateTestContext(nil)
// Call the function
user, exists := GetUserFromContext(c)
// Assert the results
assert.False(t, exists)
assert.Equal(t, models.User{}, user)
})
}