Skip to content

Commit d78994d

Browse files
authored
[DE-665]: GET /_api/transaction (#283)
* initial commit * fix: lint * todo: test_transaction_list * new: `test_transaction_list` * cleanup * fix: `test_transaction_list` running test on separate db
1 parent 9776c1e commit d78994d

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

Diff for: arango/database.py

+20
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
TaskGetError,
6868
TaskListError,
6969
TransactionExecuteError,
70+
TransactionListError,
7071
UserCreateError,
7172
UserDeleteError,
7273
UserGetError,
@@ -311,6 +312,25 @@ def response_handler(resp: Response) -> Any:
311312

312313
return self._execute(request, response_handler)
313314

315+
def list_transactions(self) -> Result[Jsons]:
316+
"""Return the list of running stream transactions.
317+
318+
:return: The list of transactions, with each transaction
319+
containing an "id" and a "state" field.
320+
:rtype: List[Dict[str, Any]]
321+
:raise arango.exceptions.TransactionListError: If retrieval fails.
322+
"""
323+
request = Request(method="get", endpoint="/_api/transaction")
324+
325+
def response_handler(resp: Response) -> Jsons:
326+
if not resp.is_success:
327+
raise TransactionListError(resp, request)
328+
329+
result: Jsons = resp.body["transactions"]
330+
return result
331+
332+
return self._execute(request, response_handler)
333+
314334
def version(self, details: bool = False) -> Result[Any]:
315335
"""Return ArangoDB server version.
316336
:param details: Return more detailed version output

Diff for: arango/exceptions.py

+4
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,10 @@ class TransactionAbortError(ArangoServerError):
744744
"""Failed to abort transaction."""
745745

746746

747+
class TransactionListError(ArangoServerError):
748+
"""Failed to retrieve transactions."""
749+
750+
747751
###################
748752
# User Exceptions #
749753
###################

Diff for: tests/test_transaction.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
TransactionInitError,
99
TransactionStatusError,
1010
)
11-
from tests.helpers import extract
11+
from tests.helpers import extract, generate_db_name
1212

1313

1414
def test_transaction_execute_raw(db, col, docs):
@@ -149,3 +149,34 @@ def test_transaction_graph(db, graph, fvcol, fvdocs):
149149
assert len(vcol) == 0
150150

151151
txn_db.commit_transaction()
152+
153+
154+
def test_transaction_list(client, sys_db, username, password):
155+
db_name = generate_db_name()
156+
157+
sys_db.create_database(
158+
name=db_name,
159+
users=[{"username": username, "password": password, "active": True}],
160+
)
161+
162+
db = client.db(db_name, username, password)
163+
164+
assert db.list_transactions() == []
165+
166+
txn_db = db.begin_transaction()
167+
txn_db.aql.execute("RETURN 1")
168+
169+
txn_db_2 = db.begin_transaction()
170+
txn_db_2.aql.execute("RETURN 1")
171+
172+
assert len(db.list_transactions()) == 2
173+
174+
txn_db.commit_transaction()
175+
176+
assert len(db.list_transactions()) == 1
177+
178+
txn_db_2.commit_transaction()
179+
180+
assert db.list_transactions() == []
181+
182+
sys_db.delete_database(db_name)

0 commit comments

Comments
 (0)