Skip to content

Commit

Permalink
Issue #46: Added tests for new UI apps.
Browse files Browse the repository at this point in the history
  • Loading branch information
MattClarkson committed Mar 18, 2022
1 parent 1ee579d commit a594a73
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/ui/test_video_calibration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Tests for command line application """
import pytest
from sksurgerycalibration.ui.video_calibration_command_line import main

def test_cl_no_config():
""" Run command line app with no config file. The parser should
raise SystemExit due to missing required argument"""
with pytest.raises(SystemExit) as pytest_wrapped_e:
main([])

#I'm not sure how useful the next 2 asserts are. We already know it's
#a SystemExit, if the code value specific to the parser?
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 2


def test_cl_with_config():
""" Run command line app with config """
main(['-c', 'config/recorded_chessboard.json'])
60 changes: 60 additions & 0 deletions tests/ui/test_video_calibration_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Tests for command line application """
import copy
import os
import pytest
from sksurgerycalibration.ui.video_calibration_app import run_video_calibration

config = { "method": "chessboard",
"source": "tests/data/laparoscope_calibration/left/left.ogv",
"corners": [14, 10],
"square size in mm": 6,
"minimum number of views": 5,
"keypress delay": 0,
"interactive" : False,
"sample frequency" : 2
}

def _clean_up(prefix):
"""Helper to clean up calibration results"""
for i in range(5):
os.remove(prefix + ".extrinsics." + str(i) + ".txt")
os.remove(prefix + ".ids." + str(i) + ".txt")
os.remove(prefix + ".image_points." + str(i) + ".txt")
os.remove(prefix + ".object_points." + str(i) + ".txt")
os.remove(prefix + ".images." + str(i) + ".png")
os.remove(prefix + ".distortion.txt")
os.remove(prefix + ".handeye.txt")
os.remove(prefix + ".intrinsics.txt")
os.remove(prefix + ".pattern2marker.txt")


def test_with_save_prefix():
""" Run command line app with a save prefix"""
run_video_calibration(config, prefix = "testjunk")
_clean_up("testjunk")

def test_with_save_directory():
""" Run command line app with a save prefix"""
run_video_calibration(config, save_dir = "testjunk")
_clean_up("testjunk/calib")
os.rmdir("testjunk")

def test_with_invalid_method():
"""Should throw a value error if method is not supported"""
duff_config = copy.deepcopy(config)
duff_config['method'] = 'not chessboard'
with pytest.raises(ValueError):
run_video_calibration(duff_config)

def test_with_invalid_capture():
"""Should throw a runtime error if we can't open video capture"""
duff_config = copy.deepcopy(config)
duff_config['source'] = 'bad source'
with pytest.raises(RuntimeError):
run_video_calibration(duff_config)

def test_with_custome_window_size():
"""We should be able to set the window size in config"""
ok_config = copy.deepcopy(config)
ok_config['window size'] = [640, 480]
run_video_calibration(ok_config)
21 changes: 21 additions & 0 deletions tests/ui/test_video_calibration_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Tests for command line application """
import pytest
from sksurgerycalibration.ui.video_calibration_checker_command_line import main

def test_cl_no_config():
""" Run command line app with no config file. The parser should
raise SystemExit due to missing required argument"""
with pytest.raises(SystemExit) as pytest_wrapped_e:
main([])

#I'm not sure how useful the next 2 asserts are. We already know it's
#a SystemExit, if the code value specific to the parser?
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 2


def test_cl_with_config():
""" Run command line app with config """
main(['-c', 'config/recorded_chessboard.json',
'-d', 'tests/data/laparoscope_calibration/cbh-viking/',
'-p', 'calib.left'])
48 changes: 48 additions & 0 deletions tests/ui/test_video_calibration_checker_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Tests for command line application """
import copy
import pytest
from sksurgerycalibration.ui.video_calibration_checker_app import \
run_video_calibration_checker

config = { "method": "chessboard",
"source": "tests/data/laparoscope_calibration/left/left.ogv",
"corners": [14, 10],
"square size in mm": 6,
"minimum number of views": 5,
"keypress delay": 0,
"interactive" : False,
"sample frequency" : 2
}

def test_with_no_config():
"""It shouldn't run with no configuration file"""
with pytest.raises(ValueError):
run_video_calibration_checker(None,
calib_dir = 'tests/data/laparoscope_calibration/cbh-viking',
prefix = "calib.right")


def test_with_prefix():
""" Run command line app with an existing calibration"""
run_video_calibration_checker(config,
calib_dir = 'tests/data/laparoscope_calibration/cbh-viking',
prefix = "calib.right")


def test_with_invalid_capture():
"""Should throw a runtime error if we can't open video capture"""
duff_config = copy.deepcopy(config)
duff_config['source'] = 'bad source'
with pytest.raises(RuntimeError):
run_video_calibration_checker(duff_config,
calib_dir = 'tests/data/laparoscope_calibration/cbh-viking',
prefix = "calib.right")


def test_with_custome_window_size():
"""We should be able to set the window size in config"""
ok_config = copy.deepcopy(config)
ok_config['window size'] = [640, 480]
run_video_calibration_checker(ok_config,
calib_dir = 'tests/data/laparoscope_calibration/cbh-viking',
prefix = "calib.right")

0 comments on commit a594a73

Please sign in to comment.