File: test_items.py

package info (click to toggle)
python-ijson 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 668 kB
  • sloc: python: 2,687; ansic: 1,776; sh: 4; makefile: 3
file content (98 lines) | stat: -rw-r--r-- 3,370 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Tests for the ijson.items method"""

import collections
import pytest

from .test_base import ARRAY_JSON, ARRAY_JSON_OBJECT, EMPTY_MEMBER_TEST_CASES, JSON, JSON_OBJECT

from ijson import JSONError

def test_items(adaptor):
    assert [JSON_OBJECT] == adaptor.items(JSON, '')


def test_items_array(adaptor):
    assert [ARRAY_JSON_OBJECT] == adaptor.items(ARRAY_JSON, '')


def test_items_twodictlevels(adaptor):
    json = b'{"meta":{"view":{"columns":[{"id": -1}, {"id": -2}]}}}'
    ids = adaptor.items(json, 'meta.view.columns.item.id')
    assert 2 == len(ids)
    assert [-2,-1], sorted(ids)


@pytest.mark.parametrize(
    "json, prefix, expected_items",
    (
        (b'{"0.1": 0}', '0.1', [0]),
        (b'{"0.1": [{"a.b": 0}]}', '0.1.item.a.b', [0]),
        (b'{"0.1": 0, "0": {"1": 1}}', '0.1', [0, 1]),
        (b'{"abc.def": 0}', 'abc.def', [0]),
        (b'{"abc.def": 0}', 'abc', []),
        (b'{"abc.def": 0}', 'def', []),
    )
)
def test_items_with_dotted_name(adaptor, json, prefix, expected_items):
    assert expected_items == adaptor.items(json, prefix)


def test_map_type(adaptor):
    obj = adaptor.items(JSON, '')[0]
    assert isinstance(obj, dict)
    obj = adaptor.items(JSON, '', map_type=collections.OrderedDict)[0]
    assert isinstance(obj, collections.OrderedDict)


@pytest.mark.parametrize("test_case", [
    pytest.param(value, id=name) for name, value in EMPTY_MEMBER_TEST_CASES.items()
])
def test_items_empty_member(adaptor, test_case):
    assert test_case.items == adaptor.items(test_case.json, test_case.prefix)


def test_multiple_values_raises_if_not_supported(adaptor):
    """Test that setting multiple_values raises if not supported"""
    if not adaptor.backend.capabilities.multiple_values:
        with pytest.raises(ValueError):
            adaptor.items("", "", multiple_values=True)


def test_multiple_values(adaptor):
    """Test that multiple_values are supported"""
    multiple_json = JSON + JSON + JSON
    with pytest.raises(JSONError):
        adaptor.items(multiple_json, "")
    with pytest.raises(JSONError):
        adaptor.items(multiple_json, "", multiple_values=False)
    result = adaptor.items(multiple_json, "", multiple_values=True)
    assert [JSON_OBJECT, JSON_OBJECT, JSON_OBJECT] == result


def test_coro_needs_input_with_three_elements(backend):
    int_element_parse_events = list(backend.parse(b'0'))
    # all good
    assert [0] == list(backend.items(int_element_parse_events, ''))
    # one more element in event
    with pytest.raises(ValueError, match="too many values"):
        next(backend.items((event + ('extra dummy',) for event in int_element_parse_events), ''))
    # one less
    with pytest.raises(ValueError, match="not enough values"):
        next(backend.items((event[:-1] for event in int_element_parse_events), ''))
    # not an iterable
    with pytest.raises(TypeError, match="cannot unpack"):
        next(backend.items([None], ''))


def test_user_events(backend):
    """
    User-provided events work correct -- yajl2_c used to fail with event names
    that weren't generated by itself.
    """
    embedded_empty_list_events = [
        ('', 'start_array', None),
        ('item', 'start_array', None),
        ('item', 'end_array', None),
        ('', 'end_array', None),
    ]
    assert [[]] == list(backend.items(embedded_empty_list_events, 'item'))