Skip to content

Commit 06ce411

Browse files
committed
Add tests for building pipelines by piping stages together
1 parent 0b31a60 commit 06ce411

File tree

1 file changed

+101
-7
lines changed

1 file changed

+101
-7
lines changed

test/test_pipeline.py

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,41 @@ def get_pipeline(filename, validate=True):
2121
return pipeline
2222

2323

24+
def test_dimensions():
25+
"""Ask PDAL for its valid dimensions list"""
26+
dims = pdal.dimensions
27+
assert 71 < len(dims) < 120
28+
29+
2430
class TestPipeline:
2531
@pytest.mark.parametrize("filename", ["sort.json", "sort.py"])
2632
def test_construction(self, filename):
2733
"""Can we construct a PDAL pipeline"""
2834
assert isinstance(get_pipeline(filename), pdal.Pipeline)
2935

36+
# construct Pipeline from a sequence of stages
37+
r = pdal.Reader("r")
38+
f = pdal.Filter("f")
39+
for spec in (r, f), [r, f]:
40+
p = pdal.Pipeline(spec)
41+
assert isinstance(p, pdal.Pipeline)
42+
assert len(p.stages) == 2
43+
44+
@pytest.mark.parametrize(
45+
"pipeline",
46+
[
47+
"{}",
48+
'{"foo": []}',
49+
"[1, 2]",
50+
'{"pipeline": [["a.las", "b.las"], "c.las"]}',
51+
],
52+
)
53+
def test_invalid_json(self, pipeline):
54+
"""Do we complain with bad pipelines"""
55+
json.loads(pipeline)
56+
with pytest.raises(ValueError):
57+
pdal.Pipeline(pipeline)
58+
3059
@pytest.mark.parametrize("filename", ["sort.json", "sort.py"])
3160
def test_execution(self, filename):
3261
"""Can we execute a PDAL pipeline"""
@@ -87,6 +116,78 @@ def test_merged_arrays(self, filename):
87116
arrays = r.arrays
88117
assert len(arrays) == 43
89118

119+
@pytest.mark.parametrize("filename", ["chip.json", "chip.py"])
120+
def test_stages(self, filename):
121+
"""Can we break up a pipeline as a sequence of stages"""
122+
stages = pdal.Reader("test/data/autzen-utm.las").pipeline().stages
123+
assert len(stages) == 1
124+
125+
stages = get_pipeline(filename).stages
126+
assert len(stages) == 3
127+
128+
assert isinstance(stages[0], pdal.Reader)
129+
assert stages[0].type == "readers.las"
130+
131+
assert isinstance(stages[1], pdal.Filter)
132+
assert stages[1].type == "filters.chipper"
133+
134+
assert isinstance(stages[2], pdal.Writer)
135+
assert stages[2].type == "writers.las"
136+
137+
def test_pipe_stages(self):
138+
"""Can we build a pipeline by piping stages together"""
139+
read = pdal.Reader("test/data/autzen-utm.las")
140+
frange = pdal.Filter.range(limits="Intensity[50:200)")
141+
fsplitter = pdal.Filter.splitter(length=1000)
142+
fdelaunay = pdal.Filter.delaunay(inputs=[frange, fsplitter])
143+
144+
# pipe stages together
145+
pipeline = read | frange | fsplitter | fdelaunay
146+
assert pipeline.validate()
147+
148+
# pipe a pipeline to a stage
149+
pipeline = read | (frange | fsplitter | fdelaunay)
150+
assert pipeline.validate()
151+
152+
# pipe a pipeline to a pipeline
153+
pipeline = (read | frange) | (fsplitter | fdelaunay)
154+
assert pipeline.validate()
155+
156+
def test_pipe_stage_errors(self):
157+
"""Do we complain with piping invalid objects"""
158+
r = pdal.Reader("r", tag="r")
159+
f = pdal.Filter("f")
160+
w = pdal.Writer("w", inputs=["r", f])
161+
162+
with pytest.raises(TypeError):
163+
r | (f, w)
164+
with pytest.raises(TypeError):
165+
(r, f) | w
166+
with pytest.raises(TypeError):
167+
(r, f) | (f, w)
168+
169+
pipeline = r | w
170+
with pytest.raises(RuntimeError) as ctx:
171+
pipeline.validate()
172+
assert "Undefined stage 'f'" in str(ctx.value)
173+
174+
def test_inputs(self):
175+
"""Can we combine pipelines with inputs"""
176+
data = np.load(os.path.join(DATADIRECTORY, "test3d.npy"))
177+
f = pdal.Filter.splitter(length=1000)
178+
pipeline = f.pipeline(data)
179+
assert pipeline.validate()
180+
181+
# a pipeline with inputs can be followed by stage/pipeline
182+
assert (pipeline | pdal.Writer.null()).validate()
183+
assert (pipeline | (f | pdal.Writer.null())).validate()
184+
185+
# a pipeline with inputs cannot follow another stage/pipeline
186+
with pytest.raises(ValueError):
187+
pdal.Reader("r") | pipeline
188+
with pytest.raises(ValueError):
189+
(pdal.Reader("r") | f) | pipeline
190+
90191
@pytest.mark.parametrize("filename", ["reproject.json", "reproject.py"])
91192
def test_logging(self, filename):
92193
"""Can we fetch log output"""
@@ -173,13 +274,6 @@ def test_reference_counting(self):
173274
assert refcount == 1
174275

175276

176-
class TestDimensions:
177-
def test_fetch_dimensions(self):
178-
"""Ask PDAL for its valid dimensions list"""
179-
dims = pdal.dimensions
180-
assert 71 < len(dims) < 120
181-
182-
183277
class TestMesh:
184278
@pytest.mark.parametrize("filename", ["sort.json", "sort.py"])
185279
def test_no_execute(self, filename):

0 commit comments

Comments
 (0)