def test_dangerously_convert_cache_to_gzip(self): html_string = "<html></html>".encode("utf-8") example_url = "http://www.example.com/testpage.html" cache = Cache(CONVERT_DIRECTORY) cache.save(example_url, html_string) self.assertTrue(cache.has(example_url)) dangerously_convert_cache_to_gzip(CONVERT_DIRECTORY) gcache = GzipCache(CONVERT_DIRECTORY) self.assertTrue(gcache.has(example_url)) for name in os.listdir(CONVERT_DIRECTORY): shutil.rmtree(os.path.join(CONVERT_DIRECTORY, name))
def test_cache_get(self): c = GzipCache(COMPRESS_CACHE_DIRECTORY) exists = [ "http://www.example.com/somepage.html", "http://www.example.com/otherpage" ] for filename in exists: self.assertIsNotNone(c.get(filename)) does_not_exist = [ "http://www.example.com/doesnotexist.html", "https://en.wikipedia.org/wiki/Python_(programming_language)" ] for filename in does_not_exist: self.assertIsNone(c.get(filename))
def test_cache_setup(self): c = GzipCache(COMPRESS_CACHE_DIRECTORY) self.assertEqual(c.folder, COMPRESS_CACHE_DIRECTORY) # verify that each folder has been loaded for name in os.listdir(os.path.join(COMPRESS_CACHE_DIRECTORY)): if os.path.isdir(os.path.join(COMPRESS_CACHE_DIRECTORY, name)): self.assertIn(name, c.sites)
def test_cache_save_existing(self): c = GzipCache(COMPRESS_CACHE_DIRECTORY) # verify that it adds a file to a pre-existing cached site html_string = "<html></html>".encode("utf-8") example_url = "http://www.example.com/testpage.html" d = dir_domain(example_url) f = clean_url_hash(example_url) full_save_name = os.path.join(COMPRESS_CACHE_DIRECTORY, d, f) self.assertNotIn(full_save_name, c.sites[d]) c.save(example_url, html_string) zip_name = "{}.gz".format(full_save_name) self.assertTrue(os.path.exists(zip_name)) self.assertIn(full_save_name, c.sites[d]) # cleanup os.remove(zip_name)
def test_cache_save_new(self): c = GzipCache(COMPRESS_CACHE_DIRECTORY) html_string = "<html></html>".encode("utf-8") sample_url = "http://www.sample.com/testpage.html" d = dir_domain(sample_url) f = clean_url_hash(sample_url) DIRECTORY = os.path.join(COMPRESS_CACHE_DIRECTORY, d) # the www_sample_com directory should not exist until the file is cached self.assertFalse(os.path.exists(DIRECTORY)) self.assertNotIn(d, c.sites) c.save(sample_url, html_string) full_save_name = os.path.join(DIRECTORY, f) zip_name = "{}.gz".format(full_save_name) self.assertIn(full_save_name, c.sites[d]) self.assertTrue(os.path.exists(zip_name)) # remove this after the test is done shutil.rmtree(DIRECTORY)