mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
ec03340b67
* fix some spelling errors
* client credential auth
* implementation of client auth
* improve error handling
* unit test command package
* unit test database package
* unit test query package
* cleanup unused tracing func
* fix integration tests
* errz to zerrors
* fix linting and import issues
* fix another linting error
* integration test with client secret
* Revert "integration test with client secret"
This reverts commit 0814ba522f
.
* add integration tests
* client credentials integration test
* resolve comments
* pin oidc v3.5.0
139 lines
2.9 KiB
Go
139 lines
2.9 KiB
Go
package mock
|
|
|
|
import (
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
)
|
|
|
|
type SQLMock struct {
|
|
DB *sql.DB
|
|
mock sqlmock.Sqlmock
|
|
}
|
|
|
|
type expectation func(m sqlmock.Sqlmock)
|
|
|
|
func NewSQLMock(t *testing.T, expectations ...expectation) *SQLMock {
|
|
db, mock, err := sqlmock.New(
|
|
sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual),
|
|
)
|
|
if err != nil {
|
|
t.Fatal("create mock failed", err)
|
|
}
|
|
|
|
for _, expectation := range expectations {
|
|
expectation(mock)
|
|
}
|
|
|
|
return &SQLMock{
|
|
DB: db,
|
|
mock: mock,
|
|
}
|
|
}
|
|
|
|
func (m *SQLMock) Assert(t *testing.T) {
|
|
t.Helper()
|
|
|
|
if err := m.mock.ExpectationsWereMet(); err != nil {
|
|
t.Errorf("expectations not met: %v", err)
|
|
}
|
|
|
|
m.DB.Close()
|
|
}
|
|
|
|
func ExpectBegin(err error) expectation {
|
|
return func(m sqlmock.Sqlmock) {
|
|
e := m.ExpectBegin()
|
|
if err != nil {
|
|
e.WillReturnError(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ExpectCommit(err error) expectation {
|
|
return func(m sqlmock.Sqlmock) {
|
|
e := m.ExpectCommit()
|
|
if err != nil {
|
|
e.WillReturnError(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
type ExecOpt func(e *sqlmock.ExpectedExec) *sqlmock.ExpectedExec
|
|
|
|
func WithExecArgs(args ...driver.Value) ExecOpt {
|
|
return func(e *sqlmock.ExpectedExec) *sqlmock.ExpectedExec {
|
|
return e.WithArgs(args...)
|
|
}
|
|
}
|
|
|
|
func WithExecErr(err error) ExecOpt {
|
|
return func(e *sqlmock.ExpectedExec) *sqlmock.ExpectedExec {
|
|
return e.WillReturnError(err)
|
|
}
|
|
}
|
|
|
|
func WithExecNoRowsAffected() ExecOpt {
|
|
return func(e *sqlmock.ExpectedExec) *sqlmock.ExpectedExec {
|
|
return e.WillReturnResult(driver.ResultNoRows)
|
|
}
|
|
}
|
|
|
|
func WithExecRowsAffected(affected driver.RowsAffected) ExecOpt {
|
|
return func(e *sqlmock.ExpectedExec) *sqlmock.ExpectedExec {
|
|
return e.WillReturnResult(affected)
|
|
}
|
|
}
|
|
|
|
func ExcpectExec(stmt string, opts ...ExecOpt) expectation {
|
|
return func(m sqlmock.Sqlmock) {
|
|
e := m.ExpectExec(stmt)
|
|
for _, opt := range opts {
|
|
e = opt(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
type QueryOpt func(e *sqlmock.ExpectedQuery) *sqlmock.ExpectedQuery
|
|
|
|
func WithQueryArgs(args ...driver.Value) QueryOpt {
|
|
return func(e *sqlmock.ExpectedQuery) *sqlmock.ExpectedQuery {
|
|
return e.WithArgs(args...)
|
|
}
|
|
}
|
|
|
|
func WithQueryErr(err error) QueryOpt {
|
|
return func(e *sqlmock.ExpectedQuery) *sqlmock.ExpectedQuery {
|
|
return e.WillReturnError(err)
|
|
}
|
|
}
|
|
|
|
func WithQueryResult(columns []string, rows [][]driver.Value) QueryOpt {
|
|
return func(e *sqlmock.ExpectedQuery) *sqlmock.ExpectedQuery {
|
|
mockedRows := sqlmock.NewRows(columns)
|
|
for _, row := range rows {
|
|
mockedRows = mockedRows.AddRow(row...)
|
|
}
|
|
return e.WillReturnRows(mockedRows)
|
|
}
|
|
}
|
|
|
|
func ExpectQuery(stmt string, opts ...QueryOpt) expectation {
|
|
return func(m sqlmock.Sqlmock) {
|
|
e := m.ExpectQuery(stmt)
|
|
for _, opt := range opts {
|
|
e = opt(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
type AnyType[T interface{}] struct{}
|
|
|
|
// Match satisfies sqlmock.Argument interface
|
|
func (a AnyType[T]) Match(v driver.Value) bool {
|
|
return reflect.TypeOf(new(T)).Elem().Kind().String() == reflect.TypeOf(v).Kind().String()
|
|
}
|