Skip to content

Commit f8987bd

Browse files
committed
add integration tests
1 parent 4953ed9 commit f8987bd

File tree

7 files changed

+119
-0
lines changed

7 files changed

+119
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Integration Tests
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
jobs:
8+
integration-tests:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- name: Set up Python
13+
uses: actions/setup-python@v4
14+
with:
15+
python-version: "3.11"
16+
- name: Install Poetry
17+
uses: Gr1N/setup-poetry@v8
18+
- uses: actions/cache@v3
19+
with:
20+
path: ~/.cache/pypoetry/virtualenvs
21+
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}
22+
- name: Checks
23+
run: |
24+
poetry --version
25+
poetry check --no-interaction
26+
- name: Install project
27+
run: poetry install --no-interaction
28+
- name: Run integration tests
29+
run: poetry run pytest integration_tests -v

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dist/
1111
.env
1212
.venv
1313
.envrc
14+
.env.integration
1415

1516
# mypy
1617
.mypy_cache/

integration_tests/__init__.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
import taskbadger as badger
7+
8+
9+
def _load_config():
10+
path = Path(__file__).parent.parent / ".env.integration"
11+
if not path.exists():
12+
return
13+
with path.open() as f:
14+
for line in f:
15+
if line.startswith("#") or not line.strip():
16+
continue
17+
key, value = line.strip().split("=", 1)
18+
os.environ[key] = value
19+
20+
21+
_load_config()
22+
ORG = os.environ.get("TASKBADGER_ORG", "")
23+
PROJECT = os.environ.get("TASKBADGER_PROJECT", "")
24+
API_KEY = os.environ.get("TASKBADGER_API_KEY", "")
25+
26+
if not ORG or not PROJECT or not API_KEY:
27+
pytest.fail("Integration test config missing", pytrace=False)
28+
else:
29+
badger.init(
30+
os.environ.get("TASKBADGER_ORG", ""),
31+
os.environ.get("TASKBADGER_PROJECT", ""),
32+
os.environ.get("TASKBADGER_API_KEY", ""),
33+
)

integration_tests/tasks.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from celery import shared_task
2+
3+
import taskbadger.celery
4+
5+
6+
@shared_task(bind=True, base=taskbadger.celery.Task)
7+
def add(self, x, y):
8+
assert self.taskbadger_task is not None, "missing task on self"
9+
self.taskbadger_task.update(value=100, data={"result": x + y})
10+
return x + y

integration_tests/test_basics.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from datetime import datetime
2+
3+
import taskbadger as badger
4+
from taskbadger import StatusEnum
5+
6+
7+
def test_basics():
8+
data = {"now": datetime.utcnow().isoformat()}
9+
task = badger.create_task("test basics", data=data)
10+
task.success(100)
11+
assert task.status == StatusEnum.SUCCESS
12+
13+
fresh = badger.get_task(task.id)
14+
assert fresh.status == StatusEnum.SUCCESS
15+
assert fresh.value == 100
16+
assert fresh.data == data

integration_tests/test_celery.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import random
2+
3+
import pytest
4+
5+
from taskbadger import StatusEnum
6+
7+
from .tasks import add
8+
9+
10+
@pytest.fixture(scope="session", autouse=True)
11+
def celery_includes():
12+
return [
13+
"integration_tests.tasks",
14+
]
15+
16+
17+
def test_celery(celery_worker):
18+
a, b = random.randint(1, 1000), random.randint(1, 1000)
19+
result = add.delay(a, b)
20+
assert result.get(timeout=10, propagate=True) == a + b
21+
22+
tb_task = result.get_taskbadger_task()
23+
assert tb_task is not None
24+
assert tb_task.status == StatusEnum.SUCCESS
25+
assert tb_task.value == 100
26+
assert tb_task.data == {"result": a + b}

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ exclude = '''
7777
[tool.isort]
7878
line_length = 120
7979
profile = "black"
80+
81+
[tool.pytest.ini_options]
82+
# don't run integration tests unless specifically requested
83+
norecursedirs = ".* integration_tests"

0 commit comments

Comments
 (0)