package organization import ( "context" "errors" "testing" "git.sendico.io/tech/sxchnge/bff/pkg/db/repository" "git.sendico.io/tech/sxchnge/bff/pkg/merrors" "go.mongodb.org/mongo-driver/v2/mongo" ) func TestDropSupersededProvisioningIndexesIgnoresMissingIndexes(t *testing.T) { repo := &dropIndexRepository{dropErrors: []error{ mongo.CommandError{Code: mongoIndexNotFoundCode}, mongo.CommandError{Code: mongoNamespaceNotFoundCode}, }} if err := dropSupersededProvisioningIndexes(context.Background(), repo); err != nil { t.Fatalf("missing superseded indexes must be ignored: %v", err) } if len(repo.dropped) != len(supersededProvisioningIndexes) { t.Fatalf("drop count mismatch: got=%d want=%d", len(repo.dropped), len(supersededProvisioningIndexes)) } } func TestDropSupersededProvisioningIndexesPreservesUnexpectedFailure(t *testing.T) { wantErr := merrors.ServiceUnavailable("MongoDB is unavailable") repo := &dropIndexRepository{dropErrors: []error{wantErr}} err := dropSupersededProvisioningIndexes(context.Background(), repo) if !errors.Is(err, wantErr) { t.Fatalf("unexpected drop failure must be preserved: got=%v want=%v", err, wantErr) } if len(repo.dropped) != 1 { t.Fatalf("cleanup must stop after an unexpected failure: dropped=%v", repo.dropped) } } type dropIndexRepository struct { repository.Repository dropErrors []error dropped []string } func (r *dropIndexRepository) DropIndex(_ context.Context, name string) error { r.dropped = append(r.dropped, name) if len(r.dropErrors) == 0 { return nil } err := r.dropErrors[0] r.dropErrors = r.dropErrors[1:] return err }