Skip to content

Commit 33e8b86

Browse files
committed
Added unit tests for History and Statement
1 parent 41ec8d9 commit 33e8b86

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

tests/test_history.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,72 @@ def hist():
6060
return h
6161

6262

63+
# Represents the hist fixture's JSON
64+
hist_json = (
65+
'{\n'
66+
' "history_version": "1.0.0",\n'
67+
' "history_items": [\n'
68+
' {\n'
69+
' "statement": {\n'
70+
' "args": "",\n'
71+
' "raw": "first",\n'
72+
' "command": "",\n'
73+
' "arg_list": [],\n'
74+
' "multiline_command": "",\n'
75+
' "terminator": "",\n'
76+
' "suffix": "",\n'
77+
' "pipe_to": "",\n'
78+
' "output": "",\n'
79+
' "output_to": ""\n'
80+
' }\n'
81+
' },\n'
82+
' {\n'
83+
' "statement": {\n'
84+
' "args": "",\n'
85+
' "raw": "second",\n'
86+
' "command": "",\n'
87+
' "arg_list": [],\n'
88+
' "multiline_command": "",\n'
89+
' "terminator": "",\n'
90+
' "suffix": "",\n'
91+
' "pipe_to": "",\n'
92+
' "output": "",\n'
93+
' "output_to": ""\n'
94+
' }\n'
95+
' },\n'
96+
' {\n'
97+
' "statement": {\n'
98+
' "args": "",\n'
99+
' "raw": "third",\n'
100+
' "command": "",\n'
101+
' "arg_list": [],\n'
102+
' "multiline_command": "",\n'
103+
' "terminator": "",\n'
104+
' "suffix": "",\n'
105+
' "pipe_to": "",\n'
106+
' "output": "",\n'
107+
' "output_to": ""\n'
108+
' }\n'
109+
' },\n'
110+
' {\n'
111+
' "statement": {\n'
112+
' "args": "",\n'
113+
' "raw": "fourth",\n'
114+
' "command": "",\n'
115+
' "arg_list": [],\n'
116+
' "multiline_command": "",\n'
117+
' "terminator": "",\n'
118+
' "suffix": "",\n'
119+
' "pipe_to": "",\n'
120+
' "output": "",\n'
121+
' "output_to": ""\n'
122+
' }\n'
123+
' }\n'
124+
' ]\n'
125+
'}'
126+
)
127+
128+
63129
@pytest.fixture
64130
def persisted_hist():
65131
from cmd2.cmd2 import (
@@ -256,6 +322,37 @@ def test_history_max_length(hist):
256322
assert hist.get(2).statement.raw == 'fourth'
257323

258324

325+
def test_history_to_json(hist):
326+
assert hist_json == hist.to_json()
327+
328+
329+
def test_history_from_json(hist):
330+
import json
331+
332+
from cmd2.history import (
333+
History,
334+
)
335+
336+
assert hist.from_json(hist_json) == hist
337+
338+
# Test invalid JSON
339+
with pytest.raises(json.JSONDecodeError):
340+
hist.from_json("")
341+
342+
# Send JSON with missing required element
343+
with pytest.raises(KeyError):
344+
hist.from_json("{}")
345+
346+
# Create JSON with invalid history version
347+
backed_up_ver = History._history_version
348+
History._history_version = "BAD_VERSION"
349+
invalid_ver_json = hist.to_json()
350+
History._history_version = backed_up_ver
351+
352+
with pytest.raises(ValueError):
353+
hist.from_json(invalid_ver_json)
354+
355+
259356
#
260357
# test HistoryItem()
261358
#

tests/test_parsing.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
utils,
1414
)
1515
from cmd2.parsing import (
16+
Statement,
1617
StatementParser,
1718
shlex_split,
1819
)
@@ -944,6 +945,26 @@ def test_statement_is_immutable():
944945
statement.raw = 'baz'
945946

946947

948+
def test_statement_as_dict(parser):
949+
# Make sure to_dict() results can be restored to identical Statement
950+
statement = parser.parse("!ls > out.txt")
951+
assert statement == Statement.from_dict(statement.to_dict())
952+
953+
statement = parser.parse("!ls | grep text")
954+
assert statement == Statement.from_dict(statement.to_dict())
955+
956+
statement = parser.parse("multiline arg; suffix")
957+
assert statement == Statement.from_dict(statement.to_dict())
958+
959+
# from_dict() should raise KeyError if required field is missing
960+
statement = parser.parse("command")
961+
statement_dict = statement.to_dict()
962+
del statement_dict[Statement._args_field]
963+
964+
with pytest.raises(KeyError):
965+
Statement.from_dict(statement_dict)
966+
967+
947968
def test_is_valid_command_invalid(mocker, parser):
948969
# Non-string command
949970
# noinspection PyTypeChecker

0 commit comments

Comments
 (0)