Skip to content

Commit

Permalink
Test new exit points of refactored main
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-jdelapuente committed Aug 19, 2024
1 parent 98baf4e commit 8b5e9ff
Showing 1 changed file with 58 additions and 13 deletions.
71 changes: 58 additions & 13 deletions tools/wheel_resolver/__init___test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import click.testing
import pytest
import unittest.mock
import requests

import tools.wheel_resolver as sut


Expand All @@ -12,15 +9,7 @@ def test_help(self) -> None:
result = runner.invoke(cli=sut.main, args=["--help"])
assert result.exit_code == 0

@unittest.mock.patch.object(sut.output, "try_download")
@unittest.mock.patch.object(sut.requests, "head")
def test_any_in_platforms(
self, _mock_requests_head: unittest.mock.MagicMock, _mock_try_download: unittest.mock.MagicMock
) -> None:
_mock_try_download.return_value = True
_mock_requests_head.return_value = requests.Response()
_mock_requests_head.return_value.status_code = requests.codes.ok

def test_any_in_platforms(self) -> None:
runner = click.testing.CliRunner()
with unittest.mock.patch.object(sut.wheel, "url") as mock_url:
result = runner.invoke(
Expand All @@ -38,4 +27,60 @@ def test_any_in_platforms(
# Due to tags being a required keyword argument
if "tags" in kwargs:
assert any([t for t in kwargs["tags"] if t.endswith("any")])
assert result.exit_code == 0

@unittest.mock.patch.object(sut, "_LOGGER")
@unittest.mock.patch.object(sut.output, "get")
def test_output_not_set_error(
self,
mock_output_get: unittest.mock.MagicMock,
mock_logger: unittest.mock.MagicMock,
) -> None:
mock_output_get.side_effect = sut.output.OutputNotSetError

runner = click.testing.CliRunner()
result = runner.invoke(cli=sut.main, args=["--package-name", "some-package"])

mock_logger.error.assert_called_once_with("could not get $OUTS")
assert result.exit_code == 1

@unittest.mock.patch.object(sut.output, "get")
@unittest.mock.patch.object(sut.output, "download")
@unittest.mock.patch.object(sut.wheel, "url")
@unittest.mock.patch.object(sut, "_LOGGER")
def test_wheel_url_error(
self,
mock_logger: unittest.mock.MagicMock,
mock_wheel_url: unittest.mock.MagicMock,
mock_output_download: unittest.mock.MagicMock,
mock_output_get: unittest.mock.MagicMock,
) -> None:

# Setup variables
package_name = "test-package"
package_version = "1.0.0"
exception_message = "Test Exception"

# Set up mocks
mock_output_get.return_value = "output_name"
mock_output_download.return_value = False
mock_wheel_url.side_effect = Exception(exception_message)

# Run the command
runner = click.testing.CliRunner()
result = runner.invoke(
cli=sut.main,
args=[
"--package-name",
f"{package_name}",
"--package-version",
f"{package_version}",
],
)

# Check that the error was logged correctly
mock_logger.error.assert_any_call(
f"could not find PyPI URL for {package_name}-{package_version}",
)

# Check that the program exited with an error code
assert result.exit_code == 1

0 comments on commit 8b5e9ff

Please sign in to comment.