Skip to content

Commit ba2b68a

Browse files
committed
refactor: update debug message format in Notecard class
1 parent 7b2a4b4 commit ba2b68a

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

notecard/notecard.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,9 @@ def Transaction(self, req, lock=True):
364364
elif '{heartbeat}' in rsp_json['err']:
365365
if self._debug:
366366
try:
367-
print('Response has heartbeat field indicating ' + \
368-
f'heartbeat: {rsp_json["status"]}')
369-
except Exception as e:
370-
print(e)
367+
print(f'[DEBUG] {rsp_json["status"]}')
368+
except:
369+
pass
371370

372371
error = False
373372
continue

test/test_notecard.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,12 @@ def test_transaction_continues_after_heartbeat_to_get_valid_response(
332332
req = {"req": "note.add"}
333333

334334
# num_heartbeats of heartbeat responses followed by valid response
335-
heartbeat_response = b'{"err":"{heartbeat}"}\r\n'
335+
heartbeat_response = b'{"err":"{heartbeat}","status":"testing"}\r\n'
336+
json_responses = [{'err': '{heartbeat}', 'status': 'testing'}] * num_heartbeats + [{'total': 42}]
337+
336338
valid_response = b'{"total":42}\r\n'
337339
card._transact.side_effect = [heartbeat_response] * num_heartbeats + [valid_response]
338340

339-
json_responses = [{'err': '{heartbeat}'}] * num_heartbeats + [{'total': 42}]
340-
341341
with patch('notecard.notecard.json.loads') as mock_loads:
342342
mock_loads.side_effect = json_responses
343343
if debug_enabled:
@@ -374,11 +374,12 @@ def test_transaction_debug_heartbeat_with_and_without_status(
374374
with patch('builtins.print') as mock_print:
375375
result = card.Transaction(req)
376376

377-
# Verify the status message was printed for first heartbeat
378-
mock_print.assert_any_call('Response has heartbeat field indicating heartbeat: testing stsafe')
379-
# Verify the exception was printed for second heartbeat (KeyError: 'status')
380-
printed_calls = [str(call) for call in mock_print.call_args_list]
381-
assert any("KeyError" in call and "'status'" in call for call in printed_calls)
377+
# Verify the debug message was printed for first heartbeat (has status)
378+
mock_print.assert_any_call('[DEBUG] testing stsafe')
379+
# For second heartbeat (no status), exception is silently ignored (pass)
380+
# So we should only see one debug print call
381+
debug_calls = [call for call in mock_print.call_args_list if '[DEBUG]' in str(call)]
382+
assert len(debug_calls) == 1
382383
assert result == {'total': 42}
383384

384385
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)