def test_station_cache_readonly_mode(tmpdir): """ Tests the readonly mode of the station cache. There appears to be no simple way to check if the database is actually opened in read-only mode without using a different database wrapper. So this will have to do for now. """ # Most generic way to get the actual data directory. data_dir = os.path.join( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), "data", "station_files") # Create a temporary directory. directory = str(tmpdir) cache_file = os.path.join(directory, "cache.sqlite") seed_directory = os.path.join(directory, "SEED") resp_directory = os.path.join(directory, "RESP") stationxml_directory = os.path.join(directory, "StationXML") os.makedirs(resp_directory) # Add some more RESP files. shutil.copy(os.path.join(data_dir, "resp", "RESP.AF.DODT..BHE"), os.path.join(resp_directory, "RESP.AF.DODT..BHE")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHN"), os.path.join(resp_directory, "RESP.G.FDF.00.BHN")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHZ"), os.path.join(resp_directory, "RESP.G.FDF.00.BHZ")) # Init the station cache once more. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) original = station_cache.get_values() # Now open the same database in read_only mode. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=True) new = station_cache.get_values() assert original == new
def test_station_cache_readonly_mode(tmpdir): """ Tests the readonly mode of the station cache. There appears to be no simple way to check if the database is actually opened in read-only mode without using a different database wrapper. So this will have to do for now. """ # Most generic way to get the actual data directory. data_dir = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile( inspect.currentframe()))), "data", "station_files") # Create a temporary directory. directory = str(tmpdir) cache_file = os.path.join(directory, "cache.sqlite") seed_directory = os.path.join(directory, "SEED") resp_directory = os.path.join(directory, "RESP") stationxml_directory = os.path.join(directory, "StationXML") os.makedirs(resp_directory) # Add some more RESP files. shutil.copy(os.path.join(data_dir, "resp", "RESP.AF.DODT..BHE"), os.path.join(resp_directory, "RESP.AF.DODT..BHE")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHN"), os.path.join(resp_directory, "RESP.G.FDF.00.BHN")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHZ"), os.path.join(resp_directory, "RESP.G.FDF.00.BHZ")) # Init the station cache once more. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) original = station_cache.get_values() # Now open the same database in read_only mode. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=True) new = station_cache.get_values() assert original == new
def test_station_cache(tmpdir): """ Single test case checking the basic workflow. """ # Most generic way to get the actual data directory. data_dir = os.path.join(os.path.dirname(os.path.abspath(inspect.getfile( inspect.currentframe()))), "data", "station_files") # Create a temporary directory. directory = str(tmpdir) cache_file = os.path.join(directory, "cache.sqlite") seed_directory = os.path.join(directory, "SEED") resp_directory = os.path.join(directory, "RESP") stationxml_directory = os.path.join(directory, "StationXML") os.makedirs(seed_directory) os.makedirs(resp_directory) # Copy the SEED file. This files contains exactly one channel, # IU.PAB.00.BHE. shutil.copy(os.path.join(data_dir, "seed", "dataless.IU_PAB"), os.path.join(seed_directory, "dataless.IU_PAB")) # Init the station cache. station_cache = StationCache(cache_file, seed_directory, resp_directory, stationxml_directory) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. Right now the folder only # contains one station. assert len(channels) == 1 assert len(channels["IU.PAB.00.BHE"]) == 1 del station_cache # This SEED file contains three more channels. seed_file = os.path.join(seed_directory, "dataless.BW_FURT") # Copy one more SEED file and check if the changes are reflected. shutil.copy(os.path.join(data_dir, "seed", "dataless.BW_FURT"), seed_file) # Init the station cache once more. station_cache = StationCache(cache_file, seed_directory, resp_directory, stationxml_directory) # Get the list of available channels. It should not contain 4 channels. channels = station_cache.get_channels() assert len(channels) == 4 assert "BW.FURT..EHE" in channels assert "BW.FURT..EHN" in channels assert "BW.FURT..EHZ" in channels # Now attempt to only retrieve the stations. It should have 2 stations. # One from each SEED file. stations = station_cache.get_stations() assert len(stations) == 2 del station_cache # Delete the file, and check if everything else is removed as well. It # should not only contain one channel. os.remove(seed_file) station_cache = StationCache(cache_file, seed_directory, resp_directory, stationxml_directory) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 1 assert len(channels["IU.PAB.00.BHE"]) == 1 # Add the file once again... del station_cache shutil.copy(os.path.join(data_dir, "seed", "dataless.BW_FURT"), seed_file) station_cache = StationCache(cache_file, seed_directory, resp_directory, stationxml_directory) # It should now again contain 4 channels. channels = station_cache.get_channels() assert len(channels) == 4 del station_cache # The tolerance in last modified time is 0.2. Set it much higher as # machines occasionaly hick up for some reason. time.sleep(1.5) # Now replace the file with an empty SEED file and assure that all # associated channels have been removed. shutil.copy(os.path.join(data_dir, "seed", "channelless_datalessSEED"), seed_file) # Init the station cache once more. station_cache = StationCache(cache_file, seed_directory, resp_directory, stationxml_directory) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 1 assert len(channels["IU.PAB.00.BHE"]) == 1 del station_cache # Now copy some RESP files. resp_file = os.path.join(resp_directory, "RESP.G.FDF.00.BHE") shutil.copy(os.path.join(data_dir, "resp", os.path.basename(resp_file)), resp_file) # Init the station cache once more. station_cache = StationCache(cache_file, seed_directory, resp_directory, stationxml_directory) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 2 assert "IU.PAB.00.BHE" in channels assert "G.FDF.00.BHE" in channels # Also get the stations once again. stations = station_cache.get_stations() assert len(stations) == 2 del station_cache # Add some more RESP files. shutil.copy(os.path.join(data_dir, "resp", "RESP.AF.DODT..BHE"), os.path.join(resp_directory, "RESP.AF.DODT..BHE")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHN"), os.path.join(resp_directory, "RESP.G.FDF.00.BHN")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHZ"), os.path.join(resp_directory, "RESP.G.FDF.00.BHZ")) # Init the station cache once more. station_cache = StationCache(cache_file, seed_directory, resp_directory, stationxml_directory) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 5 assert "IU.PAB.00.BHE" in channels assert "G.FDF.00.BHE" in channels assert "G.FDF.00.BHN" in channels assert "G.FDF.00.BHZ" in channels assert "AF.DODT..BHE" in channels # The duplicates in the one RESP files should not show up. assert len(channels["AF.DODT..BHE"]) == 1 # Also get the stations once again. stations = station_cache.get_stations() assert len(stations) == 3 # Check the get_values() method. all_values = station_cache.get_values() assert len(all_values) == 5 # Test the retrieval of only a single record by its filename. single_value = station_cache.get_details(all_values[0]["filename"])[0] assert single_value == all_values[0]
def test_station_cache(tmpdir): """ Single test case checking the basic workflow. """ # Most generic way to get the actual data directory. data_dir = os.path.join( os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))), "data", "station_files") # Create a temporary directory. directory = str(tmpdir) cache_file = os.path.join(directory, "cache.sqlite") seed_directory = os.path.join(directory, "SEED") resp_directory = os.path.join(directory, "RESP") stationxml_directory = os.path.join(directory, "StationXML") os.makedirs(seed_directory) os.makedirs(resp_directory) # Copy the SEED file. This files contains exactly one channel, # IU.PAB.00.BHE. shutil.copy(os.path.join(data_dir, "seed", "dataless.IU_PAB"), os.path.join(seed_directory, "dataless.IU_PAB")) # Init the station cache. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. Right now the folder only # contains one station. assert len(channels) == 1 assert len(channels["IU.PAB.00.BHE"]) == 1 del station_cache # This SEED file contains three more channels. seed_file = os.path.join(seed_directory, "dataless.BW_FURT") # Copy one more SEED file and check if the changes are reflected. shutil.copy(os.path.join(data_dir, "seed", "dataless.BW_FURT"), seed_file) # Init the station cache once more. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) # Get the list of available channels. It should not contain 4 channels. channels = station_cache.get_channels() assert len(channels) == 4 assert "BW.FURT..EHE" in channels assert "BW.FURT..EHN" in channels assert "BW.FURT..EHZ" in channels # Now attempt to only retrieve the stations. It should have 2 stations. # One from each SEED file. stations = station_cache.get_stations() assert len(stations) == 2 # Test the file_count, index_count, and total_size properties. assert station_cache.file_count == 2 assert station_cache.index_count == 4 assert station_cache.total_size == 12288 + 28672 del station_cache # Delete the file, and check if everything else is removed as well. It # should not only contain one channel. os.remove(seed_file) station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 1 assert len(channels["IU.PAB.00.BHE"]) == 1 # Add the file once again... del station_cache shutil.copy(os.path.join(data_dir, "seed", "dataless.BW_FURT"), seed_file) station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) # It should now again contain 4 channels. channels = station_cache.get_channels() assert len(channels) == 4 del station_cache # The tolerance in last modified time is 0.2. Set it much higher as # machines occasionaly hick up for some reason. time.sleep(1.5) # Now replace the file with an empty SEED file and assure that all # associated channels have been removed. shutil.copy(os.path.join(data_dir, "seed", "channelless_datalessSEED"), seed_file) # Init the station cache once more. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 1 assert len(channels["IU.PAB.00.BHE"]) == 1 del station_cache # Now copy some RESP files. resp_file = os.path.join(resp_directory, "RESP.G.FDF.00.BHE") shutil.copy(os.path.join(data_dir, "resp", os.path.basename(resp_file)), resp_file) # Init the station cache once more. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 2 assert "IU.PAB.00.BHE" in channels assert "G.FDF.00.BHE" in channels # Also get the stations once again. stations = station_cache.get_stations() assert len(stations) == 2 del station_cache # Add some more RESP files. shutil.copy(os.path.join(data_dir, "resp", "RESP.AF.DODT..BHE"), os.path.join(resp_directory, "RESP.AF.DODT..BHE")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHN"), os.path.join(resp_directory, "RESP.G.FDF.00.BHN")) shutil.copy(os.path.join(data_dir, "resp", "RESP.G.FDF.00.BHZ"), os.path.join(resp_directory, "RESP.G.FDF.00.BHZ")) # Init the station cache once more. station_cache = StationCache(cache_file, directory, seed_directory, resp_directory, stationxml_directory, read_only=False) # Get the list of available channels. channels = station_cache.get_channels() # Check that the correct station is in there. assert len(channels) == 5 assert "IU.PAB.00.BHE" in channels assert "G.FDF.00.BHE" in channels assert "G.FDF.00.BHN" in channels assert "G.FDF.00.BHZ" in channels assert "AF.DODT..BHE" in channels # The duplicates in the one RESP files should not show up. assert len(channels["AF.DODT..BHE"]) == 1 # Also get the stations once again. stations = station_cache.get_stations() assert len(stations) == 3 # Check the get_values() method. all_values = station_cache.get_values() assert len(all_values) == 5 single_value = station_cache.get_details(all_values[0]["filename"])[0] assert single_value == all_values[0]