Skip to content

Commit

Permalink
test: update cli test for coregistration
Browse files Browse the repository at this point in the history
  • Loading branch information
vschaffn committed Nov 12, 2024
1 parent 8b0c053 commit 45fcd54
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
"""Function to test the CLI"""

import os
import subprocess

import rasterio

import xdem
from xdem import dem_coregistration


class TestCLI:
# Define paths to the DEM files using xDEM examples
ref_dem_path = xdem.examples.get_path("longyearbyen_ref_dem")
tba_dem_path = xdem.examples.get_path("longyearbyen_tba_dem")
aligned_dem_path = "aligned_dem.tiff"
inlier_mask_path = "inlier_mask.tiff"

def test_xdem_cli(self) -> None:
def test_xdem_cli_coreg(self) -> None:
try:
# Run the xDEM CLI command with the reference and secondary DEM files
result = subprocess.run(
["xdem", self.ref_dem_path, self.tba_dem_path],
["xdem", "coregister", self.ref_dem_path, self.tba_dem_path],
capture_output=True,
text=True,
)
assert "hello world" in result.stdout

# Assert ClI ran successfully
assert result.returncode == 0

# Verify the existence of the output files
assert os.path.exists(self.aligned_dem_path), f"Aligned DEM not found: {self.aligned_dem_path}"
assert os.path.exists(self.inlier_mask_path), f"Inlier mask not found: {self.inlier_mask_path}"

# Retrieve ground truth
true_coreg_dem, coreg_method, out_stats, true_inlier_mask = dem_coregistration(
xdem.DEM(self.tba_dem_path), xdem.DEM(self.ref_dem_path), self.aligned_dem_path
)

# Load elements processed by the xDEM CLI command
aligned_dem = xdem.DEM(self.aligned_dem_path)
with rasterio.open(self.inlier_mask_path) as src:
inlier_mask = src.read(1)

# Verify match with ground truth
assert aligned_dem == true_coreg_dem, "Aligned DEM does not match the ground truth."
assert inlier_mask.all() == true_inlier_mask.all(), "Inlier mask does not match the ground truth."

# Erase files
os.remove(self.aligned_dem_path)
os.remove(self.inlier_mask_path)

except FileNotFoundError as e:
# In case 'xdem' is not found
raise AssertionError(f"CLI command 'xdem' not found : {e}")
Expand Down

0 comments on commit 45fcd54

Please sign in to comment.