Skip to content

Commit

Permalink
Restore green state for Py2
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Dec 24, 2019
1 parent c0a714e commit b719e63
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 63 deletions.
5 changes: 3 additions & 2 deletions ckan/cli/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ def check_translation(validator, msgid, msgstr):

def check_po_file(path):
errors = []

po = polib.pofile(path)
for entry in po.translated_entries():
if entry.msgid_plural and entry.msgstr_plural:
Expand All @@ -163,5 +162,7 @@ def check_po_file(path):
for function in (
simple_conv_specs, mapping_keys, replacement_fields
):
check_translation(function, entry.msgid, entry.msgstr)
error = check_translation(function, entry.msgid, entry.msgstr)
if error:
errors.append(error)
return errors
2 changes: 1 addition & 1 deletion ckan/lib/webassets_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)
env = None

yaml.warnings({'YAMLLoadWarning': False})
yaml.warnings({u'YAMLLoadWarning': False})


def create_library(name, path):
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/action/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -3372,7 +3372,7 @@ def config_option_list(context, data_dict):

schema = ckan.logic.schema.update_configuration_schema()

return schema.keys()
return list(schema.keys())


@logic.validate(logic.schema.job_list_schema)
Expand Down
22 changes: 11 additions & 11 deletions ckan/tests/controllers/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import json
import re

try:
import builtins
except ImportError:
import __builtin__ as builtins

import mock
import pytest
import six
Expand All @@ -22,6 +17,11 @@
from ckan.tests import factories
from ckan.lib import uploader as ckan_uploader

try:
import __builtin__ as builtins
except ImportError:
import builtins

fs = fake_filesystem.FakeFilesystem()
fake_os = fake_filesystem.FakeOsModule(fs)
fake_open = fake_filesystem.FakeFileOpen(fs)
Expand All @@ -38,10 +38,9 @@ def mock_open_if_open_fails(*args, **kwargs):
@pytest.mark.usefixtures("clean_db")
class TestApiController(object):
@pytest.mark.ckan_config("ckan.storage_path", "/doesnt_exist")
@mock.patch.object(builtins, "open", side_effect=mock_open_if_open_fails)
@mock.patch.object(ckan_uploader, "os", fake_os)
@mock.patch.object(ckan_uploader, "_storage_path", new="/doesnt_exist")
def test_resource_create_upload_file(self, _, app):
def test_resource_create_upload_file(self, app, monkeypatch):
user = factories.User()
pkg = factories.Dataset(creator_user_id=user["id"])

Expand All @@ -53,9 +52,10 @@ def test_resource_create_upload_file(self, _, app):
)
env = {"REMOTE_USER": six.ensure_str(user["name"])}
postparams = {"name": "test-flask-upload", "package_id": pkg["id"]}
upload_content = "test-content"
upload_content = six.ensure_binary("test-content")
upload_info = ("upload", "test-upload.txt", upload_content)

monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)
resp = app.post(
url,
params=postparams,
Expand All @@ -76,7 +76,7 @@ def test_unicode_in_error_message_works_ok(self, app):
response = app.post(url=org_url, params=postparams, status=404)
# The unicode is backslash encoded (because that is the default when
# you do str(exception) )
assert "Delta symbol: \\u0394" in response.body
assert helpers.body_contains(response, "Delta symbol: \\u0394")

@pytest.mark.usefixtures("clean_index")
def test_dataset_autocomplete_name(self, app):
Expand Down Expand Up @@ -249,7 +249,7 @@ def test_jsonp_works_on_get_requests(self, app):
)

res = app.get(url=url, params={"callback": "my_callback"})
assert re.match(r"my_callback\(.*\);", res.body), res
assert re.match(r"my_callback\(.*\);", six.ensure_str(res.body)), res
# Unwrap JSONP callback (we want to look at the data).
start = len("my_callback") + 1
msg = res.body[start:-2]
Expand Down Expand Up @@ -285,7 +285,7 @@ def test_jsonp_does_not_work_on_post_requests(self, app):

