Skip to content

Conversation

@shaunpatterson
Copy link

Summary

This PR adds comprehensive test coverage for the context manager functionality introduced in PR #185, which adds Python with statement support to the Txn and DgraphClientStub classes.

Background

PR #185 (#185) introduced context manager support but lacked comprehensive tests. The main concern from reviewers was the absence of thorough testing. This PR addresses that concern by adding extensive test coverage.

Tests Added

Txn Context Manager Tests (11 new tests)

  1. test_context_manager_auto_commit - Verifies write transactions automatically commit on successful completion
  2. test_context_manager_read_only_auto_discard - Verifies read-only transactions automatically discard
  3. test_context_manager_exception_handling - Tests exception handling with automatic discard and re-raising
  4. test_context_manager_transaction_finished_after_exit - Validates transaction state after context manager exit
  5. test_context_manager_multiple_mutations - Tests multiple mutations within a single context manager
  6. test_context_manager_query_and_mutate - Tests combined query and mutate operations
  7. test_context_manager_invalid_nquad_exception - Tests proper exception handling for invalid operations
  8. test_context_manager_read_only_cannot_mutate - Verifies read-only transactions cannot mutate
  9. test_context_manager_no_mutations_auto_commit - Tests transactions with no mutations don't error
  10. test_context_manager_by_contextlib - Tests context manager via client.begin()
  11. test_context_manager_by_class - Tests context manager using Txn class directly

DgraphClientStub Context Manager Tests (8 new tests)

  1. test_context_manager_closes_stub - Verifies stub is properly closed after exiting
  2. test_context_manager_with_client - Tests using stub context manager with DgraphClient
  3. test_context_manager_exception_still_closes - Verifies stub closes even on exceptions
  4. test_context_manager_function_wrapper_closes - Tests client_stub() function wrapper
  5. test_context_manager_multiple_operations - Tests multiple operations within context manager
  6. test_context_manager_nested_with_client_operations - Tests full workflow with nested context managers
  7. test_context_manager - Basic context manager usage
  8. test_context_manager_code_exception - Exception handling within context manager

Test Coverage

These tests ensure:

  • ✅ Proper resource cleanup (commit/discard for transactions, close for stubs)
  • ✅ Exception handling with proper cleanup
  • ✅ Transaction lifecycle management
  • ✅ State validation after context manager exit
  • ✅ Integration with existing DgraphClient operations
  • ✅ Both read-only and write transaction scenarios
  • ✅ Multiple operations within a single context manager
  • ✅ Nested context manager scenarios

Related to

🤖 Generated with Claude Code

@CLAassistant
Copy link

CLAassistant commented Dec 8, 2025

CLA assistant check
All committers have signed the CLA.

@shaunpatterson shaunpatterson changed the title Add comprehensive tests for context manager implementation (PR #185) Add context manager to Txn and DgraphClientStub. Dec 8, 2025
garylavayou and others added 10 commits December 7, 2025 23:42
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 <[email protected]>
@shaunpatterson shaunpatterson force-pushed the add-context-manager-tests branch from cb32f56 to 2c9782b Compare December 8, 2025 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants