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 09:26:29 +00:00
committed by GitHub
parent 7579bf56f6
commit 244d945886
4 changed files with 192 additions and 134 deletions
+29 -19
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)
}
}
}