File: test_collector.py

package info (click to toggle)
mkdocstrings-python-legacy 0.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 536 kB
  • sloc: python: 907; makefile: 29; sh: 17; javascript: 13
file content (35 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Tests for the `collector` module."""

from unittest import mock

import pytest
from mkdocstrings import CollectionError

from mkdocstrings_handlers.python import get_handler


class _FakeMkDocsConfig:
    config_file_path = "mkdocs.yml"


@pytest.mark.parametrize(
    ("retval", "exp_res"),
    [
        ({"error": "error1", "traceback": "hello"}, "error1\nhello"),
        ({"error": "error1"}, "error1"),
        ({"error": "", "traceback": "hello"}, "\nhello"),
    ],
)
def test_collect_result_error(retval: dict, exp_res: str) -> None:
    """Test handling of errors when collecting an object.

    Args:
        retval: Return value to mock `json.loads` with.
        exp_res: Expected result.
    """
    with mock.patch("mkdocstrings_handlers.python.handler.json.loads") as m_loads:  # noqa: SIM117
        with pytest.raises(CollectionError) as excinfo:  # noqa: PT012
            m_loads.return_value = retval
            handler = get_handler({}, _FakeMkDocsConfig, theme="material")  # type: ignore[arg-type]
            assert handler.collect("", {})
            assert str(excinfo.value) == exp_res