コード例 #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
コード例 #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