|
| 1 | +import sys |
| 2 | +import typing as t |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pytest_mock import MockFixture |
| 7 | + |
| 8 | +from mysql_to_sqlite3.debug_info import _implementation, _mysql_version, info |
| 9 | + |
| 10 | + |
| 11 | +class TestDebugInfo: |
| 12 | + def test_implementation_cpython(self, mocker: MockFixture) -> None: |
| 13 | + """Test _implementation function with CPython.""" |
| 14 | + mocker.patch("platform.python_implementation", return_value="CPython") |
| 15 | + mocker.patch("platform.python_version", return_value="3.8.10") |
| 16 | + |
| 17 | + result = _implementation() |
| 18 | + assert result == "CPython 3.8.10" |
| 19 | + |
| 20 | + def test_implementation_pypy(self, mocker: MockFixture) -> None: |
| 21 | + """Test _implementation function with PyPy.""" |
| 22 | + mocker.patch("platform.python_implementation", return_value="PyPy") |
| 23 | + |
| 24 | + # Create a mock for pypy_version_info |
| 25 | + mock_version_info = MagicMock() |
| 26 | + mock_version_info.major = 3 |
| 27 | + mock_version_info.minor = 7 |
| 28 | + mock_version_info.micro = 4 |
| 29 | + mock_version_info.releaselevel = "final" |
| 30 | + |
| 31 | + # Need to use patch instead of mocker.patch for sys module attributes |
| 32 | + with patch.object(sys, "pypy_version_info", mock_version_info, create=True): |
| 33 | + result = _implementation() |
| 34 | + assert result == "PyPy 3.7.4" |
| 35 | + |
| 36 | + def test_implementation_pypy_non_final(self, mocker: MockFixture) -> None: |
| 37 | + """Test _implementation function with PyPy non-final release.""" |
| 38 | + mocker.patch("platform.python_implementation", return_value="PyPy") |
| 39 | + |
| 40 | + # Create a mock for pypy_version_info |
| 41 | + mock_version_info = MagicMock() |
| 42 | + mock_version_info.major = 3 |
| 43 | + mock_version_info.minor = 7 |
| 44 | + mock_version_info.micro = 4 |
| 45 | + mock_version_info.releaselevel = "beta" |
| 46 | + |
| 47 | + # Need to use patch instead of mocker.patch for sys module attributes |
| 48 | + with patch.object(sys, "pypy_version_info", mock_version_info, create=True): |
| 49 | + result = _implementation() |
| 50 | + assert result == "PyPy 3.7.4beta" |
| 51 | + |
| 52 | + def test_implementation_jython(self, mocker: MockFixture) -> None: |
| 53 | + """Test _implementation function with Jython.""" |
| 54 | + mocker.patch("platform.python_implementation", return_value="Jython") |
| 55 | + mocker.patch("platform.python_version", return_value="2.7.2") |
| 56 | + |
| 57 | + result = _implementation() |
| 58 | + assert result == "Jython 2.7.2" |
| 59 | + |
| 60 | + def test_implementation_ironpython(self, mocker: MockFixture) -> None: |
| 61 | + """Test _implementation function with IronPython.""" |
| 62 | + mocker.patch("platform.python_implementation", return_value="IronPython") |
| 63 | + mocker.patch("platform.python_version", return_value="2.7.9") |
| 64 | + |
| 65 | + result = _implementation() |
| 66 | + assert result == "IronPython 2.7.9" |
| 67 | + |
| 68 | + def test_implementation_unknown(self, mocker: MockFixture) -> None: |
| 69 | + """Test _implementation function with unknown implementation.""" |
| 70 | + mocker.patch("platform.python_implementation", return_value="UnknownPython") |
| 71 | + |
| 72 | + result = _implementation() |
| 73 | + assert result == "UnknownPython Unknown" |
| 74 | + |
| 75 | + def test_mysql_version_success(self, mocker: MockFixture) -> None: |
| 76 | + """Test _mysql_version function when mysql client is available.""" |
| 77 | + mocker.patch("mysql_to_sqlite3.debug_info.which", return_value="/usr/bin/mysql") |
| 78 | + mocker.patch( |
| 79 | + "mysql_to_sqlite3.debug_info.check_output", |
| 80 | + return_value=b"mysql Ver 8.0.26 for Linux on x86_64", |
| 81 | + ) |
| 82 | + |
| 83 | + result = _mysql_version() |
| 84 | + assert result == "mysql Ver 8.0.26 for Linux on x86_64" |
| 85 | + |
| 86 | + def test_mysql_version_bytes_decode_error(self, mocker: MockFixture) -> None: |
| 87 | + """Test _mysql_version function when bytes decoding fails.""" |
| 88 | + mocker.patch("mysql_to_sqlite3.debug_info.which", return_value="/usr/bin/mysql") |
| 89 | + mock_output = MagicMock() |
| 90 | + mock_output.decode.side_effect = UnicodeDecodeError("utf-8", b"", 0, 1, "invalid") |
| 91 | + mocker.patch( |
| 92 | + "mysql_to_sqlite3.debug_info.check_output", |
| 93 | + return_value=mock_output, |
| 94 | + ) |
| 95 | + |
| 96 | + result = _mysql_version() |
| 97 | + assert isinstance(result, str) |
| 98 | + |
| 99 | + def test_mysql_version_exception(self, mocker: MockFixture) -> None: |
| 100 | + """Test _mysql_version function when an exception occurs.""" |
| 101 | + mocker.patch("mysql_to_sqlite3.debug_info.which", return_value="/usr/bin/mysql") |
| 102 | + mocker.patch( |
| 103 | + "mysql_to_sqlite3.debug_info.check_output", |
| 104 | + side_effect=Exception("Command failed"), |
| 105 | + ) |
| 106 | + |
| 107 | + result = _mysql_version() |
| 108 | + assert result == "MySQL client not found on the system" |
| 109 | + |
| 110 | + def test_mysql_version_not_found(self, mocker: MockFixture) -> None: |
| 111 | + """Test _mysql_version function when mysql client is not found.""" |
| 112 | + mocker.patch("mysql_to_sqlite3.debug_info.which", return_value=None) |
| 113 | + |
| 114 | + result = _mysql_version() |
| 115 | + assert result == "MySQL client not found on the system" |
| 116 | + |
| 117 | + def test_info_success(self, mocker: MockFixture) -> None: |
| 118 | + """Test info function.""" |
| 119 | + mocker.patch("platform.system", return_value="Linux") |
| 120 | + mocker.patch("platform.release", return_value="5.4.0-80-generic") |
| 121 | + mocker.patch("mysql_to_sqlite3.debug_info._implementation", return_value="CPython 3.8.10") |
| 122 | + mocker.patch("mysql_to_sqlite3.debug_info._mysql_version", return_value="mysql Ver 8.0.26 for Linux on x86_64") |
| 123 | + |
| 124 | + result = info() |
| 125 | + assert isinstance(result, list) |
| 126 | + assert len(result) > 0 |
| 127 | + assert result[2] == ["Operating System", "Linux 5.4.0-80-generic"] |
| 128 | + assert result[3] == ["Python", "CPython 3.8.10"] |
| 129 | + assert result[4] == ["MySQL", "mysql Ver 8.0.26 for Linux on x86_64"] |
| 130 | + |
| 131 | + def test_info_platform_error(self, mocker: MockFixture) -> None: |
| 132 | + """Test info function when platform.system raises IOError.""" |
| 133 | + mocker.patch("platform.system", side_effect=IOError("Platform error")) |
| 134 | + |
| 135 | + result = info() |
| 136 | + assert isinstance(result, list) |
| 137 | + assert len(result) > 0 |
| 138 | + assert result[2] == ["Operating System", "Unknown"] |
0 commit comments