diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-06-10 13:34:23 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-06-14 11:28:25 -0400 |
commit | 33e8b86310d1f160ef77365a2d09665af9ce27d7 (patch) | |
tree | 0a188af9c03471cdd03db048d90586b4862c23e6 | |
parent | 41ec8d98493b77a9dc6888d3b6b576204b4199f2 (diff) | |
download | cmd2-git-33e8b86310d1f160ef77365a2d09665af9ce27d7.tar.gz |
Added unit tests for History and Statement
-rwxr-xr-x | tests/test_history.py | 97 | ||||
-rwxr-xr-x | tests/test_parsing.py | 21 |
2 files changed, 118 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() # diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 9776dace..59b8905b 100755 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -13,6 +13,7 @@ from cmd2 import ( utils, ) from cmd2.parsing import ( + Statement, StatementParser, shlex_split, ) @@ -944,6 +945,26 @@ def test_statement_is_immutable(): statement.raw = 'baz' +def test_statement_as_dict(parser): + # Make sure to_dict() results can be restored to identical Statement + statement = parser.parse("!ls > out.txt") + assert statement == Statement.from_dict(statement.to_dict()) + + statement = parser.parse("!ls | grep text") + assert statement == Statement.from_dict(statement.to_dict()) + + statement = parser.parse("multiline arg; suffix") + assert statement == Statement.from_dict(statement.to_dict()) + + # from_dict() should raise KeyError if required field is missing + statement = parser.parse("command") + statement_dict = statement.to_dict() + del statement_dict[Statement._args_field] + + with pytest.raises(KeyError): + Statement.from_dict(statement_dict) + + def test_is_valid_command_invalid(mocker, parser): # Non-string command # noinspection PyTypeChecker |