Skip to content

Commit 3de04af

Browse files
wesleykendallqqii
andauthored
Support async pghistory.context (#220)
* Support async pghistory.context * Fix test Co-authored-by: qqii <[email protected]>
1 parent 6d39ff1 commit 3de04af

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

docs/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
arrow==1.3.0 ; python_full_version >= "3.9.0" and python_version < "4"
22
asgiref==3.7.2 ; python_full_version >= "3.9.0" and python_version < "4"
33
babel==2.13.0 ; python_full_version >= "3.9.0" and python_version < "4"
4+
backports-asyncio-runner==1.2.0 ; python_version >= "3.9" and python_version < "3.11"
45
backrefs==5.8 ; python_version >= "3.9" and python_version < "4"
56
beautifulsoup4==4.11.1 ; python_full_version >= "3.9.0" and python_version < "4"
67
binaryornot==0.4.4 ; python_full_version >= "3.9.0" and python_version < "4"
@@ -81,6 +82,7 @@ pymdown-extensions==10.3 ; python_version >= "3.9" and python_version < "4"
8182
pyproject-api==1.8.0 ; python_full_version >= "3.9.0" and python_version < "4"
8283
pyproject-hooks==1.2.0 ; python_full_version >= "3.9.0" and python_version < "4.0"
8384
pyright==1.1.399 ; python_full_version >= "3.9.0" and python_version < "4"
85+
pytest-asyncio==1.1.0 ; python_version >= "3.9" and python_version < "4"
8486
pytest-cov==6.1.1 ; python_version >= "3.9" and python_version < "4"
8587
pytest-django==4.11.1 ; python_full_version >= "3.9.0" and python_version < "4"
8688
pytest-dotenv==0.5.2 ; python_full_version >= "3.9.0" and python_version < "4"

pghistory/runtime.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import uuid
66
from typing import Any, Dict, Tuple, Union
77

8+
from asgiref.sync import sync_to_async
89
from django.db import connection
910

1011
from pghistory import config, utils
@@ -184,7 +185,13 @@ def __enter__(self):
184185

185186
return _tracker.value
186187

188+
async def __aenter__(self):
189+
return await sync_to_async(self.__enter__)()
190+
187191
def __exit__(self, *exc):
188192
if self._pre_execute_hook:
189193
delattr(_tracker, "value")
190194
self._pre_execute_hook.__exit__(*exc)
195+
196+
async def __aexit__(self, *exc):
197+
return await sync_to_async(self.__exit__)(*exc)

pghistory/tests/test_tracking.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
import pghistory.tests.models as test_models
77

88

9+
@pytest.mark.asyncio
10+
@pytest.mark.django_db
11+
async def test_async_context():
12+
async with pghistory.context(example="context"):
13+
dummy = await test_models.BigAutoFieldModel.objects.acreate()
14+
15+
event = await test_models.BigAutoFieldModel.pgh_event_model.objects.select_related(
16+
"pgh_context"
17+
).aget()
18+
assert event.pgh_obj_id == dummy.id
19+
assert event.pgh_context is not None
20+
assert event.pgh_context.metadata == {"example": "context"}
21+
22+
# async pytest django does not clean up the database. Clean up manually
23+
await test_models.BigAutoFieldModel.pgh_event_model.objects.all().adelete()
24+
await test_models.BigAutoFieldModel.objects.all().adelete()
25+
await pghistory.models.Context.objects.all().adelete()
26+
27+
928
@pytest.mark.django_db(transaction=True)
1029
def test_concurrent_index_creation():
1130
"""

poetry.lock

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ dj-database-url = "2.3.0"
8787
psycopg2-binary = "2.9.10"
8888
pytest-django = "4.11.1"
8989
django-dynamic-fixture = "4.0.1"
90+
pytest-asyncio = "1.1.0"
9091

9192
[tool.pytest.ini_options]
9293
xfail_strict = true

0 commit comments

Comments
 (0)