Skip to content

Commit 51db15c

Browse files
committed
test: Enable doctests on source modules
1 parent a0b5521 commit 51db15c

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
@@ -294,10 +294,11 @@ def showxml(obj: capellambse.ModelObject | etree._Element) -> None:
294294
295295
Examples
296296
--------
297-
>>> my_obj = model.search("LogicalComponent").by_name("My Component")
297+
>>> my_obj = model.search("LogicalComponent").by_name("School")
298298
>>> showxml(my_obj)
299-
<ownedLogicalComponents name="My Component">
300-
...
299+
<ownedLogicalComponents xsi:type="org.polarsys.capella.core.data.la:LogicalComponent"
300+
id="a58821df-c5b4-4958-9455-0d30755be6b1" name="School">
301+
...
301302
</ownedLogicalComponents>
302303
"""
303304
if isinstance(obj, etree._Element):
@@ -319,10 +320,10 @@ def fzf(
319320
Examples
320321
--------
321322
>>> # Select a LogicalComponent by name
322-
>>> obj = fzf(model.search("LogicalComponent"))
323+
>>> obj = fzf(model.search("LogicalComponent")) # doctest: +SKIP
323324
324325
>>> # Select a ComponentExchange by the name of its target component
325-
>>> obj = fzf(model.search("ComponentExchange"), "target.parent.name")
326+
>>> obj = fzf(model.search("ComponentExchange"), "target.parent.name") # doctest: +SKIP
326327
"""
327328

328329
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
@@ -787,13 +787,14 @@ def new_uuid(
787787
.. note:: You still need to call :meth:`idcache_index()` on the
788788
newly inserted element!
789789
790-
Example usage::
791-
792-
>>> with ldr.new_uuid(parent_elm) as obj_id:
793-
... child_elm = parent_elm.makeelement("ownedObjects")
794-
... child_elm.set("id", obj_id)
795-
... parent_elm.append(child_elm)
796-
... ldr.idcache_index(child_elm)
790+
Example usage:
791+
792+
>>> parent_elm = loader["08e02248-504d-4ed8-a295-c7682a614f66"]
793+
>>> with loader.new_uuid(parent_elm) as obj_id:
794+
... child_elm = parent_elm.makeelement("ownedObjects")
795+
... child_elm.set("id", obj_id)
796+
... parent_elm.append(child_elm)
797+
... loader.idcache_index(child_elm)
797798
798799
If you intend to reserve a UUID that should be inserted later,
799800
use :meth:`generate_uuid()` directly.

src/capellambse/model/_descriptors.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,16 @@ class Single(Accessor[T_co | None], t.Generic[T_co]):
350350
351351
Examples
352352
--------
353-
>>> class Foo(capellacore.CapellaElement):
354-
... bar = Single["Bar"](Containment("bar", (NS, "Bar")))
353+
This example creates two classes Foo and Bar, where Bar may contain
354+
a single Foo object:
355+
356+
>>> import capellambse.model as m
357+
>>> from capellambse.metamodel import capellacore
358+
>>> TEST_NS = m.Namespace(m.VIRTUAL_NAMESPACE_PREFIX + "/test", "test")
359+
>>>
360+
>>> class Foo(capellacore.CapellaElement, ns=TEST_NS): ...
361+
>>> class Bar(capellacore.CapellaElement, ns=TEST_NS):
362+
... foo = Single["Foo"](Containment("ownedFoo", (TEST_NS, "Foo")))
355363
"""
356364

357365
def __init__(

src/capellambse/model/_model.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,17 @@ class as the superclass of every concrete model element
383383
return a list of every Logical Component in the model:
384384
385385
>>> model.search("LogicalComponent")
386+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
387+
...
386388
>>> model.search("org.polarsys.capella.core.data.la:LogicalComponent")
389+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
390+
...
387391
>>> model.search( (capellambse.metamodel.la.NS, "LogicalComponent") )
392+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
393+
...
388394
>>> model.search( ("org.polarsys.capella.core.data.la", "LogicalComponent") )
395+
[0] <LogicalComponent 'Hogwarts' (0d2edb8f-fa34-4e73-89ec-fb9a63001440)>
396+
...
389397
"""
390398
classes: set[type[_obj.ModelObject]] = set()
391399
for clsname in clsnames:
@@ -628,18 +636,18 @@ def update_diagram_cache(
628636
Passing a bare filename looks up the executable in the PATH,
629637
after replacing a possible '{VERSION}' field:
630638
631-
>>> model.update_diagram_cache("capella", "png")
632-
>>> model.update_diagram_cache("capella{VERSION}", "png")
639+
>>> model.update_diagram_cache("capella", "png") # doctest: +SKIP
640+
>>> model.update_diagram_cache("capella{VERSION}", "png") # doctest: +SKIP
633641
634642
Passing an absolute path to a local installation of Capella that
635643
contains the Capella version will use that executable:
636644
637-
>>> model.update_diagram_cache("/opt/capella{VERSION}/capella", "png")
645+
>>> model.update_diagram_cache("/opt/capella{VERSION}/capella", "png") # doctest: +SKIP
638646
639647
Passing a docker image name will launch a docker container, using the
640648
Capella binary at the image's ENTRYPOINT:
641649
642-
>>> model.update_diagram_cache(
650+
>>> model.update_diagram_cache( # doctest: +SKIP
643651
... "ghcr.io/dsd-dbs/capella-dockerimages/capella/base:{VERSION}-selected-dropins-main",
644652
... "png",
645653
... )

0 commit comments

Comments
 (0)