def check_raise(data, exception, regexp): """Make sure that load_coordinates() raises 'exception' when a file containing 'data' is parsed. 'regexp' is the regular expression that must be matched by the string representation of the raised exception""" with tempinput(data) as path: with self.assertRaisesRegexp(exception, regexp): list(load_coordinates(path))
def test_load_custom_filters(self): section_header = "[%s]" % passband.CUSTOM_SECTION # Name and description of user-defined filters custom1 = "R-EROS", "R (EROS-2 survey)" custom2 = "NO", "Blank Filter" custom_filters = [custom1, custom2] data = "\n".join([section_header] + ["%s = %s" % x for x in custom_filters]) with util.tempinput(data) as path: loaded = list(passband.load_custom_filters(path)) self.assertEqual(len(custom_filters), len(loaded)) for input, output in zip(custom_filters, loaded): # For example, ('R-EROS', 'R (EROS-2 survey)') self.assertEqual(input[0], output[0]) self.assertEqual(input[1], output[1]) def assert_returns_nothing(path): """Assert that Passband.load_custom_filters() does not return anything when the 'path' configuration file is parsed""" self.assertEqual([], list(passband.load_custom_filters(path))) # Does not return anything if: # (1) The configuration file file does not exist with util.tempinput("") as path: pass self.assertFalse(os.path.exists(path)) assert_returns_nothing(path) # (2) Does not have the CUSTOM_SECTION section data = "" with util.tempinput(data) as path: assert_returns_nothing(path) # (3) The CUSTOM_SECTION section is empty data = section_header with util.tempinput(data) as path: assert_returns_nothing(path)
def test_load_coordinates_scientific_notation_with_propper_motion(self): data = "7.9720694373e-05 44.6352243008 [0.00123] [0.0000432]" with tempinput(data) as path: coordinates = load_coordinates(path) coords_list = list(coordinates) self.assertEqual(len(coords_list), 1) ra, dec, pm_ra, pm_dec = coords_list[0] self.assertAlmostEqual(ra, 7.9720694373e-05) self.assertAlmostEqual(dec, 44.6352243008) self.assertAlmostEqual(pm_ra, 0.00123) self.assertAlmostEqual(pm_dec, 4.32e-05)
def test_load_coordinates_scientific_notation(self): # For some datasets that generate coords so close to zero that they end up in scientific notation data = "7.9720694373e-05 44.6352243008" with tempinput(data) as path: coordinates = load_coordinates(path) coords_list = list(coordinates) self.assertEqual(len(coords_list), 1) ra, dec, pm_ra, pm_dec = coords_list[0] self.assertAlmostEqual(ra, 7.9720694373e-05) self.assertAlmostEqual(dec, 44.6352243008)
def test_load_coordinates(self): for _ in xrange(NITERS): # Randomly choose some of the SIMBAD astronomical objects and write # them to a temporary file, formatting their coordinates and proper # motions in four columns (or just two, if the proper motions are # not known) and inserting a random number of separators before, # between and after the columns and brackets. Then make sure that # load_coordinates() returns the same astronomical objects, in the # same order and with the same coordinates that we wrote. n = random.randint(*self.NCOORDS) objects = random.sample(self.COORDINATES.values(), n) data = self.get_coords_data(objects) with tempinput(data) as path: coordinates = load_coordinates(path) for coords, expected in zip(coordinates, objects): self.assertEqual(coords, expected)
def test_load_coordinates_empty_lines_and_comments(self): # The same as test_load_coordinates(), but randomly inserting a few # empty and comment lines, as well as inline comments, all of which # must be ignored by load_coordinates(). for _ in xrange(NITERS): n = random.randint(*self.NCOORDS) objects = random.sample(self.COORDINATES.values(), n) data = self.get_coords_data(objects) lines = data.split("\n") # Randomly insert inline comments for index in range(len(lines)): if random.random() < self.COMMENT_PROB: sep = self.get_seps(0) comment = self.get_comment() lines[index] += sep + comment # Randomly insert empty lines for _ in range(random.randint(*self.NEMPTY)): index = random.randint(0, len(lines)) empty = self.get_seps(0) lines.insert(index, empty) # Randomly insert comment lines for _ in range(random.randint(*self.NCOMMENTS)): index = random.randint(0, len(lines)) sep = self.get_seps(0) comment = self.get_comment() lines.insert(index, sep + comment) data = "\n".join(lines) with tempinput(data) as path: coordinates = load_coordinates(path) for coords, expected in zip(coordinates, objects): self.assertEqual(coords, expected)
def test_load_coordinates_empty_file(self): # If the file is empty, nothing is returned with tempinput("") as path: self.assertEqual([], list(load_coordinates(path)))