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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
"""
Tests for the jsonlines library.
"""
import codecs
import collections
import io
import json
import tempfile
import jsonlines
import pytest
SAMPLE_BYTES = b'{"a": 1}\n{"b": 2}\n'
SAMPLE_TEXT = SAMPLE_BYTES.decode("utf-8")
def is_json_decode_error(exc: object) -> bool:
if type(exc).__module__ == "ujson":
# The ujson package has its own ujson.JSONDecodeError; because of the
# line above this function also works if it's not installed.
import ujson
return isinstance(exc, ujson.JSONDecodeError)
else:
# Otherwise, this should be a stdlib json.JSONDecodeError, which also
# works for orjson since orjson.JSONDecodeError inherits from it.
return isinstance(exc, json.JSONDecodeError)
def test_reader() -> None:
fp = io.BytesIO(SAMPLE_BYTES)
with jsonlines.Reader(fp) as reader:
it = iter(reader)
assert next(it) == {"a": 1}
assert next(it) == {"b": 2}
with pytest.raises(StopIteration):
next(it)
with pytest.raises(EOFError):
reader.read()
def test_reading_from_iterable() -> None:
with jsonlines.Reader(["1", b"{}"]) as reader:
assert list(reader) == [1, {}]
assert "wrapping <list at " in repr(reader)
def test_reader_rfc7464_text_sequences() -> None:
fp = io.BytesIO(b'\x1e"a"\x0a\x1e"b"\x0a')
with jsonlines.Reader(fp) as reader:
assert list(reader) == ["a", "b"]
def test_reader_utf8_bom_bytes() -> None:
"""
UTF-8 BOM is ignored, even if it occurs in the middle of a stream.
"""
chunks = [
codecs.BOM_UTF8,
b"1\n",
codecs.BOM_UTF8,
b"2\n",
]
fp = io.BytesIO(b"".join(chunks))
with jsonlines.Reader(fp) as reader:
assert list(reader) == [1, 2]
def test_reader_utf8_bom_text() -> None:
"""
Text version of ``test_reader_utf8_bom_bytes()``.
"""
chunks = [
"1\n",
codecs.BOM_UTF8.decode(),
"2\n",
]
fp = io.StringIO("".join(chunks))
with jsonlines.Reader(fp) as reader:
assert list(reader) == [1, 2]
def test_reader_utf8_bom_bom_bom() -> None:
"""
Too many UTF-8 BOM BOM BOM chars cause BOOM 💥 BOOM.
"""
reader = jsonlines.Reader([codecs.BOM_UTF8.decode() * 3 + "1\n"])
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
reader.read()
exc = excinfo.value
assert "invalid json" in str(exc)
assert is_json_decode_error(exc.__cause__)
def test_writer_text() -> None:
fp = io.StringIO()
with jsonlines.Writer(fp) as writer:
writer.write({"a": 1})
writer.write({"b": 2})
assert fp.getvalue() == SAMPLE_TEXT
def test_writer_binary() -> None:
fp = io.BytesIO()
with jsonlines.Writer(fp) as writer:
writer.write_all(
[
{"a": 1},
{"b": 2},
]
)
assert fp.getvalue() == SAMPLE_BYTES
def test_closing() -> None:
reader = jsonlines.Reader([])
reader.close()
with pytest.raises(RuntimeError):
reader.read()
writer = jsonlines.Writer(io.BytesIO())
writer.close()
writer.close() # no-op
with pytest.raises(RuntimeError):
writer.write(123)
def test_invalid_lines() -> None:
data = "[1, 2"
with jsonlines.Reader(io.StringIO(data)) as reader:
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
reader.read()
exc = excinfo.value
assert "invalid json" in str(exc)
assert exc.line == data
assert is_json_decode_error(exc.__cause__)
def test_skip_invalid() -> None:
fp = io.StringIO("12\ninvalid\n34")
reader = jsonlines.Reader(fp)
it = reader.iter(skip_invalid=True)
assert next(it) == 12
assert next(it) == 34
def test_empty_strings_in_iterable() -> None:
input = ["123", "", "456"]
it = iter(jsonlines.Reader(input))
assert next(it) == 123
with pytest.raises(jsonlines.InvalidLineError):
next(it)
with pytest.raises(StopIteration):
next(it)
it = jsonlines.Reader(input).iter(skip_empty=True)
assert list(it) == [123, 456]
def test_invalid_utf8() -> None:
with jsonlines.Reader([b"\xff\xff"]) as reader:
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
reader.read()
assert "line is not valid utf-8" in str(excinfo.value)
def test_empty_lines() -> None:
data_with_empty_line = b"1\n\n2\n"
with jsonlines.Reader(io.BytesIO(data_with_empty_line)) as reader:
assert reader.read()
with pytest.raises(jsonlines.InvalidLineError):
reader.read()
assert reader.read() == 2
with pytest.raises(EOFError):
reader.read()
with jsonlines.Reader(io.BytesIO(data_with_empty_line)) as reader:
assert list(reader.iter(skip_empty=True)) == [1, 2]
def test_typed_reads() -> None:
with jsonlines.Reader(io.StringIO('12\ntrue\n"foo"\n')) as reader:
assert reader.read(type=int) == 12
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
reader.read(type=int)
exc = excinfo.value
assert "does not match requested type" in str(exc)
assert exc.line == "true"
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
reader.read(type=float)
exc = excinfo.value
assert "does not match requested type" in str(exc)
assert exc.line == '"foo"'
def test_typed_read_invalid_type() -> None:
reader = jsonlines.Reader([])
with pytest.raises(ValueError) as excinfo:
reader.read(type="nope") # type: ignore[call-overload]
exc = excinfo.value
assert str(exc) == "invalid type specified"
def test_typed_iteration() -> None:
fp = io.StringIO("1\n2\n")
with jsonlines.Reader(fp) as reader:
actual = list(reader.iter(type=int))
assert actual == [1, 2]
fp = io.StringIO("1\n2\n")
with jsonlines.Reader(fp) as reader:
it = reader.iter(type=str)
with pytest.raises(jsonlines.InvalidLineError) as excinfo:
next(it)
exc = excinfo.value
assert "does not match requested type" in str(exc)
def test_writer_flags() -> None:
fp = io.BytesIO()
with jsonlines.Writer(fp, compact=True, sort_keys=True) as writer:
writer.write(
collections.OrderedDict(
[
("b", 2),
("a", 1),
]
)
)
assert fp.getvalue() == b'{"a":1,"b":2}\n'
def test_custom_dumps() -> None:
fp = io.BytesIO()
writer = jsonlines.Writer(fp, dumps=lambda obj: "oh hai")
with writer:
nbytes = writer.write({})
assert nbytes == len(b"oh hai\n")
assert fp.getvalue() == b"oh hai\n"
def test_custom_dumps_bytes() -> None:
"""
A custom dump function that returns bytes (e.g. ‘orjson’) should work.
"""
fp = io.BytesIO()
writer = jsonlines.Writer(fp, dumps=lambda obj: b"some bytes")
with writer:
writer.write(123)
assert fp.getvalue() == b"some bytes\n"
def test_custom_loads() -> None:
fp = io.BytesIO(b"{}\n")
with jsonlines.Reader(fp, loads=lambda s: "uh what") as reader:
assert reader.read() == "uh what"
def test_open_reading() -> None:
with tempfile.NamedTemporaryFile("wb") as fp:
fp.write(b"123\n")
fp.flush()
with jsonlines.open(fp.name) as reader:
assert list(reader) == [123]
def test_open_reading_with_utf8_bom() -> None:
"""
The ``.open()`` helper ignores a UTF-8 BOM.
"""
with tempfile.NamedTemporaryFile("wb") as fp:
fp.write(codecs.BOM_UTF8)
fp.write(b"123\n")
fp.flush()
with jsonlines.open(fp.name) as reader:
assert list(reader) == [123]
def test_open_writing() -> None:
with tempfile.NamedTemporaryFile("w+b") as fp:
with jsonlines.open(fp.name, mode="w") as writer:
writer.write(123)
assert fp.read() == b"123\n"
assert fp.name in repr(writer)
def test_open_and_append_writing() -> None:
with tempfile.NamedTemporaryFile("w+b") as fp:
with jsonlines.open(fp.name, mode="w") as writer:
nbytes = writer.write(123)
assert nbytes == len(str(123)) + 1
with jsonlines.open(fp.name, mode="a") as writer:
nbytes = writer.write(456)
assert nbytes == len(str(456)) + 1
assert fp.read() == b"123\n456\n"
def test_open_invalid_mode() -> None:
with pytest.raises(ValueError) as excinfo:
jsonlines.open("foo", mode="foo")
assert "mode" in str(excinfo.value)
def test_single_char_stripping() -> None:
""" "
Sanity check that a helper constant actually contains single-char strings.
"""
assert all(len(s) == 1 for s in jsonlines.jsonlines.SKIPPABLE_SINGLE_INITIAL_CHARS)
|