From b613634e1cd2894b07e628e9f0c6ce5b603765f1 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Wed, 26 Jun 2024 12:08:39 +0100 Subject: [PATCH] improve coverage (#117) --- crates/jiter-python/tests/test_jiter.py | 29 +++++++++++++++++++++++++ crates/jiter/tests/python.rs | 10 ++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/jiter-python/tests/test_jiter.py b/crates/jiter-python/tests/test_jiter.py index 0612ba8..c0e6abb 100644 --- a/crates/jiter-python/tests/test_jiter.py +++ b/crates/jiter-python/tests/test_jiter.py @@ -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] @@ -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") @@ -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) diff --git a/crates/jiter/tests/python.rs b/crates/jiter/tests/python.rs index 9a5fa32..2124b9d 100644 --- a/crates/jiter/tests/python.rs +++ b/crates/jiter/tests/python.rs @@ -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() { @@ -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]"); + }) +}