def test_get_catalogs_in_db(self): ''' Test the query of catalog names from the database. ''' catalog = pick_core.Catalog(name='catalog_name_1', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) catalog = pick_core.Catalog(name='catalog_name_2', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) catalog = pick_core.Catalog(name='catalog_name_3', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) # Create the library. library = pick_core.Library(name='test_name') catalog_names = library.get_catalogs_in_db(project=self.project) self.assertIsInstance(catalog_names, list) self.assertEqual(len(catalog_names), 3) self.assertListEqual( catalog_names, ['catalog_name_1', 'catalog_name_2', 'catalog_name_3'])
def test_load_picks_from_database(self): ''' Test the loading of the picks from the database. ''' catalog = pick_core.Catalog(name = 'test', description = 'A test description.', agency_uri = 'uot', author_uri = 'tester') # Create a pick. pick_time = UTCDateTime('2011-01-01T00:00:00') channel = self.project.geometry_inventory.get_channel(name = 'HHZ', station = 'SITA') channel = channel[0] pick1 = pick_core.Pick(label = 'P', time = pick_time, amp1 = 10, channel = channel) pick2 = pick_core.Pick(label = 'S', time = pick_time + 10, amp1 = 20, channel = channel) catalog.add_picks([pick1, pick2]) catalog.write_to_database(self.project) load_catalog = pick_core.Catalog(name = 'load', db_id = catalog.db_id) load_catalog.load_picks(self.project) self.assertEqual(len(load_catalog.picks), 2)
def test_write_to_database_with_picks(self): ''' Test the writing to the database of a catalog with events. ''' catalog = pick_core.Catalog(name = 'test', description = 'A test description.', agency_uri = 'uot', author_uri = 'tester') # Create a pick. pick_time = UTCDateTime('2011-01-01T00:00:00') channel = self.project.geometry_inventory.get_channel(name = 'HHZ', station = 'SITA') channel = channel[0] pick1 = pick_core.Pick(label = 'P', time = pick_time, amp1 = 10, channel = channel) pick2 = pick_core.Pick(label = 'S', time = pick_time + 10, amp1 = 20, channel = channel) catalog.add_picks([pick1, pick2]) catalog.write_to_database(self.project) catalog_orm_class = self.project.dbTables['pick_catalog'] db_session = self.project.getDbSession() result = db_session.query(catalog_orm_class).all() self.assertEqual(len(result), 1) tmp = result[0] self.assertEqual(len(tmp.picks), 2) self.assertEqual(tmp.picks[0].catalog_id, catalog.db_id) db_session.close() # Add a third pick and update the catalog in the database. channel = self.project.geometry_inventory.get_channel(name = 'HHZ', station = 'GUWA') channel = channel[0] pick3 = pick_core.Pick(label = 'P', time = pick_time, amp1 = 30, channel = channel) catalog.add_picks([pick3,]) catalog.write_to_database(self.project) db_session = self.project.getDbSession() result = db_session.query(catalog_orm_class).all() self.assertEqual(len(result), 1) tmp = result[0] self.assertEqual(len(tmp.picks), 3) self.assertEqual(tmp.picks[0].catalog_id, catalog.db_id) self.assertEqual(tmp.picks[1].catalog_id, catalog.db_id) self.assertEqual(tmp.picks[2].catalog_id, catalog.db_id) db_session.close()
def create_catalog(self, mode, name, description): ''' Create a new catalog in the database. ''' catalog = pick_core.Catalog(name = name, mode = mode, description = description, agency_uri = self.parent.project.activeUser.agency_uri, author_uri = self.parent.project.activeUser.author_uri, creation_time = UTCDateTime().isoformat()) catalog.write_to_database(self.parent.project) cur_limit = self.pref_manager.get_limit('pick_catalog') cur_limit.append(catalog.name) self.pref_manager.set_limit('pick_catalog', cur_limit)
def test_catalog_creation(self): ''' Test the pSysmon Event class. ''' # Create an event with valid time limits. catalog = pick_core.Catalog(name = 'test_name') self.assertIsInstance(catalog, pick_core.Catalog) self.assertEqual(catalog.name, 'test_name') self.assertEqual(catalog.mode, 'time') self.assertIsNone(catalog.db_id) self.assertIsNone(catalog.description) self.assertIsNone(catalog.agency_uri) self.assertIsNone(catalog.author_uri) self.assertIsNotNone(catalog.creation_time) self.assertListEqual(catalog.picks, [])
def test_add_picks(self): ''' Test the add_picks method. ''' catalog = pick_core.Catalog(name = 'test') # Create the pick pick_time = UTCDateTime('2011-01-01T00:00:00') channel = self.project.geometry_inventory.get_channel(name = 'HHZ', station = 'SITA') channel = channel[0] pick = pick_core.Pick(label = 'P', time = pick_time, amp1 = 10, channel = channel) catalog.add_picks([pick,]) self.assertEqual(len(catalog.picks), 1) self.assertEqual(catalog.picks[0], pick) self.assertEqual(pick.parent, catalog)
def test_write_to_database(self): ''' Test the write_to_database method. ''' catalog = pick_core.Catalog(name = 'test', description = 'A test description.', agency_uri = 'uot', author_uri = 'tester') catalog.write_to_database(self.project) catalog_orm_class = self.project.dbTables['pick_catalog'] db_session = self.project.getDbSession() result = db_session.query(catalog_orm_class).all() db_session.close() self.assertEqual(len(result), 1) tmp = result[0] self.assertEqual(tmp.name, 'test') self.assertEqual(tmp.description, 'A test description.') self.assertEqual(tmp.agency_uri, 'uot') self.assertEqual(tmp.author_uri, 'tester')
def test_load_catalog_from_db(self): ''' Test the loading of catalogs from the database. ''' catalog = pick_core.Catalog(name='catalog_name_1', description='A test description.', agency_uri='uot', author_uri='tester') # Create some picks. pick_time = UTCDateTime('2011-01-01T00:00:00') channel = self.project.geometry_inventory.get_channel(name='HHZ', station='SITA') channel = channel[0] pick1 = pick_core.Pick(label='P', time=pick_time, amp1=10, channel=channel) pick2 = pick_core.Pick(label='S', time=pick_time + 10, amp1=20, channel=channel) catalog.add_picks([pick1, pick2]) catalog.write_to_database(self.project) # Create a second catalog. catalog = pick_core.Catalog(name='catalog_name_2', description='A test description.', agency_uri='uot', author_uri='tester') catalog.write_to_database(self.project) # Create the library. library = pick_core.Library(name='test_name') # Test the loading of one catalog without picks. library.load_catalog_from_db(project=self.project, name='catalog_name_1') self.assertEqual(len(library.catalogs), 1) self.assertEqual(iter(library.catalogs.keys()), ['catalog_name_1']) self.assertIsInstance(library.catalogs['catalog_name_1'], pick_core.Catalog) cur_catalog = library.catalogs['catalog_name_1'] self.assertEqual(len(cur_catalog.picks), 0) # Test the loading of one catalog without picks. library.clear() library.load_catalog_from_db(project=self.project, name='catalog_name_1', load_picks=True) self.assertEqual(len(library.catalogs), 1) self.assertEqual(iter(library.catalogs.keys()), ['catalog_name_1']) self.assertIsInstance(library.catalogs['catalog_name_1'], pick_core.Catalog) cur_catalog = library.catalogs['catalog_name_1'] self.assertEqual(len(cur_catalog.picks), 2) # Test the loading of all catalogs. library.clear() catalog_names = library.get_catalogs_in_db(project=self.project) library.load_catalog_from_db(project=self.project, name=catalog_names) self.assertEqual(len(library.catalogs), 2) self.assertListEqual(sorted(library.catalogs.keys()), ['catalog_name_1', 'catalog_name_2'])