From 244d945886c6c115ac39b51c49a60b2079a9bf4f Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Thu, 30 Sep 2021 11:26:29 +0200 Subject: [PATCH] chore: enable testExecuter to test multi statement (#2448) * chore: enable testExecuter to test multi statement * remove todo --- internal/query/projection/action_test.go | 93 ++++++++++++--------- internal/query/projection/flow_test.go | 90 ++++++++++++-------- internal/query/projection/org_test.go | 95 ++++++++++++---------- internal/query/projection/test_executer.go | 48 ++++++----- 4 files changed, 192 insertions(+), 134 deletions(-) diff --git a/internal/query/projection/action_test.go b/internal/query/projection/action_test.go index abc20f8c4d..2c535a66dc 100644 --- a/internal/query/projection/action_test.go +++ b/internal/query/projection/action_test.go @@ -38,19 +38,22 @@ func TestActionProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - 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", - anyArg{}, - anyArg{}, - "ro-id", - uint64(15), - "name", - "name(){}", - 3 * time.Second, - true, - domain.ActionStateActive, + 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", + anyArg{}, + anyArg{}, + "ro-id", + uint64(15), + "name", + "name(){}", + 3 * time.Second, + true, + domain.ActionStateActive, + }, + }, }, }, }, @@ -71,14 +74,17 @@ func TestActionProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, name, script) = ($1, $2, $3, $4) WHERE (id = $5)", - expectedArgs: []interface{}{ - anyArg{}, - uint64(15), - "name2", - "name2(){}", - "agg-id", + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, name, script) = ($1, $2, $3, $4) WHERE (id = $5)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + "name2", + "name2(){}", + "agg-id", + }, + }, }, }, }, @@ -99,13 +105,16 @@ func TestActionProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)", - expectedArgs: []interface{}{ - anyArg{}, - uint64(15), - domain.ActionStateInactive, - "agg-id", + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + domain.ActionStateInactive, + "agg-id", + }, + }, }, }, }, @@ -126,13 +135,16 @@ func TestActionProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)", - expectedArgs: []interface{}{ - anyArg{}, - uint64(15), - domain.ActionStateActive, - "agg-id", + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + domain.ActionStateActive, + "agg-id", + }, + }, }, }, }, @@ -153,10 +165,13 @@ func TestActionProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "DELETE FROM zitadel.projections.actions WHERE (id = $1)", - expectedArgs: []interface{}{ - "agg-id", + executions: []execution{ + { + expectedStmt: "DELETE FROM zitadel.projections.actions WHERE (id = $1)", + expectedArgs: []interface{}{ + "agg-id", + }, + }, }, }, }, diff --git a/internal/query/projection/flow_test.go b/internal/query/projection/flow_test.go index 0ffd34efbf..562e4f2097 100644 --- a/internal/query/projection/flow_test.go +++ b/internal/query/projection/flow_test.go @@ -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,10 +85,13 @@ func TestFlowProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1)", - expectedArgs: []interface{}{ - domain.FlowTypeExternalAuthentication, + executions: []execution{ + { + expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1)", + expectedArgs: []interface{}{ + domain.FlowTypeExternalAuthentication, + }, + }, }, }, }, diff --git a/internal/query/projection/org_test.go b/internal/query/projection/org_test.go index 9376613308..698a7ea41a 100644 --- a/internal/query/projection/org_test.go +++ b/internal/query/projection/org_test.go @@ -37,13 +37,16 @@ func TestOrgProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, primary_domain) = ($1, $2, $3) WHERE (id = $4)", - expectedArgs: []interface{}{ - anyArg{}, - uint64(15), - "domain.new", - "agg-id", + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, primary_domain) = ($1, $2, $3) WHERE (id = $4)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + "domain.new", + "agg-id", + }, + }, }, }, }, @@ -64,13 +67,16 @@ func TestOrgProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)", - expectedArgs: []interface{}{ - anyArg{}, - uint64(15), - domain.OrgStateActive, - "agg-id", + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + domain.OrgStateActive, + "agg-id", + }, + }, }, }, }, @@ -91,13 +97,16 @@ func TestOrgProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)", - expectedArgs: []interface{}{ - anyArg{}, - uint64(15), - domain.OrgStateInactive, - "agg-id", + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + domain.OrgStateInactive, + "agg-id", + }, + }, }, }, }, @@ -118,13 +127,16 @@ func TestOrgProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, name) = ($1, $2, $3) WHERE (id = $4)", - expectedArgs: []interface{}{ - anyArg{}, - uint64(15), - "new name", - "agg-id", + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, name) = ($1, $2, $3) WHERE (id = $4)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + "new name", + "agg-id", + }, + }, }, }, }, @@ -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,16 +175,19 @@ func TestOrgProjection_reduces(t *testing.T) { sequence: 15, previousSequence: 10, executer: &testExecuter{ - shouldExec: true, - 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", - anyArg{}, - anyArg{}, - "ro-id", - uint64(15), - "name", - domain.OrgStateActive, + 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", + anyArg{}, + anyArg{}, + "ro-id", + uint64(15), + "name", + domain.OrgStateActive, + }, + }, }, }, }, diff --git a/internal/query/projection/test_executer.go b/internal/query/projection/test_executer.go index dd201e6c15..e22528e7ab 100644 --- a/internal/query/projection/test_executer.go +++ b/internal/query/projection/test_executer.go @@ -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)) - } else { - for i := 0; i < len(e.expectedArgs); i++ { - if _, ok := e.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]) + 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(execution.expectedArgs); i++ { + if _, ok := execution.expectedArgs[i].(anyArg); ok { + continue + } + 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) + } } }