chore: enable testExecuter to test multi statement (#2448)

* chore: enable testExecuter to test multi statement

* remove todo
This commit is contained in:
Livio Amstutz 2021-09-30 11:26:29 +02:00 committed by GitHub
parent 7579bf56f6
commit 244d945886
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 192 additions and 134 deletions

View File

@ -38,7 +38,8 @@ func TestActionProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ 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)", 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{}{ expectedArgs: []interface{}{
"agg-id", "agg-id",
@ -55,6 +56,8 @@ func TestActionProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceActionChanged", name: "reduceActionChanged",
args: args{ args: args{
@ -71,7 +74,8 @@ func TestActionProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, name, script) = ($1, $2, $3, $4) WHERE (id = $5)", expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, name, script) = ($1, $2, $3, $4) WHERE (id = $5)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
anyArg{}, anyArg{},
@ -83,6 +87,8 @@ func TestActionProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceActionDeactivated", name: "reduceActionDeactivated",
args: args{ args: args{
@ -99,7 +105,8 @@ func TestActionProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)", expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
anyArg{}, anyArg{},
@ -110,6 +117,8 @@ func TestActionProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceActionReactivated", name: "reduceActionReactivated",
args: args{ args: args{
@ -126,7 +135,8 @@ func TestActionProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)", expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
anyArg{}, anyArg{},
@ -137,6 +147,8 @@ func TestActionProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceActionRemoved", name: "reduceActionRemoved",
args: args{ args: args{
@ -153,7 +165,8 @@ func TestActionProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "DELETE FROM zitadel.projections.actions WHERE (id = $1)", expectedStmt: "DELETE FROM zitadel.projections.actions WHERE (id = $1)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
"agg-id", "agg-id",
@ -161,6 +174,8 @@ func TestActionProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -21,37 +21,54 @@ func TestFlowProjection_reduces(t *testing.T) {
reduce func(event eventstore.EventReader) (*handler.Statement, error) reduce func(event eventstore.EventReader) (*handler.Statement, error)
want wantReduce want wantReduce
}{ }{
//TODO: multi stmt tests {
//{ name: "reduceTriggerActionsSetEventType",
// name: "reduceTriggerActionsSetEventType", args: args{
// args: args{ event: getEvent(testEvent(
// event: getEvent(testEvent( repository.EventType(org.TriggerActionsSetEventType),
// repository.EventType(org.TriggerActionsSetEventType), org.AggregateType,
// org.AggregateType, []byte(`{"flowType": 1, "triggerType": 1, "actionIDs": ["id1", "id2"]}`),
// []byte(`{"flowType": 1, "triggerType": 1, "actionIDs": ["id1", "id2"]}`), ), org.TriggerActionsSetEventMapper),
// ), org.TriggerActionsSetEventMapper), },
// }, reduce: (&FlowProjection{}).reduceTriggerActionsSetEventType,
// reduce: (&FlowProjection{}).reduceTriggerActionsSetEventType, want: wantReduce{
// want: wantReduce{ projection: FlowTriggerTable,
// projection: FlowTriggerTable, aggregateType: eventstore.AggregateType("org"),
// aggregateType: eventstore.AggregateType("org"), sequence: 15,
// sequence: 15, previousSequence: 10,
// previousSequence: 10, executer: &testExecuter{
// executer: &testExecuter{ executions: []execution{
// 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)", expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1) AND (trigger_type = $2)",
// expectedArgs: []interface{}{ expectedArgs: []interface{}{
// domain.FlowTypeExternalAuthentication, domain.FlowTypeExternalAuthentication,
// domain.TriggerTypePostAuthentication, domain.TriggerTypePostAuthentication,
// "ro-id", },
// "id1", },
// 0, {
// "id2", expectedStmt: "INSERT INTO zitadel.projections.flows_triggers (resource_owner, flow_type, trigger_type, action_id, trigger_sequence) VALUES ($1, $2, $3, $4, $5)",
// 1, 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", name: "reduceFlowClearedEventType",
args: args{ args: args{
@ -68,7 +85,8 @@ func TestFlowProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1)", expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
domain.FlowTypeExternalAuthentication, domain.FlowTypeExternalAuthentication,
@ -76,6 +94,8 @@ func TestFlowProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -37,7 +37,8 @@ func TestOrgProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, primary_domain) = ($1, $2, $3) WHERE (id = $4)", expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, primary_domain) = ($1, $2, $3) WHERE (id = $4)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
anyArg{}, anyArg{},
@ -48,6 +49,8 @@ func TestOrgProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceOrgReactivated", name: "reduceOrgReactivated",
args: args{ args: args{
@ -64,7 +67,8 @@ func TestOrgProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)", expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
anyArg{}, anyArg{},
@ -75,6 +79,8 @@ func TestOrgProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceOrgDeactivated", name: "reduceOrgDeactivated",
args: args{ args: args{
@ -91,7 +97,8 @@ func TestOrgProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)", expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
anyArg{}, anyArg{},
@ -102,6 +109,8 @@ func TestOrgProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceOrgChanged", name: "reduceOrgChanged",
args: args{ args: args{
@ -118,7 +127,8 @@ func TestOrgProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{
shouldExec: true, executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, name) = ($1, $2, $3) WHERE (id = $4)", expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, name) = ($1, $2, $3) WHERE (id = $4)",
expectedArgs: []interface{}{ expectedArgs: []interface{}{
anyArg{}, anyArg{},
@ -129,6 +139,8 @@ func TestOrgProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
{ {
name: "reduceOrgChanged no changes", name: "reduceOrgChanged no changes",
args: args{ args: args{
@ -144,9 +156,7 @@ func TestOrgProjection_reduces(t *testing.T) {
aggregateType: eventstore.AggregateType("org"), aggregateType: eventstore.AggregateType("org"),
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ executer: &testExecuter{},
shouldExec: false,
},
}, },
}, },
{ {
@ -165,7 +175,8 @@ func TestOrgProjection_reduces(t *testing.T) {
sequence: 15, sequence: 15,
previousSequence: 10, previousSequence: 10,
executer: &testExecuter{ 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)", 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{}{ expectedArgs: []interface{}{
"agg-id", "agg-id",
@ -179,6 +190,8 @@ func TestOrgProjection_reduces(t *testing.T) {
}, },
}, },
}, },
},
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@ -3,46 +3,56 @@ package projection
import ( import (
"database/sql" "database/sql"
"testing" "testing"
"github.com/caos/zitadel/internal/errors"
) )
type testExecuter struct { type testExecuter struct {
execIdx int
executions []execution
}
type execution struct {
expectedStmt string expectedStmt string
gottenStmt string gottenStmt string
shouldExec bool
expectedArgs []interface{} expectedArgs []interface{}
gottenArgs []interface{} gottenArgs []interface{}
gotExecuted bool
} }
type anyArg struct{} type anyArg struct{}
func (e *testExecuter) Exec(stmt string, args ...interface{}) (sql.Result, error) { func (e *testExecuter) Exec(stmt string, args ...interface{}) (sql.Result, error) {
e.gottenStmt = stmt if e.execIdx >= len(e.executions) {
e.gottenArgs = args return nil, errors.ThrowInternal(nil, "PROJE-8TNoE", "too many executions")
e.gotExecuted = true }
e.executions[e.execIdx].gottenArgs = args
e.executions[e.execIdx].gottenStmt = stmt
e.execIdx++
return nil, nil return nil, nil
} }
func (e *testExecuter) Validate(t *testing.T) { func (e *testExecuter) Validate(t *testing.T) {
t.Helper() t.Helper()
if e.shouldExec != e.gotExecuted { if e.execIdx != len(e.executions) {
t.Error("expected to be executed") t.Errorf("not all expected execs executed. got: %d, want: %d", e.execIdx, len(e.executions))
return return
} }
if len(e.gottenArgs) != len(e.expectedArgs) { for _, execution := range e.executions {
t.Errorf("wrong arg len expected: %d got: %d", len(e.expectedArgs), len(e.gottenArgs)) if len(execution.gottenArgs) != len(execution.expectedArgs) {
t.Errorf("wrong arg len expected: %d got: %d", len(execution.expectedArgs), len(execution.gottenArgs))
} else { } else {
for i := 0; i < len(e.expectedArgs); i++ { for i := 0; i < len(execution.expectedArgs); i++ {
if _, ok := e.expectedArgs[i].(anyArg); ok { if _, ok := execution.expectedArgs[i].(anyArg); ok {
continue continue
} }
if e.expectedArgs[i] != e.gottenArgs[i] { if execution.expectedArgs[i] != execution.gottenArgs[i] {
t.Errorf("wrong argument at index %d: got: %v want: %v", i, e.gottenArgs[i], e.expectedArgs[i]) t.Errorf("wrong argument at index %d: got: %v want: %v", i, execution.gottenArgs[i], execution.expectedArgs[i])
} }
} }
} }
if e.gottenStmt != e.expectedStmt { if execution.gottenStmt != execution.expectedStmt {
t.Errorf("wrong stmt want:\n%s\ngot:\n%s", e.expectedStmt, e.gottenStmt) t.Errorf("wrong stmt want:\n%s\ngot:\n%s", execution.expectedStmt, execution.gottenStmt)
}
} }
} }