From 88531cccf3ec669fb657ff8d6cddcf85ce57d517 Mon Sep 17 00:00:00 2001 From: Jared Smith Date: Wed, 19 Oct 2016 14:07:17 -0700 Subject: [PATCH] @amercader is a hero, test passing with fakefs Fixing pep8 --- ckan/tests/logic/action/test_create.py | 42 +++++++++++++++++++++++--- ckan/tests/logic/action/test_update.py | 41 ++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/ckan/tests/logic/action/test_create.py b/ckan/tests/logic/action/test_create.py index 9a1b0eb479d..7ad4bcec9e5 100644 --- a/ckan/tests/logic/action/test_create.py +++ b/ckan/tests/logic/action/test_create.py @@ -3,7 +3,7 @@ '''Unit tests for ckan/logic/auth/create.py. ''' - +import ckan from ckan.common import config import mock import nose.tools @@ -17,6 +17,22 @@ assert_equals = nose.tools.assert_equals assert_raises = nose.tools.assert_raises +# Mock file uploads +import __builtin__ as builtins +from pyfakefs import fake_filesystem + +real_open = open +fs = fake_filesystem.FakeFilesystem() +fake_os = fake_filesystem.FakeOsModule(fs) +fake_open = fake_filesystem.FakeFileOpen(fs) + + +def mock_open_if_open_fails(*args, **kwargs): + try: + return real_open(*args, **kwargs) + except (OSError, IOError): + return fake_open(*args, **kwargs) + class TestUserInvite(object): @@ -450,7 +466,11 @@ def test_doesnt_require_url(self): assert not stored_resource['url'] - def test_mimetype_by_url(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_mimetype_by_url(self, mock_open): """ The mimetype is guessed from the url @@ -489,7 +509,11 @@ def test_mimetype_by_user(self): mimetype = result.pop('mimetype') assert_equals(mimetype, 'application/csv') - def test_mimetype_by_upload_by_filename(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_mimetype_by_upload_by_filename(self, mock_open): """ The mimetype is guessed from an uploaded file with a filename @@ -532,7 +556,11 @@ def test_mimetype_by_upload_by_filename(self): assert_equals(mimetype, 'application/json') @helpers.change_config('ckan.mimetype_guess', 'file_contents') - def test_mimetype_by_upload_by_file(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_mimetype_by_upload_by_file(self, mock_open): """ The mimetype is guessed from an uploaded file by the contents inside @@ -563,7 +591,11 @@ def test_mimetype_by_upload_by_file(self): assert mimetype assert_equals(mimetype, 'text/plain') - def test_size_of_resource_by_upload(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_size_of_resource_by_upload(self, mock_open): """ The size of the resource determined by the uploaded file """ diff --git a/ckan/tests/logic/action/test_update.py b/ckan/tests/logic/action/test_update.py index 892cb09e889..1a4a95366ed 100644 --- a/ckan/tests/logic/action/test_update.py +++ b/ckan/tests/logic/action/test_update.py @@ -5,6 +5,7 @@ import nose.tools import mock +import ckan from ckan.common import config import ckan.logic as logic @@ -17,6 +18,22 @@ assert_equals = eq_ = nose.tools.assert_equals assert_raises = nose.tools.assert_raises +# Mock file uploads +import __builtin__ as builtins +from pyfakefs import fake_filesystem + +real_open = open +fs = fake_filesystem.FakeFilesystem() +fake_os = fake_filesystem.FakeOsModule(fs) +fake_open = fake_filesystem.FakeFileOpen(fs) + + +def mock_open_if_open_fails(*args, **kwargs): + try: + return real_open(*args, **kwargs) + except (OSError, IOError): + return fake_open(*args, **kwargs) + def datetime_from_string(s): '''Return a standard datetime.datetime object initialised from a string in @@ -803,7 +820,11 @@ def test_datastore_active_not_present_if_not_provided_and_not_datastore_plugin_e assert 'datastore_active' not in res_returned - def test_mimetype_by_url(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_mimetype_by_url(self, mock_open): """ The mimetype is guessed from the url @@ -849,7 +870,11 @@ def test_mimetype_by_user(self): assert_equals(upd_mimetype, 'text/plain') @helpers.change_config('ckan.mimetype_guess', 'file_contents') - def test_mimetype_by_upload_by_file(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_mimetype_by_upload_by_file(self, mock_open): """ The mimetype is guessed from an uploaded file by the contents inside @@ -882,7 +907,11 @@ def test_mimetype_by_upload_by_file(self): assert org_mimetype != upd_mimetype assert_equals(upd_mimetype, 'text/plain') - def test_mimetype_by_upload_by_filename(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_mimetype_by_upload_by_filename(self, mock_open): """ The mimetype is guessed from an uploaded file with a filename @@ -957,7 +986,11 @@ def test_size_of_resource_by_user(self): assert org_size < upd_size - def test_size_of_resource_by_upload(self): + @helpers.change_config('ckan.storage_path', '/doesnt_exist') + @mock.patch.object(ckan.lib.uploader, 'os', fake_os) + @mock.patch.object(builtins, 'open', side_effect=mock_open_if_open_fails) + @mock.patch.object(ckan.lib.uploader, '_storage_path', new='/doesnt_exist') + def test_size_of_resource_by_upload(self, mock_open): """ The size of the resource determined by the uploaded file """