Skip to content

Commit

Permalink
Add basic tests and CI workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmflynn committed Jan 19, 2024
1 parent 054589b commit c4d3a53
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Minimal and Full Tests
on:
schedule:
- cron: "0 16 * * *" # Daily at noon EST
workflow_dispatch:

jobs:
run:
name: ${{ matrix.os }} Python ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: s-weigand/setup-conda@v1
- uses: actions/checkout@v3
- run: git fetch --prune --unshallow --tags
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Global Setup
run: |
python -m pip install -U pip # Official recommended way
pip install pytest-xdist
- name: Install tqdm_publisher with minimal requirements
run: pip install .[test]

- name: Run full pytest with coverage
run: pytest -rsx -n auto --dist loadscope --cov=./ --cov-report xml:./codecov.xml
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__pycache__/
*.egg-info
dist
dist

.coverage
.coverage.*
codecov.xml
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ classifiers = [
"Operating System :: Unix",
]
dependencies = [
'tqdm>=4.49.0'
"tqdm>=4.49.0"
]

[project.optional-dependencies]
test = [
"pytest",
"pytest-asyncio",
"pytest-cov"
]

[project.urls]
Expand Down
6 changes: 5 additions & 1 deletion src/tqdm_publisher/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ def subscribe(self, callback):

# Unsubscribe from updates
def unsubscribe(self, callback_id):
del self.callbacks[callback_id]
try:
del self.callbacks[callback_id]
return True
except KeyError:
return False
40 changes: 40 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from tqdm_publisher import TQDMPublisher
import pytest
from utils import create_tasks
import asyncio

def test_initialization():
publisher = TQDMPublisher()
assert len(publisher.callbacks) == 0

@pytest.mark.asyncio
async def test_subscription_and_callback_execution():
n_callback_executions = 0

def test_callback(data):
nonlocal n_callback_executions
n_callback_executions += 1
assert 'n' in data and 'total' in data

tasks = create_tasks()
publisher = TQDMPublisher(asyncio.as_completed(tasks), total=len(tasks))
callback_id = publisher.subscribe(test_callback)

assert callback_id in publisher.callbacks

# Simulate an update to trigger the callback
for f in publisher:
await f

assert n_callback_executions > 0

def test_unsubscription():
def dummy_callback(data):
pass

tasks = []
publisher = TQDMPublisher(asyncio.as_completed(tasks), total=len(tasks))
callback_id = publisher.subscribe(dummy_callback)
result = publisher.unsubscribe(callback_id)
assert result == True
assert callback_id not in publisher.callbacks
17 changes: 17 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import asyncio
import random

async def sleep_func(sleep_duration: float = 1) -> float:
await asyncio.sleep(delay=sleep_duration)


def create_tasks():
n = 10**5
sleep_durations = [random.uniform(0, 5.0) for _ in range(n)]
tasks = []

for sleep_duration in sleep_durations:
task = asyncio.create_task(sleep_func(sleep_duration=sleep_duration))
tasks.append(task)

return tasks

0 comments on commit c4d3a53

Please sign in to comment.