Skip to content

Commit 24e987c

Browse files
committed
Python: add minimal tests
Signed-off-by: Tristram Gräbener <tristram+git@tristramg.eu>
1 parent 8474e27 commit 24e987c

4 files changed

Lines changed: 152 additions & 1 deletion

File tree

python/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

python/pyproject.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
[project]
2+
name = "liblrs_python"
3+
version = "0.2.6"
4+
requires-python = ">=3.12"
5+
dependencies = ["pip>=25.2"]
6+
17
[build-system]
28
requires = ["maturin>=1.0,<2.0"]
39
build-backend = "maturin"
@@ -10,6 +16,9 @@ features = ["pyo3/extension-module"]
1016
# That is why we add it explicitely when using sdist
1117
include = [{ path = "liblrs_python.pyi", format = "sdist" }]
1218
# Avoid a collision with the README.md at the root of the directory
13-
exclude = [{path = "README.md", format = "sdist"}]
19+
exclude = [{ path = "README.md", format = "sdist" }]
1420
# Reduce the binary size (from 8.9 to 1.4 when tested)
1521
strip = true
22+
23+
[dependency-groups]
24+
dev = ["pytest"]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from pathlib import Path
2+
from liblrs_python import Builder, Point, Lrs, SegmentOfTraversal, AnchorOnLrm
3+
4+
def build_lrm() -> Builder:
5+
"""Helper function that builds an example LRS with one lrm"""
6+
7+
# First we initialize the builder that will help us as we create new data
8+
builder = Builder()
9+
10+
# We want to create a segment that has coordinates and nodes at its extremity
11+
coords = [Point(0, 0), Point(0, 1)]
12+
segment_id = "a small section of rail"
13+
start_node = 0
14+
end_node = 1
15+
segment_handle = builder.add_segment(segment_id, coords, start_node, end_node)
16+
17+
# Then we build a traversal with that segment
18+
# Typically, a highway will be composed of many segments between each intersection
19+
segment_reversed = False
20+
segment_of_traversal = SegmentOfTraversal(segment_handle, segment_reversed)
21+
traversal_id = "A very long railroad composed of many segments"
22+
traversal_handle = builder.add_traversal(traversal_id, [segment_of_traversal])
23+
24+
# Anchors are reference points with coordinates that will help us to position ourself by measuring a distance from then
25+
green_anchor = builder.add_anchor(id="Green House", coord=Point(0.01, 0), name="GH", properties={})
26+
red_anchor = builder.add_anchor(id="Red House", coord=Point(-0.01, 1), name="RH", properties={})
27+
28+
# We associate those anchors to a traversal by defining a distance along that traversal
29+
green_on_traversal = AnchorOnLrm(green_anchor, 0)
30+
red_on_traversal = AnchorOnLrm(red_anchor, 1000)
31+
32+
# Now we have every thing to create a linear reference model
33+
builder.add_lrm("My first Lrm", traversal_handle, [green_on_traversal, red_on_traversal], {})
34+
35+
return builder
36+
37+
38+
def test_build_in_memory():
39+
builder = build_lrm()
40+
lrs = builder.build_lrs({})
41+
assert lrs.lrm_len() == 1
42+
43+
def test_build_to_file(tmp_path: Path):
44+
path = Path(tmp_path, "my_lrs")
45+
builder = build_lrm()
46+
builder.save(path, {})
47+
with open(path, "rb") as lrs_file:
48+
lrs = Lrs(lrs_file.read())
49+
assert lrs.lrm_len() == 1

python/uv.lock

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)