|
| 1 | +"""Tests for ``openapi_spec`` (load ``info`` from YAML).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pydantic import ValidationError |
| 10 | + |
| 11 | +from recipes_api.openapi_spec import load_openapi_info |
| 12 | + |
| 13 | + |
| 14 | +@patch("recipes_api.openapi_spec.yaml.safe_load") |
| 15 | +def test_load_openapi_info_returns_info_from_yaml(mock_safe_load: MagicMock, tmp_path: Path) -> None: |
| 16 | + mock_safe_load.return_value = { |
| 17 | + "info": { |
| 18 | + "title": "Bread Recipes API", |
| 19 | + "version": "0.1.0", |
| 20 | + "description": "REST API for browsing bread recipes (list overview and full detail).", |
| 21 | + } |
| 22 | + } |
| 23 | + path = tmp_path / "spec.yaml" |
| 24 | + path.write_text("ignored: true\n", encoding="utf-8") |
| 25 | + info = load_openapi_info(path) |
| 26 | + assert info == { |
| 27 | + "title": "Bread Recipes API", |
| 28 | + "version": "0.1.0", |
| 29 | + "description": "REST API for browsing bread recipes (list overview and full detail).", |
| 30 | + } |
| 31 | + mock_safe_load.assert_called_once() |
| 32 | + |
| 33 | + |
| 34 | +@patch("recipes_api.openapi_spec.yaml.safe_load") |
| 35 | +def test_load_openapi_info_ignores_extra_openapi_keys(mock_safe_load: MagicMock, tmp_path: Path) -> None: |
| 36 | + mock_safe_load.return_value = { |
| 37 | + "openapi": "3.1.0", |
| 38 | + "info": { |
| 39 | + "title": "T", |
| 40 | + "version": "1.0.0", |
| 41 | + "description": "D", |
| 42 | + "license": {"name": "MIT"}, |
| 43 | + }, |
| 44 | + "paths": {}, |
| 45 | + } |
| 46 | + path = tmp_path / "spec.yaml" |
| 47 | + path.write_text("x", encoding="utf-8") |
| 48 | + assert load_openapi_info(path) == { |
| 49 | + "title": "T", |
| 50 | + "version": "1.0.0", |
| 51 | + "description": "D", |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +@patch("recipes_api.openapi_spec.yaml.safe_load") |
| 56 | +@pytest.mark.parametrize( |
| 57 | + "parsed", |
| 58 | + [ |
| 59 | + None, |
| 60 | + [], |
| 61 | + {}, |
| 62 | + {"openapi": "3.1.0"}, |
| 63 | + {"info": {"title": "only title"}}, |
| 64 | + {"info": {"title": "t", "version": "v", "description": 123}}, |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_load_openapi_info_invalid_document_raises_value_error( |
| 68 | + mock_safe_load: MagicMock, |
| 69 | + tmp_path: Path, |
| 70 | + parsed: object, |
| 71 | +) -> None: |
| 72 | + mock_safe_load.return_value = parsed |
| 73 | + path = tmp_path / "spec.yaml" |
| 74 | + path.write_text("x", encoding="utf-8") |
| 75 | + with pytest.raises(ValueError, match="invalid or missing required info") as exc_info: |
| 76 | + load_openapi_info(path) |
| 77 | + assert isinstance(exc_info.value.__cause__, ValidationError) |
| 78 | + |
| 79 | + |
| 80 | +def test_load_openapi_info_missing_file_raises() -> None: |
| 81 | + path = MagicMock(spec=Path) |
| 82 | + path.is_file.return_value = False |
| 83 | + with pytest.raises(FileNotFoundError, match="OpenAPI spec not found"): |
| 84 | + load_openapi_info(path) |
0 commit comments