From 3bd6fbdc27c19d8e6bf279a773457766f28ac3bf Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Fri, 1 Sep 2023 12:25:27 -0400 Subject: [PATCH 1/6] initial commit --- arango/database.py | 19 +++++++++++++++++++ arango/exceptions.py | 4 ++++ tests/test_transaction.py | 5 +++++ 3 files changed, 28 insertions(+) diff --git a/arango/database.py b/arango/database.py index 875a04e0..93b62629 100644 --- a/arango/database.py +++ b/arango/database.py @@ -76,6 +76,7 @@ ViewRenameError, ViewReplaceError, ViewUpdateError, + TransactionListError ) from arango.executor import ( AsyncApiExecutor, @@ -307,6 +308,24 @@ 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) + + return resp.body.get("transactions") + + 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 diff --git a/arango/exceptions.py b/arango/exceptions.py index f9b6de2d..3a41e885 100644 --- a/arango/exceptions.py +++ b/arango/exceptions.py @@ -724,6 +724,10 @@ class TransactionAbortError(ArangoServerError): """Failed to abort transaction.""" +class TransactionListError(ArangoServerError): + """Failed to retrieve transactions.""" + + ################### # User Exceptions # ################### diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 4308e4c6..01e68bdd 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -41,6 +41,11 @@ def test_transaction_execute_raw(db, col, docs): db.execute_transaction(command="INVALID COMMAND") assert err.value.error_code == 10 +def test_transaction_list(db, col, docs): + no_transactions = db.list_transactions() + assert no_transactions == [] + # TODO: Add proper transaction test here + def test_transaction_init(db, bad_db, col, username): txn_db = db.begin_transaction() From e1806882e1c171de0de84b864d416f616d16209c Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Fri, 1 Sep 2023 12:26:52 -0400 Subject: [PATCH 2/6] fix: lint --- arango/database.py | 5 +++-- tests/test_transaction.py | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/arango/database.py b/arango/database.py index 93b62629..bb25e595 100644 --- a/arango/database.py +++ b/arango/database.py @@ -63,6 +63,7 @@ TaskGetError, TaskListError, TransactionExecuteError, + TransactionListError, UserCreateError, UserDeleteError, UserGetError, @@ -76,7 +77,6 @@ ViewRenameError, ViewReplaceError, ViewUpdateError, - TransactionListError ) from arango.executor import ( AsyncApiExecutor, @@ -322,7 +322,8 @@ def response_handler(resp: Response) -> Jsons: if not resp.is_success: raise TransactionListError(resp, request) - return resp.body.get("transactions") + result: Jsons = resp.body["transactions"] + return result return self._execute(request, response_handler) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 01e68bdd..00c93ba3 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -41,6 +41,7 @@ def test_transaction_execute_raw(db, col, docs): db.execute_transaction(command="INVALID COMMAND") assert err.value.error_code == 10 + def test_transaction_list(db, col, docs): no_transactions = db.list_transactions() assert no_transactions == [] From 51d1537b9704b2e61beeff27fd852b27110b79c1 Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Fri, 1 Sep 2023 13:54:17 -0400 Subject: [PATCH 3/6] todo: test_transaction_list --- tests/test_transaction.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 00c93ba3..5339b765 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -43,8 +43,8 @@ def test_transaction_execute_raw(db, col, docs): def test_transaction_list(db, col, docs): - no_transactions = db.list_transactions() - assert no_transactions == [] + transactions = db.list_transactions() + assert isinstance(transactions, list) # TODO: Add proper transaction test here From 3d6c24117f51bf59c68b5c7aeea6f32ad018365a Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Fri, 15 Sep 2023 11:57:26 -0400 Subject: [PATCH 4/6] new: `test_transaction_list` --- tests/test_transaction.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 5339b765..4904a948 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -42,12 +42,6 @@ def test_transaction_execute_raw(db, col, docs): assert err.value.error_code == 10 -def test_transaction_list(db, col, docs): - transactions = db.list_transactions() - assert isinstance(transactions, list) - # TODO: Add proper transaction test here - - def test_transaction_init(db, bad_db, col, username): txn_db = db.begin_transaction() @@ -155,3 +149,24 @@ def test_transaction_graph(db, graph, fvcol, fvdocs): assert len(vcol) == 0 txn_db.commit_transaction() + + +def test_transaction_list(db): + transactions = db.list_transactions() + assert transactions == [] + + txn_db = db.begin_transaction() + txn_db.aql.execute("RETURN 1") + + txt_db_2 = db.begin_transaction() + txt_db_2.aql.execute("RETURN 1") + + assert len(db.list_transactions()) == 2 + + txn_db.commit_transaction() + + assert len(db.list_transactions()) == 1 + + txt_db_2.commit_transaction() + + assert db.list_transactions() == [] From 85cef6db7df8a3071ccd7d29578a99a84068b172 Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Fri, 15 Sep 2023 12:00:06 -0400 Subject: [PATCH 5/6] cleanup --- tests/test_transaction.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 4904a948..dca3db4b 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -152,14 +152,13 @@ def test_transaction_graph(db, graph, fvcol, fvdocs): def test_transaction_list(db): - transactions = db.list_transactions() - assert transactions == [] + assert db.list_transactions() == [] txn_db = db.begin_transaction() txn_db.aql.execute("RETURN 1") - txt_db_2 = db.begin_transaction() - txt_db_2.aql.execute("RETURN 1") + txn_db_2 = db.begin_transaction() + txn_db_2.aql.execute("RETURN 1") assert len(db.list_transactions()) == 2 @@ -167,6 +166,6 @@ def test_transaction_list(db): assert len(db.list_transactions()) == 1 - txt_db_2.commit_transaction() + txn_db_2.commit_transaction() assert db.list_transactions() == [] From fde83e470db86165eace1e2dbd9e281553548440 Mon Sep 17 00:00:00 2001 From: Anthony Mahanna Date: Fri, 15 Sep 2023 12:09:58 -0400 Subject: [PATCH 6/6] fix: `test_transaction_list` running test on separate db --- tests/test_transaction.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index dca3db4b..59e86b7c 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -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): @@ -151,7 +151,16 @@ def test_transaction_graph(db, graph, fvcol, fvdocs): txn_db.commit_transaction() -def test_transaction_list(db): +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() @@ -169,3 +178,5 @@ def test_transaction_list(db): txn_db_2.commit_transaction() assert db.list_transactions() == [] + + sys_db.delete_database(db_name)