@@ -60,6 +60,72 @@ def hist():
60
60
return h
61
61
62
62
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
+
63
129
@pytest .fixture
64
130
def persisted_hist ():
65
131
from cmd2 .cmd2 import (
@@ -256,6 +322,37 @@ def test_history_max_length(hist):
256
322
assert hist .get (2 ).statement .raw == 'fourth'
257
323
258
324
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
+
259
356
#
260
357
# test HistoryItem()
261
358
#
0 commit comments