aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/cyson/ut/test_control_attributes.py
blob: 221542b12dc7a8ab7bf30d704fdae0e7562e5129 (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-

from __future__ import print_function, absolute_import, division

import itertools
from functools import partial

import pytest
import six

from cyson import (
    YsonEntity, InputStream,
    list_fragments, key_switched_list_fragments,
    Reader, UnicodeReader
)


def filter_control_records(list):
    return [
        _ for _ in list
        if not isinstance(_[2], YsonEntity)
    ]


def canonize(val, as_unicode):
    _canonize = partial(canonize, as_unicode=as_unicode)

    if isinstance(val, six.binary_type) and as_unicode:
        return val.decode('utf8')
    elif isinstance(val, six.text_type) and not as_unicode:
        return val.encode('utf8')
    elif isinstance(val, (list, tuple)):
        return [_canonize(elem) for elem in val]
    elif isinstance(val, dict):
        return {_canonize(k): _canonize(v) for k, v in val.items()}
    return val


@pytest.mark.parametrize(
    'reader, as_unicode', [
        [Reader, False],
        [UnicodeReader, True],
    ],
)
@pytest.mark.parametrize(
    'keep_control_records', [True, False]
)
def test_row_index(keep_control_records, reader, as_unicode):
    _ = partial(canonize, as_unicode=as_unicode)

    data = b"""
        <row_index=0>#;
        {a=1;b=2};
        {a=2;b=3};
        {a=3;b=4};
        <row_index=10000>#;
        {a=-1;b=-1};
        {a=-2;b=-2};
    """

    iter = list_fragments(
        stream=InputStream.from_string(data),
        Reader=reader,
        process_attributes=True,
        keep_control_records=keep_control_records,
    )
    records = [(iter.range_index, iter.row_index, __) for __ in iter]

    etalon = [
        (None, -1, YsonEntity(attributes={b'row_index': 0})),
        (None, 0, _({b'a': 1, b'b': 2})),
        (None, 1, _({b'a': 2, b'b': 3})),
        (None, 2, _({b'a': 3, b'b': 4})),
        (None, 9999, YsonEntity(attributes={b'row_index': 10000})),
        (None, 10000, _({b'a': -1, b'b': -1})),
        (None, 10001, _({b'a': -2, b'b': -2})),
    ]

    if not keep_control_records:
        etalon = filter_control_records(etalon)

    assert records == etalon


@pytest.mark.parametrize(
    'reader, as_unicode', [
        [Reader, False],
        [UnicodeReader, True],
    ]
)
@pytest.mark.parametrize(
    'keep_control_records', [True, False],
)
@pytest.mark.parametrize(
    'parameter_name',
    ['process_attributes', 'process_table_index']
)
def test_range_index(parameter_name, keep_control_records, reader, as_unicode):
    _ = partial(canonize, as_unicode=as_unicode)

    data = b"""
        <range_index=2; row_index=0>#;
        {a=1;b=2};
        {a=2;b=3};
        {a=3;b=4};
        <range_index=0; row_index=10000>#;
        {a=-1;b=-1};
        {a=-2;b=-2};
    """

    iter = list_fragments(
        stream=InputStream.from_string(data),
        Reader=reader,
        **{parameter_name: True, 'keep_control_records': keep_control_records}
    )
    records = [(iter.range_index, iter.row_index, __) for __ in iter]

    etalon = [
        (2, -1, YsonEntity(attributes={b'range_index': 2, b'row_index': 0})),
        (2, 0, _({b'a': 1, b'b': 2})),
        (2, 1, _({b'a': 2, b'b': 3})),
        (2, 2, _({b'a': 3, b'b': 4})),
        (0, 9999, YsonEntity(attributes={b'range_index': 0, b'row_index': 10000})),
        (0, 10000, _({b'a': -1, b'b': -1})),
        (0, 10001, _({b'a': -2, b'b': -2})),
    ]

    if not keep_control_records:
        etalon = filter_control_records(etalon)

    assert records == etalon


@pytest.mark.parametrize(
    'reader, as_unicode', [
        [Reader, False],
        [UnicodeReader, True],
    ]
)
def test_key_switch_first(reader, as_unicode):
    _ = partial(canonize, as_unicode=as_unicode)

    data = b"""
        <key_switch=True>#;
        {k=1;a=1;b=2};
        {k=1;a=2;b=3};
        {k=1;a=3;b=4};
        <key_switch=True>#;
        {k=2;a=-1;b=-1};
        {k=2;a=-2;b=-2};
    """

    iter = key_switched_list_fragments(
        stream=InputStream.from_string(data),
        Reader=reader,
    )
    records = [list(__) for __ in iter]

    assert records == [
        [
            _({b'k': 1, b'a': 1, b'b': 2}),
            _({b'k': 1, b'a': 2, b'b': 3}),
            _({b'k': 1, b'a': 3, b'b': 4}),
        ],
        [
            _({b'k': 2, b'a': -1, b'b': -1}),
            _({b'k': 2, b'a': -2, b'b': -2}),
        ]
    ]


@pytest.mark.parametrize(
    'reader, as_unicode', [
        [Reader, False],
        [UnicodeReader, True],
    ]
)
def test_key_switch_nofirst(reader, as_unicode):
    _ = partial(canonize, as_unicode=as_unicode)

    data = b"""
        {k=1;a=1;b=2};
        {k=1;a=2;b=3};
        {k=1;a=3;b=4};
        <key_switch=True>#;
        {k=2;a=-1;b=-1};
        {k=2;a=-2;b=-2};
    """

    iter = key_switched_list_fragments(
        stream=InputStream.from_string(data),
        Reader=reader
    )
    records = [list(__) for __ in iter]

    assert records == [
        [
            _({b'k': 1, b'a': 1, b'b': 2}),
            _({b'k': 1, b'a': 2, b'b': 3}),
            _({b'k': 1, b'a': 3, b'b': 4}),
        ],
        [
            _({b'k': 2, b'a': -1, b'b': -1}),
            _({b'k': 2, b'a': -2, b'b': -2}),
        ]
    ]


@pytest.mark.parametrize(
    'reader, as_unicode', [
        [Reader, False],
        [UnicodeReader, True],
    ]
)
def test_key_switch_exhaust_unused_records(reader, as_unicode):
    _ = partial(canonize, as_unicode=as_unicode)

    data = b"""
        {k=1;a=1;b=2};
        {k=1;a=2;b=3};
        {k=1;a=3;b=4};
        <key_switch=True>#;
        {k=2;a=-1;b=-1};
        {k=2;a=-2;b=-2};
    """

    iter = key_switched_list_fragments(
        stream=InputStream.from_string(data),
        Reader=reader,
    )

    records = []

    for group in iter:
        records.append(
            list(itertools.islice(group, 2))
        )

    assert records == [
        [
            _({b'k': 1, b'a': 1, b'b': 2}),
            _({b'k': 1, b'a': 2, b'b': 3}),
        ],
        [
            _({b'k': 2, b'a': -1, b'b': -1}),
            _({b'k': 2, b'a': -2, b'b': -2}),
        ]
    ]


@pytest.mark.parametrize('reader', [Reader, UnicodeReader])
def test_key_switch_empty(reader):
    assert list(
        key_switched_list_fragments(
            stream=InputStream.from_string(""),
            Reader=reader,
        )
    ) == []