File: test_adapters.py

package info (click to toggle)
python-ijson 3.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 696 kB
  • sloc: python: 2,816; ansic: 1,776; sh: 4; makefile: 3
file content (69 lines) | stat: -rw-r--r-- 2,071 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import asyncio
import ijson
import pytest


def test_from_iter_read0_does_not_consume():
    chunks = [b'{"key":', b'"value"}']
    file_obj = ijson.from_iter(iter(chunks))
    assert file_obj.read(0) == b""
    assert file_obj.read(1) == b'{"key":'
    assert file_obj.read(1) == b'"value"}'
    assert file_obj.read(1) == b""


@pytest.mark.parametrize(
    "chunks_factory",
    [list, tuple, iter, lambda chunks: (chunk for chunk in chunks)],
    ids=["list", "tuple", "iter", "generator"],
)
def test_from_iter_accepts_iterable(chunks_factory):
    chunks = chunks_factory([b'{"key":', b'"value"}'])
    file_obj = ijson.from_iter(chunks)
    assert file_obj.read(1) == b'{"key":'
    assert file_obj.read(1) == b'"value"}'
    assert file_obj.read(1) == b""


@pytest.mark.parametrize(
    "chunks_factory",
    [list, tuple, iter, lambda chunks: (chunk for chunk in chunks)],
    ids=["list", "tuple", "iter", "generator"],
)
def test_from_iter_accepts_string_and_warns(chunks_factory):
    chunks = chunks_factory(['{"key":', '"value"}'])
    file_obj = ijson.from_iter(chunks)
    with pytest.deprecated_call():
        assert file_obj.read(1) == b'{"key":'
    assert file_obj.read(1) == b'"value"}'
    assert file_obj.read(1) == b""


def test_from_iter_accepts_aiterable():
    async def chunks():
        yield b'{"key":'
        yield b'"value"}'

    async def main():
        file_obj = ijson.from_iter(chunks())
        assert await file_obj.read(0) == b""
        assert await file_obj.read(1) == b'{"key":'
        assert await file_obj.read(1) == b'"value"}'
        assert await file_obj.read(1) == b""

    asyncio.run(main())


def test_from_iter_accepts_async_string_chunks_and_warns():
    async def chunks():
        yield '{"key":'
        yield '"value"}'

    async def main():
        file_obj = ijson.from_iter(chunks())
        with pytest.deprecated_call():
            assert await file_obj.read(1) == b'{"key":'
        assert await file_obj.read(1) == b'"value"}'
        assert await file_obj.read(1) == b""

    asyncio.run(main())