From a9aca8a8b2ac44dbe287dd2901a1a79201a7acdb Mon Sep 17 00:00:00 2001 From: rashidakanchwala Date: Tue, 26 Nov 2024 14:47:50 +0000 Subject: [PATCH] fix tests Signed-off-by: rashidakanchwala --- .../tests/test_launchers/test_cli/test_run.py | 21 +++++++++++ package/tests/test_server.py | 35 +++++-------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/package/tests/test_launchers/test_cli/test_run.py b/package/tests/test_launchers/test_cli/test_run.py index 95a809d2e..ccf579b0f 100644 --- a/package/tests/test_launchers/test_cli/test_run.py +++ b/package/tests/test_launchers/test_cli/test_run.py @@ -1,3 +1,4 @@ +from pathlib import Path from unittest.mock import call import pytest @@ -411,3 +412,23 @@ def test_find_available_port_with_occupied_ports(self, mocker): assert ( available_port == 4143 ), "Expected port 4143 to be returned as the available port" + + +def test_invalid_load_file_directory(mocker): + """ + Test that Kedro-Viz raises a ValueError when an invalid filepath + is provided to the `--load-file` argument. + """ + runner = CliRunner() + + # Mock the existence of the file path to always return False (invalid path) + mocker.patch.object(Path, "exists", return_value=False) + + # Invoke the CLI with an invalid `--load-file` path + result = runner.invoke( + main.viz_cli, ["viz", "run", "--load-file", "nonexistent_path.json"] + ) + + assert "The provided filepath 'nonexistent_path.json' does not exist." == str( + result.exception + ) diff --git a/package/tests/test_server.py b/package/tests/test_server.py index 2169e9d4d..ca8d19a2c 100644 --- a/package/tests/test_server.py +++ b/package/tests/test_server.py @@ -121,32 +121,15 @@ def test_specific_pipeline( {"data_science": example_pipelines["data_science"]} ) - @pytest.mark.parametrize( - "file_path, expected_exception", - [ - ("test.json", ValueError), # File does not exist, expect ValueError - ("test.json", None), # File exists, expect no ValueError - ], - ) - def test_load_file( - self, file_path, expected_exception, patched_create_api_app_from_file, tmp_path - ): - if expected_exception is not None: - with pytest.raises(expected_exception) as exc_info: - run_server(load_file=file_path) - - # Check if the error message contains the expected message - assert "The provided filepath" in str(exc_info.value) - assert "does not exist." in str(exc_info.value) - else: - json_file_path = tmp_path / file_path - - # File exists, no exception expected - with json_file_path.open("w") as file: - json.dump({"name": "John", "age": 30}, file) - - run_server(load_file=json_file_path) - patched_create_api_app_from_file.assert_called_once() + def test_load_file(self, patched_create_api_app_from_file, tmp_path): + file_path = "test.json" + json_file_path = tmp_path / file_path + + with json_file_path.open("w") as file: + json.dump({"name": "John", "age": 30}, file) + + run_server(load_file=json_file_path) + patched_create_api_app_from_file.assert_called_once() def test_save_file(self, tmp_path, mocker): mock_filesystem = mocker.patch("fsspec.filesystem")