Skip to content

Commit

Permalink
Add test for autosummary with autodoc-pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
aeisenbarth committed Apr 11, 2024
1 parent 5ab45c1 commit 200575b
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{{ fullname | escape | underline}}

.. automodule:: {{ fullname }}

{% block attribute %}
{% if attribute %}
.. rubric:: {{ _('Module Attributes') }}

.. autosummary::
{% for item in attribute %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block function %}
{% if function %}
.. rubric:: {{ _('Functions') }}

.. autosummary::
{% for item in function %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block class %}
{% if class %}
.. rubric:: {{ _('Classes') }}

.. autosummary::
{% for item in class %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block pydantic_model %}
{% if pydantic_model %}
.. rubric:: {{ _('Models') }}

.. autosummary::
:toctree:
{% for item in pydantic_model %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block pydantic_settings %}
{% if pydantic_settings %}
.. rubric:: {{ _('Settings') }}

.. autosummary::
:toctree:
{% for item in pydantic_settings %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block exception %}
{% if exception %}
.. rubric:: {{ _('Exceptions') }}

.. autosummary::
{% for item in exception %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}

{% block modules %}
{% if modules %}
.. rubric:: {{ _('Modules') }}

.. autosummary::
:toctree:
:recursive:
{% for item in modules %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
__all__ = [
"MyModel",
]

from pydantic import BaseModel


class MyModel(BaseModel):
attr: str
12 changes: 12 additions & 0 deletions tests/roots/test-ext-autosummary-pydantic/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
import sys

sys.path.insert(0, os.path.abspath('.'))

extensions = ['sphinx.ext.autosummary', "sphinxcontrib.autodoc_pydantic"]
autosummary_generate = True

# The suffix of source filenames.
source_suffix = '.rst'

templates_path = ["_templates"]
8 changes: 8 additions & 0 deletions tests/roots/test-ext-autosummary-pydantic/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test-ext-autosummary-pydantic
=============================

.. autosummary::
:toctree: generated
:recursive:

autosummary_dummy_module_with_pydantic
17 changes: 17 additions & 0 deletions tests/test_extensions/test_ext_autosummary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test the autosummary extension."""

import importlib.util
import sys
from io import StringIO
from pathlib import Path
Expand Down Expand Up @@ -268,6 +269,22 @@ def test_autosummary_generate_content_for_module_by_objtype(app):
assert context['module'] == 'autosummary_dummy_module'


@pytest.mark.skipif(not importlib.util.find_spec("sphinxcontrib.autodoc_pydantic"), reason="Requires pydantic, autodoc-pydantic")
@pytest.mark.sphinx(testroot='ext-autosummary-pydantic')
def test_autosummary_generate_content_for_module_with_pydantic(app):
import autosummary_dummy_module_with_pydantic
template = Mock()

generate_autosummary_content('autosummary_dummy_module_with_pydantic', autosummary_dummy_module_with_pydantic, None,
template, None, False, app, False, {})
assert template.render.call_args[0][0] == 'module'

context = template.render.call_args[0][1]

assert context['pydantic_model'] == ['MyModel']
assert 'MyModel' not in context['class']


@pytest.mark.sphinx(testroot='ext-autosummary')
def test_autosummary_generate_content_for_module___all__(app):
import autosummary_dummy_module
Expand Down

0 comments on commit 200575b

Please sign in to comment.