|
| 1 | +"""Tests for YAML backend detection and ryaml/PyYAML switching.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import io |
| 6 | +from typing import TYPE_CHECKING |
| 7 | +from unittest.mock import MagicMock, patch |
| 8 | + |
| 9 | +import pytest |
| 10 | +import yaml |
| 11 | + |
| 12 | +from datamodel_code_generator import InputFileType, infer_input_type, load_yaml |
| 13 | +from datamodel_code_generator.util import get_yaml_backend, get_yaml_parse_errors |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from collections.abc import Iterator |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(autouse=True) |
| 20 | +def _clear_caches() -> Iterator[None]: |
| 21 | + """Clear lru_cache before and after each test.""" |
| 22 | + get_yaml_backend.cache_clear() |
| 23 | + get_yaml_parse_errors.cache_clear() |
| 24 | + yield |
| 25 | + get_yaml_backend.cache_clear() |
| 26 | + get_yaml_parse_errors.cache_clear() |
| 27 | + |
| 28 | + |
| 29 | +class TestGetYamlBackend: |
| 30 | + """Tests for get_yaml_backend().""" |
| 31 | + |
| 32 | + def test_without_ryaml(self) -> None: |
| 33 | + """When ryaml is not importable, returns 'pyyaml'.""" |
| 34 | + with patch.dict("sys.modules", {"ryaml": None}): |
| 35 | + assert get_yaml_backend() == "pyyaml" |
| 36 | + |
| 37 | + def test_with_ryaml(self) -> None: |
| 38 | + """When ryaml is importable, returns 'ryaml'.""" |
| 39 | + mock_ryaml = MagicMock() |
| 40 | + with patch.dict("sys.modules", {"ryaml": mock_ryaml}): |
| 41 | + assert get_yaml_backend() == "ryaml" |
| 42 | + |
| 43 | + |
| 44 | +class TestGetYamlParseErrors: |
| 45 | + """Tests for get_yaml_parse_errors().""" |
| 46 | + |
| 47 | + def test_pyyaml_only(self) -> None: |
| 48 | + """Without ryaml, only yaml.YAMLError is returned.""" |
| 49 | + with patch.dict("sys.modules", {"ryaml": None}): |
| 50 | + errors = get_yaml_parse_errors() |
| 51 | + assert errors == (yaml.YAMLError,) |
| 52 | + |
| 53 | + def test_includes_ryaml(self) -> None: |
| 54 | + """With ryaml, InvalidYamlError is included.""" |
| 55 | + mock_ryaml = MagicMock() |
| 56 | + mock_ryaml.InvalidYamlError = type("InvalidYamlError", (Exception,), {}) |
| 57 | + with patch.dict("sys.modules", {"ryaml": mock_ryaml}): |
| 58 | + errors = get_yaml_parse_errors() |
| 59 | + assert yaml.YAMLError in errors |
| 60 | + assert mock_ryaml.InvalidYamlError in errors |
| 61 | + assert len(errors) == 2 |
| 62 | + |
| 63 | + |
| 64 | +class TestLoadYaml: |
| 65 | + """Tests for load_yaml() with backend switching.""" |
| 66 | + |
| 67 | + def test_pyyaml_fallback_string(self) -> None: |
| 68 | + """When ryaml is unavailable, PyYAML is used for string input.""" |
| 69 | + with patch.dict("sys.modules", {"ryaml": None}): |
| 70 | + result = load_yaml("key: value") |
| 71 | + assert result == {"key": "value"} |
| 72 | + |
| 73 | + def test_pyyaml_fallback_textio(self) -> None: |
| 74 | + """When ryaml is unavailable, PyYAML is used for TextIO input.""" |
| 75 | + with patch.dict("sys.modules", {"ryaml": None}): |
| 76 | + result = load_yaml(io.StringIO("key: value")) |
| 77 | + assert result == {"key": "value"} |
| 78 | + |
| 79 | + def test_with_ryaml_string(self) -> None: |
| 80 | + """When ryaml is available, ryaml.loads() is used for string input.""" |
| 81 | + mock_ryaml = MagicMock() |
| 82 | + mock_ryaml.loads.return_value = {"key": "value"} |
| 83 | + with patch.dict("sys.modules", {"ryaml": mock_ryaml}): |
| 84 | + result = load_yaml("key: value") |
| 85 | + mock_ryaml.loads.assert_called_once_with("key: value") |
| 86 | + assert result == {"key": "value"} |
| 87 | + |
| 88 | + def test_with_ryaml_textio(self) -> None: |
| 89 | + """When ryaml is available, TextIO.read() is called before ryaml.loads().""" |
| 90 | + mock_ryaml = MagicMock() |
| 91 | + mock_ryaml.loads.return_value = {"key": "value"} |
| 92 | + stream = io.StringIO("key: value") |
| 93 | + with patch.dict("sys.modules", {"ryaml": mock_ryaml}): |
| 94 | + result = load_yaml(stream) |
| 95 | + mock_ryaml.loads.assert_called_once_with("key: value") |
| 96 | + assert result == {"key": "value"} |
| 97 | + |
| 98 | + |
| 99 | +class TestInferInputType: |
| 100 | + """Tests for infer_input_type() with backend error handling.""" |
| 101 | + |
| 102 | + def test_csv_with_pyyaml_error(self) -> None: |
| 103 | + """YAML parse error from PyYAML returns CSV type.""" |
| 104 | + with patch.dict("sys.modules", {"ryaml": None}): |
| 105 | + result = infer_input_type("a,b,c\n1,2,3\n::") |
| 106 | + assert result == InputFileType.CSV |
| 107 | + |
| 108 | + def test_csv_with_ryaml_error(self) -> None: |
| 109 | + """YAML parse error from ryaml returns CSV type.""" |
| 110 | + mock_invalid_yaml_error = type("InvalidYamlError", (Exception,), {}) |
| 111 | + mock_ryaml = MagicMock() |
| 112 | + mock_ryaml.InvalidYamlError = mock_invalid_yaml_error |
| 113 | + mock_ryaml.loads.side_effect = mock_invalid_yaml_error("parse error") |
| 114 | + with patch.dict("sys.modules", {"ryaml": mock_ryaml}): |
| 115 | + result = infer_input_type(":::invalid yaml:::") |
| 116 | + assert result == InputFileType.CSV |
| 117 | + |
| 118 | + def test_openapi_detection(self) -> None: |
| 119 | + """OpenAPI input is detected correctly regardless of backend.""" |
| 120 | + with patch.dict("sys.modules", {"ryaml": None}): |
| 121 | + result = infer_input_type("openapi: '3.0.0'\ninfo:\n title: Test\n version: '1.0'") |
| 122 | + assert result == InputFileType.OpenAPI |
0 commit comments