Skip to content

Commit

Permalink
Enable py3 workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 2, 2020
1 parent 671057a commit edc0fd9
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -143,4 +143,4 @@ workflows:
build_and_test:
jobs:
- test-python-2
# - test-python-3
- test-python-3
10 changes: 5 additions & 5 deletions ckan/tests/legacy/functional/api/test_email_notifications.py
@@ -1,6 +1,7 @@
# encoding: utf-8

import time
import pytest

import ckan.model as model
import ckan.tests.legacy as tests
Expand Down Expand Up @@ -419,11 +420,10 @@ def test_00_email_notifications_disabled_by_default(self, mail_server):
assert len(mail_server.get_smtp_messages()) == 0


class TestEmailNotificationsIniSetting(ControllerTestCase):
class TestEmailNotificationsIniSetting(object):
"""Tests for the ckan.activity_streams_email_notifications config setting.
"""

@classmethod
def setup_class(cls):

Expand All @@ -441,7 +441,7 @@ def setup_class(cls):
"apikey": testsysadmin.apikey,
}

@helpers.change_config("ckan.activity_streams_email_notifications", False)
@pytest.mark.ckan_config("ckan.activity_streams_email_notifications", False)
def test_00_send_email_notifications_feature_disabled(self, mail_server):
"""Send_email_notifications API should error when feature disabled."""

Expand Down Expand Up @@ -506,7 +506,7 @@ def test_00_send_email_notifications_feature_disabled(self, mail_server):
status=409,
)

@helpers.change_config("ckan.activity_streams_email_notifications", False)
@pytest.mark.ckan_config("ckan.activity_streams_email_notifications", False)
def test_01_no_emails_sent_if_turned_off(self, mail_server):
"""No emails should be sent if the feature is disabled site-wide."""

Expand Down Expand Up @@ -534,7 +534,7 @@ def setup_class(cls):

# Don't send email notifications for activities older than 1
# microsecond
@helpers.change_config("ckan.email_notifications_since", ".000001")
@pytest.mark.ckan_config("ckan.email_notifications_since", ".000001")
def test_00_email_notifications_since(self, mail_server):
"""No emails should be sent for activities older than
email_notifications_since.
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/model/test_user.py
Expand Up @@ -19,9 +19,9 @@ def _set_password(password):
This is needed to create old password hashes in the tests
"""
# if isinstance(password, text_type):
# password_8bit = password.encode("ascii", "ignore")
# password_8bit = password.encode("ascii", "ignore")
# else:
# password_8bit = password
# password_8bit = password

salt = hashlib.sha1(os.urandom(60))
hash = hashlib.sha1(six.ensure_binary(password + salt.hexdigest()))
Expand Down
10 changes: 2 additions & 8 deletions ckanext/datastore/tests/test_plugin.py
Expand Up @@ -61,15 +61,9 @@ def test_loading_datastore_last_doesnt_work(self):
)


@pytest.mark.ckan_config("ckan.plugins", "datastore")
@pytest.mark.usefixtures("clean_index", "with_plugins", "with_request_context")
class TestPluginDatastoreSearch(object):
@classmethod
def setup_class(cls):
p.load("datastore")

@classmethod
def teardown_class(cls):
p.unload("datastore")

@pytest.mark.ckan_config("ckan.datastore.default_fts_lang", None)
def test_english_is_default_fts_language(self):
expected_ts_query = ", plainto_tsquery('english', 'foo') \"query\""
Expand Down
4 changes: 2 additions & 2 deletions ckanext/example_iuploader/test/test_plugin.py
Expand Up @@ -25,7 +25,7 @@
fs = fake_filesystem.FakeFilesystem()
fake_os = fake_filesystem.FakeOsModule(fs)
fake_open = fake_filesystem.FakeFileOpen(fs)
CONTENT = b"data"
CONTENT = "data"


def mock_open_if_open_fails(*args, **kwargs):
Expand All @@ -43,7 +43,7 @@ def mock_open_if_open_fails(*args, **kwargs):
# open)
@pytest.mark.ckan_config("ckan.plugins", "example_iuploader")
@pytest.mark.ckan_config("ckan.webassets.path", "/tmp/webassets")
@pytest.mark.usefixtures("with_plugins", "clean_db")
@pytest.mark.usefixtures("with_plugins", "clean_db", "with_request_context")
@patch.object(ckan.lib.uploader, "os", fake_os)
@patch.object(flask, "send_file", side_effect=[CONTENT])
@patch.object(config["pylons.h"], "uploads_enabled", return_value=True)
Expand Down
2 changes: 1 addition & 1 deletion ckanext/multilingual/plugin.py
Expand Up @@ -226,7 +226,7 @@ def before_index(self, search_data):

