Skip to content

Commit

Permalink
@amercader is a hero, test passing with fakefs
Browse files Browse the repository at this point in the history
Fixing pep8
  • Loading branch information
jrods committed Oct 25, 2016
1 parent d381d1e commit 88531cc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
42 changes: 37 additions & 5 deletions ckan/tests/logic/action/test_create.py
Expand Up @@ -3,7 +3,7 @@
'''Unit tests for ckan/logic/auth/create.py.
'''

import ckan
from ckan.common import config
import mock
import nose.tools
Expand All @@ -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):

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
"""
Expand Down
41 changes: 37 additions & 4 deletions ckan/tests/logic/action/test_update.py
Expand Up @@ -5,6 +5,7 @@

import nose.tools
import mock
import ckan
from ckan.common import config

import ckan.logic as logic
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
"""
Expand Down

0 comments on commit 88531cc

Please sign in to comment.