Skip to content

Commit

Permalink
Have meaningful fixture name
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Feb 7, 2022
1 parent 32a7946 commit aa5c54d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
4 changes: 2 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def fc():
return FakeCounter()


@pytest.fixture
def fg():
@pytest.fixture(name="fake_gauge")
def _fake_gauge():
return FakeGauge()


Expand Down
37 changes: 24 additions & 13 deletions tests/test_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,35 +246,48 @@ async def test_future_exc(self, fc):
assert 1 == fc._val


@pytest.mark.asyncio
class TestTrackInprogress:
@pytest.mark.asyncio
async def test_coroutine(self, fg):
async def test_async_decorator(self, fake_gauge):
"""
Works as a decorator of async functions.
"""

@aio.track_inprogress(fake_gauge)
async def f():
await asyncio.sleep(0)

await f()

assert 0 == fake_gauge._val
assert 2 == fake_gauge._calls

async def test_coroutine(self, fake_gauge):
"""
Incs and decs.
"""
f = aio.track_inprogress(fg)(coro)
f = aio.track_inprogress(fake_gauge)(coro)

await f()

assert 0 == fg._val
assert 2 == fg._calls
assert 0 == fake_gauge._val
assert 2 == fake_gauge._calls

@pytest.mark.asyncio
async def test_future(self, fg):
async def test_future(self, fake_gauge):
"""
Incs and decs.
"""
fut = asyncio.Future()

wrapped = aio.track_inprogress(fg, fut)
wrapped = aio.track_inprogress(fake_gauge, fut)

assert 1 == fg._val
assert 1 == fake_gauge._val

fut.set_result(42)

await wrapped

assert 0 == fg._val
assert 0 == fake_gauge._val


class FakeSD:
Expand Down Expand Up @@ -436,9 +449,9 @@ async def test_url(self, addr, url):


@pytest.mark.skipif(aiohttp is None, reason="Needs aiohttp.")
@pytest.mark.asyncio
class TestConsulAgent:
@pytest.mark.parametrize("deregister", [True, False])
@pytest.mark.asyncio
async def test_integration(self, deregister):
"""
Integration test with a real consul agent. Start a service, register
Expand Down Expand Up @@ -484,7 +497,6 @@ async def test_integration(self, deregister):
assert 200 == resp.status

@pytest.mark.parametrize("deregister", [True, False])
@pytest.mark.asyncio
@pytest.mark.skipif(sys.version_info < (3, 8), reason="AsyncMock is 3.8+")
async def test_mocked(self, deregister):
"""
Expand Down Expand Up @@ -526,7 +538,6 @@ async def test_mocked(self, deregister):
else:
con.deregister_service.assert_not_called()

@pytest.mark.asyncio
async def test_none_if_register_fails(self):
"""
If register fails, return None.
Expand Down
24 changes: 12 additions & 12 deletions tests/test_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,52 +170,52 @@ def test_deferred_no_exc(self, fc):

class TestTrackInprogress:
@pytest_twisted.inlineCallbacks
def test_deferred(self, fg):
def test_deferred(self, fake_gauge):
"""
Incs and decs if its passed a Deferred.
"""
d = tx.track_inprogress(fg, Deferred())
d = tx.track_inprogress(fake_gauge, Deferred())

assert 1 == fg._val
assert 1 == fake_gauge._val

d.callback(42)
rv = yield d

assert 42 == rv
assert 0 == fg._val
assert 0 == fake_gauge._val

@pytest_twisted.inlineCallbacks
def test_decorator_deferred(self, fg):
def test_decorator_deferred(self, fake_gauge):
"""
Incs and decs if the decorated function returns a Deferred.
"""
d = Deferred()

@tx.track_inprogress(fg)
@tx.track_inprogress(fake_gauge)
def func():
return d

rv = func()

assert 1 == fg._val
assert 1 == fake_gauge._val

d.callback(42)
rv = yield rv

assert 42 == rv
assert 0 == fg._val
assert 0 == fake_gauge._val

def test_decorator_value(self, fg):
def test_decorator_value(self, fake_gauge):
"""
Incs and decs if the decorated function returns a value.
"""

@tx.track_inprogress(fg)
@tx.track_inprogress(fake_gauge)
def func():
return 42

rv = func()

assert 42 == rv
assert 0 == fg._val
assert 2 == fg._calls
assert 0 == fake_gauge._val
assert 2 == fake_gauge._calls

0 comments on commit aa5c54d

Please sign in to comment.