Add audit to replace, update set
This commit is contained in:
@@ -2,6 +2,7 @@ package mongo
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"context"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
@@ -18,21 +19,23 @@ func TestReplace(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Insert to mycollection
|
||||
o, err := client.InsertOne(ctx, "mydb", "mycollection", data)
|
||||
if err != nil { t.Fatalf("Failed to insertOne %#v", err) }
|
||||
|
||||
id := o["_id"].(string)
|
||||
|
||||
// first we retrieve the entity
|
||||
// Retrieve from mycollection
|
||||
fetched, err := client.GetOne(ctx, "mydb", "mycollection", id)
|
||||
if err != nil { t.Fatalf("Failed to fetch %#v", err) }
|
||||
|
||||
fetched["name"] = "Noah Patel"
|
||||
|
||||
|
||||
// Replace in mycollection
|
||||
replaced, err := client.Replace(ctx, "mydb", "mycollection", id, fetched)
|
||||
if err != nil { t.Fatalf("Failed to replace %#v", err) }
|
||||
|
||||
// t.Fatalf("-> %v", replaced)
|
||||
if replaced["_id"] != fetched["_id"] {
|
||||
t.Fatalf("Not the same entity")
|
||||
}
|
||||
@@ -177,5 +180,86 @@ func TestReplace_Discrimination_EnsureStore(t *testing.T) {
|
||||
if changed["store"] != "str_1234" {
|
||||
t.Fatal("Should have ensured store")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplace_WithAudit(t *testing.T) {
|
||||
client := GetMongoClient()
|
||||
|
||||
// Insert sample
|
||||
data := map[string]any {
|
||||
"_id": "su_123458",
|
||||
"name": "MyNameTODelete",
|
||||
"age": int32(25),
|
||||
}
|
||||
|
||||
before, err := client.InsertOne(context.Background(), "mydb", "mycollection", data)
|
||||
if err != nil { t.Fatalf("Failed to insertOne %#v", err) }
|
||||
|
||||
var (
|
||||
onAudit_calls int
|
||||
onAudit_data any
|
||||
onAudit_before any
|
||||
onAudit_after any
|
||||
onAudit_context any
|
||||
)
|
||||
|
||||
cancel := client.Subscribe(func(audit *AuditResult) error {
|
||||
onAudit_calls++
|
||||
onAudit_data = audit.Data
|
||||
onAudit_before = audit.Before
|
||||
onAudit_after = audit.After
|
||||
onAudit_context = audit.Context
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
toupdate := map[string]any { "name": "** CHANGED NAME **" }
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "account", "xxxxxx")
|
||||
ctx = context.WithValue(ctx, "store", "str_4321")
|
||||
o, err := client.Replace(ctx, "mydb", "mycollection", "su_123458", toupdate)
|
||||
if err != nil { t.Fatalf("Failed to replace %#v", err) }
|
||||
|
||||
cancel()
|
||||
|
||||
if onAudit_calls != 1 {
|
||||
t.Fatalf("ondelete should have been called once, not %d", onAudit_calls)
|
||||
}
|
||||
|
||||
if onAudit_data != nil {
|
||||
dta := onAudit_data.(bson.M)
|
||||
AssertSubset(t, dta, toupdate, "Should have been equal")
|
||||
} else {
|
||||
t.Fatal("should have data")
|
||||
}
|
||||
|
||||
if onAudit_before != nil {
|
||||
bf := onAudit_before.(bson.M)
|
||||
AssertSubset(t, bf, before, "Should have been equal")
|
||||
} else {
|
||||
t.Fatal("should have before")
|
||||
}
|
||||
|
||||
if onAudit_after != nil {
|
||||
tp := fmt.Sprintf("%T", onAudit_after)
|
||||
|
||||
expectedType := fmt.Sprintf("%T", map[string]any{})
|
||||
if tp != expectedType {
|
||||
t.Fatalf("after has the wrong type %s", tp)
|
||||
}
|
||||
|
||||
after := onAudit_after.(map[string]any)
|
||||
|
||||
AssertSubset(t, after, o, "Should have been equal")
|
||||
} else {
|
||||
t.Fatal("should have after")
|
||||
}
|
||||
|
||||
if onAudit_context != nil {
|
||||
ctx := onAudit_context.(map[string]any)
|
||||
AssertSubset(t, ctx, map[string]any{"account": "xxxxxx", "store": "str_4321"}, "Should have been equal")
|
||||
} else {
|
||||
t.Fatal("should have context")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user