Skip to content

Commit 79bc5d9

Browse files
added test for exception with invalid input
1 parent 05cc7ec commit 79bc5d9

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

cs_util/cfis.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ def get_tile_coord_from_nixy(nix, niy):
6565
Parameters
6666
----------
6767
nix : str or list
68-
tile number(s) for x
68+
tile number(s) for x coordinate(s);
6969
niy : str or list
70-
tile number(s) for y
70+
tile number(s) for y coordinate(s)
7171
7272
See also
7373
--------
@@ -79,12 +79,25 @@ def get_tile_coord_from_nixy(nix, niy):
7979
right ascension and declination
8080
8181
"""
82-
# Transform to int
82+
number_pattern = re.compile(r'\d{3}')
83+
84+
# Transform from str to int
8385
if not np.isscalar(nix):
86+
# Check input numbers
87+
if not all(
88+
[bool(number_pattern.fullmatch(number)) for number in nix + niy]
89+
):
90+
raise ValueError('Input needs to be three-digit numbers')
91+
8492
# Transform to list
8593
xi = np.array(nix).astype(int)
8694
yi = np.array(niy).astype(int)
8795
else:
96+
if not all(
97+
[bool(number_pattern.fullmatch(number)) for number in [nix, niy]]
98+
):
99+
raise ValueError('Input needs to be three-digit numbers')
100+
88101
xi = int(nix)
89102
yi = int(niy)
90103

cs_util/tests/test_cfis.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def setUp(self):
3030
self._ra = [216.86237, 214.43017]
3131
self._unit = units.deg
3232

33+
self._nix_nok = ['23x', '1234']
3334
self._tile_number_nok = '12x.456'
3435

3536
def tearDown(self):
@@ -109,3 +110,23 @@ def test_get_tile_coord_from_nixy(self):
109110
# Test units
110111
self.assertTrue(ra[idx].unit == self._unit)
111112
self.assertTrue(dec[idx].unit == self._unit)
113+
114+
# Test exception for invalid input
115+
self.assertRaises(
116+
ValueError,
117+
cfis.get_tile_coord_from_nixy,
118+
self._nix_nok,
119+
self._niy,
120+
)
121+
self.assertRaises(
122+
ValueError,
123+
cfis.get_tile_coord_from_nixy,
124+
self._niy,
125+
self._nix_nok,
126+
)
127+
self.assertRaises(
128+
ValueError,
129+
cfis.get_tile_coord_from_nixy,
130+
self._nix_nok[0],
131+
self._niy[0],
132+
)

0 commit comments

Comments
 (0)