Skip to content

Commit

Permalink
Fixing unit test break for mp_user_session
Browse files Browse the repository at this point in the history
Adding ruff to pre-commit-config.yaml
  • Loading branch information
ianhelle committed Oct 20, 2024
1 parent e1205c1 commit 349b134
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 39 deletions.
14 changes: 10 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ repos:
- id: pydocstyle
args:
- --convention=numpy
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v1.0.1
# hooks:
# - id: mypy
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.0
hooks:
# Run the linter.
- id: ruff
types_or: [ python, pyi, jupyter ]
args:
- msticpy
- --fix
- repo: local
hooks:
- id: check_reqs_all
Expand Down
1 change: 1 addition & 0 deletions conda/conda-reqs-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pyroma>=3.1
pytest-check>=1.0.1
pytest-xdist>=2.5.0
respx>=0.20.1
ruff>=0.6.6
sphinx_rtd_theme>=0.5.1
sphinx>=2.1.2
virtualenv
Expand Down
2 changes: 1 addition & 1 deletion msticpy/init/mp_user_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def load_user_session(
by default False.
"""
current_level = logger.level
if verbose:
current_level = logger.level
if current_level > logging.INFO:
logger.setLevel(logging.INFO)
try:
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pytest>=5.0.1
readthedocs-sphinx-ext==2.2.5
responses>=0.13.2
respx>=0.20.1
ruff>=0.6.6
sphinx-rtd-theme>=1.0.0
sphinx>=5.0.1,<8.0.0
types-attrs>=19.0.0
Expand Down
46 changes: 12 additions & 34 deletions tests/init/test_mp_user_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@
__author__ = "Ian Hellen"


@patch("builtins.print")
@patch("msticpy.init.mp_user_session.Path.exists", return_value=False)
def test_load_user_session_file_not_found(mock_exists, mock_print):
"""
Test that the function prints an error message when the session file is not found.
Parameters
----------
mock_exists : unittest.mock.Mock
Mock for Path.exists method.
mock_print : unittest.mock.Mock
Mock for builtins.print function.
"""
load_user_session("non_existent_file.yaml")
mock_print.assert_called_with("Session file non_existent_file.yaml not found.")


@pytest.fixture
def mp_session_yaml(tmp_path):
"""
Expand Down Expand Up @@ -243,11 +226,8 @@ def test_load_user_session_loads_config(
assert "Components" in user_config


@patch("builtins.print")
@patch("msticpy.init.mp_user_session.Path.read_text")
def test_load_user_session_bad_missing_file(
mock_path_read_text, mock_print, mp_session_yaml
):
def test_load_user_session_bad_missing_file(mock_path_read_text, mp_session_yaml):
"""
Test that the function handles invalid YAML content gracefully.
Expand All @@ -257,8 +237,8 @@ def test_load_user_session_bad_missing_file(
Mock for Path().read_text method.
"""
load_user_session("non_existent_file.yaml")
mock_print.assert_called_with("Session file non_existent_file.yaml not found.")
with pytest.raises(FileNotFoundError):
load_user_session("non_existent_file.yaml")

invalid_yaml = "QueryProviders: ["
mock_path_read_text.return_value = invalid_yaml
Expand All @@ -267,11 +247,11 @@ def test_load_user_session_bad_missing_file(
load_user_session(mp_session_yaml)


@patch("builtins.print")
@patch("msticpy.init.mp_user_session.logger")
@patch("msticpy.init.mp_user_session.QueryProvider", autospec=True)
@patch("msticpy.init.mp_user_session._load_mp_components")
def test_query_provider_instantiate_fail(
mock_load_components, mock_query_prov, mock_print, mp_session_yaml
mock_load_components, mock_query_prov, mock_logger, mp_session_yaml
):
"""
Test load_user_session function.
Expand All @@ -282,8 +262,8 @@ def test_query_provider_instantiate_fail(
Mocked _load_mp_components function.
mock_load_providers : MagicMock
Mocked _load_query_providers function.
mock_print : MagicMock
Mocked print function.
mock_logger : MagicMock
Mocked logger.
tmp_yaml_file : pathlib.Path
Path to the temporary YAML file.
Expand All @@ -296,15 +276,15 @@ def test_query_provider_instantiate_fail(

load_user_session(session_file=mp_session_yaml, namespace=namespace)

mock_print.assert_called_with(
"Failed to create MyQueryProvider: Failed to instantiate QueryProvider"
mock_logger.exception.assert_called_with(
"Failed to initialize %s", "MyQueryProvider", exc_info=True
)
mock_load_components.assert_called_once()
mock_query_prov.assert_called_with("MSSentinel", debug=True)
assert "MyQueryProvider" not in namespace

# Fail at connect call
mock_print.reset_mock()
mock_logger.reset_mock()
mock_query_prov.side_effect = None
mock_qp_instance = MagicMock()
mock_query_prov.return_value = mock_qp_instance
Expand All @@ -315,9 +295,7 @@ def test_query_provider_instantiate_fail(
mock_qp_instance.connect.assert_called_once_with(
workspace="MySoc", auth_methods=["cli", "device_code"]
)
assert (
mock_print.call_args_list[1][0][0]
== "Failed to connect to MyQueryProvider: Failed to connect QueryProvider"
mock_logger.exception.assert_called_with(
"Failed to connect to %s", "MyQueryProvider", exc_info=True
)
assert mock_print.call_args_list[0][0][0] == "Connecting to MyQueryProvider"
assert "MyQueryProvider" not in namespace

0 comments on commit 349b134

Please sign in to comment.