mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-05 10:28:52 +00:00
chore: enable testExecuter to test multi statement (#2448)
* chore: enable testExecuter to test multi statement * remove todo
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user