Skip to content

Commit

Permalink
[DE-665]: GET /_api/transaction (#283)
Browse files Browse the repository at this point in the history
* initial commit

* fix: lint

* todo: test_transaction_list

* new: `test_transaction_list`

* cleanup

* fix: `test_transaction_list`

running test on separate db
  • Loading branch information
aMahanna committed Sep 21, 2023
1 parent 9776c1e commit d78994d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
20 changes: 20 additions & 0 deletions arango/database.py
Expand Up @@ -67,6 +67,7 @@
TaskGetError,
TaskListError,
TransactionExecuteError,
TransactionListError,
UserCreateError,
UserDeleteError,
UserGetError,
Expand Down Expand Up @@ -311,6 +312,25 @@ def response_handler(resp: Response) -> Any:

return self._execute(request, response_handler)

def list_transactions(self) -> Result[Jsons]:
"""Return the list of running stream transactions.
:return: The list of transactions, with each transaction
containing an "id" and a "state" field.
:rtype: List[Dict[str, Any]]
:raise arango.exceptions.TransactionListError: If retrieval fails.
"""
request = Request(method="get", endpoint="/_api/transaction")

def response_handler(resp: Response) -> Jsons:
if not resp.is_success:
raise TransactionListError(resp, request)

result: Jsons = resp.body["transactions"]
return result

return self._execute(request, response_handler)

def version(self, details: bool = False) -> Result[Any]:
"""Return ArangoDB server version.
:param details: Return more detailed version output
Expand Down
4 changes: 4 additions & 0 deletions arango/exceptions.py
Expand Up @@ -744,6 +744,10 @@ class TransactionAbortError(ArangoServerError):
"""Failed to abort transaction."""


class TransactionListError(ArangoServerError):
"""Failed to retrieve transactions."""


###################
# User Exceptions #
###################
Expand Down
33 changes: 32 additions & 1 deletion tests/test_transaction.py
Expand Up @@ -8,7 +8,7 @@
TransactionInitError,
TransactionStatusError,
)
from tests.helpers import extract
from tests.helpers import extract, generate_db_name


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

txn_db.commit_transaction()


def test_transaction_list(client, sys_db, username, password):
db_name = generate_db_name()

sys_db.create_database(
name=db_name,
users=[{"username": username, "password": password, "active": True}],
)

db = client.db(db_name, username, password)

assert db.list_transactions() == []

txn_db = db.begin_transaction()
txn_db.aql.execute("RETURN 1")

txn_db_2 = db.begin_transaction()
txn_db_2.aql.execute("RETURN 1")

assert len(db.list_transactions()) == 2

txn_db.commit_transaction()

assert len(db.list_transactions()) == 1

txn_db_2.commit_transaction()

assert db.list_transactions() == []

sys_db.delete_database(db_name)

0 comments on commit d78994d

Please sign in to comment.