|
| 1 | +import platform |
| 2 | +import sqlite3 |
| 3 | +import sys |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from sqlite3_to_mysql.debug_info import _implementation, _mysql_version, info |
| 9 | + |
| 10 | + |
| 11 | +class TestDebugInfo: |
| 12 | + def test_implementation_cpython(self) -> None: |
| 13 | + """Test _implementation function with CPython.""" |
| 14 | + with patch("platform.python_implementation", return_value="CPython"): |
| 15 | + with patch("platform.python_version", return_value="3.8.0"): |
| 16 | + assert _implementation() == "CPython 3.8.0" |
| 17 | + |
| 18 | + def test_implementation_pypy(self) -> None: |
| 19 | + """Test _implementation function with PyPy.""" |
| 20 | + with patch("platform.python_implementation", return_value="PyPy"): |
| 21 | + # Mock sys.pypy_version_info |
| 22 | + mock_version_info = MagicMock() |
| 23 | + mock_version_info.major = 7 |
| 24 | + mock_version_info.minor = 3 |
| 25 | + mock_version_info.micro = 1 |
| 26 | + mock_version_info.releaselevel = "final" |
| 27 | + |
| 28 | + with patch.object(sys, "pypy_version_info", mock_version_info, create=True): |
| 29 | + assert _implementation() == "PyPy 7.3.1" |
| 30 | + |
| 31 | + def test_implementation_pypy_non_final(self) -> None: |
| 32 | + """Test _implementation function with PyPy non-final release.""" |
| 33 | + with patch("platform.python_implementation", return_value="PyPy"): |
| 34 | + # Mock sys.pypy_version_info |
| 35 | + mock_version_info = MagicMock() |
| 36 | + mock_version_info.major = 7 |
| 37 | + mock_version_info.minor = 3 |
| 38 | + mock_version_info.micro = 1 |
| 39 | + mock_version_info.releaselevel = "beta" |
| 40 | + |
| 41 | + with patch.object(sys, "pypy_version_info", mock_version_info, create=True): |
| 42 | + assert _implementation() == "PyPy 7.3.1beta" |
| 43 | + |
| 44 | + def test_implementation_jython(self) -> None: |
| 45 | + """Test _implementation function with Jython.""" |
| 46 | + with patch("platform.python_implementation", return_value="Jython"): |
| 47 | + with patch("platform.python_version", return_value="2.7.2"): |
| 48 | + assert _implementation() == "Jython 2.7.2" |
| 49 | + |
| 50 | + def test_implementation_ironpython(self) -> None: |
| 51 | + """Test _implementation function with IronPython.""" |
| 52 | + with patch("platform.python_implementation", return_value="IronPython"): |
| 53 | + with patch("platform.python_version", return_value="2.7.9"): |
| 54 | + assert _implementation() == "IronPython 2.7.9" |
| 55 | + |
| 56 | + def test_implementation_unknown(self) -> None: |
| 57 | + """Test _implementation function with unknown implementation.""" |
| 58 | + with patch("platform.python_implementation", return_value="Unknown"): |
| 59 | + assert _implementation() == "Unknown Unknown" |
| 60 | + |
| 61 | + def test_mysql_version_found(self) -> None: |
| 62 | + """Test _mysql_version function when MySQL is found.""" |
| 63 | + with patch("sqlite3_to_mysql.debug_info.which", return_value="/usr/bin/mysql"): |
| 64 | + with patch("sqlite3_to_mysql.debug_info.check_output", return_value=b"mysql Ver 8.0.23"): |
| 65 | + assert _mysql_version() == "mysql Ver 8.0.23" |
| 66 | + |
| 67 | + def test_mysql_version_decode_error(self) -> None: |
| 68 | + """Test _mysql_version function with decode error.""" |
| 69 | + with patch("sqlite3_to_mysql.debug_info.which", return_value="/usr/bin/mysql"): |
| 70 | + with patch("sqlite3_to_mysql.debug_info.check_output", return_value=b"\xff\xfe"): |
| 71 | + # This should trigger a UnicodeDecodeError |
| 72 | + result = _mysql_version() |
| 73 | + assert "b'" in result # Should contain the string representation of bytes |
| 74 | + |
| 75 | + def test_mysql_version_exception(self) -> None: |
| 76 | + """Test _mysql_version function with exception.""" |
| 77 | + with patch("sqlite3_to_mysql.debug_info.which", return_value="/usr/bin/mysql"): |
| 78 | + with patch("sqlite3_to_mysql.debug_info.check_output", side_effect=Exception("Command failed")): |
| 79 | + assert _mysql_version() == "MySQL client not found on the system" |
| 80 | + |
| 81 | + def test_mysql_version_not_found(self) -> None: |
| 82 | + """Test _mysql_version function when MySQL is not found.""" |
| 83 | + with patch("sqlite3_to_mysql.debug_info.which", return_value=None): |
| 84 | + assert _mysql_version() == "MySQL client not found on the system" |
| 85 | + |
| 86 | + def test_info(self) -> None: |
| 87 | + """Test info function.""" |
| 88 | + with patch("platform.system", return_value="Linux"): |
| 89 | + with patch("platform.release", return_value="5.4.0"): |
| 90 | + with patch("sqlite3_to_mysql.debug_info._implementation", return_value="CPython 3.8.0"): |
| 91 | + with patch("sqlite3_to_mysql.debug_info._mysql_version", return_value="mysql Ver 8.0.23"): |
| 92 | + with patch.object(sqlite3, "sqlite_version", "3.32.3"): |
| 93 | + result = info() |
| 94 | + assert result[0][0] == "sqlite3-to-mysql" |
| 95 | + assert result[2][0] == "Operating System" |
| 96 | + assert result[2][1] == "Linux 5.4.0" |
| 97 | + assert result[3][0] == "Python" |
| 98 | + assert result[3][1] == "CPython 3.8.0" |
| 99 | + assert result[4][0] == "MySQL" |
| 100 | + assert result[4][1] == "mysql Ver 8.0.23" |
| 101 | + assert result[5][0] == "SQLite" |
| 102 | + assert result[5][1] == "3.32.3" |
| 103 | + |
| 104 | + def test_info_platform_error(self) -> None: |
| 105 | + """Test info function with platform error.""" |
| 106 | + with patch("platform.system", side_effect=IOError("Platform error")): |
| 107 | + with patch("sqlite3_to_mysql.debug_info._implementation", return_value="CPython 3.8.0"): |
| 108 | + with patch("sqlite3_to_mysql.debug_info._mysql_version", return_value="mysql Ver 8.0.23"): |
| 109 | + with patch.object(sqlite3, "sqlite_version", "3.32.3"): |
| 110 | + result = info() |
| 111 | + assert result[0][0] == "sqlite3-to-mysql" |
| 112 | + assert result[2][0] == "Operating System" |
| 113 | + assert result[2][1] == "Unknown" |
| 114 | + assert result[3][0] == "Python" |
| 115 | + assert result[3][1] == "CPython 3.8.0" |
| 116 | + assert result[4][0] == "MySQL" |
| 117 | + assert result[4][1] == "mysql Ver 8.0.23" |
| 118 | + assert result[5][0] == "SQLite" |
| 119 | + assert result[5][1] == "3.32.3" |
0 commit comments