add cache mock for organization

This commit is contained in:
Marco Ardizzone
2025-09-11 18:05:25 +02:00
parent eda9a0e2fe
commit dbd2411f10
3 changed files with 55 additions and 5 deletions

View File

@@ -27,22 +27,22 @@ type Organization struct {
Domains []*OrganizationDomain `json:"domains,omitempty" db:"-"` // domains need to be handled separately
}
type orgCacheIndex uint8
type OrgCacheIndex uint8
const (
orgCacheIndexUndefined orgCacheIndex = iota
orgCacheIndexUndefined OrgCacheIndex = iota
orgCacheIndexID
)
// Keys implements the [cache.Entry].
func (o *Organization) Keys(index orgCacheIndex) (key []string) {
func (o *Organization) Keys(index OrgCacheIndex) (key []string) {
if index == orgCacheIndexID {
return []string{o.ID}
}
return nil
}
var _ cache.Entry[orgCacheIndex, string] = (*Organization)(nil)
var _ cache.Entry[OrgCacheIndex, string] = (*Organization)(nil)
// OrgIdentifierCondition is used to help specify a single Organization,
// it will either be used as the organization ID or organization name,

View File

@@ -12,7 +12,7 @@ func TestOrganization_Keys(t *testing.T) {
tt := []struct {
name string
org *Organization
index orgCacheIndex
index OrgCacheIndex
wantKeys []string
}{
{

View File

@@ -0,0 +1,50 @@
package cachemock
import (
"context"
"github.com/zitadel/zitadel/backend/v3/domain"
"github.com/zitadel/zitadel/backend/v3/storage/cache"
)
type OrganizationCacheMock struct {
c map[string]*domain.Organization
}
func NewOrganizationCacheMock() *OrganizationCacheMock {
return &OrganizationCacheMock{
c: make(map[string]*domain.Organization),
}
}
// Delete implements cache.Cache.
func (o *OrganizationCacheMock) Delete(_ context.Context, _ domain.OrgCacheIndex, keys ...string) error {
for _, key := range keys {
delete(o.c, key)
}
return nil
}
// Get implements cache.Cache.
func (o *OrganizationCacheMock) Get(_ context.Context, _ domain.OrgCacheIndex, key string) (*domain.Organization, bool) {
res, ok := o.c[key]
return res, ok
}
// Invalidate implements cache.Cache.
func (o *OrganizationCacheMock) Invalidate(ctx context.Context, i domain.OrgCacheIndex, key ...string) error {
return o.Delete(ctx, i, key...)
}
// Set implements cache.Cache.
func (o *OrganizationCacheMock) Set(_ context.Context, organization *domain.Organization) {
o.c[organization.ID] = organization
}
// Truncate implements cache.Cache.
func (o *OrganizationCacheMock) Truncate(_ context.Context) error {
o.c = make(map[string]*domain.Organization)
return nil
}
var _ cache.Cache[domain.OrgCacheIndex, string, *domain.Organization] = (*OrganizationCacheMock)(nil)