Skip to content

Commit

Permalink
Clean test code
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Jun 12, 2024
1 parent 767f358 commit 36186cd
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions tests/utils/pubsub/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
from nextline.utils import PubSubItem, aiterable


async def test_close():
async def test_close() -> None:
async with PubSubItem[str]() as obj:
assert obj


async def test_close_multiple_times():
async def test_close_multiple_times() -> None:
async with PubSubItem[str]() as obj:
await obj.close()
await obj.close()


@given(items=st.lists(st.text()))
async def test_get(items: Sequence[str]):
@given(...)
async def test_get(items: list[str]) -> None:
async with PubSubItem[str]() as obj:
with pytest.raises(LookupError):
obj.latest()
Expand All @@ -30,16 +30,16 @@ async def test_get(items: Sequence[str]):


@given(items=st.lists(st.text()), n_subscriptions=st.integers(0, 5))
async def test_subscribe(items: Sequence[str], n_subscriptions: int):
async def test_subscribe(items: Sequence[str], n_subscriptions: int) -> None:
items = tuple(items)
expected = [items] * n_subscriptions

async with PubSubItem[str]() as obj:

async def receive():
async def receive() -> tuple[str, ...]:
return tuple([i async for i in obj.subscribe()])

async def send():
async def send() -> None:
async for i in aiterable(items):
await obj.publish(i)
await obj.close()
Expand All @@ -53,10 +53,10 @@ async def send():


@given(n_subscriptions=st.integers(0, 100))
async def test_n_subscriptions(n_subscriptions: int):
async def test_n_subscriptions(n_subscriptions: int) -> None:
async with PubSubItem[str]() as obj:

async def receive():
async def receive() -> tuple[str, ...]:
return tuple([i async for i in obj.subscribe()])

task = asyncio.gather(*(receive() for _ in range(n_subscriptions)))
Expand All @@ -77,17 +77,17 @@ async def test_last(
items: Sequence[int],
last: bool,
n_subscriptions: int,
):
) -> None:
pre_items = tuple(pre_items)
items = tuple(items)
expected = [pre_items[-1:] + items if last else items] * n_subscriptions

async with PubSubItem[int | str]() as obj:

async def receive():
async def receive() -> tuple[int | str, ...]:
return tuple([i async for i in obj.subscribe(last=last)])

async def send():
async def send() -> None:
async for i in aiterable(items):
await obj.publish(i)
await obj.close()
Expand All @@ -104,8 +104,8 @@ async def send():
assert results == expected


@given(items=st.lists(st.text()), last_item=st.text())
async def test_put_after_close(items: Sequence[str], last_item: str):
@given(...)
async def test_put_after_close(items: list[str], last_item: str) -> None:
async with PubSubItem[str]() as obj:
for i in items:
await obj.publish(i)
Expand All @@ -118,15 +118,15 @@ async def test_put_after_close(items: Sequence[str], last_item: str):
obj.latest()


@given(items=st.lists(st.text()))
async def test_subscribe_after_close(items: Sequence[str]):
@given(...)
async def test_subscribe_after_close(items: list[str]) -> None:
async with PubSubItem[str]() as obj:
for i in items:
await obj.publish(i)

await asyncio.sleep(0.001)

async def receive():
async def receive() -> tuple[str, ...]:
return tuple([i async for i in obj.subscribe()])

results = await receive()
Expand All @@ -135,7 +135,7 @@ async def receive():


@given(st.data())
async def test_break(data: st.DataObject):
async def test_break(data: st.DataObject) -> None:
items = tuple(data.draw(st.lists(st.text(), min_size=1, unique=True)))
idx = data.draw(st.integers(0, len(items) - 1))
at = items[idx]
Expand All @@ -144,7 +144,7 @@ async def test_break(data: st.DataObject):

async with PubSubItem[str]() as obj:

async def receive():
async def receive() -> tuple[str, ...]:
ret = []
assert obj.n_subscriptions == 0
async for i in obj.subscribe():
Expand All @@ -156,7 +156,7 @@ async def receive():
assert obj.n_subscriptions == 0 # the queue is closed
return tuple(ret)

async def send():
async def send() -> None:
async for i in aiterable(items):
await obj.publish(i)
await obj.close()
Expand All @@ -166,7 +166,7 @@ async def send():


@given(st.data())
async def test_no_missing_or_duplicate(data: st.DataObject):
async def test_no_missing_or_duplicate(data: st.DataObject) -> None:
'''Assert that the issue is resolved
https://github.com/simonsobs/nextline/issues/2
Expand All @@ -184,11 +184,11 @@ async def test_no_missing_or_duplicate(data: st.DataObject):

async with PubSubItem[str]() as obj:

async def receive():
async def receive() -> tuple[str, ...]:
await event.wait()
return tuple([i async for i in obj.subscribe()])

async def send():
async def send() -> None:
async for i in aiterable(pre_items):
await obj.publish(i)
event.set()
Expand Down

0 comments on commit 36186cd

Please sign in to comment.