Skip to content

Commit d46ce7c

Browse files
committed
test: Enable doctests on source modules
1 parent af32a48 commit d46ce7c

File tree

7 files changed

+62
-22
lines changed

7 files changed

+62
-22
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,13 @@ ignore_missing_imports = true
253253
[tool.pytest.ini_options]
254254
addopts = """
255255
-W error::pytest.PytestUnraisableExceptionWarning
256+
--doctest-modules
256257
--import-mode=importlib
257258
--strict-config
258259
--strict-markers
259260
--tb=short
260261
"""
261-
testpaths = ["tests"]
262+
testpaths = ["src/capellambse", "tests"]
262263
xfail_strict = true
263264

264265
[tool.ruff]

src/capellambse/_scripts/repl.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,11 @@ def showxml(obj: capellambse.ModelObject | etree._Element) -> None:
306306
307307
Examples
308308
--------
309-
>>> my_obj = model.search("LogicalComponent").by_name("My Component")
309+
>>> my_obj = model.search("LogicalComponent").by_name("School")
310310
>>> showxml(my_obj)
311-
<ownedLogicalComponents name="My Component">
312-
...
311+
<ownedLogicalComponents xsi:type="org.polarsys.capella.core.data.la:LogicalComponent"
312+
id="a58821df-c5b4-4958-9455-0d30755be6b1" name="School">
313+
...
313314
</ownedLogicalComponents>
314315
"""
315316
if isinstance(obj, etree._Element):
@@ -331,10 +332,10 @@ def fzf(
331332
Examples
332333
--------
333334
>>> # Select a LogicalComponent by name
334-
>>> obj = fzf(model.search("LogicalComponent"))
335+
>>> obj = fzf(model.search("LogicalComponent")) # doctest: +SKIP
335336
336337
>>> # Select a ComponentExchange by the name of its target component
337-
>>> obj = fzf(model.search("ComponentExchange"), "target.parent.name")
338+
>>> obj = fzf(model.search("ComponentExchange"), "target.parent.name") # doctest: +SKIP
338339
"""
339340

340341
def repr(obj):

src/capellambse/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: Copyright DB InfraGO AG
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Test fixtures for capellambse's doctests."""
4+
5+
import pytest
6+
7+
import capellambse
8+
from capellambse import helpers
9+
10+
11+
@pytest.fixture(autouse=True)
12+
def load_test_models(doctest_namespace):
13+
model = capellambse.loadcli("test-5.0")
14+
doctest_namespace["model"] = model
15+
doctest_namespace["loader"] = model._loader
16+
17+
18+
@pytest.fixture(autouse=True)
19+
def deterministic_ids():
20+
with helpers.deterministic_ids():
21+
yield

src/capellambse/extensions/pvmt/_objects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class ObjectPVMT:
3434
3535
>>> obj = model.by_uuid("08e02248-504d-4ed8-a295-c7682a614f66")
3636
>>> obj.pvmt["DarkMagic.Power.Max"]
37-
1600
37+
1600.0
3838
>>> obj.pvmt["DarkMagic.Power.Max"] = 2000
3939
>>> obj.pvmt["DarkMagic.Power.Max"]
40-
2000
40+
2000.0
4141
4242
2. It's also possible to retrieve a managed group with the same
4343
syntax, by omitting the 'property' part of the path. The
@@ -46,7 +46,7 @@ class ObjectPVMT:
4646
4747
>>> power = obj.pvmt["DarkMagic.Power"]
4848
>>> power["Max"]
49-
2000
49+
2000.0
5050
"""
5151

5252
_model: capellambse.MelodyModel

src/capellambse/loader/core.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -793,13 +793,14 @@ def new_uuid(
793793
.. note:: You still need to call :meth:`idcache_index()` on the
794794
newly inserted element!
795795
796-
Example usage::
797-
798-
>>> with ldr.new_uuid(parent_elm) as obj_id:
799-
... child_elm = parent_elm.makeelement("ownedObjects")
800-
... child_elm.set("id", obj_id)
801-
... parent_elm.append(child_elm)
802-
... ldr.idcache_index(child_elm)
796+
Example usage:
797+
798+
>>> parent_elm = loader["08e02248-504d-4ed8-a295-c7682a614f66"]
799+
>>> with loader.new_uuid(parent_elm) as obj_id:
800+
... child_elm = parent_elm.makeelement("ownedObjects")
801+
... child_elm.set("id", obj_id)
802+
... parent_elm.append(child_elm)
803+
... loader.idcache_index(child_elm)
803804
804805
If you intend to reserve a UUID that should be inserted later,
805806
use :meth:`generate_uuid()` directly.

src/capellambse/model/_descriptors.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,16 @@ class Single(Accessor[T_co | None], t.Generic[T_co]):
394394
395395
Examples
396396
--------
397-
>>> class Foo(capellacore.CapellaElement):
398-
... bar = Single["Bar"](Containment("bar", (NS, "Bar")))
397+
This example creates two classes Foo and Bar, where Bar may contain
398+
a single Foo object:
399+
400+
>>> import capellambse.model as m
401+
>>> from capellambse.metamodel import capellacore
402+
>>> TEST_NS = m.Namespace(m.VIRTUAL_NAMESPACE_PREFIX + "/test", "test")
403+
>>>
404+
>>> class Foo(capellacore.CapellaElement, ns=TEST_NS): ...
405+
>>> class Bar(capellacore.CapellaElement, ns=TEST_NS):
406+
... foo = Single["Foo"](Containment("ownedFoo", (TEST_NS, "Foo")))
399407
"""
400408

401409
def __init__(

src/capellambse/model/_model.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,17 @@ class as the superclass of every concrete model element
387387
return a list of every Logical Component in the model:
388388
389389
>>> model.search("LogicalComponent")
390+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
391+
...
390392
>>> model.search("org.polarsys.capella.core.data.la:LogicalComponent")
393+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
394+
...
391395
>>> model.search( (capellambse.metamodel.la.NS, "LogicalComponent") )
396+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
397+
...
392398
>>> model.search( ("org.polarsys.capella.core.data.la", "LogicalComponent") )
399+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
400+
...
393401
"""
394402
classes: set[type[_obj.ModelObject]] = set()
395403
for clsname in clsnames:
@@ -632,18 +640,18 @@ def update_diagram_cache(
632640
Passing a bare filename looks up the executable in the PATH,
633641
after replacing a possible '{VERSION}' field:
634642
635-
>>> model.update_diagram_cache("capella", "png")
636-
>>> model.update_diagram_cache("capella{VERSION}", "png")
643+
>>> model.update_diagram_cache("capella", "png") # doctest: +SKIP
644+
>>> model.update_diagram_cache("capella{VERSION}", "png") # doctest: +SKIP
637645
638646
Passing an absolute path to a local installation of Capella that
639647
contains the Capella version will use that executable:
640648
641-
>>> model.update_diagram_cache("/opt/capella{VERSION}/capella", "png")
649+
>>> model.update_diagram_cache("/opt/capella{VERSION}/capella", "png") # doctest: +SKIP
642650
643651
Passing a docker image name will launch a docker container, using the
644652
Capella binary at the image's ENTRYPOINT:
645653
646-
>>> model.update_diagram_cache(
654+
>>> model.update_diagram_cache( # doctest: +SKIP
647655
... "ghcr.io/dbinfrago/capella-dockerimages/capella/base:{VERSION}-selected-dropins-main",
648656
... "png",
649657
... )

0 commit comments

Comments
 (0)