summaryrefslogtreecommitdiff
path: root/tests/staticfiles_tests/test_finders.py
diff options
context:
space:
mode:
authorMoritz Sichert <moritz.sichert@googlemail.com>2015-07-01 13:43:25 +0200
committerTim Graham <timograham@gmail.com>2015-07-01 09:41:27 -0400
commitb35b43dff81d46e930ffa1e05eb50968b8557102 (patch)
tree7b4bd08113c07e74a6834247a09fd775828a3639 /tests/staticfiles_tests/test_finders.py
parent3d7a713156c39ee197ee447130b3e6e078acbadc (diff)
Fixed #24982 -- Split staticfiles tests into multiple files
Diffstat (limited to 'tests/staticfiles_tests/test_finders.py')
-rw-r--r--tests/staticfiles_tests/test_finders.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/staticfiles_tests/test_finders.py b/tests/staticfiles_tests/test_finders.py
new file mode 100644
index 0000000000..a88770be48
--- /dev/null
+++ b/tests/staticfiles_tests/test_finders.py
@@ -0,0 +1,118 @@
+from __future__ import unicode_literals
+
+import os
+
+from django.conf import settings
+from django.contrib.staticfiles import finders, storage
+from django.core.exceptions import ImproperlyConfigured
+from django.test import SimpleTestCase, override_settings
+
+from .cases import StaticFilesTestCase
+from .settings import TEST_ROOT
+
+
+class FinderTestCase(object):
+ """
+ Base finder test mixin.
+
+ On Windows, sometimes the case of the path we ask the finders for and the
+ path(s) they find can differ. Compare them using os.path.normcase() to
+ avoid false negatives.
+ """
+ def test_find_first(self):
+ src, dst = self.find_first
+ found = self.finder.find(src)
+ self.assertEqual(os.path.normcase(found), os.path.normcase(dst))
+
+ def test_find_all(self):
+ src, dst = self.find_all
+ found = self.finder.find(src, all=True)
+ found = [os.path.normcase(f) for f in found]
+ dst = [os.path.normcase(d) for d in dst]
+ self.assertEqual(found, dst)
+
+
+class TestFileSystemFinder(StaticFilesTestCase, FinderTestCase):
+ """
+ Test FileSystemFinder.
+ """
+ def setUp(self):
+ super(TestFileSystemFinder, self).setUp()
+ self.finder = finders.FileSystemFinder()
+ test_file_path = os.path.join(TEST_ROOT, 'project', 'documents', 'test', 'file.txt')
+ self.find_first = (os.path.join('test', 'file.txt'), test_file_path)
+ self.find_all = (os.path.join('test', 'file.txt'), [test_file_path])
+
+
+class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase):
+ """
+ Test AppDirectoriesFinder.
+ """
+ def setUp(self):
+ super(TestAppDirectoriesFinder, self).setUp()
+ self.finder = finders.AppDirectoriesFinder()
+ test_file_path = os.path.join(TEST_ROOT, 'apps', 'test', 'static', 'test', 'file1.txt')
+ self.find_first = (os.path.join('test', 'file1.txt'), test_file_path)
+ self.find_all = (os.path.join('test', 'file1.txt'), [test_file_path])
+
+
+class TestDefaultStorageFinder(StaticFilesTestCase, FinderTestCase):
+ """
+ Test DefaultStorageFinder.
+ """
+ def setUp(self):
+ super(TestDefaultStorageFinder, self).setUp()
+ self.finder = finders.DefaultStorageFinder(
+ storage=storage.StaticFilesStorage(location=settings.MEDIA_ROOT))
+ test_file_path = os.path.join(settings.MEDIA_ROOT, 'media-file.txt')
+ self.find_first = ('media-file.txt', test_file_path)
+ self.find_all = ('media-file.txt', [test_file_path])
+
+
+@override_settings(
+ STATICFILES_FINDERS=['django.contrib.staticfiles.finders.FileSystemFinder'],
+ STATICFILES_DIRS=[os.path.join(TEST_ROOT, 'project', 'documents')],
+)
+class TestMiscFinder(SimpleTestCase):
+ """
+ A few misc finder tests.
+ """
+ def test_get_finder(self):
+ self.assertIsInstance(finders.get_finder(
+ 'django.contrib.staticfiles.finders.FileSystemFinder'),
+ finders.FileSystemFinder)
+
+ def test_get_finder_bad_classname(self):
+ with self.assertRaises(ImportError):
+ finders.get_finder('django.contrib.staticfiles.finders.FooBarFinder')
+
+ def test_get_finder_bad_module(self):
+ with self.assertRaises(ImportError):
+ 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)
+
+ def test_searched_locations(self):
+ finders.find('spam')
+ self.assertEqual(
+ finders.searched_locations,
+ [os.path.join(TEST_ROOT, 'project', 'documents')]
+ )
+
+ @override_settings(STATICFILES_DIRS='a string')
+ def test_non_tuple_raises_exception(self):
+ """
+ We can't determine if STATICFILES_DIRS is set correctly just by
+ looking at the type, but we can determine if it's definitely wrong.
+ """
+ self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
+
+ @override_settings(MEDIA_ROOT='')
+ def test_location_empty(self):
+ self.assertRaises(ImproperlyConfigured, finders.DefaultStorageFinder)