2023-07-06 06:38:13 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-10-28 08:29:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-07-06 06:38:13 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2023-07-06 06:38:13 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/milestone"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-07-06 06:38:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMilestonesProjection_reduces(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
event func(t *testing.T) eventstore.Event
|
|
|
|
}
|
|
|
|
now := time.Now()
|
2024-10-28 08:29:34 +00:00
|
|
|
date, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
|
|
|
|
require.NoError(t, err)
|
2023-07-06 06:38:13 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reduceInstanceAdded",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(timedTestEvent(
|
2024-10-28 08:29:34 +00:00
|
|
|
milestone.ReachedEventType,
|
|
|
|
milestone.AggregateType,
|
2024-11-11 11:28:27 +00:00
|
|
|
[]byte(`{"type": "InstanceCreated"}`),
|
2023-07-06 06:38:13 +00:00
|
|
|
now,
|
2024-10-28 08:29:34 +00:00
|
|
|
withVersion(milestone.AggregateVersion),
|
|
|
|
), milestone.ReachedEventMapper),
|
2023-07-06 06:38:13 +00:00
|
|
|
},
|
2024-10-28 08:29:34 +00:00
|
|
|
reduce: (&milestoneProjection{}).reduceReached,
|
2023-07-06 06:38:13 +00:00
|
|
|
want: wantReduce{
|
2024-10-28 08:29:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("milestone"),
|
2023-10-19 10:19:10 +00:00
|
|
|
sequence: 15,
|
2023-07-06 06:38:13 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2024-11-11 11:28:27 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.milestones3 (instance_id, type, reached_date) VALUES ($1, $2, $3)",
|
2023-07-06 06:38:13 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"instance-id",
|
|
|
|
milestone.InstanceCreated,
|
|
|
|
now,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-28 08:29:34 +00:00
|
|
|
name: "reduceInstanceAdded with reached date",
|
2023-07-06 06:38:13 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(timedTestEvent(
|
2024-10-28 08:29:34 +00:00
|
|
|
milestone.ReachedEventType,
|
|
|
|
milestone.AggregateType,
|
2024-11-11 11:28:27 +00:00
|
|
|
[]byte(`{"type": "InstanceCreated", "reachedDate":"2006-01-02T15:04:05Z"}`),
|
2024-06-12 04:49:14 +00:00
|
|
|
now,
|
2024-10-28 08:29:34 +00:00
|
|
|
withVersion(milestone.AggregateVersion),
|
|
|
|
), milestone.ReachedEventMapper),
|
2024-06-12 04:49:14 +00:00
|
|
|
},
|
2024-10-28 08:29:34 +00:00
|
|
|
reduce: (&milestoneProjection{}).reduceReached,
|
2024-06-12 04:49:14 +00:00
|
|
|
want: wantReduce{
|
2024-10-28 08:29:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("milestone"),
|
2024-06-12 04:49:14 +00:00
|
|
|
sequence: 15,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2024-11-11 11:28:27 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.milestones3 (instance_id, type, reached_date) VALUES ($1, $2, $3)",
|
2024-06-12 04:49:14 +00:00
|
|
|
expectedArgs: []interface{}{
|
2023-07-06 06:38:13 +00:00
|
|
|
"instance-id",
|
2024-10-28 08:29:34 +00:00
|
|
|
milestone.InstanceCreated,
|
|
|
|
date,
|
2023-07-06 06:38:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-28 08:29:34 +00:00
|
|
|
name: "reduceMilestonePushed normal milestone",
|
2023-07-06 06:38:13 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(timedTestEvent(
|
2024-10-28 08:29:34 +00:00
|
|
|
milestone.PushedEventType,
|
|
|
|
milestone.AggregateType,
|
2024-11-11 11:28:27 +00:00
|
|
|
[]byte(`{"type": "ProjectCreated"}`),
|
2023-07-06 06:38:13 +00:00
|
|
|
now,
|
2024-10-28 08:29:34 +00:00
|
|
|
withVersion(milestone.AggregateVersion),
|
|
|
|
), milestone.PushedEventMapper),
|
2023-07-06 06:38:13 +00:00
|
|
|
},
|
2024-10-28 08:29:34 +00:00
|
|
|
reduce: (&milestoneProjection{}).reducePushed,
|
2023-07-06 06:38:13 +00:00
|
|
|
want: wantReduce{
|
2024-10-28 08:29:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("milestone"),
|
2023-10-19 10:19:10 +00:00
|
|
|
sequence: 15,
|
2023-07-06 06:38:13 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2024-11-11 11:28:27 +00:00
|
|
|
expectedStmt: "UPDATE projections.milestones3 SET last_pushed_date = $1 WHERE (instance_id = $2) AND (type = $3)",
|
2023-07-06 06:38:13 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
now,
|
|
|
|
"instance-id",
|
2024-10-28 08:29:34 +00:00
|
|
|
milestone.ProjectCreated,
|
2023-07-06 06:38:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-10-28 08:29:34 +00:00
|
|
|
name: "reduceMilestonePushed normal milestone with pushed date",
|
2023-07-06 06:38:13 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(timedTestEvent(
|
2023-10-19 10:19:10 +00:00
|
|
|
milestone.PushedEventType,
|
2023-07-06 06:38:13 +00:00
|
|
|
milestone.AggregateType,
|
2024-11-11 11:28:27 +00:00
|
|
|
[]byte(`{"type": "ProjectCreated", "pushedDate":"2006-01-02T15:04:05Z"}`),
|
2023-07-06 06:38:13 +00:00
|
|
|
now,
|
2024-10-28 08:29:34 +00:00
|
|
|
withVersion(milestone.AggregateVersion),
|
2023-07-06 06:38:13 +00:00
|
|
|
), milestone.PushedEventMapper),
|
|
|
|
},
|
2024-10-28 08:29:34 +00:00
|
|
|
reduce: (&milestoneProjection{}).reducePushed,
|
2023-07-06 06:38:13 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: eventstore.AggregateType("milestone"),
|
|
|
|
sequence: 15,
|
2023-07-06 06:38:13 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2024-11-11 11:28:27 +00:00
|
|
|
expectedStmt: "UPDATE projections.milestones3 SET last_pushed_date = $1 WHERE (instance_id = $2) AND (type = $3)",
|
2023-07-06 06:38:13 +00:00
|
|
|
expectedArgs: []interface{}{
|
2024-10-28 08:29:34 +00:00
|
|
|
date,
|
2023-07-06 06:38:13 +00:00
|
|
|
"instance-id",
|
|
|
|
milestone.ProjectCreated,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceMilestonePushed instance deleted milestone",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2023-10-19 10:19:10 +00:00
|
|
|
milestone.PushedEventType,
|
2023-07-06 06:38:13 +00:00
|
|
|
milestone.AggregateType,
|
2024-11-11 11:28:27 +00:00
|
|
|
[]byte(`{"type": "InstanceDeleted"}`),
|
2024-10-28 08:29:34 +00:00
|
|
|
withVersion(milestone.AggregateVersion),
|
2023-07-06 06:38:13 +00:00
|
|
|
), milestone.PushedEventMapper),
|
|
|
|
},
|
2024-10-28 08:29:34 +00:00
|
|
|
reduce: (&milestoneProjection{}).reducePushed,
|
2023-07-06 06:38:13 +00:00
|
|
|
want: wantReduce{
|
2023-10-19 10:19:10 +00:00
|
|
|
aggregateType: eventstore.AggregateType("milestone"),
|
|
|
|
sequence: 15,
|
2023-07-06 06:38:13 +00:00
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2024-11-11 11:28:27 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.milestones3 WHERE (instance_id = $1)",
|
2023-07-06 06:38:13 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"instance-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
2023-12-08 14:30:55 +00:00
|
|
|
if !zerrors.IsErrorInvalidArgument(err) {
|
2023-07-06 06:38:13 +00:00
|
|
|
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
|
|
|
}
|
|
|
|
event = tt.args.event(t)
|
|
|
|
got, err = tt.reduce(event)
|
|
|
|
assertReduce(t, got, err, MilestonesProjectionTable, tt.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|