Commit 6be4c2ff authored by Matt Clarkson's avatar Matt Clarkson

Issue #6: Add generate validation tests in validate.py

parent ff7edbd8
Pipeline #663 passed with stages
in 7 minutes and 44 seconds
# coding=utf-8
"""
Various validation functions.
"""
def validate_is_string_or_number(var):
"""
Checks if the input is either a str, int or float.
:param var: input
:raises: TypeError if not
:returns: True
"""
valid_types = (str, int, float)
if not isinstance(var, valid_types):
raise TypeError("Input is not a string or a number:" + str(var))
return True
def validate_width_height(dims):
"""
Checks if dims (width, height) is a valid specification,
meaning width and height are both integers above zero.
:param dims: (width, height)
:raises: TypeError, ValueError
:returns: True
"""
if dims is None:
raise ValueError("Null input for (width, height).")
width, height = dims
if not isinstance(width, int):
raise TypeError("Width should be an integer.")
if not isinstance(height, int):
raise TypeError("Height should be an integer.")
if width < 1:
raise ValueError("Width should be >= 1.")
if height < 1:
raise ValueError("Height should be >= 1.")
return True
......@@ -11,6 +11,10 @@ import os
def validate_is_file(file_name):
"""
Check if file_name is a file.
:param file_name: string containing path name
:raises: ValueError if not
:returns: True
"""
if not file_name:
raise ValueError("Empty parameter file_name")
......@@ -24,6 +28,10 @@ def validate_is_file(file_name):
def validate_is_writable_file(file_name):
"""
Check if file_name is a writable file.
:param file_name: string containing path name
:raises: ValueError if not
:returns: True
"""
validate_is_file(file_name)
......
......@@ -17,7 +17,7 @@ def validate_camera_matrix(matrix):
4. Has 3 columns
:param matrix: camera matrix
:raises: TypeError, ValueError if it fails
:raises: TypeError, ValueError if not
:returns: True
"""
if not isinstance(matrix, np.ndarray):
......@@ -43,7 +43,7 @@ def validate_distortion_coefficients(matrix):
:param matrix: set of distortion coefficients
:raises: TypeError, ValueError if if fails
:raises: TypeError, ValueError if not
:returns: True
"""
if not isinstance(matrix, np.ndarray):
......@@ -68,7 +68,7 @@ def validate_rotation_matrix(matrix):
4. Has 3 columns
:param matrix: rotation matrix
:raises: TypeError, ValueError if it fails
:raises: TypeError, ValueError if not
:returns: True
"""
if not isinstance(matrix, np.ndarray):
......@@ -79,6 +79,7 @@ def validate_rotation_matrix(matrix):
raise ValueError("Rotation matrix should have 3 rows.")
if matrix.shape[1] != 3:
raise ValueError("Rotation matrix should have 3 columns.")
return True
# pylint: disable=invalid-name
......@@ -92,7 +93,7 @@ def validate_translation_column_vector(matrix):
4. Has 1 column
:param matrix: translation matrix
:raises: TypeError, ValueError if it fails
:raises: TypeError, ValueError if not
:return: True
"""
if not isinstance(matrix, np.ndarray):
......@@ -103,3 +104,4 @@ def validate_translation_column_vector(matrix):
raise ValueError("Translation matrix should have 3 rows.")
if matrix.shape[1] != 1:
raise ValueError("Translation matrix should have 1 column.")
return True
# coding=utf-8
"""
Tests for validate.py
"""
import pytest
from sksurgerycore.utilities import validate as v
def test_is_string_or_number():
string = "test"
integer = 1
fp = 1.01
list_invalid = [1, 2, 3]
assert v.validate_is_string_or_number(string)
assert v.validate_is_string_or_number(integer)
assert v.validate_is_string_or_number(fp)
with pytest.raises(TypeError):
v.validate_is_string_or_number(list_invalid)
def test_width_height_invalid_because_none_input():
with pytest.raises(ValueError):
v.validate_width_height(None)
def test_width_height_invalid_because_empty_input():
with pytest.raises(ValueError):
v.validate_width_height(())
def test_width_height_invalid_because_width_wrong_type():
with pytest.raises(TypeError):
v.validate_width_height(("", 2))
def test_width_height_invalid_because_width_too_low():
with pytest.raises(ValueError):
v.validate_width_height((0, 2))
def test_width_height_invalid_because_height_wrong_type():
with pytest.raises(TypeError):
v.validate_width_height((1, ""))
def test_width_height_invalid_because_height_too_low():
with pytest.raises(ValueError):
v.validate_width_height((1, 0))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment