97 lines
3.4 KiB
Go
97 lines
3.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/administration"
|
|
)
|
|
|
|
func TestAdministrationListAccountsUsesExplicitColumnsAndParameterizedFilters(t *testing.T) {
|
|
rows := &identityRows{}
|
|
querier := &identityQuerier{rows: rows}
|
|
db := NewDatabase(Config{Backend: BackendPostgres}, querier)
|
|
_, err := db.ListAccounts(context.Background(), administration.AccountFilters{OrganizationID: "org-1", Role: administration.RoleUser, IncludeDisabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if querier.sql != ListAdministrationAccountsSQL {
|
|
t.Fatalf("SQL=%q", querier.sql)
|
|
}
|
|
if !reflect.DeepEqual(querier.args, []any{"org-1", administration.RoleUser, true}) {
|
|
t.Fatalf("args=%#v", querier.args)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccountArchivesAllOwnedDataAndIdentityInOneTransaction(t *testing.T) {
|
|
tx := &administrationTransaction{}
|
|
db := NewDatabase(Config{Backend: BackendPostgres}, &administrationPool{tx: tx})
|
|
if err := db.DeleteAccount(context.Background(), "user-1", "archive:org-1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantSQL := []string{ArchiveAssetsSQL, ArchiveGenerationJobsSQL, ArchiveProjectsSQL, ArchiveImageTemplatesSQL, DeleteAdministrationAccountSQL}
|
|
if !reflect.DeepEqual(tx.sql, wantSQL) {
|
|
t.Fatalf("SQL sequence=%#v want %#v", tx.sql, wantSQL)
|
|
}
|
|
for _, args := range tx.args {
|
|
if !reflect.DeepEqual(args, []any{"user-1", "archive:org-1"}) && !reflect.DeepEqual(args, []any{"user-1"}) {
|
|
t.Fatalf("args=%#v", args)
|
|
}
|
|
}
|
|
if tx.commits != 1 || tx.rollbacks != 0 {
|
|
t.Fatalf("commits=%d rollbacks=%d", tx.commits, tx.rollbacks)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAccountRollsBackArchiveTransactionFailure(t *testing.T) {
|
|
tx := &administrationTransaction{failAt: 3, err: errors.New("projects failed")}
|
|
db := NewDatabase(Config{Backend: BackendPostgres}, &administrationPool{tx: tx})
|
|
err := db.DeleteAccount(context.Background(), "user-1", "archive:org-1")
|
|
if !errors.Is(err, tx.err) || tx.commits != 0 || tx.rollbacks != 1 {
|
|
t.Fatalf("err=%v commits=%d rollbacks=%d", err, tx.commits, tx.rollbacks)
|
|
}
|
|
}
|
|
|
|
func TestAdministrationStoreFailsClosedWithoutPostgres(t *testing.T) {
|
|
db := NewDatabase(Config{Backend: BackendLocal}, &identityQuerier{})
|
|
if _, err := db.ListOrganizations(context.Background(), true); err == nil {
|
|
t.Fatal("ListOrganizations error=nil")
|
|
}
|
|
if err := db.DeleteAccount(context.Background(), "u", "a"); err == nil {
|
|
t.Fatal("DeleteAccount error=nil")
|
|
}
|
|
}
|
|
|
|
var _ administration.Store = (*Database)(nil)
|
|
|
|
type administrationPool struct{ tx *administrationTransaction }
|
|
|
|
func (p *administrationPool) Query(context.Context, string, ...any) (Rows, error) {
|
|
return nil, errors.New("outside transaction")
|
|
}
|
|
func (p *administrationPool) Begin(context.Context) (Transaction, error) { return p.tx, nil }
|
|
|
|
type administrationTransaction struct {
|
|
sql []string
|
|
args [][]any
|
|
failAt int
|
|
err error
|
|
commits, rollbacks int
|
|
}
|
|
|
|
func (t *administrationTransaction) Query(context.Context, string, ...any) (Rows, error) {
|
|
return nil, errors.New("unexpected query")
|
|
}
|
|
func (t *administrationTransaction) Exec(_ context.Context, query string, args ...any) error {
|
|
t.sql = append(t.sql, query)
|
|
t.args = append(t.args, args)
|
|
if t.failAt == len(t.sql) {
|
|
return t.err
|
|
}
|
|
return nil
|
|
}
|
|
func (t *administrationTransaction) Commit(context.Context) error { t.commits++; return nil }
|
|
func (t *administrationTransaction) Rollback(context.Context) error { t.rollbacks++; return nil }
|