Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
(yes I'm ashamed to shove all of those in a single commit)
  • Loading branch information
ewjoachim committed Jan 7, 2024
1 parent 076367c commit c1d848e
Show file tree
Hide file tree
Showing 21 changed files with 525 additions and 540 deletions.
17 changes: 8 additions & 9 deletions tests/acceptance/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time

import procrastinate
from procrastinate.contrib import aiopg

from .param import Param

Expand All @@ -26,17 +27,13 @@ def decode(dct):
json_loads = functools.partial(json.loads, object_hook=decode)

app = procrastinate.App(
connector=procrastinate.AiopgConnector(json_dumps=json_dumps, json_loads=json_loads)
)
app.open()

sync_app = procrastinate.App(
connector=procrastinate.Psycopg2Connector(
connector=procrastinate.PsycopgConnector(
json_dumps=json_dumps, json_loads=json_loads
)
)
sync_app.open()

app_aiopg = app.with_connector(
aiopg.AiopgConnector(json_dumps=json_dumps, json_loads=json_loads)
)

# Check that tasks can be added from blueprints
bp = procrastinate.Blueprint()
Expand Down Expand Up @@ -108,7 +105,9 @@ def sleep_and_write(sleep, write_before, write_after):


cron_app = procrastinate.App(
connector=procrastinate.AiopgConnector(json_dumps=json_dumps, json_loads=json_loads)
connector=procrastinate.PsycopgConnector(
json_dumps=json_dumps, json_loads=json_loads
)
)


Expand Down
11 changes: 1 addition & 10 deletions tests/acceptance/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import pytest

import procrastinate

APP_MODULE = "tests.acceptance.app"


Expand Down Expand Up @@ -34,15 +32,8 @@ def func(task_name, args=None, app="app", **kwargs):
args = args or []
full_task_name = f"{APP_MODULE}.{task_name}"
subprocess.check_output(
["procrastinate", "defer", full_task_name, *args, json_dumps(kwargs)],
["procrastinate", "defer", full_task_name, json_dumps(kwargs), *args],
env=process_env(app=app),
)

return func


@pytest.fixture
async def async_app_explicit_open(not_opened_aiopg_connector):
app = await procrastinate.App(connector=not_opened_aiopg_connector).open_async()
yield app
await app.close_async()
1 change: 1 addition & 0 deletions tests/acceptance/django_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
INSTALLED_APPS = [
"procrastinate.contrib.django",
]
USE_TZ = True # To avoid RemovedInDjango50Warning
34 changes: 13 additions & 21 deletions tests/acceptance/test_async.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import pytest

import procrastinate


@pytest.fixture
async def async_app_context_manager(not_opened_aiopg_connector):
async with procrastinate.App(
connector=not_opened_aiopg_connector
).open_async() as app:
from procrastinate import app as app_module
from procrastinate.contrib import aiopg


@pytest.fixture(params=["psycopg_connector", "aiopg_connector"])
async def async_app(request, psycopg_connector, connection_params):
app = app_module.App(
connector={
"psycopg_connector": psycopg_connector,
"aiopg_connector": aiopg.AiopgConnector(**connection_params),
}[request.param]
)
async with app.open_async():
yield app


@pytest.fixture(
params=[
pytest.param(False, id="explicit open"),
pytest.param(True, id="context manager open"),
]
)
async def async_app(request, async_app_explicit_open, async_app_context_manager):
if request.param:
yield async_app_explicit_open
else:
yield async_app_context_manager


async def test_defer(async_app):
sum_results = []
product_results = []
Expand Down
28 changes: 14 additions & 14 deletions tests/acceptance/test_nominal.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,42 @@ def func(*queues, name="worker", app="app"):
return func


def test_nominal(defer, worker):
@pytest.mark.parametrize("app", ["app", "app_aiopg"])
def test_nominal(defer, worker, app):
from .param import Param

defer("sum_task", a=5, b=7)
defer("sum_task_param", p1=Param(3), p2=Param(4))
defer("increment_task", a=3)

stdout, stderr = worker()
stdout, stderr = worker(app=app)
print(stdout, stderr)

assert stdout.splitlines() == ["Launching a worker on all queues", "12", "7", "4"]
assert stdout.splitlines() == ["12", "7", "4"]
assert stderr.startswith("DEBUG:procrastinate.")

defer("product_task", a=5, b=4)

stdout, stderr = worker("default")
stdout, stderr = worker("default", app=app)
print(stdout, stderr)
assert "20" not in stdout

stdout, stderr = worker("product_queue")
stdout, stderr = worker("product_queue", app=app)
print(stdout, stderr)
assert stdout.splitlines() == ["Launching a worker on product_queue", "20"]
assert stdout.strip() == "20"

defer("two_fails")
stdout, stderr = worker()
stdout, stderr = worker(app=app)
print(stdout, stderr)
assert "Print something to stdout" in stdout
assert stderr.count("Exception: This should fail") == 2

defer("multiple_exception_failures")
stdout, stderr = worker()
stdout, stderr = worker(app=app)
print(stdout, stderr)
assert (
stdout
== """Launching a worker on all queues
Try 0
== """Try 0
Try 1
Try 2
"""
Expand Down Expand Up @@ -144,16 +144,16 @@ def test_queueing_lock(defer, running_worker):
assert excinfo.value.returncode == 1

with pytest.raises(subprocess.CalledProcessError) as excinfo:
defer("sometask", ["--queueing-lock", "a"], app="sync_app")
defer("sometask", ["--queueing-lock", "a"], app="app")

defer("sometask", ["--queueing-lock", "c"], app="sync_app")
defer("sometask", ["--queueing-lock", "c"], app="app")

# This one doesn't raise
defer("sometask", ["--queueing-lock", "a", "--ignore-already-enqueued"])
defer(
"sometask",
["--queueing-lock", "a", "--ignore-already-enqueued"],
app="sync_app",
app="app",
)


Expand All @@ -171,7 +171,7 @@ def test_periodic_deferrer(worker):
# We're making a dict from the output
results = dict(
(int(a) for a in e[5:].split())
for e in stdout.splitlines()[1:]
for e in stdout.splitlines()
if e.startswith("tick ")
)
assert list(results)[:2] == [0, 1]
Expand Down
42 changes: 19 additions & 23 deletions tests/acceptance/test_sync.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import pytest

import procrastinate
from procrastinate.contrib import psycopg2


@pytest.fixture
def sync_app_explicit_open(not_opened_psycopg2_connector):
app = procrastinate.App(connector=not_opened_psycopg2_connector).open()
yield app
app.close()

@pytest.fixture(params=["sync_psycopg_connector", "psycopg2_connector"])
async def sync_app(request, sync_psycopg_connector, connection_params):
app = procrastinate.App(
connector={
"sync_psycopg_connector": sync_psycopg_connector,
"psycopg2_connector": psycopg2.Psycopg2Connector(**connection_params),
}[request.param]
)

@pytest.fixture
def sync_app_context_manager(not_opened_psycopg2_connector):
with procrastinate.App(connector=not_opened_psycopg2_connector).open() as app:
with app.open():
yield app


@pytest.fixture(
params=[
pytest.param(False, id="explicit open"),
pytest.param(True, id="context manager open"),
]
)
async def sync_app(request, sync_app_explicit_open, sync_app_context_manager):
if request.param:
yield sync_app_explicit_open
else:
yield sync_app_context_manager
@pytest.fixture
async def async_app(not_opened_psycopg_connector):
app = procrastinate.App(connector=not_opened_psycopg_connector)
async with app.open_async():
yield app


# Even if we test the purely sync parts, we'll still need an async worker to execute
# the tasks
async def test_defer(sync_app, async_app_explicit_open):
async def test_defer(sync_app, async_app):
sum_results = []
product_results = []

Expand All @@ -48,8 +43,9 @@ async def product_task(a, b):
sync_app.configure_task(name="sum_task").defer(a=5, b=6)
product_task.defer(a=3, b=4)

async_app_explicit_open.tasks = sync_app.tasks
await async_app_explicit_open.run_worker_async(queues=["default"], wait=False)
# We need to run the async app to execute the tasks
async_app.tasks = sync_app.tasks
await async_app.run_worker_async(queues=["default"], wait=False)

assert sum_results == [3, 7, 11]
assert product_results == [12]
Loading

0 comments on commit c1d848e

Please sign in to comment.