Skip to content

Commit a7795d4

Browse files
committed
Fix jsonSet method to correct buffer extraction and add tests for string and stream conversions
1 parent 34734e7 commit a7795d4

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/iop/cls/IOP/Message.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Method jsonSet(pInput) As %Status
9595
// write in a stream
9696
Set stream = ##class(%Stream.GlobalCharacter).%New()
9797
for i=1:..#BUFFER:$LENGTH(pInput) {
98-
Set sc = stream.Write($EXTRACT(pInput,i,(i+..#BUFFER)))
98+
Set sc = stream.Write($EXTRACT(pInput,i,(i+..#BUFFER-1)))
9999
Quit:$$$ISERR(sc)
100100
}
101101
Set r%jsonStream=stream, i%type="Stream"

src/tests/test_iop_utils.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,60 @@ def test_migrate_only_classes():
282282
# Act
283283
_Utils.migrate()
284284
# Assert
285-
assert True # if no exception is raised, the test is ok
285+
assert True # if no exception is raised, the test is ok
286+
287+
def test_string_to_stream():
288+
string = 'test'
289+
result = _Utils.string_to_stream(string)
290+
expect = 'test'
291+
292+
assert result.Read() == expect
293+
294+
def test_stream_to_string():
295+
stream = iris.cls('%Stream.GlobalCharacter')._New()
296+
stream.Write('test')
297+
result = _Utils.stream_to_string(stream)
298+
expect = 'test'
299+
300+
assert result == expect
301+
302+
def test_stream_to_string_empty():
303+
stream = iris.cls('%Stream.GlobalCharacter')._New()
304+
result = _Utils.stream_to_string(stream)
305+
expect = ''
306+
307+
assert result == expect
308+
309+
def test_stream_to_string_huge():
310+
stream = iris.cls('%Stream.GlobalCharacter')._New()
311+
for i in range(1000):
312+
stream.Write('test'*1000)
313+
result = _Utils.stream_to_string(stream)
314+
expect = 'test'*1000000
315+
316+
assert result == expect
317+
318+
class TestIOPMessage:
319+
320+
def test_set_json_string(self):
321+
msg = iris.cls('IOP.Message')._New()
322+
msg.json = '{"test": "test"}'
323+
324+
assert msg.json == '{"test": "test"}'
325+
assert msg.type == 'String'
326+
327+
def test_set_json_huge_string(self):
328+
msg = iris.cls('IOP.Message')._New()
329+
msg.json = '{"test": "test"}'*100000
330+
331+
assert _Utils.stream_to_string(msg.json) == '{"test": "test"}'*100000
332+
assert msg.type == 'Stream'
333+
334+
def test_set_json_stream(self):
335+
msg = iris.cls('IOP.Message')._New()
336+
stream = iris.cls('%Stream.GlobalCharacter')._New()
337+
stream.Write('{"test": "test"}')
338+
msg.json = stream
339+
340+
assert msg.json.Read() == '{"test": "test"}'
341+
assert msg.type == 'Stream'

0 commit comments

Comments
 (0)