summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBouke Haarsma <bouke@webatoom.nl>2013-11-01 21:15:41 +0100
committerBaptiste Mispelon <bmispelon@gmail.com>2013-11-11 08:53:09 +0100
commit9b7455e918a437c3db91e88dcbf6d9c93fef96f8 (patch)
tree4cda7466ec20e5255633e7447eaa03ad5450bf24 /tests
parent6c5f5b9a414b8bdfafc45db5710acf200cca9885 (diff)
Fixed #21351 -- Replaced memoize with Python's lru_cache.
Replaced the custom, untested memoize with a similar decorator from Python's 3.2 stdlib. Although some minor performance degradation (see ticket), it is expected that in the long run lru_cache will outperform memoize once it is implemented in C. Thanks to EvilDMP for the report and Baptiste Mispelon for the idea of replacing memoize with lru_cache.
Diffstat (limited to 'tests')
-rw-r--r--tests/decorators/tests.py6
-rw-r--r--tests/deprecation/tests.py16
-rw-r--r--tests/staticfiles_tests/tests.py13
-rw-r--r--tests/urlpatterns_reverse/tests.py17
4 files changed, 45 insertions, 7 deletions
diff --git a/tests/decorators/tests.py b/tests/decorators/tests.py
index db00f36051..9c654e500a 100644
--- a/tests/decorators/tests.py
+++ b/tests/decorators/tests.py
@@ -1,5 +1,6 @@
from functools import wraps
from unittest import TestCase
+import warnings
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
@@ -58,11 +59,14 @@ full_decorator = compose(
staff_member_required,
# django.utils.functional
- lambda f: memoize(f, {}, 1),
allow_lazy,
lazy,
)
+# suppress the deprecation warning of memoize
+with warnings.catch_warnings(record=True):
+ fully_decorated = memoize(fully_decorated, {}, 1)
+
fully_decorated = full_decorator(fully_decorated)
diff --git a/tests/deprecation/tests.py b/tests/deprecation/tests.py
index 7d9ed16636..2a0922a0a8 100644
--- a/tests/deprecation/tests.py
+++ b/tests/deprecation/tests.py
@@ -4,6 +4,7 @@ import warnings
from django.test import SimpleTestCase, RequestFactory, override_settings
from django.utils import six, translation
from django.utils.deprecation import RenameMethodsBase
+from django.utils.functional import memoize
class RenameManagerMethods(RenameMethodsBase):
@@ -205,3 +206,18 @@ class DeprecatedChineseLanguageCodes(SimpleTestCase):
"The use of the language code 'zh-tw' is deprecated. "
"Please use the 'zh-hant' translation instead.",
])
+
+
+class DeprecatingMemoizeTest(SimpleTestCase):
+ def test_deprecated_memoize(self):
+ """
+ Ensure the correct warning is raised when memoize is used.
+ """
+ warnings.simplefilter('always')
+
+ with warnings.catch_warnings(record=True) as recorded:
+ memoize(lambda x: x, {}, 1)
+ msg = str(recorded.pop().message)
+ self.assertEqual(msg,
+ 'memoize wrapper is deprecated and will be removed in Django '
+ '1.9. Use django.utils.lru_cache instead.')
diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py
index 463ab47030..39ce49817c 100644
--- a/tests/staticfiles_tests/tests.py
+++ b/tests/staticfiles_tests/tests.py
@@ -55,7 +55,7 @@ class BaseStaticFilesTestCase(object):
storage.staticfiles_storage._wrapped = empty
# Clear the cached staticfile finders, so they are reinitialized every
# run and pick up changes in settings.STATICFILES_DIRS.
- finders._finders.clear()
+ finders.get_finder.cache_clear()
testfiles_path = os.path.join(TEST_ROOT, 'apps', 'test', 'static', 'test')
# To make sure SVN doesn't hangs itself with the non-ASCII characters
@@ -561,7 +561,7 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
Test that post_processing indicates the origin of the error when it
fails. Regression test for #18986.
"""
- finders._finders.clear()
+ finders.get_finder.cache_clear()
err = six.StringIO()
with self.assertRaises(Exception):
call_command('collectstatic', interactive=False, verbosity=0, stderr=err)
@@ -756,6 +756,15 @@ class TestMiscFinder(TestCase):
self.assertRaises(ImproperlyConfigured,
finders.get_finder, 'foo.bar.FooBarFinder')
+ def test_cache(self):
+ finders.get_finder.cache_clear()
+ for n in range(10):
+ finders.get_finder(
+ 'django.contrib.staticfiles.finders.FileSystemFinder')
+ cache_info = finders.get_finder.cache_info()
+ self.assertEqual(cache_info.hits, 9)
+ self.assertEqual(cache_info.currsize, 1)
+
@override_settings(STATICFILES_DIRS='a string')
def test_non_tuple_raises_exception(self):
"""
diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py
index b2a6d83443..b4056ac7fa 100644
--- a/tests/urlpatterns_reverse/tests.py
+++ b/tests/urlpatterns_reverse/tests.py
@@ -17,6 +17,7 @@ from django.test.utils import override_settings
from django.utils import six
from . import urlconf_outer, middleware, views
+from .views import empty_view
resolve_test_data = (
@@ -662,12 +663,20 @@ class ErroneousViewTests(TestCase):
class ViewLoadingTests(TestCase):
def test_view_loading(self):
+ self.assertEqual(get_callable('urlpatterns_reverse.views.empty_view'),
+ empty_view)
+
+ # passing a callable should return the callable
+ self.assertEqual(get_callable(empty_view), empty_view)
+
+ def test_exceptions(self):
# A missing view (identified by an AttributeError) should raise
# ViewDoesNotExist, ...
- six.assertRaisesRegex(self, ViewDoesNotExist, ".*View does not exist in.*",
- get_callable,
- 'urlpatterns_reverse.views.i_should_not_exist')
+ six.assertRaisesRegex(self, ViewDoesNotExist,
+ ".*View does not exist in.*",
+ get_callable,
+ 'urlpatterns_reverse.views.i_should_not_exist')
# ... but if the AttributeError is caused by something else don't
# swallow it.
self.assertRaises(AttributeError, get_callable,
- 'urlpatterns_reverse.views_broken.i_am_broken')
+ 'urlpatterns_reverse.views_broken.i_am_broken')