MCPcopy
hub / github.com/apache/casbin / TestTransactionBasicOperations

Function TestTransactionBasicOperations

transaction_test.go:97–133  ·  view source on GitHub ↗

Test basic transaction functionality.

(t *testing.T)

Source from the content-addressed store, hash-verified

95
96// Test basic transaction functionality.
97func TestTransactionBasicOperations(t *testing.T) {
98 adapter := NewMockTransactionalAdapter()
99 e, err := NewTransactionalEnforcer("examples/rbac_model.conf", adapter)
100 if err != nil {
101 t.Fatalf("Failed to create transactional enforcer: %v", err)
102 }
103 adapter.Enforcer = e.Enforcer
104
105 ctx := context.Background()
106
107 // Begin transaction.
108 tx, err := e.BeginTransaction(ctx)
109 if err != nil {
110 t.Fatalf("Failed to begin transaction: %v", err)
111 }
112
113 // Add policies in transaction.
114 ok, err := tx.AddPolicy("alice", "data1", "read")
115 if !ok || err != nil {
116 t.Fatalf("Failed to add policy in transaction: %v", err)
117 }
118
119 ok, err = tx.AddPolicy("bob", "data2", "write")
120 if !ok || err != nil {
121 t.Fatalf("Failed to add policy in transaction: %v", err)
122 }
123
124 // Commit transaction.
125 if err := tx.Commit(); err != nil {
126 t.Fatalf("Failed to commit transaction: %v", err)
127 }
128
129 // Verify transaction was committed.
130 if !tx.IsCommitted() {
131 t.Error("Transaction should be committed")
132 }
133}
134
135// Test transaction rollback.
136func TestTransactionRollback(t *testing.T) {

Callers

nothing calls this directly

Calls 7

BeginTransactionMethod · 0.95
NewTransactionalEnforcerFunction · 0.85
IsCommittedMethod · 0.80
AddPolicyMethod · 0.65
CommitMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…