Files
online-backend/internal/cards/visamasteractions/diff_test.go
GuwandykA 11e1fa4972
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
visamaster: record order actions for the "Ammallar" table
Every create, update and delete of a visa/master or sber order is now
recorded in visa_master_order_actions: who did it, finished or failed
(with the exception), and for an update only the columns that changed -
original before, changes after. Marking an order paid when a payment is
finalized is recorded the same way, with no user when the GBÜS callback
did it.

Actions are written in the transaction that changes the order, so one is
on record exactly when its change is; failed ones are written after the
rollback with the values they attempted. The payment finalize records
after its commit instead, so recording never holds up a payment GBÜS has
already taken. Updates that change nothing and creates that fail (no
order to attach to) are not recorded.

GET /visa-master-payment-orders/{id}/actions lists them, newest first;
GET /visa-master-payment-orders/{id}/actions/{actionId} adds original,
changes and exception. Both are staff-only and order-scoped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 22:39:42 +05:00

65 lines
2.1 KiB
Go

package visamasteractions
import (
"encoding/json"
"reflect"
"testing"
)
func TestDiffKeepsOnlyChangedColumns(t *testing.T) {
before := map[string]interface{}{"status": "pending", "paid": false, "amount_usd": 250.0, "notes": nil}
after := map[string]interface{}{"status": "completed", "paid": false, "amount_usd": 250.0, "notes": nil}
original, changes := Diff(before, after)
if !reflect.DeepEqual(original, map[string]interface{}{"status": "pending"}) {
t.Errorf("original = %v, want only status: pending", original)
}
if !reflect.DeepEqual(changes, map[string]interface{}{"status": "completed"}) {
t.Errorf("changes = %v, want only status: completed", changes)
}
}
func TestDiffNothingChanged(t *testing.T) {
row := map[string]interface{}{"status": "pending", "sender_datas": map[string]interface{}{"a": 1.0}}
if original, changes := Diff(row, row); original != nil || changes != nil {
t.Errorf("Diff of equal rows = %v, %v, want nil, nil", original, changes)
}
}
func TestDiffMissingColumnCountsAsNull(t *testing.T) {
before := map[string]interface{}{"status": "pending", "notes": "old"}
after := map[string]interface{}{"status": "pending", "email": "a@b.tm"}
original, changes := Diff(before, after)
if !reflect.DeepEqual(original, map[string]interface{}{"notes": "old", "email": nil}) {
t.Errorf("original = %v", original)
}
if !reflect.DeepEqual(changes, map[string]interface{}{"notes": nil, "email": "a@b.tm"}) {
t.Errorf("changes = %v", changes)
}
}
func TestNormalizeMatchesJSONBSnapshot(t *testing.T) {
status := "pending"
attempted, err := Normalize(map[string]interface{}{
"status": &status,
"amount_usd": 250.0,
"sender_datas": json.RawMessage(`{"a": 1}`),
"notes": (*string)(nil),
})
if err != nil {
t.Fatal(err)
}
var snapshot map[string]interface{}
if err := json.Unmarshal([]byte(`{"status": "pending", "amount_usd": 250, "sender_datas": {"a": 1}, "notes": null}`), &snapshot); err != nil {
t.Fatal(err)
}
if original, changes := Diff(snapshot, attempted); original != nil || changes != nil {
t.Errorf("normalized values differ from the snapshot: %v -> %v", original, changes)
}
}