From c49ff1a712d32470d69e5f3f558721f8d4bb13ac Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Tue, 7 May 2024 16:28:37 -0700 Subject: [PATCH] fix up parameterization of tests and tests of tests --- py/desiutil/names.py | 17 +++++++++-------- py/desiutil/test/test_names.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/py/desiutil/names.py b/py/desiutil/names.py index fe56799..2919e8f 100644 --- a/py/desiutil/names.py +++ b/py/desiutil/names.py @@ -42,15 +42,16 @@ def radec_to_desiname(target_ra, target_dec): # Convert to numpy array in case inputs are scalars or lists target_ra, target_dec = np.atleast_1d(target_ra), np.atleast_1d(target_dec) - inputs = {'target_ra': target_ra, 'target_dec': target_dec} - tests = (('NaN values', np.isnan), - ('Infinite values', np.isinf), - ('RA not in range [0, 360)', lambda x: (x < 0) | (x >= 360)), - ('Dec not in range [-90, 90]', lambda x: (x < -90) | (x > 90))) + base_tests = [('NaN values', np.isnan), + ('Infinite values', np.isinf),] + inputs = {'target_ra': {'data': target_ra, + 'tests': base_tests + [('RA not in range [0, 360)', lambda x: (x < 0) | (x >= 360))]}, + 'target_dec': {'data': target_dec, + 'tests': base_tests + [('Dec not in range [-90, 90]', lambda x: (x < -90) | (x > 90))]}} for i in inputs: - for key, check in tests: - if (check(inputs[i])).any(): - raise ValueError(f"{key} detected in {i}!") + for message, check in inputs[i]['tests']: + if check(inputs[i]['data']).any(): + raise ValueError(f"{message} detected in {i}!") # Number of decimal places in final naming convention precision = 4 diff --git a/py/desiutil/test/test_names.py b/py/desiutil/test/test_names.py index 49ab2f6..7e6dd14 100644 --- a/py/desiutil/test/test_names.py +++ b/py/desiutil/test/test_names.py @@ -52,12 +52,27 @@ def test_radec_to_desiname_bad_values(self): 235.25235223446, 99.9999999999999] decs = [29.974787585945496, -42.945872347904356, -0.9968423456, 8.45677345352345, 89.234958294953] - ras[2] = np.nan - with self.assertRaises(ValueError) as e: - outnames = radec_to_desiname(ras, decs) - self.assertEqual(str(e.exception), "NaN values detected in target_ra!") - - ras[2] = np.inf - with self.assertRaises(ValueError) as e: - outnames = radec_to_desiname(ras, decs) - self.assertEqual(str(e.exception), "Infinite values detected in target_ra!") + + original_ra = ras[2] + for message, value in [("NaN values detected in target_ra!", np.nan), + ("Infinite values detected in target_ra!", np.inf), + ("RA not in range [0, 360) detected in target_ra!", -23.914121939862518), + ("RA not in range [0, 360) detected in target_ra!", 360.23454570972834)]: + ras[2] = value + with self.assertRaises(ValueError) as e: + outnames = radec_to_desiname(ras, decs) + self.assertEqual(str(e.exception), message) + + ras[2] = original_ra + + original_dec = decs[2] + for message, value in [("NaN values detected in target_dec!", np.nan), + ("Infinite values detected in target_dec!", np.inf), + ("Dec not in range [-90, 90] detected in target_dec!", -90.9968423456), + ("Dec not in range [-90, 90] detected in target_dec!", 90.9968423456)]: + decs[2] = value + with self.assertRaises(ValueError) as e: + outnames = radec_to_desiname(ras, decs) + self.assertEqual(str(e.exception), message) + + decs[2] = original_dec