Debian Codegraph @

tests/lib/streaming/helpers.py

raw
1 from __future__ import annotations
2
3 import os
4 from typing import TypeVar, Iterator
5 from typing_extensions import AsyncIterator
6
7 _T = TypeVar("_T")
8
9
10 def load_fixture(fixture_name: str) -> str:
11 """Load a fixture file from the fixtures directory."""
12 current_dir = os.path.dirname(os.path.abspath(__file__))
13 fixtures_dir = os.path.join(current_dir, "fixtures")
14 with open(os.path.join(fixtures_dir, fixture_name), "r") as f:
15 return f.read()
16
17
18 def get_response(fixture_name: str) -> Iterator[bytes]:
19 """Convert a fixture file into a stream of bytes for testing."""
20 content = load_fixture(fixture_name)
21 for line in content.splitlines():
22 yield line.encode() + b"\n"
23
24
25 async def to_async_iter(iter: Iterator[_T]) -> AsyncIterator[_T]:
26 """Convert a synchronous iterator to an asynchronous one."""
27 for event in iter:
28 yield event