From a99d3f6525ff2ffd5e0b9177ed049736b7d1aded Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Wed, 21 Oct 2015 13:37:47 +0200 Subject: [PATCH 01/15] Add first 3 tests for browser.cache Note: test_cache_deactivated_private_browsing is currently failing --- tests/unit/browser/test_cache.py | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/unit/browser/test_cache.py diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py new file mode 100644 index 000000000..4f310c7b4 --- /dev/null +++ b/tests/unit/browser/test_cache.py @@ -0,0 +1,55 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 lamarpavel +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Tests for qutebrowser.browser.cache""" + +from unittest import mock + +from qutebrowser.browser import cache +from qutebrowser.utils import objreg + + +def test_cache_size_leq_max_cache_size(config_stub): + """Test cacheSize <= MaximumCacheSize when cache is activated.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } + disk_cache = cache.DiskCache("/foo/bar") + assert(1024 >= disk_cache.cacheSize()) + + +def test_cache_deactivated_private_browsing(config_stub): + """Test if cache is deactivated in private-browsing mode.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': True} + } + disk_cache = cache.DiskCache("/foo/bar") + assert(0 == disk_cache.cacheSize()) + + +def test_cache_deactivated_no_cachedir(config_stub): + """Test if cache is deactivated when there is no cache-dir.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } + disk_cache = cache.DiskCache("") + assert(0 == disk_cache.cacheSize()) From da800e3fa76ea8f7976692eb19acf5d45be5ce8f Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Wed, 21 Oct 2015 14:12:05 +0200 Subject: [PATCH 02/15] Don't setMaximumCacheSize if there is no cache --- qutebrowser/browser/cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/browser/cache.py b/qutebrowser/browser/cache.py index 6663f40b1..3d023d635 100644 --- a/qutebrowser/browser/cache.py +++ b/qutebrowser/browser/cache.py @@ -43,7 +43,6 @@ class DiskCache(QNetworkDiskCache): self._cache_dir = cache_dir self._http_cache_dir = os.path.join(cache_dir, 'http') self._maybe_activate() - self.setMaximumCacheSize(config.get('storage', 'cache-size')) objreg.get('config').changed.connect(self.on_config_changed) def __repr__(self): @@ -59,6 +58,7 @@ class DiskCache(QNetworkDiskCache): else: self._activated = True self.setCacheDirectory(self._http_cache_dir) + self.setMaximumCacheSize(config.get('storage', 'cache-size')) @pyqtSlot(str, str) def on_config_changed(self, section, option): From aa62a547d517df4ff68d77f711681099bcaf05a3 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Wed, 21 Oct 2015 14:20:01 +0200 Subject: [PATCH 03/15] Use tmpdir fixture instead of "/foo/bar" --- tests/unit/browser/test_cache.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 4f310c7b4..3049c3df2 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -25,23 +25,23 @@ from qutebrowser.browser import cache from qutebrowser.utils import objreg -def test_cache_size_leq_max_cache_size(config_stub): +def test_cache_size_leq_max_cache_size(config_stub, tmpdir): """Test cacheSize <= MaximumCacheSize when cache is activated.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': False} } - disk_cache = cache.DiskCache("/foo/bar") + disk_cache = cache.DiskCache(str(tmpdir)) assert(1024 >= disk_cache.cacheSize()) -def test_cache_deactivated_private_browsing(config_stub): +def test_cache_deactivated_private_browsing(config_stub, tmpdir): """Test if cache is deactivated in private-browsing mode.""" config_stub.data = { 'storage': {'cache-size': 1024}, 'general': {'private-browsing': True} } - disk_cache = cache.DiskCache("/foo/bar") + disk_cache = cache.DiskCache(str(tmpdir)) assert(0 == disk_cache.cacheSize()) From 0b78fb65c9b7ba14972412c3d81a9214a4dcf7af Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Wed, 21 Oct 2015 14:33:45 +0200 Subject: [PATCH 04/15] Test if cleared cache is actually empty --- tests/unit/browser/test_cache.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 3049c3df2..378ec491d 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -53,3 +53,14 @@ def test_cache_deactivated_no_cachedir(config_stub): } disk_cache = cache.DiskCache("") assert(0 == disk_cache.cacheSize()) + + +def test_clear_cache_activated(config_stub, tmpdir): + """Test if cache empty after clearing it.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } + disk_cache = cache.DiskCache(str(tmpdir)) + disk_cache.clear() + assert(0 == disk_cache.cacheSize()) From 82c608038d8bae92a540376601fc49480b5f98ff Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Wed, 21 Oct 2015 14:38:08 +0200 Subject: [PATCH 05/15] Coding style --- tests/unit/browser/test_cache.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 378ec491d..e7199bfc1 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -28,39 +28,39 @@ from qutebrowser.utils import objreg def test_cache_size_leq_max_cache_size(config_stub, tmpdir): """Test cacheSize <= MaximumCacheSize when cache is activated.""" config_stub.data = { - 'storage': {'cache-size': 1024}, - 'general': {'private-browsing': False} - } + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } disk_cache = cache.DiskCache(str(tmpdir)) - assert(1024 >= disk_cache.cacheSize()) + assert(disk_cache.cacheSize() <= 1024) def test_cache_deactivated_private_browsing(config_stub, tmpdir): """Test if cache is deactivated in private-browsing mode.""" config_stub.data = { - 'storage': {'cache-size': 1024}, - 'general': {'private-browsing': True} - } + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': True} + } disk_cache = cache.DiskCache(str(tmpdir)) - assert(0 == disk_cache.cacheSize()) + assert(disk_cache.cacheSize() == 0) def test_cache_deactivated_no_cachedir(config_stub): """Test if cache is deactivated when there is no cache-dir.""" config_stub.data = { - 'storage': {'cache-size': 1024}, - 'general': {'private-browsing': False} - } + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } disk_cache = cache.DiskCache("") - assert(0 == disk_cache.cacheSize()) + assert(disk_cache.cacheSize() == 0) def test_clear_cache_activated(config_stub, tmpdir): - """Test if cache empty after clearing it.""" + """Test if cache is empty after clearing it.""" config_stub.data = { - 'storage': {'cache-size': 1024}, - 'general': {'private-browsing': False} - } + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } disk_cache = cache.DiskCache(str(tmpdir)) disk_cache.clear() assert(0 == disk_cache.cacheSize()) From 4a8dec5eb8d90e6a5a3e434169098e4da8cbbb90 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Wed, 21 Oct 2015 21:38:40 +0200 Subject: [PATCH 06/15] Code style; complete first batch of tests. --- tests/unit/browser/test_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index e7199bfc1..8fe69734a 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -63,4 +63,4 @@ def test_clear_cache_activated(config_stub, tmpdir): } disk_cache = cache.DiskCache(str(tmpdir)) disk_cache.clear() - assert(0 == disk_cache.cacheSize()) + assert(disk_cache.cacheSize() == 0) From 24887a6564463ac901834d94081f8fbb8e1a7af2 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Fri, 23 Oct 2015 01:49:00 +0200 Subject: [PATCH 07/15] Adjust coding style based on pylint warnings --- tests/unit/browser/test_cache.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 8fe69734a..2f17889a3 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -19,10 +19,7 @@ """Tests for qutebrowser.browser.cache""" -from unittest import mock - from qutebrowser.browser import cache -from qutebrowser.utils import objreg def test_cache_size_leq_max_cache_size(config_stub, tmpdir): @@ -32,7 +29,7 @@ def test_cache_size_leq_max_cache_size(config_stub, tmpdir): 'general': {'private-browsing': False} } disk_cache = cache.DiskCache(str(tmpdir)) - assert(disk_cache.cacheSize() <= 1024) + assert disk_cache.cacheSize() <= 1024 def test_cache_deactivated_private_browsing(config_stub, tmpdir): @@ -42,7 +39,7 @@ def test_cache_deactivated_private_browsing(config_stub, tmpdir): 'general': {'private-browsing': True} } disk_cache = cache.DiskCache(str(tmpdir)) - assert(disk_cache.cacheSize() == 0) + assert disk_cache.cacheSize() == 0 def test_cache_deactivated_no_cachedir(config_stub): @@ -52,7 +49,7 @@ def test_cache_deactivated_no_cachedir(config_stub): 'general': {'private-browsing': False} } disk_cache = cache.DiskCache("") - assert(disk_cache.cacheSize() == 0) + assert disk_cache.cacheSize() == 0 def test_clear_cache_activated(config_stub, tmpdir): @@ -63,4 +60,4 @@ def test_clear_cache_activated(config_stub, tmpdir): } disk_cache = cache.DiskCache(str(tmpdir)) disk_cache.clear() - assert(disk_cache.cacheSize() == 0) + assert disk_cache.cacheSize() == 0 From e4c79a68d1491aea5028d9b94d93d6bcb4e862a2 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Thu, 29 Oct 2015 02:28:57 +0100 Subject: [PATCH 08/15] re-write tests to fill the cache Previously the tests were not adding any data to the cache, making the tests for a cache size other than 0 meaningless. The new tests create cache entries and fill them with some data that can be tested against. --- tests/unit/browser/test_cache.py | 58 +++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 2f17889a3..83a8fc035 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -19,17 +19,52 @@ """Tests for qutebrowser.browser.cache""" +from PyQt5.QtCore import QUrl +from PyQt5.QtNetwork import QNetworkDiskCache, QNetworkCacheMetaData + from qutebrowser.browser import cache +def preload_cache(cache, url='http://www.example.com/', content=b'foobar'): + metadata = QNetworkCacheMetaData() + metadata.setUrl(QUrl(url)) + assert metadata.isValid() + device = cache.prepare(metadata) + assert device is not None + device.write(content) + cache.insert(device) + + +def test_cache_insert_data(tmpdir): + """Test if entries inserted into the cache are actually there.""" + URL = 'http://qutebrowser.org' + CONTENT = b'foobar' + cache = QNetworkDiskCache() + cache.setCacheDirectory(str(tmpdir)) + assert cache.cacheSize() == 0 + + preload_cache(cache, URL, CONTENT) + + assert cache.cacheSize() != 0 + assert cache.data(QUrl(URL)).readAll() == CONTENT + + def test_cache_size_leq_max_cache_size(config_stub, tmpdir): """Test cacheSize <= MaximumCacheSize when cache is activated.""" + LIMIT = 100 config_stub.data = { - 'storage': {'cache-size': 1024}, + 'storage': {'cache-size': LIMIT}, 'general': {'private-browsing': False} } disk_cache = cache.DiskCache(str(tmpdir)) - assert disk_cache.cacheSize() <= 1024 + assert disk_cache.maximumCacheSize() == LIMIT + + preload_cache(disk_cache, 'http://www.example/com/') + preload_cache(disk_cache, 'http://qutebrowser.org') + preload_cache(disk_cache, 'http://foo.xxx') + preload_cache(disk_cache, 'http://bar.net') + assert disk_cache.expire() < LIMIT + assert disk_cache.cacheSize() <= LIMIT def test_cache_deactivated_private_browsing(config_stub, tmpdir): @@ -39,17 +74,11 @@ def test_cache_deactivated_private_browsing(config_stub, tmpdir): 'general': {'private-browsing': True} } disk_cache = cache.DiskCache(str(tmpdir)) - assert disk_cache.cacheSize() == 0 - -def test_cache_deactivated_no_cachedir(config_stub): - """Test if cache is deactivated when there is no cache-dir.""" - config_stub.data = { - 'storage': {'cache-size': 1024}, - 'general': {'private-browsing': False} - } - disk_cache = cache.DiskCache("") - assert disk_cache.cacheSize() == 0 + metadata = QNetworkCacheMetaData() + metadata.setUrl(QUrl('http://www.example.com/')) + assert metadata.isValid() + assert disk_cache.prepare(metadata) is None def test_clear_cache_activated(config_stub, tmpdir): @@ -59,5 +88,10 @@ def test_clear_cache_activated(config_stub, tmpdir): 'general': {'private-browsing': False} } disk_cache = cache.DiskCache(str(tmpdir)) + assert disk_cache.cacheSize() == 0 + + preload_cache(disk_cache) + assert disk_cache.cacheSize() != 0 + disk_cache.clear() assert disk_cache.cacheSize() == 0 From 2cab750a54a32bfdc7632a4bcb84f0d9f3cd1880 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Thu, 29 Oct 2015 02:37:32 +0100 Subject: [PATCH 09/15] Add test for DiskCache.remove() --- tests/unit/browser/test_cache.py | 40 +++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 83a8fc035..8090f48e3 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -35,20 +35,6 @@ def preload_cache(cache, url='http://www.example.com/', content=b'foobar'): cache.insert(device) -def test_cache_insert_data(tmpdir): - """Test if entries inserted into the cache are actually there.""" - URL = 'http://qutebrowser.org' - CONTENT = b'foobar' - cache = QNetworkDiskCache() - cache.setCacheDirectory(str(tmpdir)) - assert cache.cacheSize() == 0 - - preload_cache(cache, URL, CONTENT) - - assert cache.cacheSize() != 0 - assert cache.data(QUrl(URL)).readAll() == CONTENT - - def test_cache_size_leq_max_cache_size(config_stub, tmpdir): """Test cacheSize <= MaximumCacheSize when cache is activated.""" LIMIT = 100 @@ -81,6 +67,32 @@ def test_cache_deactivated_private_browsing(config_stub, tmpdir): assert disk_cache.prepare(metadata) is None +def test_cache_insert_data(tmpdir): + """Test if entries inserted into the cache are actually there.""" + URL = 'http://qutebrowser.org' + CONTENT = b'foobar' + disk_cache = QNetworkDiskCache() + disk_cache.setCacheDirectory(str(tmpdir)) + assert disk_cache.cacheSize() == 0 + + preload_cache(disk_cache, URL, CONTENT) + + assert disk_cache.cacheSize() != 0 + assert disk_cache.data(QUrl(URL)).readAll() == CONTENT + + +def test_cache_remove_data(tmpdir): + """Test if a previously inserted entry can be removed from the cache.""" + URL = 'http://qutebrowser.org' + disk_cache = QNetworkDiskCache() + disk_cache.setCacheDirectory(str(tmpdir)) + preload_cache(disk_cache, URL) + assert disk_cache.cacheSize() > 0 + + assert disk_cache.remove(QUrl(URL)) + assert disk_cache.cacheSize() == 0 + + def test_clear_cache_activated(config_stub, tmpdir): """Test if cache is empty after clearing it.""" config_stub.data = { From a6526a1be2f74f55cbc26842f398613637ecc03a Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Thu, 29 Oct 2015 02:56:40 +0100 Subject: [PATCH 10/15] Add tests to update and verify meta data of cache entries --- tests/unit/browser/test_cache.py | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 8090f48e3..056eef32f 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -45,7 +45,7 @@ def test_cache_size_leq_max_cache_size(config_stub, tmpdir): disk_cache = cache.DiskCache(str(tmpdir)) assert disk_cache.maximumCacheSize() == LIMIT - preload_cache(disk_cache, 'http://www.example/com/') + preload_cache(disk_cache, 'http://www.example.com/') preload_cache(disk_cache, 'http://qutebrowser.org') preload_cache(disk_cache, 'http://foo.xxx') preload_cache(disk_cache, 'http://bar.net') @@ -93,7 +93,7 @@ def test_cache_remove_data(tmpdir): assert disk_cache.cacheSize() == 0 -def test_clear_cache_activated(config_stub, tmpdir): +def test_cache_clear_activated(config_stub, tmpdir): """Test if cache is empty after clearing it.""" config_stub.data = { 'storage': {'cache-size': 1024}, @@ -107,3 +107,33 @@ def test_clear_cache_activated(config_stub, tmpdir): disk_cache.clear() assert disk_cache.cacheSize() == 0 + + +def test_cache_metadata(tmpdir): + """Ensure that DiskCache.metaData() returns exactly what was inserted.""" + URL = 'http://qutebrowser.org' + metadata = QNetworkCacheMetaData() + metadata.setUrl(QUrl(URL)) + assert metadata.isValid() + disk_cache = QNetworkDiskCache() + disk_cache.setCacheDirectory(str(tmpdir)) + device = disk_cache.prepare(metadata) + device.write(b'foobar') + disk_cache.insert(device) + + assert disk_cache.metaData(QUrl(URL)) == metadata + + +def test_cache_update_metadata(tmpdir): + """Test updating the meta data for an existing cache entry.""" + URL = 'http://qutebrowser.org' + disk_cache = QNetworkDiskCache() + disk_cache.setCacheDirectory(str(tmpdir)) + preload_cache(disk_cache, URL, b'foo') + assert disk_cache.cacheSize() > 0 + + metadata = QNetworkCacheMetaData() + metadata.setUrl(QUrl(URL)) + assert metadata.isValid() + disk_cache.updateMetaData(metadata) + assert disk_cache.metaData(QUrl(URL)) == metadata From 9a8032fa91e6ebf5ba752a6bdd3acae9b0bb8470 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Thu, 29 Oct 2015 03:13:25 +0100 Subject: [PATCH 11/15] Fix pylint errors (upper case variable names) --- tests/unit/browser/test_cache.py | 38 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 056eef32f..45916136a 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -37,20 +37,20 @@ def preload_cache(cache, url='http://www.example.com/', content=b'foobar'): def test_cache_size_leq_max_cache_size(config_stub, tmpdir): """Test cacheSize <= MaximumCacheSize when cache is activated.""" - LIMIT = 100 + limit = 100 config_stub.data = { - 'storage': {'cache-size': LIMIT}, + 'storage': {'cache-size': limit}, 'general': {'private-browsing': False} } disk_cache = cache.DiskCache(str(tmpdir)) - assert disk_cache.maximumCacheSize() == LIMIT + assert disk_cache.maximumCacheSize() == limit preload_cache(disk_cache, 'http://www.example.com/') preload_cache(disk_cache, 'http://qutebrowser.org') preload_cache(disk_cache, 'http://foo.xxx') preload_cache(disk_cache, 'http://bar.net') - assert disk_cache.expire() < LIMIT - assert disk_cache.cacheSize() <= LIMIT + assert disk_cache.expire() < limit + assert disk_cache.cacheSize() <= limit def test_cache_deactivated_private_browsing(config_stub, tmpdir): @@ -69,27 +69,27 @@ def test_cache_deactivated_private_browsing(config_stub, tmpdir): def test_cache_insert_data(tmpdir): """Test if entries inserted into the cache are actually there.""" - URL = 'http://qutebrowser.org' - CONTENT = b'foobar' + url = 'http://qutebrowser.org' + content = b'foobar' disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) assert disk_cache.cacheSize() == 0 - preload_cache(disk_cache, URL, CONTENT) + preload_cache(disk_cache, url, content) assert disk_cache.cacheSize() != 0 - assert disk_cache.data(QUrl(URL)).readAll() == CONTENT + assert disk_cache.data(QUrl(url)).readAll() == content def test_cache_remove_data(tmpdir): """Test if a previously inserted entry can be removed from the cache.""" - URL = 'http://qutebrowser.org' + url = 'http://qutebrowser.org' disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) - preload_cache(disk_cache, URL) + preload_cache(disk_cache, url) assert disk_cache.cacheSize() > 0 - assert disk_cache.remove(QUrl(URL)) + assert disk_cache.remove(QUrl(url)) assert disk_cache.cacheSize() == 0 @@ -111,9 +111,9 @@ def test_cache_clear_activated(config_stub, tmpdir): def test_cache_metadata(tmpdir): """Ensure that DiskCache.metaData() returns exactly what was inserted.""" - URL = 'http://qutebrowser.org' + url = 'http://qutebrowser.org' metadata = QNetworkCacheMetaData() - metadata.setUrl(QUrl(URL)) + metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) @@ -121,19 +121,19 @@ def test_cache_metadata(tmpdir): device.write(b'foobar') disk_cache.insert(device) - assert disk_cache.metaData(QUrl(URL)) == metadata + assert disk_cache.metaData(QUrl(url)) == metadata def test_cache_update_metadata(tmpdir): """Test updating the meta data for an existing cache entry.""" - URL = 'http://qutebrowser.org' + url = 'http://qutebrowser.org' disk_cache = QNetworkDiskCache() disk_cache.setCacheDirectory(str(tmpdir)) - preload_cache(disk_cache, URL, b'foo') + preload_cache(disk_cache, url, b'foo') assert disk_cache.cacheSize() > 0 metadata = QNetworkCacheMetaData() - metadata.setUrl(QUrl(URL)) + metadata.setUrl(QUrl(url)) assert metadata.isValid() disk_cache.updateMetaData(metadata) - assert disk_cache.metaData(QUrl(URL)) == metadata + assert disk_cache.metaData(QUrl(url)) == metadata From 05994ad90e3df95b89a24ca6b0b4a2883f1c17eb Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Fri, 30 Oct 2015 01:22:16 +0100 Subject: [PATCH 12/15] Add theshold to cache-size test The strict test condition before was violated by Qt internals which seem to violate documented guarantees but are not part of qutebrowsers code and thus can only be tested to a certain extent. --- tests/unit/browser/test_cache.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 45916136a..8ee12e798 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -50,7 +50,8 @@ def test_cache_size_leq_max_cache_size(config_stub, tmpdir): preload_cache(disk_cache, 'http://foo.xxx') preload_cache(disk_cache, 'http://bar.net') assert disk_cache.expire() < limit - assert disk_cache.cacheSize() <= limit + # Add a threshold to the limit due to unforseeable Qt internals + assert disk_cache.cacheSize() < limit+100 def test_cache_deactivated_private_browsing(config_stub, tmpdir): From b3fa19eb96aa8840dbe2d8ca1794d90edaf643d6 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Fri, 30 Oct 2015 02:03:34 +0100 Subject: [PATCH 13/15] Add sanity test involving almost everything --- tests/unit/browser/test_cache.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index 8ee12e798..f391a0361 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -19,7 +19,7 @@ """Tests for qutebrowser.browser.cache""" -from PyQt5.QtCore import QUrl +from PyQt5.QtCore import QUrl, QDateTime from PyQt5.QtNetwork import QNetworkDiskCache, QNetworkCacheMetaData from qutebrowser.browser import cache @@ -138,3 +138,32 @@ def test_cache_update_metadata(tmpdir): assert metadata.isValid() disk_cache.updateMetaData(metadata) assert disk_cache.metaData(QUrl(url)) == metadata + + +def test_cache_full(config_stub, tmpdir): + """Do a sanity test involving everything.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } + disk_cache = QNetworkDiskCache() + disk_cache.setCacheDirectory(str(tmpdir)) + + url = 'http://qutebrowser.org' + content = b'cutebowser' + preload_cache(disk_cache, url, content) + url2 = 'https://qutebrowser.org' + content2 = b'ohmycert' + preload_cache(disk_cache, url2, content2) + + metadata = QNetworkCacheMetaData() + metadata.setUrl(QUrl(url)) + soon = QDateTime.currentDateTime().addMonths(4) + assert soon.isValid() + metadata.setLastModified(soon) + assert metadata.isValid() + disk_cache.updateMetaData(metadata) + disk_cache.remove(QUrl(url2)) + + assert disk_cache.metaData(QUrl(url)).lastModified() == soon + assert disk_cache.data(QUrl(url)).readAll() == content From b5af1c87306863b153efcd363319636f2c2d7955 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Fri, 30 Oct 2015 02:41:17 +0100 Subject: [PATCH 14/15] Add three tests querying and removing data Two of these are testing return values of a deactivated cache and the third is trying to query data that was never inserted into an active cache. --- tests/unit/browser/test_cache.py | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index f391a0361..e3553bcfc 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -68,6 +68,42 @@ def test_cache_deactivated_private_browsing(config_stub, tmpdir): assert disk_cache.prepare(metadata) is None +def test_cache_deactivated_get_data(config_stub, tmpdir): + """Query some data from a deactivated cache.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': True} + } + disk_cache = cache.DiskCache(str(tmpdir)) + + url = QUrl('http://www.example.com/') + assert disk_cache.data(url) is None + + +def test_cache_get_nonexistant_data(config_stub, tmpdir): + """Test querying some data that was never inserted.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': False} + } + 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_deactivated_remove_data(config_stub, tmpdir): + """Test removing some data from a deactivated cache.""" + config_stub.data = { + 'storage': {'cache-size': 1024}, + 'general': {'private-browsing': True} + } + disk_cache = cache.DiskCache(str(tmpdir)) + + url = QUrl('http://www.example.com/') + assert disk_cache.remove(url) == False + + def test_cache_insert_data(tmpdir): """Test if entries inserted into the cache are actually there.""" url = 'http://qutebrowser.org' From 27ec9e1c4330d14ff684af546731c7d2c8071c63 Mon Sep 17 00:00:00 2001 From: Lamar Pavel Date: Fri, 30 Oct 2015 02:46:43 +0100 Subject: [PATCH 15/15] Fix typo --- tests/unit/browser/test_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/browser/test_cache.py b/tests/unit/browser/test_cache.py index e3553bcfc..831b232eb 100644 --- a/tests/unit/browser/test_cache.py +++ b/tests/unit/browser/test_cache.py @@ -80,7 +80,7 @@ def test_cache_deactivated_get_data(config_stub, tmpdir): assert disk_cache.data(url) is None -def test_cache_get_nonexistant_data(config_stub, tmpdir): +def test_cache_get_nonexistent_data(config_stub, tmpdir): """Test querying some data that was never inserted.""" config_stub.data = { 'storage': {'cache-size': 1024},