|
14 | 14 | # limitations under the License.
|
15 | 15 | from __future__ import annotations
|
16 | 16 |
|
| 17 | +import json |
17 | 18 | from unittest.mock import MagicMock
|
18 | 19 |
|
19 | 20 | import pytest
|
|
22 | 23 | EntityRelationExtractor,
|
23 | 24 | LLMEntityRelationExtractor,
|
24 | 25 | OnError,
|
| 26 | + balance_curly_braces, |
| 27 | + fix_invalid_json, |
25 | 28 | )
|
26 | 29 | from neo4j_genai.experimental.components.types import (
|
27 | 30 | Neo4jGraph,
|
@@ -214,3 +217,187 @@ async def test_extractor_custom_prompt() -> None:
|
214 | 217 | chunks = TextChunks(chunks=[TextChunk(text="some text")])
|
215 | 218 | await extractor.run(chunks=chunks)
|
216 | 219 | llm.invoke.assert_called_once_with("this is my prompt")
|
| 220 | + |
| 221 | + |
| 222 | +def test_fix_unquoted_keys() -> None: |
| 223 | + json_string = '{name: "John", age: "30"}' |
| 224 | + expected_result = '{"name": "John", "age": "30"}' |
| 225 | + |
| 226 | + fixed_json = fix_invalid_json(json_string) |
| 227 | + |
| 228 | + assert json.loads(fixed_json) |
| 229 | + assert fixed_json == expected_result |
| 230 | + |
| 231 | + |
| 232 | +def test_fix_unquoted_string_values() -> None: |
| 233 | + json_string = '{"name": John, "age": 30}' |
| 234 | + expected_result = '{"name": "John", "age": 30}' |
| 235 | + |
| 236 | + fixed_json = fix_invalid_json(json_string) |
| 237 | + |
| 238 | + assert json.loads(fixed_json) |
| 239 | + assert fixed_json == expected_result |
| 240 | + |
| 241 | + |
| 242 | +def test_remove_trailing_commas() -> None: |
| 243 | + json_string = '{"name": "John", "age": 30,}' |
| 244 | + expected_result = '{"name": "John", "age": 30}' |
| 245 | + |
| 246 | + fixed_json = fix_invalid_json(json_string) |
| 247 | + |
| 248 | + assert json.loads(fixed_json) |
| 249 | + assert fixed_json == expected_result |
| 250 | + |
| 251 | + |
| 252 | +def test_fix_excessive_braces() -> None: |
| 253 | + json_string = '{{"name": "John"}}' |
| 254 | + expected_result = '{"name": "John"}' |
| 255 | + |
| 256 | + fixed_json = fix_invalid_json(json_string) |
| 257 | + |
| 258 | + assert json.loads(fixed_json) |
| 259 | + assert fixed_json == expected_result |
| 260 | + |
| 261 | + |
| 262 | +def test_fix_multiple_issues() -> None: |
| 263 | + json_string = '{name: John, "hobbies": ["reading", "swimming",], "age": 30}' |
| 264 | + expected_result = '{"name": "John", "hobbies": ["reading", "swimming"], "age": 30}' |
| 265 | + |
| 266 | + fixed_json = fix_invalid_json(json_string) |
| 267 | + |
| 268 | + assert json.loads(fixed_json) |
| 269 | + assert fixed_json == expected_result |
| 270 | + |
| 271 | + |
| 272 | +def test_fix_null_values() -> None: |
| 273 | + json_string = '{"name": John, "nickname": null}' |
| 274 | + expected_result = '{"name": "John", "nickname": null}' |
| 275 | + |
| 276 | + fixed_json = fix_invalid_json(json_string) |
| 277 | + |
| 278 | + assert json.loads(fixed_json) |
| 279 | + assert fixed_json == expected_result |
| 280 | + |
| 281 | + |
| 282 | +def test_fix_numeric_values() -> None: |
| 283 | + json_string = '{"age": 30, "score": 95.5}' |
| 284 | + expected_result = '{"age": 30, "score": 95.5}' |
| 285 | + |
| 286 | + fixed_json = fix_invalid_json(json_string) |
| 287 | + |
| 288 | + assert json.loads(fixed_json) |
| 289 | + assert fixed_json == expected_result |
| 290 | + |
| 291 | + |
| 292 | +def test_balance_curly_braces_missing_closing() -> None: |
| 293 | + json_string = '{"name": "John", "hobbies": {"reading": "yes"' |
| 294 | + expected_result = '{"name": "John", "hobbies": {"reading": "yes"}}' |
| 295 | + |
| 296 | + fixed_json = balance_curly_braces(json_string) |
| 297 | + |
| 298 | + assert json.loads(fixed_json) |
| 299 | + assert fixed_json == expected_result |
| 300 | + |
| 301 | + |
| 302 | +def test_balance_curly_braces_extra_closing() -> None: |
| 303 | + json_string = '{"name": "John", "hobbies": {"reading": "yes"}}}' |
| 304 | + expected_result = '{"name": "John", "hobbies": {"reading": "yes"}}' |
| 305 | + |
| 306 | + fixed_json = balance_curly_braces(json_string) |
| 307 | + |
| 308 | + assert json.loads(fixed_json) |
| 309 | + assert fixed_json == expected_result |
| 310 | + |
| 311 | + |
| 312 | +def test_balance_curly_braces_balanced_input() -> None: |
| 313 | + json_string = '{"name": "John", "hobbies": {"reading": "yes"}, "age": 30}' |
| 314 | + expected_result = json_string |
| 315 | + |
| 316 | + fixed_json = balance_curly_braces(json_string) |
| 317 | + |
| 318 | + assert json.loads(fixed_json) |
| 319 | + assert fixed_json == expected_result |
| 320 | + |
| 321 | + |
| 322 | +def test_balance_curly_braces_nested_structure() -> None: |
| 323 | + json_string = '{"person": {"name": "John", "hobbies": {"reading": "yes"}}}' |
| 324 | + expected_result = json_string |
| 325 | + |
| 326 | + fixed_json = balance_curly_braces(json_string) |
| 327 | + |
| 328 | + assert json.loads(fixed_json) |
| 329 | + assert fixed_json == expected_result |
| 330 | + |
| 331 | + |
| 332 | +def test_balance_curly_braces_unbalanced_nested() -> None: |
| 333 | + json_string = '{"person": {"name": "John", "hobbies": {"reading": "yes"}}' |
| 334 | + expected_result = '{"person": {"name": "John", "hobbies": {"reading": "yes"}}}' |
| 335 | + |
| 336 | + fixed_json = balance_curly_braces(json_string) |
| 337 | + |
| 338 | + assert json.loads(fixed_json) |
| 339 | + assert fixed_json == expected_result |
| 340 | + |
| 341 | + |
| 342 | +def test_balance_curly_braces_unmatched_openings() -> None: |
| 343 | + json_string = '{"name": "John", "hobbies": {"reading": "yes"' |
| 344 | + expected_result = '{"name": "John", "hobbies": {"reading": "yes"}}' |
| 345 | + |
| 346 | + fixed_json = balance_curly_braces(json_string) |
| 347 | + |
| 348 | + assert json.loads(fixed_json) |
| 349 | + assert fixed_json == expected_result |
| 350 | + |
| 351 | + |
| 352 | +def test_balance_curly_braces_unmatched_closings() -> None: |
| 353 | + json_string = '{"name": "John", "hobbies": {"reading": "yes"}}}' |
| 354 | + expected_result = '{"name": "John", "hobbies": {"reading": "yes"}}' |
| 355 | + |
| 356 | + fixed_json = balance_curly_braces(json_string) |
| 357 | + |
| 358 | + assert json.loads(fixed_json) |
| 359 | + assert fixed_json == expected_result |
| 360 | + |
| 361 | + |
| 362 | +def test_balance_curly_braces_complex_structure() -> None: |
| 363 | + json_string = ( |
| 364 | + '{"name": "John", "details": {"age": 30, "hobbies": {"reading": "yes"}}}' |
| 365 | + ) |
| 366 | + expected_result = json_string |
| 367 | + |
| 368 | + fixed_json = balance_curly_braces(json_string) |
| 369 | + |
| 370 | + assert json.loads(fixed_json) |
| 371 | + assert fixed_json == expected_result |
| 372 | + |
| 373 | + |
| 374 | +def test_balance_curly_braces_incorrect_nested_closings() -> None: |
| 375 | + json_string = '{"key1": {"key2": {"reading": "yes"}}, "key3": {"age": 30}}}' |
| 376 | + expected_result = '{"key1": {"key2": {"reading": "yes"}}, "key3": {"age": 30}}' |
| 377 | + |
| 378 | + fixed_json = balance_curly_braces(json_string) |
| 379 | + |
| 380 | + assert json.loads(fixed_json) |
| 381 | + assert fixed_json == expected_result |
| 382 | + |
| 383 | + |
| 384 | +def test_balance_curly_braces_braces_inside_string() -> None: |
| 385 | + json_string = '{"name": "John", "example": "a{b}c", "age": 30}' |
| 386 | + expected_result = json_string |
| 387 | + |
| 388 | + fixed_json = balance_curly_braces(json_string) |
| 389 | + |
| 390 | + assert json.loads(fixed_json) |
| 391 | + assert fixed_json == expected_result |
| 392 | + |
| 393 | + |
| 394 | +def test_balance_curly_braces_unbalanced_with_string() -> None: |
| 395 | + json_string = '{"name": "John", "example": "a{b}c", "hobbies": {"reading": "yes"' |
| 396 | + expected_result = ( |
| 397 | + '{"name": "John", "example": "a{b}c", "hobbies": {"reading": "yes"}}' |
| 398 | + ) |
| 399 | + |
| 400 | + fixed_json = balance_curly_braces(json_string) |
| 401 | + |
| 402 | + assert json.loads(fixed_json) |
| 403 | + assert fixed_json == expected_result |
0 commit comments