Move audit subscribe to client, Fix tests

This commit is contained in:
George Suntres
2026-04-24 15:51:08 -04:00
parent d673df0ca1
commit 66ba2b8874
8 changed files with 128 additions and 30 deletions

View File

@@ -10,7 +10,6 @@ import (
"go.mongodb.org/mongo-driver/v2/bson"
"git.gsuntres.com/general/commons"
"git.gsuntres.com/general/events"
)
func TestDeleteOne(t *testing.T) {
@@ -124,21 +123,23 @@ func TestDeleteOne_WithAudit(t *testing.T) {
onAudit_before any
onAudit_context any
)
events.Subscribe(func(audit *AuditResult) error {
cancel := client.Subscribe(func(audit *AuditResult) error {
onAudit_calls++
onAudit_before = audit.Before
onAudit_context = audit.Context
return nil
})
ctx := context.Background()
ctx = context.WithValue(ctx, "account", "xxxxxx")
ctx = context.WithValue(ctx, "store", "str_4321")
err = client.DeleteOne(ctx, "mydb", "mycollection", bson.M{"_id": o["_id"].(string)})
if err != nil { t.Fatalf("Failed to deleteOne %#v", err) }
cancel()
// raw query
var results bson.M
filter := bson.M{ "name": "MyName" }
@@ -149,7 +150,7 @@ func TestDeleteOne_WithAudit(t *testing.T) {
if results != nil {
t.Fatalf("Should have no results not %v", results)
}
if onAudit_calls != 1 {
t.Fatalf("ondelete should have been called once, not %d", onAudit_calls)
}
@@ -173,7 +174,7 @@ func TestDeleteOne_WithAudit(t *testing.T) {
func TestDeleteOne_WithAuditButIgnored(t *testing.T) {
client := GetMongoClient()
client.IgnoredAudit = []string{"mycollection"}
client.IgnoreAudit = []string{"mycollection"}
data := map[string]any {
"_id": "su_123458",
@@ -189,22 +190,23 @@ func TestDeleteOne_WithAuditButIgnored(t *testing.T) {
var (
onAudit_calls int
onAudit_before any
onAudit_context any
)
events.Subscribe(func(audit *AuditResult) error {
cancel := client.Subscribe(func(audit *AuditResult) error {
onAudit_calls++
onAudit_before = audit.Before
onAudit_context = audit.Context
return nil
})
ctx := context.Background()
ctx = context.WithValue(ctx, "account", "xxxxxx")
ctx = context.WithValue(ctx, "store", "str_4321")
err = client.DeleteOne(ctx, "mydb", "mycollection", bson.M{"_id": o["_id"].(string)})
if err != nil { t.Fatalf("Failed to deleteOne %#v", err) }
cancel()
client.IgnoreAudit = []string{"event"}
// raw query
var results bson.M
@@ -217,8 +219,8 @@ func TestDeleteOne_WithAuditButIgnored(t *testing.T) {
t.Fatalf("Should have no results not %v", results)
}
if onAudit_calls != 1 {
t.Fatalf("ondelete should have been called once, not %d", onAudit_calls)
if onAudit_calls != 0 {
t.Fatalf("ondelete should have not been called. [%d]", onAudit_calls)
}
if onAudit_before != nil {