mimic python error message and ensure that error is raised

This commit is contained in:
Martin Diehl 2021-02-12 17:04:35 +01:00
parent 9e8a243d6f
commit fe63adcca4
2 changed files with 42 additions and 24 deletions

View File

@ -288,8 +288,11 @@ class Orientation(Rotation):
set_rot = set(inspect.signature(function).parameters.keys()) & set(parent_dict.keys())
ori_dict = {key: parent_dict[key] for key in set_ori}
rot_dict = {key: parent_dict[key] for key in set_rot}
if(set(parent_dict.keys())-(set_ori|set_rot)):
raise KeyError(f'Unknown key {set(parent_dict.keys())-(set_ori|set_rot)} present')
invalid_keys = set(parent_dict.keys())-(set_ori|set_rot)
if invalid_keys:
raise TypeError(f"{inspect.stack()[1][3]}() got an unexpected keyword argument '{invalid_keys.pop()}'")
return rot_dict,ori_dict

View File

@ -141,7 +141,7 @@ class TestOrientation:
dict(lattice='hP',a=1.0 ),
dict(lattice='cI',a=1.0, ),
])
def test_from_direction(self,kwargs):
def test_from_directions(self,kwargs):
for a,b in np.random.random((10,2,3)):
c = np.cross(b,a)
if np.all(np.isclose(c,0)): continue
@ -151,6 +151,21 @@ class TestOrientation:
assert np.isclose(np.dot(x/np.linalg.norm(x),np.array([1,0,0])),1) \
and np.isclose(np.dot(z/np.linalg.norm(z),np.array([0,0,1])),1)
@pytest.mark.parametrize('function',[Orientation.from_random,
Orientation.from_quaternion,
Orientation.from_Euler_angles,
Orientation.from_axis_angle,
Orientation.from_basis,
Orientation.from_matrix,
Orientation.from_Rodrigues_vector,
Orientation.from_homochoric,
Orientation.from_cubochoric,
Orientation.from_spherical_component,
Orientation.from_fiber_component,
Orientation.from_directions])
def test_invalid_from(self,function):
with pytest.raises(TypeError):
function(c=.1,degrees=True,invalid=66)
def test_negative_angle(self):
with pytest.raises(ValueError):