tercul-backend/internal/platform/cache/cache_test.go
google-labs-jules[bot] 53aa4d0344
Security Hardening and GraphQL Caching (#69)
* feat: add security middleware, graphql apq, and improved linting

- Add RateLimit, RequestValidation, and CORS middleware.
- Configure middleware chain in API server.
- Implement Redis cache for GraphQL Automatic Persisted Queries.
- Add .golangci.yml and fix linting issues (shadowing, timeouts).

* feat: security, caching and linting config

- Fix .golangci.yml config for govet shadow check
- (Previous changes: Security middleware, GraphQL APQ, Linting fixes)

* fix: resolve remaining lint errors

- Fix unhandled errors in tests (errcheck)
- Define constants for repeated strings (goconst)
- Suppress high complexity warnings with nolint:gocyclo
- Fix integer overflow warnings (gosec)
- Add package comments
- Split long lines (lll)
- Rename Analyse -> Analyze (misspell)
- Fix naked returns and unused params

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-12-01 00:14:22 +01:00

69 lines
1.7 KiB
Go

package cache
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaultKeyGenerator_DefaultPrefix(t *testing.T) {
g := NewDefaultKeyGenerator("")
require.NotNil(t, g)
// Table-driven tests for key generation
tests := []struct {
name string
entity string
id uint
page int
pageSize int
queryName string
params []interface{}
wantEntity string
wantList string
wantQuery string
}{
{
name: "basic",
entity: "user",
id: 42,
page: 1,
pageSize: 20,
queryName: "byEmail",
params: []interface{}{"foo@bar.com"},
wantEntity: "tercul:user:id:42",
wantList: "tercul:user:list:1:20",
wantQuery: "tercul:user:byEmail:foo@bar.com",
},
{
name: "different entity and multiple params",
entity: "work",
id: 7,
page: 3,
pageSize: 15,
queryName: "search",
params: []interface{}{"abc", 2020, true},
wantEntity: "tercul:work:id:7",
wantList: "tercul:work:list:3:15",
wantQuery: "tercul:work:search:abc:2020:true",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantEntity, g.EntityKey(tt.entity, tt.id))
assert.Equal(t, tt.wantList, g.ListKey(tt.entity, tt.page, tt.pageSize))
assert.Equal(t, tt.wantQuery, g.QueryKey(tt.entity, tt.queryName, tt.params...))
})
}
}
func TestDefaultKeyGenerator_CustomPrefix(t *testing.T) {
g := NewDefaultKeyGenerator("mypfx:")
require.NotNil(t, g)
assert.Equal(t, "mypfx:book:id:1", g.EntityKey("book", 1))
assert.Equal(t, "mypfx:book:list:2:10", g.ListKey("book", 2, 10))
assert.Equal(t, "mypfx:book:find:tag:99", g.QueryKey("book", "find", "tag", 99))
}