Add context manager to Txn and DgraphClientStub.#185
Add context manager to Txn and DgraphClientStub.#185garylavayou wants to merge 12 commits intodgraph-io:mainfrom
Conversation
|
@garylavayou Thank you for the PR. will you be able to rebase the change on the latest master? |
I have synced the forked repo with the latest master. Is that enough? |
|
Could you also sign the CLA please before we review the code? I have two comments/questions here:
|
|
@mangalaman93 It would be better if you can add tests to the code, since I currently have no stable test environment. I am also looking forward that you can make the context manager more robust and extend the function with your opinions. |
|
@garylavayou Thanks for responding back. I am busy with a few other things, I highly doubt if I could spend time writing tests right now. But I will keep this in mind. If you do get more time and opportunity to make changes, please go ahead and keep me posted. Appreciate your time and responding back. |
|
This PR has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open. |
keep it open please! |
|
This PR has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open. |
|
bueller? |
This commit adds extensive test coverage for the context manager implementation in PR dgraph-io#185, which adds Python context manager support to the Txn and DgraphClientStub classes. Tests for Txn context manager: - Auto-commit for write transactions on successful completion - Auto-discard for read-only transactions - Exception handling with automatic discard - Transaction state validation after context manager exit - Multiple mutations within a single context manager - Query and mutate operations combined - Invalid operation exception handling - Read-only transaction mutation prevention - No-mutation transaction handling Tests for DgraphClientStub context manager: - Basic context manager usage - Exception handling - Proper resource cleanup (stub close) after exit - Exception-safe resource cleanup - Multiple operations within context manager - Integration with DgraphClient and transactions - Full workflow with nested context managers These tests ensure robust behavior of the context manager implementation including proper resource cleanup, exception handling, and transaction lifecycle management. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit adds extensive test coverage for the context manager implementation in PR dgraph-io#185, which adds Python context manager support to the Txn and DgraphClientStub classes. Tests for Txn context manager: - Auto-commit for write transactions on successful completion - Auto-discard for read-only transactions - Exception handling with automatic discard - Transaction state validation after context manager exit - Multiple mutations within a single context manager - Query and mutate operations combined - Invalid operation exception handling - Read-only transaction mutation prevention - No-mutation transaction handling Tests for DgraphClientStub context manager: - Basic context manager usage - Exception handling - Proper resource cleanup (stub close) after exit - Exception-safe resource cleanup - Multiple operations within context manager - Integration with DgraphClient and transactions - Full workflow with nested context managers These tests ensure robust behavior of the context manager implementation including proper resource cleanup, exception handling, and transaction lifecycle management. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit adds extensive test coverage for the context manager implementation in PR dgraph-io#185, which adds Python context manager support to the Txn and DgraphClientStub classes. Tests for Txn context manager: - Auto-commit for write transactions on successful completion - Auto-discard for read-only transactions - Exception handling with automatic discard - Transaction state validation after context manager exit - Multiple mutations within a single context manager - Query and mutate operations combined - Invalid operation exception handling - Read-only transaction mutation prevention - No-mutation transaction handling Tests for DgraphClientStub context manager: - Basic context manager usage - Exception handling - Proper resource cleanup (stub close) after exit - Exception-safe resource cleanup - Multiple operations within context manager - Integration with DgraphClient and transactions - Full workflow with nested context managers These tests ensure robust behavior of the context manager implementation including proper resource cleanup, exception handling, and transaction lifecycle management. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Closing this PR in favor of #284, which incorporates all the changes from this PR (along with the comprehensive tests from #279) with fixes for CI failures. Thank you @garylavayou for your excellent work on the context manager implementation! |
Using Transaction with Context Manager
The Python context manager will automatically perform the "
commit" action after all queries and mutations have been done and perform "discard" action to clean the transaction. When something goes wrong in the scope of context manager, "commit" will not be called, and the "discard" action will be called to drop any potential changes.or you can directly create a transaction from the
Txnclass.Use context manager to automatically clean resources
Use function call:
Use class constructor:
Note:
clientshould be used inside the "with-as" block. The resources related toclientwill be automatically released outside the block andclientis not usable anymore.