Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Feb 18, 2021
1 parent a2c2553 commit 6bdaec5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/contextvars.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ The general flow is:
>>> # middleware), clear the threadlocal context and bind some common
>>> # values:
>>> clear_contextvars()
>>> bind_contextvars(a=1, b=2)
>>> bind_contextvars(a=1)
>>> bind_contextvars(b=2)
>>> # Then use loggers as per normal
>>> # (perhaps by using structlog.get_logger() to create them).
>>> log.msg("hello")
Expand Down
3 changes: 2 additions & 1 deletion src/structlog/contextvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
.. versionadded:: 20.1.0
.. versionchanged:: 21.1.0
Reimplement code without dict
Reimplemented without using a single dict as context carrier for improved
isolation. Every key-value pair is a separate `contextvars.ContextVar` now.
See :doc:`contextvars`.
"""
Expand Down
10 changes: 7 additions & 3 deletions tests/test_contextvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# repository for complete details.

import asyncio
import secrets

import pytest

Expand Down Expand Up @@ -112,7 +113,7 @@ async def coro():

assert {} == await event_loop.create_task(coro())

async def test_undbind(self, event_loop):
async def test_unbind(self, event_loop):
"""
Unbinding a previously bound variable causes it to be removed from the
result of merge_contextvars.
Expand All @@ -125,13 +126,16 @@ async def coro():

assert {"b": 2} == await event_loop.create_task(coro())

async def test_undbind_not_bound(self, event_loop):
async def test_unbind_not_bound(self, event_loop):
"""
Unbinding a not bound variable causes doesn't raise an exception.
"""

async def coro():
unbind_contextvars("a")
# Since unbinding means "setting to Ellipsis", we have to make
# some effort to ensure that the ContextVar never existed.
unbind_contextvars("a" + secrets.token_hex())

return merge_contextvars(None, None, {"b": 2})

assert {"b": 2} == await event_loop.create_task(coro())
Expand Down

0 comments on commit 6bdaec5

Please sign in to comment.