diff --git a/tests/unit/browser/webkit/test_cache.py b/tests/unit/browser/webkit/test_cache.py index c273b7a71..d2ab3da61 100644 --- a/tests/unit/browser/webkit/test_cache.py +++ b/tests/unit/browser/webkit/test_cache.py @@ -30,6 +30,11 @@ pytestmark = pytest.mark.skipif( reason="QNetworkDiskCache is broken on Qt 5.7.1 and 5.8") +@pytest.fixture +def disk_cache(tmpdir, config_stub): + return cache.DiskCache(str(tmpdir)) + + def preload_cache(cache, url='http://www.example.com/', content=b'foobar'): metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) @@ -69,7 +74,7 @@ def test_cache_size_leq_max_cache_size(config_stub, tmpdir): assert disk_cache.cacheSize() < limit + 100 -def test_cache_existing_metadata_file(config_stub, tmpdir): +def test_cache_existing_metadata_file(tmpdir, disk_cache): """Test querying existing meta data file from activated cache.""" url = 'http://qutebrowser.org' content = b'foobar' @@ -78,7 +83,6 @@ def test_cache_existing_metadata_file(config_stub, tmpdir): metadata.setUrl(QUrl(url)) assert metadata.isValid() - disk_cache = cache.DiskCache(str(tmpdir)) device = disk_cache.prepare(metadata) assert device is not None device.write(content) @@ -90,26 +94,23 @@ def test_cache_existing_metadata_file(config_stub, tmpdir): assert disk_cache.fileMetaData(str(files[0])) == metadata -def test_cache_nonexistent_metadata_file(config_stub, tmpdir): +def test_cache_nonexistent_metadata_file(disk_cache): """Test querying nonexistent meta data file from activated cache.""" - disk_cache = cache.DiskCache(str(tmpdir)) cache_file = disk_cache.fileMetaData("nosuchfile") assert not cache_file.isValid() -def test_cache_get_nonexistent_data(config_stub, tmpdir): +def test_cache_get_nonexistent_data(disk_cache): """Test querying some data that was never inserted.""" - disk_cache = cache.DiskCache(str(tmpdir)) preload_cache(disk_cache, 'https://qutebrowser.org') assert disk_cache.data(QUrl('http://qutebrowser.org')) is None -def test_cache_insert_data(config_stub, tmpdir): +def test_cache_insert_data(disk_cache): """Test if entries inserted into the cache are actually there.""" url = 'http://qutebrowser.org' content = b'foobar' - disk_cache = cache.DiskCache(str(tmpdir)) assert disk_cache.cacheSize() == 0 preload_cache(disk_cache, url, content) @@ -118,10 +119,9 @@ def test_cache_insert_data(config_stub, tmpdir): assert disk_cache.data(QUrl(url)).readAll() == content -def test_cache_remove_data(config_stub, tmpdir): +def test_cache_remove_data(disk_cache): """Test if a previously inserted entry can be removed from the cache.""" url = 'http://qutebrowser.org' - disk_cache = cache.DiskCache(str(tmpdir)) preload_cache(disk_cache, url) assert disk_cache.cacheSize() > 0 @@ -129,9 +129,8 @@ def test_cache_remove_data(config_stub, tmpdir): assert disk_cache.cacheSize() == 0 -def test_cache_clear_activated(config_stub, tmpdir): +def test_cache_clear_activated(disk_cache): """Test if cache is empty after clearing it.""" - disk_cache = cache.DiskCache(str(tmpdir)) assert disk_cache.cacheSize() == 0 preload_cache(disk_cache) @@ -141,13 +140,12 @@ def test_cache_clear_activated(config_stub, tmpdir): assert disk_cache.cacheSize() == 0 -def test_cache_metadata(config_stub, tmpdir): +def test_cache_metadata(disk_cache): """Ensure that DiskCache.metaData() returns exactly what was inserted.""" url = 'http://qutebrowser.org' metadata = QNetworkCacheMetaData() metadata.setUrl(QUrl(url)) assert metadata.isValid() - disk_cache = cache.DiskCache(str(tmpdir)) device = disk_cache.prepare(metadata) device.write(b'foobar') disk_cache.insert(device) @@ -155,10 +153,9 @@ def test_cache_metadata(config_stub, tmpdir): assert disk_cache.metaData(QUrl(url)) == metadata -def test_cache_update_metadata(config_stub, tmpdir): +def test_cache_update_metadata(disk_cache): """Test updating the meta data for an existing cache entry.""" url = 'http://qutebrowser.org' - disk_cache = cache.DiskCache(str(tmpdir)) preload_cache(disk_cache, url, b'foo') assert disk_cache.cacheSize() > 0 @@ -169,7 +166,7 @@ def test_cache_update_metadata(config_stub, tmpdir): assert disk_cache.metaData(QUrl(url)) == metadata -def test_cache_full(config_stub, tmpdir): +def test_cache_full(tmpdir): """Do a sanity test involving everything.""" disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir))