mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved This PR provides an example for testing DB reduction to relational tables without exploiting integration tests. # How the Problems Are Solved - Set up a disposable embedded Postgres DB used to store the results of the calls to the reducers and verify such results - Add utility method to return transactions from the existing DB pool. These transactions can be used by the single test cases to insert and read data from DB - Add a test to verify that the handler contains the expected reducers (checking also that they are on the right aggregate type) - Add tests to verify the single reduce functions. A parent test creates the required objects to satisfy the foreign key constraints. # Additional Changes - Typo fixed in `postgres.DecodeConfig()` - Discard unneeded `projectionName` parameter for `Executor` function # Additional Context - Closes #11430
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
// Pool is a connection pool. e.g. pgxpool
|
|
type Pool interface {
|
|
Beginner
|
|
QueryExecutor
|
|
Migrator
|
|
|
|
Acquire(ctx context.Context) (Connection, error)
|
|
Close(ctx context.Context) error
|
|
|
|
Ping(ctx context.Context) error
|
|
}
|
|
|
|
type PoolTest interface {
|
|
Pool
|
|
// MigrateTest is the same as [Migrator] but executes the migrations multiple times instead of only once.
|
|
MigrateTest(ctx context.Context) error
|
|
// RawDB returns an *[sql.DB] handle backed by the same database as the [PoolTest].
|
|
// The returned handle is intended for use in tests, and the caller is responsible for closing it when done.
|
|
RawDB() *sql.DB
|
|
}
|
|
|
|
// Connection is a single database connection which can be released back to the pool.
|
|
type Connection interface {
|
|
Beginner
|
|
QueryExecutor
|
|
Migrator
|
|
|
|
Release(ctx context.Context) error
|
|
|
|
Ping(ctx context.Context) error
|
|
}
|
|
|
|
// Querier is a database client that can execute queries and return rows.
|
|
type Querier interface {
|
|
Query(ctx context.Context, stmt string, args ...any) (Rows, error)
|
|
QueryRow(ctx context.Context, stmt string, args ...any) Row
|
|
}
|
|
|
|
// Executor is a database client that can execute statements.
|
|
// It returns the number of rows affected or an error
|
|
type Executor interface {
|
|
Exec(ctx context.Context, stmt string, args ...any) (int64, error)
|
|
}
|
|
|
|
// QueryExecutor is a database client that can execute queries and statements.
|
|
type QueryExecutor interface {
|
|
Querier
|
|
Executor
|
|
}
|
|
|
|
// Scanner scans a single row of data into the destination.
|
|
type Scanner interface {
|
|
Scan(dest ...any) error
|
|
}
|
|
|
|
// Row is an abstraction of sql.Row.
|
|
type Row interface {
|
|
Scanner
|
|
}
|
|
|
|
// Rows is an abstraction of sql.Rows.
|
|
type Rows interface {
|
|
Scanner
|
|
Next() bool
|
|
Close() error
|
|
Err() error
|
|
}
|
|
|
|
type CollectableRows interface {
|
|
// Collect collects all rows and scans them into dest.
|
|
// dest must be a pointer to a slice of pointer to structs
|
|
// e.g. *[]*MyStruct
|
|
// Rows are closed after this call.
|
|
Collect(dest any) error
|
|
// CollectFirst collects the first row and scans it into dest.
|
|
// dest must be a pointer to a struct
|
|
// e.g. *MyStruct{}
|
|
// Rows are closed after this call.
|
|
CollectFirst(dest any) error
|
|
// CollectExactlyOneRow collects exactly one row and scans it into dest.
|
|
// e.g. *MyStruct{}
|
|
// Rows are closed after this call.
|
|
CollectExactlyOneRow(dest any) error
|
|
}
|