Skip to content

Commit e11bfd1

Browse files
committed
test: add subscription_handle unit tests and CI workflow; docs: add test running instructions
Signed-off-by: Giannis <giannisgeorgiadiis@gmail.com>
1 parent 11f22f5 commit e11bfd1

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test-subscription-handle:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.11'
20+
21+
- name: Install test deps
22+
run: |
23+
python -m pip install --upgrade pip
24+
python -m pip install pytest
25+
26+
- name: Run subscription_handle tests
27+
run: |
28+
python -m pytest -q tests/test_subscription_handle.py

CONTRIBUTING.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,25 @@ Thank you for contributing to the Hiero Python SDK! 🎉
166166
- **Need help or want to connect?** Join our community on Discord! See the **[Discord Joining Guide](docs/discord.md)** for detailed steps on how to join the LFDT server
167167
- **Quick Links:**
168168
- Join the main [Linux Foundation Decentralized Trust (LFDT) Discord Server](https://discord.gg/hyperledger).
169-
- Go directly to the [#hiero-python-sdk channel](https://discord.com/channels/905194001349627914/1336494517544681563)
169+
- Go directly to the [#hiero-python-sdk channel](https://discord.com/channels/905194001349627914/1336494517544681563)
170+
171+
---
172+
173+
## Running tests locally
174+
175+
During development you may want to run a single test file in isolation to avoid importing the project's package-level `__init__` (which imports generated protobuf modules). To run a single test file without loading project-wide fixtures, use the project's virtualenv Python and run pytest on the path:
176+
177+
PowerShell example:
178+
```powershell
179+
# activate virtualenv (if not already active)
180+
C:/Users/Giannis/OneDrive/Programming/hiero-sdk-python/.venv/Scripts/Activate.ps1
181+
182+
# install test deps if needed
183+
python -m pip install -U pip
184+
python -m pip install pytest
185+
186+
# run a single test file
187+
python -m pytest -q tests/test_subscription_handle.py
188+
```
189+
190+
If you want to run a subset of tests, use `-k` with a keyword expression or provide a directory/file path. This helps avoid heavy `conftest.py` imports during quick iteration.

tests/test_subscription_handle.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from unittest.mock import Mock
2+
import importlib.util
3+
from pathlib import Path
4+
5+
# Load the module directly from the source file to avoid importing the
6+
# package `hiero_sdk_python` (its top-level __init__ triggers imports of
7+
# many modules that require generated protobufs during test collection).
8+
repo_root = Path(__file__).resolve().parent.parent
9+
module_path = repo_root / "src" / "hiero_sdk_python" / "utils" / "subscription_handle.py"
10+
spec = importlib.util.spec_from_file_location("subscription_handle", str(module_path))
11+
subscription_handle = importlib.util.module_from_spec(spec)
12+
spec.loader.exec_module(subscription_handle)
13+
SubscriptionHandle = subscription_handle.SubscriptionHandle
14+
15+
16+
def test_not_cancelled_by_default():
17+
handle = SubscriptionHandle()
18+
assert not handle.is_cancelled()
19+
20+
21+
def test_cancel_marks_as_cancelled():
22+
handle = SubscriptionHandle()
23+
handle.cancel()
24+
assert handle.is_cancelled()
25+
26+
27+
def test_set_thread_and_join_calls_thread_join_with_timeout():
28+
handle = SubscriptionHandle()
29+
mock_thread = Mock()
30+
handle.set_thread(mock_thread)
31+
handle.join(timeout=0.25)
32+
mock_thread.join.assert_called_once_with(0.25)
33+
34+
35+
def test_join_without_thread_raises_nothing():
36+
handle = SubscriptionHandle()
37+
# should not raise
38+
handle.join()

0 commit comments

Comments
 (0)