def test_incompaatible_required_shape(self): kwargs = dict(array=np.arange(3 * 4).reshape(3, 4), required_shape=(3, 5)) expected_exception = ValueError match = "array is incompatible with required_shape." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_compatible_but_not_matched_required_shape(self): kwargs = dict(array=np.arange(3 * 4).reshape(3, 4), required_shape=(4, -1)) expected_exception = ValueError match = ( "array is compatible with required_shape but does not match required_shape." ) with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_forbid_object_dtype(self): kwargs = dict(array=np.array([[], 1]), dtype=None, forbid_object_dtype=True) expected_exception = TypeError match = "Casting array to a np.ndarray produces an array of dtype object \nwhile forbid_object_dtype == True and dtype != object." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_required_ndim(self): kwargs = dict(array=np.arange(3), required_ndim=2) expected_exception = ValueError match = "If required_ndim is not None, array.ndim must be made to equal it." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_array_value_incompatible_with_dtype(self): kwargs = dict(array=np.array("string that is not an int"), dtype=int) expected_exception = ValueError match = "array has a value that is incompatible with dtype." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_array_type_incompataible_with_dtype(self): kwargs = dict(array=np.array(print), dtype=int) expected_exception = TypeError match = "array is of a type that is incompatible with dtype." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_non_type_dtype(self): kwargs = dict(array=np.arange(3), dtype="not of type type") expected_exception = TypeError match = "dtype must be either None or a valid type." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_negative_required_ndim(self): kwargs = dict(array=np.arange(3), required_ndim=-1) expected_exception = ValueError match = "required_ndim must be non-negative." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_non_int_required_ndim(self): kwargs = dict(array=np.arange(3), required_ndim=1.5) expected_exception = TypeError match = "required_ndim must be either None or of type int." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_non_int_minimum_ndim(self): kwargs = dict(array=np.arange(3), minimum_ndim=1.5) expected_exception = TypeError match = "minimum_ndim must be of type int." with pytest.raises(expected_exception, match=match): _validate_ndarray(**kwargs)
def test_wildcard_required_shape(self): kwargs = dict(array=np.arange(3 * 4).reshape(3, 4), required_shape=(3, -1)) correct_output = np.arange(3 * 4).reshape(3, 4) assert np.array_equal(_validate_ndarray(**kwargs), correct_output)
def test_reshape_to_shape(self): kwargs = dict(array=np.arange(3 * 4).reshape(3, 4), reshape_to_shape=(4, 3)) correct_output = np.arange(3 * 4).reshape(4, 3) assert np.array_equal(_validate_ndarray(**kwargs), correct_output)
def test_broadcast_to_shape(self): kwargs = dict(array=np.array([0, 1, 2]), broadcast_to_shape=(2, 3)) correct_output = np.array([[0, 1, 2], [0, 1, 2]]) assert np.array_equal(_validate_ndarray(**kwargs), correct_output)
def test_scalar_array_upcast_by_minimum_ndim(self): kwargs = dict(array=9, minimum_ndim=2) correct_output = np.array(9).reshape(1, 1) assert np.array_equal(_validate_ndarray(**kwargs), correct_output)
def test_array_as_list(self): kwargs = dict(array=[[0, 1, 2], [3, 4, 5]], dtype=float) correct_output = np.arange(2 * 3, dtype=float).reshape(2, 3) assert np.array_equal(_validate_ndarray(**kwargs), correct_output)
def test_cast_array_to_dtype(self): kwargs = dict(array=np.arange(3, dtype=int), dtype=float) correct_output = np.arange(3, dtype=float) assert np.array_equal(_validate_ndarray(**kwargs), correct_output)