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,19 +38,22 @@ 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)", {
expectedArgs: []interface{}{ 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)",
"agg-id", expectedArgs: []interface{}{
anyArg{}, "agg-id",
anyArg{}, anyArg{},
"ro-id", anyArg{},
uint64(15), "ro-id",
"name", uint64(15),
"name(){}", "name",
3 * time.Second, "name(){}",
true, 3 * time.Second,
domain.ActionStateActive, true,
domain.ActionStateActive,
},
},
}, },
}, },
}, },
@ -71,14 +74,17 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, name, script) = ($1, $2, $3, $4) WHERE (id = $5)",
anyArg{}, expectedArgs: []interface{}{
uint64(15), anyArg{},
"name2", uint64(15),
"name2(){}", "name2",
"agg-id", "name2(){}",
"agg-id",
},
},
}, },
}, },
}, },
@ -99,13 +105,16 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)",
anyArg{}, expectedArgs: []interface{}{
uint64(15), anyArg{},
domain.ActionStateInactive, uint64(15),
"agg-id", domain.ActionStateInactive,
"agg-id",
},
},
}, },
}, },
}, },
@ -126,13 +135,16 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "UPDATE zitadel.projections.actions SET (change_date, sequence, action_state) = ($1, $2, $3) WHERE (id = $4)",
anyArg{}, expectedArgs: []interface{}{
uint64(15), anyArg{},
domain.ActionStateActive, uint64(15),
"agg-id", domain.ActionStateActive,
"agg-id",
},
},
}, },
}, },
}, },
@ -153,10 +165,13 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "DELETE FROM zitadel.projections.actions WHERE (id = $1)",
"agg-id", expectedArgs: []interface{}{
"agg-id",
},
},
}, },
}, },
}, },

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,10 +85,13 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "DELETE FROM zitadel.projections.flows_triggers WHERE (flow_type = $1)",
domain.FlowTypeExternalAuthentication, expectedArgs: []interface{}{
domain.FlowTypeExternalAuthentication,
},
},
}, },
}, },
}, },

View File

@ -37,13 +37,16 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, primary_domain) = ($1, $2, $3) WHERE (id = $4)",
anyArg{}, expectedArgs: []interface{}{
uint64(15), anyArg{},
"domain.new", uint64(15),
"agg-id", "domain.new",
"agg-id",
},
},
}, },
}, },
}, },
@ -64,13 +67,16 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)",
anyArg{}, expectedArgs: []interface{}{
uint64(15), anyArg{},
domain.OrgStateActive, uint64(15),
"agg-id", domain.OrgStateActive,
"agg-id",
},
},
}, },
}, },
}, },
@ -91,13 +97,16 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, org_state) = ($1, $2, $3) WHERE (id = $4)",
anyArg{}, expectedArgs: []interface{}{
uint64(15), anyArg{},
domain.OrgStateInactive, uint64(15),
"agg-id", domain.OrgStateInactive,
"agg-id",
},
},
}, },
}, },
}, },
@ -118,13 +127,16 @@ 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)", {
expectedArgs: []interface{}{ expectedStmt: "UPDATE zitadel.projections.orgs SET (change_date, sequence, name) = ($1, $2, $3) WHERE (id = $4)",
anyArg{}, expectedArgs: []interface{}{
uint64(15), anyArg{},
"new name", uint64(15),
"agg-id", "new name",
"agg-id",
},
},
}, },
}, },
}, },
@ -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,16 +175,19 @@ 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)", {
expectedArgs: []interface{}{ 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)",
"agg-id", expectedArgs: []interface{}{
anyArg{}, "agg-id",
anyArg{}, anyArg{},
"ro-id", anyArg{},
uint64(15), "ro-id",
"name", uint64(15),
domain.OrgStateActive, "name",
domain.OrgStateActive,
},
},
}, },
}, },
}, },

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) {
} else { t.Errorf("wrong arg len expected: %d got: %d", len(execution.expectedArgs), len(execution.gottenArgs))
for i := 0; i < len(e.expectedArgs); i++ { } else {
if _, ok := e.expectedArgs[i].(anyArg); ok { for i := 0; i < len(execution.expectedArgs); i++ {
continue 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 execution.gottenStmt != execution.expectedStmt {
if e.gottenStmt != e.expectedStmt { t.Errorf("wrong stmt want:\n%s\ngot:\n%s", execution.expectedStmt, execution.gottenStmt)
t.Errorf("wrong stmt want:\n%s\ngot:\n%s", e.expectedStmt, e.gottenStmt) }
} }
} }