MCPcopy Index your code
hub / github.com/apache/casbin / TestTransactionBuffer

Function TestTransactionBuffer

transaction_test.go:288–337  ·  view source on GitHub ↗

Test transaction buffer operations.

(t *testing.T)

Source from the content-addressed store, hash-verified

286
287// Test transaction buffer operations.
288func TestTransactionBuffer(t *testing.T) {
289 adapter := NewMockTransactionalAdapter()
290 e, err := NewTransactionalEnforcer("examples/rbac_model.conf", adapter)
291 if err != nil {
292 t.Fatalf("Failed to create transactional enforcer: %v", err)
293 }
294 adapter.Enforcer = e.Enforcer
295
296 ctx := context.Background()
297
298 tx, err := e.BeginTransaction(ctx)
299 if err != nil {
300 t.Fatalf("Failed to begin transaction: %v", err)
301 }
302
303 // Initially no operations.
304 if tx.HasOperations() {
305 t.Fatal("Transaction should have no operations initially")
306 }
307
308 if tx.OperationCount() != 0 {
309 t.Fatal("Operation count should be 0 initially")
310 }
311
312 // Add some operations.
313 tx.AddPolicy("alice", "data1", "read")
314 tx.AddPolicy("bob", "data2", "write")
315
316 if !tx.HasOperations() {
317 t.Fatal("Transaction should have operations")
318 }
319
320 if tx.OperationCount() != 2 {
321 t.Fatalf("Expected 2 operations, got %d", tx.OperationCount())
322 }
323
324 // Get buffered model.
325 bufferedModel, err := tx.GetBufferedModel()
326 if err != nil {
327 t.Fatalf("Failed to get buffered model: %v", err)
328 }
329
330 // Check that buffered model contains the policies.
331 hasPolicy, _ := bufferedModel.HasPolicy("p", "p", []string{"alice", "data1", "read"})
332 if !hasPolicy {
333 t.Fatal("Buffered model should contain the added policy")
334 }
335
336 tx.Rollback()
337}

Callers

nothing calls this directly

Calls 9

BeginTransactionMethod · 0.95
NewTransactionalEnforcerFunction · 0.85
GetBufferedModelMethod · 0.80
AddPolicyMethod · 0.65
HasPolicyMethod · 0.65
RollbackMethod · 0.65
HasOperationsMethod · 0.45
OperationCountMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…