Files
zitadel/backend/v3/storage/database/change.go
Silvan f8309157be refactor: improve testability for domain (#10731)
* Abstracting the `OrganizationRepository` in `CommandOpts` to allow for
mock implementations during testing.
* Generating mocks for `OrganizationRepository` and
`OrganizationDomainRepository` using `mockgen`.
* Updating the `UpdateOrgCommand` tests to use the new mock
repositories, which simplifies the test setup and removes the need for
`dbmock`.
* Enhancing the `database.Change`, `database.Column`, and
`database.Condition` interfaces to implement `gomock.Matcher`, enabling
more effective use of gomock for matching database operations in tests.
* Introducing a `noopdb` package that provides a no-operation database
client for testing purposes.
2025-09-16 18:12:47 +02:00

100 lines
1.8 KiB
Go

package database
import (
"reflect"
"go.uber.org/mock/gomock"
)
// Change represents a change to a column in a database table.
// Its written in the SET clause of an UPDATE statement.
type Change interface {
gomock.Matcher
Write(builder *StatementBuilder)
}
type change[V Value] struct {
column Column
value V
}
// Matches implements [gomock.Matcher].
func (c *change[V]) Matches(x any) bool {
toMatch, ok := x.(*change[V])
if !ok {
return false
}
return c.column == toMatch.column && reflect.DeepEqual(c.value, toMatch.value)
}
// String implements [gomock.Matcher].
func (c *change[V]) String() string {
return "database.change"
}
var (
_ Change = (*change[string])(nil)
_ gomock.Matcher = (*change[string])(nil)
)
func NewChange[V Value](col Column, value V) Change {
return &change[V]{
column: col,
value: value,
}
}
func NewChangePtr[V Value](col Column, value *V) Change {
if value == nil {
return NewChange(col, NullInstruction)
}
return NewChange(col, *value)
}
// Write implements [Change].
func (c change[V]) Write(builder *StatementBuilder) {
c.column.WriteUnqualified(builder)
builder.WriteString(" = ")
builder.WriteArg(c.value)
}
type Changes []Change
func NewChanges(cols ...Change) Change {
return Changes(cols)
}
// Write implements [Change].
func (m Changes) Write(builder *StatementBuilder) {
for i, col := range m {
if i > 0 {
builder.WriteString(", ")
}
col.Write(builder)
}
}
// Matches implements [gomock.Matcher].
func (c Changes) Matches(x any) bool {
toMatch, ok := x.(*Changes)
if !ok {
return false
}
if len(c) != len(*toMatch) {
return false
}
for i := range c {
if !c[i].Matches((*toMatch)[i]) {
return false
}
}
return true
}
// String implements [gomock.Matcher].
func (c Changes) String() string {
return "database.Changes"
}
var _ Change = Changes(nil)