def test_cache(): cache_dir = tempfile.mkdtemp() ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert "GSOD" in ws.cache_filename assert ".json" in ws.cache_filename assert ws.tempC.shape == (0,) assert ws.tempC.shape == (0,) ws.add_year(2015) assert ws.tempC.shape == (365,) ws.add_year(2013) assert ws.tempC.shape == (365*3,) ws.save_to_cache() # new instance, loaded from full cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (365*3,) ws.clear_cache() # new instance, loaded from empty cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (0,) # cache still empty ws.load_from_cache() assert ws.tempC.shape == (0,)
def test_cache(): cache_dir = tempfile.mkdtemp() ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert "GSOD" in ws.cache_filename assert ".json" in ws.cache_filename assert ws.tempC.shape == (0,) assert ws.tempC.shape == (0,) ws.add_year(2015) assert ws.tempC.shape == (365,) ws.add_year(2013) assert ws.tempC.shape == (365*3,) ws.save_to_cache() # new instance, loaded from full cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (365*3,) # corrupt the cache with open(ws.cache_filename, 'w') as f: f.seek(1000) f.write("0#2]]]],,,sd,f,\\sf\\f\s34") # shouldn't fail - should just clear the cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (0,) # new instance, loaded from empty cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (0,) # cache still empty ws.load_from_cache() assert ws.tempC.shape == (0,) # write an all-null cache file with open(ws.cache_filename, 'w') as f: f.write('[["20110101", null]]') # new instance, loaded from empty cache ws = GSODWeatherSource('722880', cache_directory=cache_dir) assert ws.tempC.shape == (1,)