|
14 | 14 | from pathlib import Path |
15 | 15 |
|
16 | 16 | import pytest |
| 17 | +import requests |
17 | 18 |
|
18 | 19 | # Handle TOML parsing for different Python versions |
19 | 20 | try: |
@@ -220,3 +221,96 @@ def test_package_can_be_installed(): |
220 | 221 | f"STDOUT: {import_result.stdout}\n" |
221 | 222 | f"STDERR: {import_result.stderr}" |
222 | 223 | ) |
| 224 | + |
| 225 | + |
| 226 | +@pytest.mark.pypi |
| 227 | +def test_pypi_version_can_be_installed_and_used(): |
| 228 | + """Test that the current version from PyPI can be installed and used.""" |
| 229 | + if tomllib is None: |
| 230 | + pytest.skip("tomllib or tomli is required to get package name") |
| 231 | + |
| 232 | + project_root = Path(__file__).parent.parent |
| 233 | + pyproject_path = project_root / "pyproject.toml" |
| 234 | + |
| 235 | + # Get package name from pyproject.toml |
| 236 | + with open(pyproject_path, "rb") as f: |
| 237 | + config = tomllib.load(f) |
| 238 | + |
| 239 | + package_name = config.get("project", {}).get("name") |
| 240 | + if not package_name: |
| 241 | + pytest.skip("Package name not found in pyproject.toml") |
| 242 | + |
| 243 | + # Query PyPI API for latest version |
| 244 | + pypi_url = f"https://pypi.org/pypi/{package_name}/json" |
| 245 | + try: |
| 246 | + response = requests.get(pypi_url, timeout=10) |
| 247 | + response.raise_for_status() |
| 248 | + pypi_data = response.json() |
| 249 | + except requests.RequestException as e: |
| 250 | + pytest.skip(f"Failed to query PyPI: {e}") |
| 251 | + |
| 252 | + # Get the latest version |
| 253 | + latest_version = pypi_data.get("info", {}).get("version") |
| 254 | + if not latest_version: |
| 255 | + pytest.skip("No version found in PyPI response") |
| 256 | + |
| 257 | + # Create a temporary virtual environment |
| 258 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 259 | + venv_path = Path(tmpdir) / "test_venv" |
| 260 | + |
| 261 | + # Create virtual environment |
| 262 | + subprocess.run( |
| 263 | + [sys.executable, "-m", "venv", str(venv_path)], |
| 264 | + check=True, |
| 265 | + capture_output=True, |
| 266 | + ) |
| 267 | + |
| 268 | + # Get pip path |
| 269 | + if sys.platform == "win32": |
| 270 | + pip_path = venv_path / "Scripts" / "pip" |
| 271 | + python_path = venv_path / "Scripts" / "python" |
| 272 | + else: |
| 273 | + pip_path = venv_path / "bin" / "pip" |
| 274 | + python_path = venv_path / "bin" / "python" |
| 275 | + |
| 276 | + # Install the package from PyPI |
| 277 | + install_result = subprocess.run( |
| 278 | + [str(pip_path), "install", f"{package_name}=={latest_version}"], |
| 279 | + capture_output=True, |
| 280 | + text=True, |
| 281 | + check=False, |
| 282 | + ) |
| 283 | + |
| 284 | + assert install_result.returncode == 0, ( |
| 285 | + f"PyPI package installation failed.\n" |
| 286 | + f"STDOUT: {install_result.stdout}\n" |
| 287 | + f"STDERR: {install_result.stderr}" |
| 288 | + ) |
| 289 | + |
| 290 | + # Verify package can be imported |
| 291 | + import_test_code = """ |
| 292 | +import sqlalchemy_serializer |
| 293 | +from sqlalchemy_serializer import SerializerMixin, Serializer |
| 294 | +
|
| 295 | +# Test basic import |
| 296 | +assert hasattr(sqlalchemy_serializer, 'SerializerMixin') |
| 297 | +assert hasattr(sqlalchemy_serializer, 'Serializer') |
| 298 | +
|
| 299 | +# Test Serializer can be instantiated |
| 300 | +serializer = Serializer() |
| 301 | +assert serializer is not None |
| 302 | +
|
| 303 | +print('OK') |
| 304 | +""" |
| 305 | + import_result = subprocess.run( |
| 306 | + [str(python_path), "-c", import_test_code], |
| 307 | + capture_output=True, |
| 308 | + text=True, |
| 309 | + check=False, |
| 310 | + ) |
| 311 | + |
| 312 | + assert import_result.returncode == 0, ( |
| 313 | + f"Package usage test failed after PyPI installation.\n" |
| 314 | + f"STDOUT: {import_result.stdout}\n" |
| 315 | + f"STDERR: {import_result.stderr}" |
| 316 | + ) |
0 commit comments