summaryrefslogtreecommitdiff
path: root/tests/test_history.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_history.py')
-rwxr-xr-xtests/test_history.py97
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/test_history.py b/tests/test_history.py
index bb857334..a4ec755e 100755
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -60,6 +60,72 @@ def hist():
return h
+# Represents the hist fixture's JSON
+hist_json = (
+ '{\n'
+ ' "history_version": "1.0.0",\n'
+ ' "history_items": [\n'
+ ' {\n'
+ ' "statement": {\n'
+ ' "args": "",\n'
+ ' "raw": "first",\n'
+ ' "command": "",\n'
+ ' "arg_list": [],\n'
+ ' "multiline_command": "",\n'
+ ' "terminator": "",\n'
+ ' "suffix": "",\n'
+ ' "pipe_to": "",\n'
+ ' "output": "",\n'
+ ' "output_to": ""\n'
+ ' }\n'
+ ' },\n'
+ ' {\n'
+ ' "statement": {\n'
+ ' "args": "",\n'
+ ' "raw": "second",\n'
+ ' "command": "",\n'
+ ' "arg_list": [],\n'
+ ' "multiline_command": "",\n'
+ ' "terminator": "",\n'
+ ' "suffix": "",\n'
+ ' "pipe_to": "",\n'
+ ' "output": "",\n'
+ ' "output_to": ""\n'
+ ' }\n'
+ ' },\n'
+ ' {\n'
+ ' "statement": {\n'
+ ' "args": "",\n'
+ ' "raw": "third",\n'
+ ' "command": "",\n'
+ ' "arg_list": [],\n'
+ ' "multiline_command": "",\n'
+ ' "terminator": "",\n'
+ ' "suffix": "",\n'
+ ' "pipe_to": "",\n'
+ ' "output": "",\n'
+ ' "output_to": ""\n'
+ ' }\n'
+ ' },\n'
+ ' {\n'
+ ' "statement": {\n'
+ ' "args": "",\n'
+ ' "raw": "fourth",\n'
+ ' "command": "",\n'
+ ' "arg_list": [],\n'
+ ' "multiline_command": "",\n'
+ ' "terminator": "",\n'
+ ' "suffix": "",\n'
+ ' "pipe_to": "",\n'
+ ' "output": "",\n'
+ ' "output_to": ""\n'
+ ' }\n'
+ ' }\n'
+ ' ]\n'
+ '}'
+)
+
+
@pytest.fixture
def persisted_hist():
from cmd2.cmd2 import (
@@ -256,6 +322,37 @@ def test_history_max_length(hist):
assert hist.get(2).statement.raw == 'fourth'
+def test_history_to_json(hist):
+ assert hist_json == hist.to_json()
+
+
+def test_history_from_json(hist):
+ import json
+
+ from cmd2.history import (
+ History,
+ )
+
+ assert hist.from_json(hist_json) == hist
+
+ # Test invalid JSON
+ with pytest.raises(json.JSONDecodeError):
+ hist.from_json("")
+
+ # Send JSON with missing required element
+ with pytest.raises(KeyError):
+ hist.from_json("{}")
+
+ # Create JSON with invalid history version
+ backed_up_ver = History._history_version
+ History._history_version = "BAD_VERSION"
+ invalid_ver_json = hist.to_json()
+ History._history_version = backed_up_ver
+
+ with pytest.raises(ValueError):
+ hist.from_json(invalid_ver_json)
+
+
#
# test HistoryItem()
#