mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
chore: enable testExecuter to test multi statement (#2448)
* chore: enable testExecuter to test multi statement * remove todo
This commit is contained in:
parent
7579bf56f6
commit
244d945886
@ -38,7 +38,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "INSERT INTO zitadel.projections.actions (id, creation_date, change_date, resource_owner, sequence, name, script, timeout, allowed_to_fail, action_state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
@ -55,6 +56,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceActionChanged",
|
||||
args: args{
|
||||
@ -71,7 +74,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, name, script) = ($1, $2, $3, $4) WHERE (id = $5)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
@ -83,6 +87,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceActionDeactivated",
|
||||
args: args{
|
||||
@ -99,7 +105,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
@ -110,6 +117,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceActionReactivated",
|
||||
args: args{
|
||||
@ -126,7 +135,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
@ -137,6 +147,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceActionRemoved",
|
||||
args: args{
|
||||
@ -153,7 +165,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM zitadel.projections.actions WHERE (id = $1)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
@ -161,6 +174,8 @@ func TestActionProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
@ -21,37 +21,54 @@ func TestFlowProjection_reduces(t *testing.T) {
|
||||
reduce func(event eventstore.EventReader) (*handler.Statement, error)
|
||||
want wantReduce
|
||||
}{
|
||||
//TODO: multi stmt tests
|
||||
//{
|
||||
// name: "reduceTriggerActionsSetEventType",
|
||||
// args: args{
|
||||
// event: getEvent(testEvent(
|
||||
// repository.EventType(org.TriggerActionsSetEventType),
|
||||
// org.AggregateType,
|
||||
// []byte(`{"flowType": 1, "triggerType": 1, "actionIDs": ["id1", "id2"]}`),
|
||||
// ), org.TriggerActionsSetEventMapper),
|
||||
// },
|
||||
// reduce: (&FlowProjection{}).reduceTriggerActionsSetEventType,
|
||||
// want: wantReduce{
|
||||
// projection: FlowTriggerTable,
|
||||
// aggregateType: eventstore.AggregateType("org"),
|
||||
// sequence: 15,
|
||||
// previousSequence: 10,
|
||||
// executer: &testExecuter{
|
||||
// shouldExec: true,
|
||||
// expectedStmt: "DELETE FROM zitadel.projections.actions WHERE (flow_type, trigger_type) = ($1, $2); INSERT INTO zitadel.projections.actions (resource_owner, flow_type, trigger_type, action_id, trigger_sequence) = ($3, $1, $2, $4, $5); INSERT INTO zitadel.projections.actions (resource_owner, flow_type, trigger_type, action_id, trigger_sequence) = ($3, $1, $2, $6, $7)",
|
||||
// expectedArgs: []interface{}{
|
||||
// domain.FlowTypeExternalAuthentication,
|
||||
// domain.TriggerTypePostAuthentication,
|
||||
// "ro-id",
|
||||
// "id1",
|
||||
// 0,
|
||||
// "id2",
|
||||
// 1,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
//},
|
||||
{
|
||||
name: "reduceTriggerActionsSetEventType",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
repository.EventType(org.TriggerActionsSetEventType),
|
||||
org.AggregateType,
|
||||
[]byte(`{"flowType": 1, "triggerType": 1, "actionIDs": ["id1", "id2"]}`),
|
||||
), org.TriggerActionsSetEventMapper),
|
||||
},
|
||||
reduce: (&FlowProjection{}).reduceTriggerActionsSetEventType,
|
||||
want: wantReduce{
|
||||
projection: FlowTriggerTable,
|
||||
aggregateType: eventstore.AggregateType("org"),
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1) AND (trigger_type = $2)",
|
||||
expectedArgs: []interface{}{
|
||||
domain.FlowTypeExternalAuthentication,
|
||||
domain.TriggerTypePostAuthentication,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "INSERT INTO zitadel.projections.flows_triggers (resource_owner, flow_type, trigger_type, action_id, trigger_sequence) VALUES ($1, $2, $3, $4, $5)",
|
||||
expectedArgs: []interface{}{
|
||||
"ro-id",
|
||||
domain.FlowTypeExternalAuthentication,
|
||||
domain.TriggerTypePostAuthentication,
|
||||
"id1",
|
||||
0,
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedStmt: "INSERT INTO zitadel.projections.flows_triggers (resource_owner, flow_type, trigger_type, action_id, trigger_sequence) VALUES ($1, $2, $3, $4, $5)",
|
||||
expectedArgs: []interface{}{
|
||||
"ro-id",
|
||||
domain.FlowTypeExternalAuthentication,
|
||||
domain.TriggerTypePostAuthentication,
|
||||
"id2",
|
||||
1,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceFlowClearedEventType",
|
||||
args: args{
|
||||
@ -68,7 +85,8 @@ func TestFlowProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1)",
|
||||
expectedArgs: []interface{}{
|
||||
domain.FlowTypeExternalAuthentication,
|
||||
@ -76,6 +94,8 @@ func TestFlowProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
@ -37,7 +37,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, primary_domain) = ($1, $2, $3) WHERE (id = $4)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
@ -48,6 +49,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOrgReactivated",
|
||||
args: args{
|
||||
@ -64,7 +67,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
@ -75,6 +79,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOrgDeactivated",
|
||||
args: args{
|
||||
@ -91,7 +97,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
@ -102,6 +109,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOrgChanged",
|
||||
args: args{
|
||||
@ -118,7 +127,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, name) = ($1, $2, $3) WHERE (id = $4)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
@ -129,6 +139,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOrgChanged no changes",
|
||||
args: args{
|
||||
@ -144,9 +156,7 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
aggregateType: eventstore.AggregateType("org"),
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: false,
|
||||
},
|
||||
executer: &testExecuter{},
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -165,7 +175,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
shouldExec: true,
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "INSERT INTO zitadel.projections.orgs (id, creation_date, change_date, resource_owner, sequence, name, org_state) VALUES ($1, $2, $3, $4, $5, $6, $7)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
@ -179,6 +190,8 @@ func TestOrgProjection_reduces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
@ -3,46 +3,56 @@ package projection
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
)
|
||||
|
||||
type testExecuter struct {
|
||||
execIdx int
|
||||
executions []execution
|
||||
}
|
||||
|
||||
type execution struct {
|
||||
expectedStmt string
|
||||
gottenStmt string
|
||||
shouldExec bool
|
||||
|
||||
expectedArgs []interface{}
|
||||
gottenArgs []interface{}
|
||||
gotExecuted bool
|
||||
}
|
||||
|
||||
type anyArg struct{}
|
||||
|
||||
func (e *testExecuter) Exec(stmt string, args ...interface{}) (sql.Result, error) {
|
||||
e.gottenStmt = stmt
|
||||
e.gottenArgs = args
|
||||
e.gotExecuted = true
|
||||
if e.execIdx >= len(e.executions) {
|
||||
return nil, errors.ThrowInternal(nil, "PROJE-8TNoE", "too many executions")
|
||||
}
|
||||
e.executions[e.execIdx].gottenArgs = args
|
||||
e.executions[e.execIdx].gottenStmt = stmt
|
||||
e.execIdx++
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (e *testExecuter) Validate(t *testing.T) {
|
||||
t.Helper()
|
||||
if e.shouldExec != e.gotExecuted {
|
||||
t.Error("expected to be executed")
|
||||
if e.execIdx != len(e.executions) {
|
||||
t.Errorf("not all expected execs executed. got: %d, want: %d", e.execIdx, len(e.executions))
|
||||
return
|
||||
}
|
||||
if len(e.gottenArgs) != len(e.expectedArgs) {
|
||||
t.Errorf("wrong arg len expected: %d got: %d", len(e.expectedArgs), len(e.gottenArgs))
|
||||
for _, execution := range e.executions {
|
||||
if len(execution.gottenArgs) != len(execution.expectedArgs) {
|
||||
t.Errorf("wrong arg len expected: %d got: %d", len(execution.expectedArgs), len(execution.gottenArgs))
|
||||
} else {
|
||||
for i := 0; i < len(e.expectedArgs); i++ {
|
||||
if _, ok := e.expectedArgs[i].(anyArg); ok {
|
||||
for i := 0; i < len(execution.expectedArgs); i++ {
|
||||
if _, ok := execution.expectedArgs[i].(anyArg); ok {
|
||||
continue
|
||||
}
|
||||
if e.expectedArgs[i] != e.gottenArgs[i] {
|
||||
t.Errorf("wrong argument at index %d: got: %v want: %v", i, e.gottenArgs[i], e.expectedArgs[i])
|
||||
if execution.expectedArgs[i] != execution.gottenArgs[i] {
|
||||
t.Errorf("wrong argument at index %d: got: %v want: %v", i, execution.gottenArgs[i], execution.expectedArgs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
if e.gottenStmt != e.expectedStmt {
|
||||
t.Errorf("wrong stmt want:\n%s\ngot:\n%s", e.expectedStmt, e.gottenStmt)
|
||||
if execution.gottenStmt != execution.expectedStmt {
|
||||
t.Errorf("wrong stmt want:\n%s\ngot:\n%s", execution.expectedStmt, execution.gottenStmt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user