Skip to content

Commit b613634

Browse files
authored
improve coverage (#117)
1 parent 7cb88e6 commit b613634

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

crates/jiter-python/tests/test_jiter.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def test_extracted_value_error():
6464

6565
def test_partial_array():
6666
json = b'["string", true, null, 1, "foo'
67+
68+
with pytest.raises(ValueError, match='EOF while parsing a string at line 1 column 30'):
69+
jiter.from_json(json, partial_mode=False)
70+
6771
parsed = jiter.from_json(json, partial_mode=True)
6872
assert parsed == ["string", True, None, 1]
6973

@@ -150,6 +154,21 @@ def test_partial_nested():
150154
assert isinstance(parsed, dict)
151155

152156

157+
def test_partial_error():
158+
json = b'["string", true, null, 1, "foo'
159+
160+
with pytest.raises(ValueError, match='EOF while parsing a string at line 1 column 30'):
161+
jiter.from_json(json, partial_mode=False)
162+
163+
assert jiter.from_json(json, partial_mode=True) == ["string", True, None, 1]
164+
165+
msg = "Invalid partial mode, should be `'off'`, `'on'`, `'trailing-strings'` or a `bool`"
166+
with pytest.raises(ValueError, match=msg):
167+
jiter.from_json(json, partial_mode='wrong')
168+
with pytest.raises(TypeError, match=msg):
169+
jiter.from_json(json, partial_mode=123)
170+
171+
153172
def test_python_cache_usage_all():
154173
jiter.cache_clear()
155174
parsed = jiter.from_json(b'{"foo": "bar", "spam": 3}', cache_mode="all")
@@ -254,3 +273,13 @@ def test_unicode_roundtrip_ensure_ascii():
254273
json_data = json.dumps(original, ensure_ascii=False).encode()
255274
assert jiter.from_json(json_data, cache_mode=False) == original
256275
assert json.loads(json_data) == original
276+
277+
278+
def test_catch_duplicate_keys():
279+
assert jiter.from_json(b'{"foo": 1, "foo": 2}') == {"foo": 2}
280+
281+
with pytest.raises(ValueError, match='Detected duplicate key "foo" at line 1 column 18'):
282+
jiter.from_json(b'{"foo": 1, "foo": 2}', catch_duplicate_keys=True)
283+
284+
with pytest.raises(ValueError, match='Detected duplicate key "foo" at line 1 column 28'):
285+
jiter.from_json(b'{"foo": 1, "bar": 2, "foo": 2}', catch_duplicate_keys=True)

crates/jiter/tests/python.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use pyo3::prelude::*;
22
use pyo3::types::PyString;
33

4-
use jiter::{pystring_fast_new, JsonValue, StringCacheMode};
4+
use jiter::{pystring_fast_new, JsonValue, PythonParse, StringCacheMode};
55

66
#[test]
77
fn test_to_py_object_numeric() {
@@ -84,3 +84,11 @@ fn test_pystring_fast_new_ascii() {
8484
assert_eq!(s.to_string(), "100abc");
8585
})
8686
}
87+
88+
#[test]
89+
fn test_python_parse_default() {
90+
Python::with_gil(|py| {
91+
let v = PythonParse::default().python_parse(py, b"[123]").unwrap();
92+
assert_eq!(v.to_string(), "[123]");
93+
})
94+
}

0 commit comments

Comments
 (0)