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,
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",
},
},
},
},
},

View File

@ -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,
},
},
},
},
},

View File

@ -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,
},
},
},
},
},

View File

@ -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)
}
}
}