| 335 | |
| 336 | |
| 337 | def test_history_from_json(hist) -> None: |
| 338 | import json |
| 339 | |
| 340 | from cmd2.history import ( |
| 341 | History, |
| 342 | ) |
| 343 | |
| 344 | assert hist.from_json(hist_json) == hist |
| 345 | |
| 346 | # Test invalid JSON |
| 347 | with pytest.raises(json.JSONDecodeError): |
| 348 | hist.from_json("") |
| 349 | |
| 350 | # Send JSON with missing required element |
| 351 | with pytest.raises(KeyError): |
| 352 | hist.from_json("{}") |
| 353 | |
| 354 | # Create JSON with invalid history version |
| 355 | backed_up_ver = History._history_version |
| 356 | History._history_version = "BAD_VERSION" |
| 357 | invalid_ver_json = hist.to_json() |
| 358 | History._history_version = backed_up_ver |
| 359 | |
| 360 | expected_err = f"Unsupported history file version: BAD_VERSION. This application uses version {History._history_version}." |
| 361 | with pytest.raises(ValueError, match=expected_err): |
| 362 | hist.from_json(invalid_ver_json) |
| 363 | |
| 364 | |
| 365 | # |