Esempio n. 1
0
def test_simple(io_reader, data, kind, 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

    if kind == Kind.ITER:
        extracted = streamson.extract_iter((e for e in data),
                                           [(matcher, handler)], extract_path)
    elif kind == Kind.FD:
        extracted = streamson.extract_fd(io_reader, [(matcher, handler)], 5,
                                         extract_path)

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

    with pytest.raises(StopIteration):
        next(output)

    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
Esempio n. 2
0
def test_invert(io_reader, data, kind, convert, extract_path):
    matcher = ~streamson.DepthMatcher("2")
    buff_handler = BufferHandler(use_path=extract_path)
    handler = PythonConverterHandler(
        convert, extract_path) + buff_handler if convert else buff_handler

    if kind == Kind.ITER:
        extracted = streamson.extract_iter((e for e in data),
                                           [(matcher, handler)], extract_path)
    elif kind == Kind.FD:
        extracted = streamson.extract_fd(io_reader, [(matcher, handler)], 5,
                                         extract_path)
    output = Output(extracted).generator()

    assert next(output) == (
        "" if extract_path else None,
        b'{"users": ["john", "carl", "bob"], "groups": ["admins", "users"]}',
    )
    with pytest.raises(StopIteration):
        next(output)

    convert = convert if convert else (lambda x: x)
    assert buff_handler.pop_front() == (
        "" if extract_path else None,
        [
            e for e in convert(
                b'{"users": ["john", "carl", "bob"], "groups": ["admins", "users"]}'
            )
        ],
    )
    assert buff_handler.pop_front() is None
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
Esempio n. 4
0
def test_simple(io_reader, data, kind, convert):
    matcher = streamson.SimpleMatcher('{"users"}[]')
    buff_handler = BufferHandler()
    handler = PythonConverterHandler(
        convert) + buff_handler if convert else buff_handler

    if kind == Kind.ITER:
        filtered = streamson.filter_iter((e for e in data),
                                         [(matcher, handler)])
    elif kind == Kind.FD:
        filtered = streamson.filter_fd(io_reader, [(matcher, handler)], 5)

    output = Output(filtered).generator()
    assert next(output) == (None,
                            b'{"users": [], "groups": ["admins", "users"]}')
    with pytest.raises(StopIteration):
        next(output)

    convert = convert if convert else (lambda x: x)
    assert buff_handler.pop_front() == ('{"users"}[0]',
                                        [e for e in convert(b'"john"')])
    assert buff_handler.pop_front() == ('{"users"}[1]',
                                        [e for e in convert(b'"carl"')])
    assert buff_handler.pop_front() == ('{"users"}[2]',
                                        [e for e in convert(b'"bob"')])
    assert buff_handler.pop_front() is None
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