## translate rest
all_terms = []
for key, value in six.iteritems(search_data):
for key, value in sorted(six.iteritems(search_data)):
if key in KEYS_TO_IGNORE or key.startswith('title'):
continue
if not isinstance(value, list):
Expand Down
4 changes: 2 additions & 2 deletions ckanext/multilingual/tests/test_multilingual_plugin.py
Expand Up @@ -190,7 +190,7 @@ def test_translate_terms(self):
'text_de': '',
'text_pt_BR': '',
u'title_fr': u'french david',
'text_fr': u'french note french moon french boon french_moo',
'text_fr': u'french_moo french note french moon french boon',
'text_ja': '',
'text_sr': '',
'title': u'david',
Expand All @@ -208,7 +208,7 @@ def test_translate_terms(self):
'tags': [u'moon', 'boon'],
'text_el': '',
'title_en': u'david',
'text_en': u'an interesting note moon boon moo',
'text_en': u'moo an interesting note moon boon',
'text_es': '',
'text_sl': '',
'text_pl': '',
Expand Down
70 changes: 24 additions & 46 deletions ckanext/reclineview/tests/test_view.py
@@ -1,6 +1,6 @@
# encoding: utf-8

from ckan.tests.helpers import _get_test_app
import pytest
from ckan.common import config

import ckan.model as model
Expand All @@ -12,27 +12,16 @@
from ckan.tests import helpers, factories


class BaseTestReclineViewBase():
@classmethod
def setup_class(cls):
cls.config_templates = config['ckan.legacy_templates']
config['ckan.legacy_templates'] = 'false'

cls.app = _get_test_app()
p.load(cls.view_type)

cls.p = cls.view_class()
@pytest.mark.ckan_config('ckan.legacy_templates', 'false')
@pytest.mark.usefixtures("with_plugins", "with_request_context")
class BaseTestReclineViewBase(object):

@pytest.fixture(autouse=True)
def initial_data(self, clean_db, with_request_context):
create_test_data.CreateTestData.create()

cls.resource_view, cls.package, cls.resource_id = \
_create_test_view(cls.view_type)

@classmethod
def teardown_class(cls):
config['ckan.legacy_templates'] = cls.config_templates
p.unload(cls.view_type)
model.repo.rebuild_db()
self.p = self.view_class()
self.resource_view, self.package, self.resource_id = \
_create_test_view(self.view_type)

def test_can_view(self):
data_dict = {'resource': {'datastore_active': True}}
Expand All @@ -41,15 +30,16 @@ def test_can_view(self):
data_dict = {'resource': {'datastore_active': False}}
assert not self.p.can_view(data_dict)

def test_title_description_iframe_shown(self):
def test_title_description_iframe_shown(self, app):
url = h.url_for('resource.read',
id=self.package.name, resource_id=self.resource_id)
result = self.app.get(url)
result = app.get(url)
assert self.resource_view['title'] in result
assert self.resource_view['description'] in result
assert 'data-module="data-viewer"' in result.body


@pytest.mark.ckan_config('ckan.plugins', 'recline_view')
class TestReclineView(BaseTestReclineViewBase):
view_type = 'recline_view'
view_class = plugin.ReclineView
Expand Down Expand Up @@ -80,28 +70,13 @@ def test_can_view_bad_format_no_datastore(self):
assert not self.p.can_view(data_dict)


class TestReclineViewDatastoreOnly(helpers.FunctionalTestBase):

@classmethod
def setup_class(cls):
cls.app = _get_test_app()
if not p.plugin_loaded('recline_view'):
p.load('recline_view')
if not p.plugin_loaded('datastore'):
p.load('datastore')
app_config = config.copy()
app_config['ckan.legacy_templates'] = 'false'
app_config['ckan.plugins'] = 'recline_view datastore'
app_config['ckan.views.default_views'] = 'recline_view'

@classmethod
def teardown_class(cls):
if p.plugin_loaded('recline_view'):
p.unload('recline_view')
if p.plugin_loaded('datastore'):
p.unload('datastore')

def test_create_datastore_only_view(self):
@pytest.mark.ckan_config('ckan.legacy_templates', 'false')
@pytest.mark.ckan_config('ckan.plugins', 'recline_view datastore')
@pytest.mark.ckan_config('ckan.views.default_views', 'recline_view')
@pytest.mark.usefixtures("clean_db", "with_plugins")
class TestReclineViewDatastoreOnly(object):

