Skip to content

Commit

Permalink
improve coverage (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jun 26, 2024
1 parent 7cb88e6 commit b613634
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
29 changes: 29 additions & 0 deletions crates/jiter-python/tests/test_jiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def test_extracted_value_error():

def test_partial_array():
json = b'["string", true, null, 1, "foo'

with pytest.raises(ValueError, match='EOF while parsing a string at line 1 column 30'):
jiter.from_json(json, partial_mode=False)

parsed = jiter.from_json(json, partial_mode=True)
assert parsed == ["string", True, None, 1]

Expand Down Expand Up @@ -150,6 +154,21 @@ def test_partial_nested():
assert isinstance(parsed, dict)


def test_partial_error():
json = b'["string", true, null, 1, "foo'

with pytest.raises(ValueError, match='EOF while parsing a string at line 1 column 30'):
jiter.from_json(json, partial_mode=False)

assert jiter.from_json(json, partial_mode=True) == ["string", True, None, 1]

msg = "Invalid partial mode, should be `'off'`, `'on'`, `'trailing-strings'` or a `bool`"
with pytest.raises(ValueError, match=msg):
jiter.from_json(json, partial_mode='wrong')
with pytest.raises(TypeError, match=msg):
jiter.from_json(json, partial_mode=123)


def test_python_cache_usage_all():
jiter.cache_clear()
parsed = jiter.from_json(b'{"foo": "bar", "spam": 3}', cache_mode="all")
Expand Down Expand Up @@ -254,3 +273,13 @@ def test_unicode_roundtrip_ensure_ascii():
json_data = json.dumps(original, ensure_ascii=False).encode()
assert jiter.from_json(json_data, cache_mode=False) == original
assert json.loads(json_data) == original


def test_catch_duplicate_keys():
assert jiter.from_json(b'{"foo": 1, "foo": 2}') == {"foo": 2}

with pytest.raises(ValueError, match='Detected duplicate key "foo" at line 1 column 18'):
jiter.from_json(b'{"foo": 1, "foo": 2}', catch_duplicate_keys=True)

with pytest.raises(ValueError, match='Detected duplicate key "foo" at line 1 column 28'):
jiter.from_json(b'{"foo": 1, "bar": 2, "foo": 2}', catch_duplicate_keys=True)
10 changes: 9 additions & 1 deletion crates/jiter/tests/python.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pyo3::prelude::*;
use pyo3::types::PyString;

use jiter::{pystring_fast_new, JsonValue, StringCacheMode};
use jiter::{pystring_fast_new, JsonValue, PythonParse, StringCacheMode};

#[test]
fn test_to_py_object_numeric() {
Expand Down Expand Up @@ -84,3 +84,11 @@ fn test_pystring_fast_new_ascii() {
assert_eq!(s.to_string(), "100abc");
})
}

#[test]
fn test_python_parse_default() {
Python::with_gil(|py| {
let v = PythonParse::default().python_parse(py, b"[123]").unwrap();
assert_eq!(v.to_string(), "[123]");
})
}

0 comments on commit b613634

Please sign in to comment.