"""Utility functions to support database interactions in tests.""" from contextlib import contextmanager import re from unittest import mock def mock_db_session(mocker, sql_module): """Create a mock database session. Also mock the session_scope context manager to use the mock session. """ mock_session = mock.Mock(query=mock.Mock()) @contextmanager def fake_session_manager(): yield mock_session mocker.patch.object(sql_module, 'session_scope', fake_session_manager) return mock_session def remove_repeating_spaces(string): """Replace repeating spaces and newline characters with spaces. Args: string (str): Input string to be processed. Returns: str: Processed string. """ # Note: would break field values that do have multiple spaces. string = re.sub(r'\n+', ' ', string) string = re.sub(r' +', ' ', string) string = string.strip(' ') return string