I have some code like this:
class LoadDataTask():
def do(self):
mw = main_window.MainWindow.the()
mw.visual_log_signal.emit(f'loading data from file...', logging.INFO)
file_path = pathlib.Path(main_window.LOAD_FILE_PATH_STR)
try:
with open(file_path, 'r') as f:
contents = f.read()
except FileNotFoundError:
msg = f'Load file not found: {main_window.LOAD_FILE_PATH_STR}'
logger.error(msg)
mw.visual_log_signal.emit(msg, logging.ERROR)
return
except BaseException as e:
raise Exception(f'TODO: {e}')
… if the file is not found a visual_log_signal
is emitted with level ERROR
.
But a level-INFO
emit has already occurred on the same signal a few lines earlier.
This is my test:
def test_if_json_file_not_file_visual_log(qtbot):
mw = main_window.MainWindow.the()
mw.gui_stage_2()
non_existent_file_path_str = '/nowhere/not_a_directory/nonexistent_file_path.json'
with mock.patch('main_window.LOAD_FILE_PATH_STR', new_callable=mock.PropertyMock(return_value=non_existent_file_path_str)):
def check_params_cb(*args, **kwargs):
logger.info(f'+++ args {args}, kwargs {kwargs}') # args is a tuple
assert 'load file not found' in args[0]
return True
with qtbot.waitSignal(mw.visual_log_signal, check_params_cb=check_params_cb, timeout=500) as mock_signal:
load_data_task.LoadDataTask().do()
… although the logger.error(..)
line in the code runs, only one (INFO
) signal emit
is reported in check_params_cb
. How can I detect the second emit
?
NB I found qtbot.waitSignals(..)
… but this seems to be for more than one different signal. Also I couldn’t find a practical example to understand how this works.
1