res = app.post(url=url)
# The callback param is ignored and the normal response is returned
assert not res.body.startswith("my_callback")
assert not six.ensure_str(res.body).startswith("my_callback")
res_dict = json.loads(res.body)
assert res_dict["success"]
assert sorted(res_dict["result"]) == sorted(
Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/controllers/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_custom_atom_feed_works(self, app):

assert helpers.body_contains(res, u"<title>{0}</title>".format(dataset1["title"]))

assert not helpers.body_contains(u'<title">{0}</title>'.format(dataset2["title"]))
assert not helpers.body_contains(res, u'<title">{0}</title>'.format(dataset2["title"]))


@pytest.mark.ckan_config("ckan.plugins", "test_feed_plugin")
Expand Down
1 change: 0 additions & 1 deletion ckan/tests/controllers/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,6 @@ def test_search_basic(self, app):

assert helpers.body_contains(page, dataset1["name"])


def test_search_language_toggle(self, app):
dataset1 = factories.Dataset()

Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def body_contains(res, content):
body = res.data
except AttributeError:
body = res.body
body = six.ensure_str(body)
body = six.ensure_text(body)
return content in body


Expand Down
1 change: 1 addition & 0 deletions ckan/tests/legacy/functional/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from ckan.tests.helpers import body_contains


class ApiTestCase(ApiTestCase, ControllerTestCase):
def test_get_api(self, app):
offset = self.offset("")
Expand Down
1 change: 1 addition & 0 deletions ckan/tests/legacy/functional/api/test_package_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ckan.tests.legacy import TestController as ControllerTestCase
from ckan.tests.helpers import body_contains


class PackageSearchApiTestCase(ApiTestCase, ControllerTestCase):
@pytest.fixture(autouse=True)
def initial_data(self, clean_db, clean_index):
Expand Down
29 changes: 14 additions & 15 deletions ckan/tests/logic/action/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
"""Unit tests for ckan/logic/action/create.py.
"""
try:
import builtins
except ImportError:
import __builtin__ as builtins

import cgi
import mock
import pytest
Expand All @@ -23,6 +18,11 @@

from pyfakefs import fake_filesystem

try:
import __builtin__ as builtins
except ImportError:
import builtins

real_open = open
fs = fake_filesystem.FakeFilesystem()
fake_os = fake_filesystem.FakeOsModule(fs)
Expand Down Expand Up @@ -424,9 +424,8 @@ def test_doesnt_require_url(self):

@pytest.mark.ckan_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):
def test_mimetype_by_url(self, monkeypatch):
"""
The mimetype is guessed from the url
Expand All @@ -439,6 +438,7 @@ def test_mimetype_by_url(self, mock_open):
"url": "http://localhost/data.csv",
"name": "A nice resource",
}
monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)
result = helpers.call_action("resource_create", context, **params)

mimetype = result.pop("mimetype")
Expand Down Expand Up @@ -467,9 +467,8 @@ def test_mimetype_by_user(self):

@pytest.mark.ckan_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):
def test_mimetype_by_upload_by_filename(self, monkeypatch):
"""
The mimetype is guessed from an uploaded file with a filename
Expand Down Expand Up @@ -507,7 +506,9 @@ def test_mimetype_by_upload_by_filename(self, mock_open):
"upload": test_resource,
}

monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)
# Mock url_for as using a test request context interferes with the FS mocking

with mock.patch("ckan.lib.helpers.url_for"):
result = helpers.call_action("resource_create", context, **params)

Expand All @@ -519,9 +520,8 @@ def test_mimetype_by_upload_by_filename(self, mock_open):
@pytest.mark.ckan_config("ckan.mimetype_guess", "file_contents")
@pytest.mark.ckan_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):
def test_mimetype_by_upload_by_file(self, monkeypatch):
"""
The mimetype is guessed from an uploaded file by the contents inside
Expand All @@ -547,7 +547,7 @@ def test_mimetype_by_upload_by_file(self, mock_open):
"name": "A nice resource",
"upload": test_resource,
}

monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)
# Mock url_for as using a test request context interferes with the FS mocking
with mock.patch("ckan.lib.helpers.url_for"):
result = helpers.call_action("resource_create", context, **params)
Expand All @@ -559,9 +559,8 @@ def test_mimetype_by_upload_by_file(self, mock_open):

@pytest.mark.ckan_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):
def test_size_of_resource_by_upload(self, monkeypatch):
"""
The size of the resource determined by the uploaded file
"""
Expand All @@ -584,7 +583,7 @@ def test_size_of_resource_by_upload(self, mock_open):
"name": "A nice resource",
"upload": test_resource,
}

monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)
# Mock url_for as using a test request context interferes with the FS mocking
with mock.patch("ckan.lib.helpers.url_for"):
result = helpers.call_action("resource_create", context, **params)
Expand Down
30 changes: 13 additions & 17 deletions ckan/tests/logic/action/test_update.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# encoding: utf-8
"""Unit tests for ckan/logic/action/update.py."""
try:
import builtins
except ImportError:
import __builtin__ as builtins

import datetime

import mock
Expand All @@ -22,6 +17,11 @@
from six import StringIO
from pyfakefs import fake_filesystem

try:
import __builtin__ as builtins
except ImportError:
import builtins

real_open = open
fs = fake_filesystem.FakeFilesystem()
fake_os = fake_filesystem.FakeOsModule(fs)
Expand Down Expand Up @@ -1001,9 +1001,8 @@ def test_datastore_active_not_present_if_not_provided_and_not_datastore_plugin_e

@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):
def test_mimetype_by_url(self, monkeypatch):
"""
The mimetype is guessed from the url
Expand All @@ -1014,7 +1013,7 @@ def test_mimetype_by_url(self, mock_open):
resource = factories.Resource(
package=dataset, url="http://localhost/data.csv", name="Test"
)

monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)
res_update = helpers.call_action(
"resource_update",
id=resource["id"],
Expand Down Expand Up @@ -1055,9 +1054,8 @@ def test_mimetype_by_user(self):
@helpers.change_config("ckan.mimetype_guess", "file_contents")
@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):
def test_mimetype_by_upload_by_file(self, monkeypatch):
"""
The mimetype is guessed from an uploaded file by the contents inside
Expand All @@ -1081,7 +1079,7 @@ def test_mimetype_by_upload_by_file(self, mock_open):
update_resource = TestResourceUpdate.FakeFileStorage(
update_file, "update_test"
)

monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)
# Mock url_for as using a test request context interferes with the FS mocking
with mock.patch("ckan.lib.helpers.url_for"):
res_update = helpers.call_action(
Expand All @@ -1099,9 +1097,8 @@ def test_mimetype_by_upload_by_file(self, mock_open):

@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):
def test_mimetype_by_upload_by_filename(self, monkeypatch):
"""
The mimetype is guessed from an uploaded file with a filename
Expand Down Expand Up @@ -1132,6 +1129,7 @@ def test_mimetype_by_upload_by_filename(self, mock_open):
test_file, "test.json"
)
dataset = factories.Dataset()
monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)

# Mock url_for as using a test request context interferes with the FS mocking
with mock.patch("ckan.lib.helpers.url_for"):
Expand All @@ -1154,7 +1152,6 @@ def test_mimetype_by_upload_by_filename(self, mock_open):
update_resource = TestResourceUpdate.FakeFileStorage(
update_file, "update_test.csv"
)

with mock.patch("ckan.lib.helpers.url_for"):
res_update = helpers.call_action(
"resource_update",
Expand Down Expand Up @@ -1197,9 +1194,8 @@ def test_size_of_resource_by_user(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):
def test_size_of_resource_by_upload(self, monkeypatch):
"""
The size of the resource determined by the uploaded file
"""
Expand Down Expand Up @@ -1227,6 +1223,7 @@ def test_size_of_resource_by_upload(self, mock_open):
test_file, "test.json"
)
dataset = factories.Dataset()
monkeypatch.setattr(builtins, 'open', mock_open_if_open_fails)

# Mock url_for as using a test request context interferes with the FS mocking
with mock.patch("ckan.lib.helpers.url_for"):
Expand All @@ -1249,7 +1246,6 @@ def test_size_of_resource_by_upload(self, mock_open):
update_resource = TestResourceUpdate.FakeFileStorage(
update_file, "update_test.csv"
)

with mock.patch("ckan.lib.helpers.url_for"):
res_update = helpers.call_action(
"resource_update",
Expand Down
2 changes: 1 addition & 1 deletion ckan/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def _get_user_for_apikey():
apikey = u''
if not apikey:
return None
apikey = six.ensure_text(apikey, errors="ignore")
apikey = six.ensure_text(apikey, errors=u"ignore")
log.debug(u'Received API Key: %s' % apikey)
query = model.Session.query(model.User)
user = query.filter_by(apikey=apikey).first()
Expand Down
Loading

0 comments on commit b719e63

Please sign in to comment.