zitadel/internal/query/projection/event_test.go
Tim Möhlmann 4eaa3163b6
feat(storage): generic cache interface (#8628)
# Which Problems Are Solved

We identified the need of caching.
Currently we have a number of places where we use different ways of
caching, like go maps or LRU.
We might also want shared chaches in the future, like Redis-based or in
special SQL tables.

# How the Problems Are Solved

Define a generic Cache interface which allows different implementations.

- A noop implementation is provided and enabled as.
- An implementation using go maps is provided
  - disabled in defaults.yaml
  - enabled in integration tests
- Authz middleware instance objects are cached using the interface.

# Additional Changes

- Enabled integration test command raceflag
- Fix a race condition in the limits integration test client
- Fix a number of flaky integration tests. (Because zitadel is super
fast now!) 🎸 🚀

# Additional Context

Related to https://github.com/zitadel/zitadel/issues/8648
2024-09-25 21:40:21 +02:00

95 lines
2.3 KiB
Go

package projection
import (
"database/sql"
"testing"
"time"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/eventstore/repository"
)
func testEvent(
eventType eventstore.EventType,
aggregateType eventstore.AggregateType,
data []byte,
) *repository.Event {
return timedTestEvent(eventType, aggregateType, data, time.Now())
}
func toSystemEvent(event *repository.Event) *repository.Event {
event.EditorUser = "SYSTEM"
return event
}
func timedTestEvent(
eventType eventstore.EventType,
aggregateType eventstore.AggregateType,
data []byte,
creationDate time.Time,
) *repository.Event {
return &repository.Event{
Seq: 15,
CreationDate: creationDate,
Typ: eventType,
AggregateType: aggregateType,
Data: data,
Version: "v1",
AggregateID: "agg-id",
ResourceOwner: sql.NullString{String: "ro-id", Valid: true},
InstanceID: "instance-id",
ID: "event-id",
EditorUser: "editor-user",
}
}
func baseEvent(*testing.T) eventstore.Event {
return &eventstore.BaseEvent{}
}
func getEvent(event *repository.Event, mapper func(eventstore.Event) (eventstore.Event, error)) func(t *testing.T) eventstore.Event {
return func(t *testing.T) eventstore.Event {
e, err := mapper(event)
if err != nil {
t.Fatalf("mapper failed: %v", err)
}
return e
}
}
type wantReduce struct {
aggregateType eventstore.AggregateType
sequence uint64
executer *testExecuter
err func(error) bool
}
func assertReduce(t *testing.T, stmt *handler.Statement, err error, projection string, want wantReduce) {
t.Helper()
if want.err == nil && err != nil {
t.Errorf("unexpected error of type %T: %v", err, err)
return
}
if want.err != nil && want.err(err) {
return
}
if stmt.Aggregate.Type != want.aggregateType {
t.Errorf("wrong aggregate type: want: %q got: %q", want.aggregateType, stmt.Aggregate.Type)
}
if stmt.Sequence != want.sequence {
t.Errorf("wrong sequence: want: %d got: %d", want.sequence, stmt.Sequence)
}
if stmt.Execute == nil {
want.executer.Validate(t)
return
}
err = stmt.Execute(want.executer, projection)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
want.executer.Validate(t)
}