From 75b51ce214d49b68babdf12e2297a38dee1f31ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Leonhardt?= Date: Tue, 14 Jul 2015 12:03:52 +0200 Subject: [PATCH 1/2] Avoid calling get_path() twice inside get_file Overridden PathDownloadView.get_path() may contain database lookups and logging which should not be called twice if not necessary, as it was in my case. Because the acquired filename does not change inside get_file(), I replaced the duplicate call. --- django_downloadview/views/path.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_downloadview/views/path.py b/django_downloadview/views/path.py index 2a5d09d..2779ddf 100644 --- a/django_downloadview/views/path.py +++ b/django_downloadview/views/path.py @@ -36,4 +36,4 @@ class PathDownloadView(BaseDownloadView): filename = self.get_path() if not os.path.isfile(filename): raise FileNotFound('File "{0}" does not exists'.format(filename)) - return File(open(self.get_path(), 'rb')) + return File(open(filename, 'rb')) From 5e4ae2fdc9f5cbec59d17ffcea877d08ae3c7dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Leonhardt?= Date: Tue, 14 Jul 2015 14:53:14 +0200 Subject: [PATCH 2/2] Changed assertCalledOnceWith() to assert_called_once_with() When I change assertCalledOnceWith() to assert_called_once_with(), make test succeeds but shows another failure: ``` AssertionError: Expected call: mock(sentinel.size, sentinel.modified_time) Actual call: mock(sentinel.since, sentinel.modified_time, sentinel.size) ``` After changing the call accordingly, all 59 tests succeed. --- tests/views.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/views.py b/tests/views.py index c6c9517..bfa9aa7 100644 --- a/tests/views.py +++ b/tests/views.py @@ -75,7 +75,7 @@ class DownloadMixinTestCase(unittest.TestCase): self.assertIs( mixin.was_modified_since(file_wrapper, mock.sentinel.since), mock.sentinel.was_modified) - file_wrapper.was_modified_since.assertCalledOnceWith( + file_wrapper.was_modified_since.assert_called_once_with( mock.sentinel.since) def test_was_modified_since_django(self): @@ -101,9 +101,10 @@ class DownloadMixinTestCase(unittest.TestCase): self.assertIs( mixin.was_modified_since(file_wrapper, mock.sentinel.since), mock.sentinel.was_modified) - was_modified_since_mock.assertCalledOnceWith( - mock.sentinel.size, - mock.sentinel.modified_time) + was_modified_since_mock.assert_called_once_with( + mock.sentinel.since, + mock.sentinel.modified_time, + mock.sentinel.size) def test_was_modified_since_fallback(self): """DownloadMixin.was_modified_since() fallbacks to `True`.