Commit bc59a37c authored by Matt Clarkson's avatar Matt Clarkson

Merge branch '8-fix-validation-error-msgs'

parents 330b17a5 c684b3db
......@@ -13,16 +13,19 @@ def validate_is_file(file_name):
Check if file_name is a file.
:param file_name: string containing path name
:raises: ValueError if not
:raises: TypeError if not string, ValueError if not file
:returns: True
"""
if not file_name:
raise ValueError("Empty parameter file_name")
raise ValueError("Empty file_name.")
if not isinstance(file_name, str):
raise TypeError("file_name is not a string.")
if os.path.isfile(file_name):
return True
raise ValueError('File `' + file_name + '` is not a file')
raise ValueError('File `' + str(file_name) + '` is not a file')
def validate_is_writable_file(file_name):
......@@ -30,7 +33,7 @@ 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
:raises: TypeError if not string, ValueError if not file
:returns: True
"""
validate_is_file(file_name)
......@@ -38,4 +41,4 @@ def validate_is_writable_file(file_name):
if os.access(file_name, os.W_OK):
return True
raise ValueError('File `' + file_name + '` is not a writeable file.')
raise ValueError('File `' + str(file_name) + '` is not a writeable file.')
# Generated files
output/
# Mac Finder:
.DS_Store
......@@ -51,3 +51,7 @@ def test_width_height_invalid_because_height_wrong_type():
def test_width_height_invalid_because_height_too_low():
with pytest.raises(ValueError):
v.validate_width_height((1, 0))
def test_width_height_valid():
assert v.validate_width_height((1, 1))
......@@ -4,17 +4,36 @@
Tests for file_utilities.py
"""
import os
import pytest
from sksurgerycore.utilities import validate_file as f
def test_invalid_file_name_because_its_null():
with pytest.raises(ValueError):
f.validate_is_file(None)
def test_invalid_file_name_because_its_empty():
with pytest.raises(ValueError):
f.validate_is_file("")
def test_invalid_file_name_because_its_an_integer():
with pytest.raises(TypeError):
f.validate_is_file(1)
def test_invalid_file_name_because_non_existent():
with pytest.raises(ValueError):
f.validate_is_file("/invalid/file/name")
def test_invalid_file_name_because_directory():
def test_invalid_file_name_because_its_a_directory():
with pytest.raises(ValueError):
f.validate_is_file(".")
......@@ -30,3 +49,23 @@ def test_valid_writeable_file():
result = f.validate_is_writable_file("tests/data/FordPrefect.json")
assert result
def test_invalid_writeable_file_as_its_not_writeable():
output_dir = 'tests/output'
if not os.path.exists(output_dir):
os.mkdir(output_dir)
file_name = os.path.join(output_dir, 'unwriteable.txt')
with open(file_name, 'w') as out_file:
out_file.write('Hello world')
os.chmod(file_name, 0o400)
with pytest.raises(ValueError):
f.validate_is_writable_file(file_name)
os.chmod(file_name, 0o600)
os.remove(file_name)
......@@ -39,6 +39,11 @@ def test_camera_matrix_invalid_because_too_many_columns():
vm.validate_camera_matrix(np.ones((3, 4)))
def test_camera_matrix_valid():
camera_matrix = np.ones((3, 3))
assert vm.validate_camera_matrix(camera_matrix)
def test_distortion_coefficients_invalid_because_wrong_type():
with pytest.raises(TypeError):
vm.validate_distortion_coefficients(1)
......@@ -65,6 +70,11 @@ def test_distortion_coefficients_invalid_because_number_of_columns_not_in_list()
vm.validate_distortion_coefficients(np.ones((1, 6)))
def test_distortion_coefficients_valid():
distortion_coeff = np.ones((1, 5))
assert vm.validate_distortion_coefficients(distortion_coeff)
def test_rotation_matrix_invalid_because_wrong_type():
with pytest.raises(TypeError):
vm.validate_rotation_matrix(1)
......@@ -95,6 +105,11 @@ def test_rotation_matrix_invalid_because_too_few_columns():
vm.validate_rotation_matrix(np.ones((3, 2)))
def test_rotation_matrix_valid():
rotation_matrix = np.eye(3)
assert vm.validate_rotation_matrix(rotation_matrix)
def test_translation_matrix_invalid_because_wrong_type():
with pytest.raises(TypeError):
vm.validate_translation_column_vector(1)
......@@ -118,3 +133,8 @@ def test_translation_matrix_invalid_because_too_many_columns():
def test_translation_matrix_invalid_because_too_few_rows():
with pytest.raises(ValueError):
vm.validate_translation_column_vector(np.ones((2, 1)))
def test_translation_matrix_valid():
translation_matrix = np.zeros((3, 1))
assert vm.validate_translation_column_vector(translation_matrix)
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