def test_create_datastore_only_view(self, app):
dataset = factories.Dataset()
data = {
'resource': {'package_id': dataset['id']},
Expand All @@ -115,11 +90,12 @@ def test_create_datastore_only_view(self):
url = h.url_for('resource.read',
id=dataset['id'], resource_id=resource_id)

result = self.app.get(url)
result = app.get(url)

assert 'data-module="data-viewer"' in result.body


@pytest.mark.ckan_config('ckan.plugins', 'recline_grid_view')
class TestReclineGridView(BaseTestReclineViewBase):
view_type = 'recline_grid_view'
view_class = plugin.ReclineGridView
Expand All @@ -129,6 +105,7 @@ def test_it_has_no_schema(self):
assert schema is None, schema


@pytest.mark.ckan_config('ckan.plugins', 'recline_graph_view')
class TestReclineGraphView(BaseTestReclineViewBase):
view_type = 'recline_graph_view'
view_class = plugin.ReclineGraphView
Expand All @@ -139,6 +116,7 @@ def test_it_has_the_correct_schema_keys(self):
_assert_schema_exists_and_has_keys(schema, expected_keys)


@pytest.mark.ckan_config('ckan.plugins', 'recline_map_view')
class TestReclineMapView(BaseTestReclineViewBase):
view_type = 'recline_map_view'
view_class = plugin.ReclineMapView
Expand Down Expand Up @@ -170,7 +148,7 @@ def _create_test_view(view_type):
def _assert_schema_exists_and_has_keys(schema, expected_keys):
assert schema is not None, schema

keys = schema.keys()
keys = list(schema.keys())
keys.sort()
expected_keys.sort()

Expand Down
47 changes: 18 additions & 29 deletions ckanext/resourceproxy/tests/test_proxy.py
@@ -1,7 +1,7 @@
# encoding: utf-8

import pytest
import requests
import unittest
import json
import responses
import six
Expand Down Expand Up @@ -46,27 +46,15 @@ def set_resource_url(url):
return {'resource': resource, 'package': package}


class TestProxyPrettyfied(unittest.TestCase):
@pytest.mark.ckan_config('ckan.plugins', 'resource_proxy')
@pytest.mark.usefixtures("clean_db", "with_plugins", "with_request_context")
class TestProxyPrettyfied(object):

serving = False

@classmethod
def setup_class(cls):
cls._original_config = config.copy()
if not p.plugin_loaded('resource_proxy'):
p.load('resource_proxy')
config['ckan.plugins'] = 'resource_proxy'
cls.app = _get_test_app()

@pytest.fixture(autouse=True)
def initial_data(self, clean_db, with_request_context):
create_test_data.CreateTestData.create()

@classmethod
def teardown_class(cls):
config.clear()
config.update(cls._original_config)
model.repo.rebuild_db()

def setUp(self):
self.url = 'http://www.ckan.org/static/example.json'
self.data_dict = set_resource_url(self.url)

Expand All @@ -87,7 +75,7 @@ def test_resource_proxy_on_200(self):
assert "yes, I'm proxied" in result.content, result.content

@responses.activate
def test_resource_proxy_on_404(self):
def test_resource_proxy_on_404(self, app):
self.mock_out_urls(
self.url,
body="I'm not here",
Expand All @@ -99,59 +87,60 @@ def test_resource_proxy_on_404(self):
assert result.status_code == 404, result.status_code

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url)
result = app.get(proxied_url)
# we expect a 409 because the resourceproxy got an error (404)
# from the server
assert result.status_code == 409
assert '404' in result.data

@responses.activate
def test_large_file(self):
def test_large_file(self, app):
cl = blueprint.MAX_FILE_SIZE + 1
self.mock_out_urls(
self.url,
headers={'Content-Length': six.text_type(cl)},
body='c' * cl)

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url)
result = app.get(proxied_url)
assert result.status_code == 409
assert six.b('too large') in result.data

@responses.activate
def test_large_file_streaming(self):
def test_large_file_streaming(self, app):
cl = blueprint.MAX_FILE_SIZE + 1
self.mock_out_urls(
self.url,
stream=True,
body='c' * cl)

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url)
result = app.get(proxied_url)
assert result.status_code == 409
assert six.b('too large') in result.data

@responses.activate
def test_invalid_url(self):
def test_invalid_url(self, app):
responses.add_passthru(config['solr_url'])
self.data_dict = set_resource_url('http:invalid_url')

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url)
result = app.get(proxied_url)
assert result.status_code == 409
assert six.b('Invalid URL') in result.data

def test_non_existent_url(self):
def test_non_existent_url(self, app):
self.data_dict = set_resource_url('http://nonexistent.example.com')

def f1():
url = self.data_dict['resource']['url']
requests.get(url)

self.assertRaises(requests.ConnectionError, f1)
with pytest.raises(requests.ConnectionError):
f1()

proxied_url = proxy.get_proxified_resource_url(self.data_dict)
result = self.app.get(proxied_url)
result = app.get(proxied_url)
assert result.status_code == 502
assert six.b('connection error') in result.data

Expand Down

0 comments on commit edc0fd9

Please sign in to comment.