def test_no_cache(self): cache = img_info.InfoCache(root=self.SRC_IMAGE_CACHE, size=0) self.app.info_cache = cache request_uri = '/%s/%s' % (self.test_jp2_color_id,'info.json') resp = self.client.get(request_uri) assert len(self.app.info_cache) == 0
def test_looking_up_missing_item_is_keyerror(self): cache = img_info.InfoCache(root=tempfile.mkdtemp()) path = self.test_jp2_color_fp req = webapp_t._get_werkzeug_request(path=path) with pytest.raises(KeyError): cache[req]
def test_just_ram_cache_update(self): # Cache size of one, so it's easy to manipulate with tempfile.TemporaryDirectory() as cache_root: cache = img_info.InfoCache(root=cache_root, size=1) self.app.info_cache = cache expected_path_for_id = 'd7/b86/a99/44c/b2d/31d/80a/25e/38b/a3e/9f7' # First request request_uri = '/%s/%s' % (self.test_jp2_color_id, 'info.json') resp = self.client.get(request_uri) expected_path = path.join(self.app.info_cache.root, expected_path_for_id, unquote(self.test_jp2_color_id), 'info.json') fs_first_time = datetime.utcfromtimestamp( os.path.getmtime(expected_path)) # Push this entry out of the RAM cache with another push_request_uri = '/%s/%s' % (self.test_jp2_gray_id, 'info.json') resp = self.client.get(push_request_uri) # Request the first file again # It should now exist on disk, but not in RAM, so it shouldn't # have been rewritten by the second get. resp = self.client.get(request_uri) fs_second_time = datetime.utcfromtimestamp( os.path.getmtime(expected_path)) self.assertTrue(fs_first_time == fs_second_time)
def test_missing_src_file_causes_cache_miss(self): with tempfile.TemporaryDirectory() as tmp: cache = img_info.InfoCache(root=tmp) jpeg_fp = os.path.join(tmp, 'will_be_removed.jpeg') shutil.copyfile(self.test_jpeg_fp, jpeg_fp) info = img_info.ImageInfo(app=self.app, src_img_fp=jpeg_fp, src_format=self.test_jpeg_fmt) cache[self.test_jpeg_id] = info os.remove(jpeg_fp) with pytest.raises(KeyError): cache[self.test_jpeg_id]
def test_creates_cache_dir(self): with tempfile.TemporaryDirectory() as tmp: root = os.path.join(tmp, "doesnotexist") assert not os.path.exists(root) cache = img_info.InfoCache(root=root) info = img_info.ImageInfo(app=self.app, src_img_fp=self.test_jpeg_fp, src_format=self.test_jpeg_fmt) cache[self.test_jpeg_id] = info assert cache[self.test_jpeg_id][0] == info
def _cache_with_ident(self): """ Returns a tuple: an ``InfoCache`` with a single entry, and the key. """ cache = img_info.InfoCache(root=self.SRC_IMAGE_CACHE) info = img_info.ImageInfo(app=self.app, src_img_fp=self.test_jp2_color_fp, src_format=self.test_jp2_color_fmt) cache[self.test_jp2_color_id] = info return (cache, self.test_jp2_color_id)
def test_cache_limit(self): cache = img_info.InfoCache(root=self.SRC_IMAGE_CACHE, size=2) self.app.info_cache = cache request_uris = [ '/%s/%s' % (self.test_jp2_color_id,'info.json'), '/%s/%s' % (self.test_jpeg_id,'info.json'), '/%s/%s' % (self.test_png_id,'info.json'), '/%s/%s' % (self.test_jp2_gray_id,'info.json') ] for x in request_uris: resp = self.client.get(x) # Check we only cache two assert len(self.app.info_cache) == 2
def _cache_with_request(self): """ Returns a tuple: an ``InfoCache`` with a single entry, and the key. """ cache = img_info.InfoCache(root=self.SRC_IMAGE_CACHE) path = self.test_jp2_color_fp req = webapp_t._get_werkzeug_request(path=path) info = img_info.ImageInfo(self.app, self.test_jp2_color_uri, self.test_jp2_color_fp, self.test_jp2_color_fmt) cache[req] = info return (cache, req)
def test_creates_cache_dir(self): root = os.path.join(tempfile.mkdtemp(), "doesnotexist") assert not os.path.exists(root) cache = img_info.InfoCache(root=root) path = self.test_jpeg_fp req = webapp_t._get_werkzeug_request(path=path) info = img_info.ImageInfo(app=self.app, ident=self.test_jpeg_uri, src_img_fp=self.test_jpeg_fp, src_format=self.test_jpeg_fmt) cache[req] = info assert cache[req][0] == info
def test_can_delete_items_from_infocache(self): ''' Test for InfoCache.__delitem__. ''' cache = img_info.InfoCache(root=self.SRC_IMAGE_CACHE) path = self.test_jp2_color_fp req = webapp_t._get_werkzeug_request(path=path) # app = MockApp() info = img_info.ImageInfo(self.app, self.test_jp2_color_uri, self.test_jp2_color_fp, self.test_jp2_color_fmt) cache[req] = info del cache[req]
def test_just_ram_cache_update(self): # Cache size of one, so it's easy to manipulate cache = img_info.InfoCache(root=self.SRC_IMAGE_CACHE, size=1) self.app.info_cache = cache # First request request_uri = '/%s/%s' % (self.test_jp2_color_id, 'info.json') resp = self.client.get(request_uri) expected_path = path.join(self.app.info_cache.http_root, unquote(self.test_jp2_color_id), 'info.json') fs_first_time = datetime.utcfromtimestamp( os.path.getmtime(expected_path)) # Push this entry out of the RAM cache with another push_request_uri = '/%s/%s' % (self.test_jp2_gray_id, 'info.json') resp = self.client.get(push_request_uri) # Request the first file again # It should now exist on disk, but not in RAM, so it shouldn't # have been rewritten by the second get. resp = self.client.get(request_uri) fs_second_time = datetime.utcfromtimestamp( os.path.getmtime(expected_path)) self.assertTrue(fs_first_time == fs_second_time)
def test_empty_cache_has_zero_size(self): cache = img_info.InfoCache(root=self.SRC_IMAGE_CACHE) assert len(cache) == 0
def test_looking_up_missing_item_is_keyerror(self): with tempfile.TemporaryDirectory() as tmp: cache = img_info.InfoCache(root=tmp) with pytest.raises(KeyError): cache[self.test_jp2_color_id]