Пример #1
0
async def test_depth(make_async_gen, convert, extract_path):
    matcher = streamson.DepthMatcher("1")
    buff_handler = BufferHandler(use_path=extract_path)
    handler = PythonConverterHandler(
        convert, extract_path) + buff_handler if convert else buff_handler

    async_out = streamson.extract_async(make_async_gen()(),
                                        [(matcher, handler)], extract_path)

    res = []
    async for rec in async_out:
        res.append(rec)

    output = list(Output(e for e in res).generator())

    assert len(output) == 1

    assert output[0] == (
        '{"users"}' if extract_path else None,
        b'["john", "carl", "bob"]',
    )

    convert = convert if convert else (lambda x: x)
    assert buff_handler.pop_front() == (
        '{"users"}' if extract_path else None,
        [e for e in convert(b'["john", "carl", "bob"]')],
    )
    assert buff_handler.pop_front() is None
Пример #2
0
async def test_simple(make_async_gen, convert, extract_path):
    matcher = streamson.SimpleMatcher('{"users"}[]')
    buff_handler = BufferHandler(use_path=extract_path)
    handler = PythonConverterHandler(
        convert, extract_path) + buff_handler if convert else buff_handler

    async_out = streamson.extract_async(make_async_gen()(),
                                        [(matcher, handler)], extract_path)

    res = []

    async for rec in async_out:
        res.append(rec)

    output = list(Output(e for e in res).generator())
    assert len(output) == 3

    assert output[0] == ('{"users"}[0]' if extract_path else None, b'"john"')
    assert output[1] == ('{"users"}[1]' if extract_path else None, b'"carl"')
    assert output[2] == ('{"users"}[2]' if extract_path else None, b'"bob"')

    convert = convert if convert else (lambda x: x)
    assert buff_handler.pop_front() == (
        '{"users"}[0]' if extract_path else None,
        [e for e in convert(b'"john"')],
    )
    assert buff_handler.pop_front() == (
        '{"users"}[1]' if extract_path else None,
        [e for e in convert(b'"carl"')],
    )
    assert buff_handler.pop_front() == (
        '{"users"}[2]' if extract_path else None,
        [e for e in convert(b'"bob"')],
    )
    assert buff_handler.pop_front() is None