Coverage for tests/testAllObjects.py : 49%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# The following is to get the object ids in the registry
os.mkdir(catDir) dir=catDir, suffix='.txt')
continue
# if the exception was due to a failed connection # to fatboy, ignore it
else: raise
obs_metadata = dbobj.testObservationMetaData
# Get results all at once try: result = dbobj.query_columns(obs_metadata=obs_metadata) except:
# This is because the solar system object 'tables' # don't actually connect to tables on fatboy; they just # call methods stored on fatboy. Therefore, the connection # failure will not be noticed until this part of the test
ct_failed_connection += 1 list_of_failures.append(objname) msg = sys.exc_info()[1].args[0] if 'DB-Lib error' in msg: continue else: raise
ct_connected += 1
# Since there is only one chunk, try: result = next(result) except StopIteration: raise RuntimeError("No results for %s defined in %s"%(objname, inspect.getsourcefile(dbobj.__class__))) if objname.startswith('galaxy'): DummyCat.column_outputs = ['galid', 'raJ2000', 'decJ2000'] else: DummyCat.column_outputs = ['raJ2000', 'decJ2000'] cat = dbobj.getCatalog(__file__+'unit_test_catalog', obs_metadata) if os.path.exists(catName): os.unlink(catName) try: cat.write_catalog(catName) dtypeList = [(name, np.float) for name in cat._column_outputs] testData = np.genfromtxt(catName, delimiter = ', ', dtype=np.dtype(dtypeList)) self.assertGreater(len(testData), 0) finally: if os.path.exists(catName): os.unlink(catName)
os.mkdir(catDir) dir=catDir, suffix='.txt')
obs_metadata = dbobj.testObservationMetaData # To cover the central ~raft obs_metadata.boundLength = 0.4 obs_metadata.rotSkyPos = 0.0 cat = dbobj.getCatalog('obs_star_cat', obs_metadata) if os.path.exists(catName): os.unlink(catName) try: cat.write_catalog(catName) dtypeList = [(name, np.float) for name in cat._column_outputs] testData = np.genfromtxt(catName, delimiter = ', ', dtype=np.dtype(dtypeList)) self.assertGreater(len(testData), 0) finally: if os.path.exists(catName): os.unlink(catName) if os.path.exists(catDir): shutil.rmtree(catDir)
print('\ntestObsCat successfully connected to fatboy')
# if the exception was because of a failed connection # to fatboy, ignore it.
else: raise
""" Test that the limit kwarg in query_columns behaves correctly.
Will test on one star table and one galaxy table. """
# if the exception was due to a failed connection # to fatboy, ignore it else: raise
obs_metadata = dbobj.testObservationMetaData
results = dbobj.query_columns(obs_metadata=obs_metadata)
ct_res = 0 for chunk in results: for line in chunk: ct_res += 1
self.assertGreater(ct_res, 10, msg=msg)
limited_results = dbobj.query_columns(obs_metadata=obs_metadata, limit=10)
ct_limit = 0 for chunk in limited_results: for line in chunk: ct_limit += 1
self.assertEqual(ct_limit, 10, msg=msg)
"for the test on the limit kwarg")
""" Test that passing a constraint into query_columns works (i.e. if I only want to select galaxies whose varParamStr is not NULL). """
# if the exception was due to a failed connection # to fatboy, ignore it else: raise
obs_metadata = dbobj.testObservationMetaData
# query witout a constraint on varParamStr results = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], obs_metadata=obs_metadata)
# count total number of rows (ct_res) and number of rows with a null # varParamStr (ct_no_varparamstr). Note that varParamStr will be the # index=3 entry in result rows because the id of the galaxy gets # automatically added to query results. ct_res = 0 ct_no_varparamstr = 0 for chunk in results: for line in chunk: if line[3] == 'None': ct_no_varparamstr += 1 ct_res += 1
# run the same query, but demanding that varParamStr is not NULL constrained_results = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], obs_metadata=obs_metadata, constraint=constraint)
# count the number of rows with non-NULL varParamStr ct_con = 0 for chunk in constrained_results: for line in chunk: ct_con += 1 self.assertNotEqual(line[3], 'None')
# check that the number of non-NULL varParamStr and NULL varParamStr rows # compare the way that they should self.assertGreater(ct_res, ct_con) self.assertGreater(ct_no_varparamstr, 0) self.assertEqual(ct_res-ct_con, ct_no_varparamstr)
"for the test on the constraint kwarg")
""" Test that limit and constraint work together """
# if the exception was due to a failed connection # to fatboy, ignore it else: raise
obs_metadata = dbobj.testObservationMetaData
# query with a constraint on varParamStr but no limit results_no_limit = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], obs_metadata=obs_metadata, constraint=constraint)
ct_res = 0 for chunk in results_no_limit: for line in chunk: self.assertNotEqual(line[3], 'None') ct_res += 1
self.assertGreater(ct_res, 1)
# run the same query, but limiting the results limited_results = dbobj.query_columns(colnames=['raJ2000', 'decJ2000', 'varParamStr'], obs_metadata=obs_metadata, constraint=constraint, limit=ct_res-1) ct_lim = 0 for chunk in limited_results: for line in chunk: ct_lim += 1 self.assertNotEqual(line[3], 'None')
self.assertEqual(ct_lim, ct_res-1)
"for the test on the constraint and limit kwargs")
lsst.utils.tests.init() unittest.main() |