fix tests
This commit is contained in:
parent
97716a941a
commit
f0e098cd82
|
|
@ -7,16 +7,21 @@ import pytest
|
|||
|
||||
from ra_aid.version_check import check_for_newer_version
|
||||
|
||||
def test_newer_version_available(mocker):
|
||||
def test_newer_version_available(monkeypatch):
|
||||
"""Test when a newer version is available."""
|
||||
# Mock the dependencies
|
||||
mocker.patch('ra_aid.version_check.current_version', '0.15.2')
|
||||
mock_get = mocker.patch('ra_aid.version_check.requests.get')
|
||||
monkeypatch.setattr('ra_aid.version_check.current_version', '0.15.2')
|
||||
|
||||
# Mock the response
|
||||
# Create a mock response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"version": "0.16.0"}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
# Create a mock function for requests.get
|
||||
def mock_get(*args, **kwargs):
|
||||
return mock_response
|
||||
|
||||
# Set the mock function
|
||||
monkeypatch.setattr('ra_aid.version_check.requests.get', mock_get)
|
||||
|
||||
result = check_for_newer_version()
|
||||
|
||||
|
|
@ -24,69 +29,92 @@ def test_newer_version_available(mocker):
|
|||
assert "0.16.0" in result
|
||||
assert "A new version of RA.Aid is available" in result
|
||||
|
||||
def test_same_version(mocker):
|
||||
def test_same_version(monkeypatch):
|
||||
"""Test when the current version is the latest."""
|
||||
# Mock the dependencies
|
||||
mocker.patch('ra_aid.version_check.current_version', '0.15.2')
|
||||
mock_get = mocker.patch('ra_aid.version_check.requests.get')
|
||||
monkeypatch.setattr('ra_aid.version_check.current_version', '0.15.2')
|
||||
|
||||
# Mock the response
|
||||
# Create a mock response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"version": "0.15.2"}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
# Create a mock function for requests.get
|
||||
def mock_get(*args, **kwargs):
|
||||
return mock_response
|
||||
|
||||
# Set the mock function
|
||||
monkeypatch.setattr('ra_aid.version_check.requests.get', mock_get)
|
||||
|
||||
result = check_for_newer_version()
|
||||
|
||||
# Check that no message is returned
|
||||
assert result == ""
|
||||
|
||||
def test_older_version(mocker):
|
||||
def test_older_version(monkeypatch):
|
||||
"""Test when the current version is newer than the latest."""
|
||||
# Mock the dependencies
|
||||
mocker.patch('ra_aid.version_check.current_version', '0.15.2')
|
||||
mock_get = mocker.patch('ra_aid.version_check.requests.get')
|
||||
monkeypatch.setattr('ra_aid.version_check.current_version', '0.15.2')
|
||||
|
||||
# Mock the response
|
||||
# Create a mock response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"version": "0.14.0"}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
# Create a mock function for requests.get
|
||||
def mock_get(*args, **kwargs):
|
||||
return mock_response
|
||||
|
||||
# Set the mock function
|
||||
monkeypatch.setattr('ra_aid.version_check.requests.get', mock_get)
|
||||
|
||||
result = check_for_newer_version()
|
||||
|
||||
# Check that no message is returned
|
||||
assert result == ""
|
||||
|
||||
def test_connection_error(mocker):
|
||||
def test_connection_error(monkeypatch):
|
||||
"""Test handling of connection errors."""
|
||||
# Mock a connection error
|
||||
mock_get = mocker.patch('ra_aid.version_check.requests.get')
|
||||
mock_get.side_effect = requests.RequestException("Connection error")
|
||||
# Create a mock function that raises an exception
|
||||
def mock_get(*args, **kwargs):
|
||||
raise requests.RequestException("Connection error")
|
||||
|
||||
# Set the mock function
|
||||
monkeypatch.setattr('ra_aid.version_check.requests.get', mock_get)
|
||||
|
||||
result = check_for_newer_version()
|
||||
|
||||
# Check that no message is returned
|
||||
assert result == ""
|
||||
|
||||
def test_json_parse_error(mocker):
|
||||
def test_json_parse_error(monkeypatch):
|
||||
"""Test handling of JSON parsing errors."""
|
||||
# Mock the response with invalid JSON
|
||||
mock_get = mocker.patch('ra_aid.version_check.requests.get')
|
||||
# Create a mock response
|
||||
mock_response = Mock()
|
||||
mock_response.json.side_effect = ValueError("Invalid JSON")
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
# Create a mock function for requests.get
|
||||
def mock_get(*args, **kwargs):
|
||||
return mock_response
|
||||
|
||||
# Set the mock function
|
||||
monkeypatch.setattr('ra_aid.version_check.requests.get', mock_get)
|
||||
|
||||
result = check_for_newer_version()
|
||||
|
||||
# Check that no message is returned
|
||||
assert result == ""
|
||||
|
||||
def test_missing_version_key(mocker):
|
||||
def test_missing_version_key(monkeypatch):
|
||||
"""Test handling of missing version key in JSON."""
|
||||
# Mock the response with missing version key
|
||||
mock_get = mocker.patch('ra_aid.version_check.requests.get')
|
||||
# Create a mock response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"other_key": "value"}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
# Create a mock function for requests.get
|
||||
def mock_get(*args, **kwargs):
|
||||
return mock_response
|
||||
|
||||
# Set the mock function
|
||||
monkeypatch.setattr('ra_aid.version_check.requests.get', mock_get)
|
||||
|
||||
result = check_for_newer_version()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue